Fabric 简单使用
Fabric 简单使用
最近公司组织压测系统,要在多台机子上部署代码,可是机子上的代码与生产环境不一样,需要修改代码,还有有问题的地方要修改,然后再发代码。这边一共有7台服务务器,重新发代码,要一台一台 ssh 上去,输入用户名和密码,然后切换目录,再 git pull,pm2 restart ***, (是的我司使用 node),一两次还可以手动,随着修改的次数多了,这个事情变的麻烦的事情,发一次代码要开 7个ssh,手指都打断了。
终于受不了之后,决定找工具自动化解决这个问题。(我们线上是可以自动化的部署的,只是测试环境没有运维来支持,只能自己弄)。
node 做运维这块工作不是擅常的, 找了一会发现一个 python 的运维工具 fabric 。这下这个工作就简单了,学下 python 代码 10 分钟,看了fabric文档 30 分种,折腾环境 30 分钟, 到写出工具加调试 50 分钟,二个小时,把能用的工具做出来,不得不说,python 是非常强大的。
fabric 介绍
fabric 是一个使用 ssh 执行命令的工具,它是一款运维工具。用我的理场就是使用ssh自动化执行你的命令的工具。使用环境env, 你就能指定目标(user, password, hosts), 然后运行 run 命令。理解这个些概念,就可以做一款非常基础的工具了。
from fabric.api import *
env.user = 'root'
env.password = '123456'
env.hosts = [
'192.168.104.37',
'192.168.104.50',
'192.168.104.65',
'192.168.104.73',
'192.168.104.77',
'192.168.104.78',
'192.168.104.82',
]
pc_dir = '/home/test/test-node'
h5_dir = '/home/test/testwap-node'
pc_app = 'testnode'
h5_app = testwap-node'
src_dir = h5_dir if env.has_key('p') else pc_dir
app = h5_app if env.has_key('p') else pc_app
@task
@parallel
def name():
run('hostname')
@task
@parallel
def pull():
with cd(src_dir):
run('git pull')
@task
@parallel
def install():
with cd(src_dir):
run('npm i')
@task
@parallel
def deploy():
with cd(src_dir):
run('git pull')
run('pm2 restart %s' % (app))
@task
@parallel
def branch():
with cd(src_dir):
run('git branch')
@task
@parallel
def fetch():
with cd(src_dir):
run('git fetch -p')
@task
@parallel
def checkout():
with cd(src_dir):
run('git checkout feature/qps')
@task
@parallel
def start():
with cd(src_dir):
run('pm2 start process.json')
@task
@parallel
def restart():
with cd(src_dir):
run('pm2 restart %s' % (app))
@task
@parallel
def list():
with cd(src_dir):
run('pm2 list')
@task
@parallel
def log():
with cd(src_dir):
run('pm2 logs %s' % (app))
@task
@parallel
def env():
run('cat /etc/profile | grep TEST')
注意:
- fabric 可以在 windows 上使用,只是不能使用
@parallel。 - fabric 要求 python 2.7 以上, 但是不能在 python 3.X 上使用。
- 我在 windows 上使用,用的 conda 安装配置多个python 环境。
项目代码: github
相关链接
Fabric 简单使用的更多相关文章
- fabric简单案例
1 执行一个简单的task任务,显示两台机器的/home/guol/目录下的文件 from fabric.api import * env.users='root' env.port=22 env. ...
- fabric 安装及简单使用 (centos6)
fabric 是一个python的库,fabric可以通过ssh批量管理服务器. 第一步安装依赖包 安装epel源 1 wget -O /etc/yum.repos.d/epel.repo http: ...
- 系统批量运维管理器Fabric详解
系统批量运维管理器Fabric详解 Fabrici 是基于python现实的SSH命令行工具,简化了SSH的应用程序部署及系统管理任务,它提供了系统基础的操作组件,可以实现本地或远程shell命令,包 ...
- fabric 安装
fabric 是一个python的库,fabric可以通过ssh批量管理服务器. 第一步安装依赖包 安装fabric依赖及pip yum install -y python-pip gcc pytho ...
- 拥抱Service Fabric —— 目录
理解分布式 经典分布式系统设计 云时代分布式系统演进 Service Fabric基础概念 Node, Application, Service, Partition/Replicas Partiti ...
- 批量执行命令:fabric
Fabric 可以通过 SSH 在多台客户端主机上批量执行任务,是基于 paramiko 封装开发的,paramiko 更底层一些,安装方法如下: [root@localhost ~]$ yum in ...
- python 各种开源库
测试开发 来源:https://www.jianshu.com/p/ea6f7fb69501 Web UI测试自动化 splinter - web UI测试工具,基于selnium封装. 链接 sel ...
- Python测试 ——开发工具库
Web UI测试自动化 splinter - web UI测试工具,基于selnium封装. selenium - web UI自动化测试. mechanize- Python中有状态的程序化Web浏 ...
- 第10章:深入浅出Ansible
1.Ansible介绍 1).Ansible的优点 Ansible是一个简单的自动化引擎,可完成配置管理.引用部署.服务编排以及其他各种IT需求 Ansible是Python开发并实现的开源软件,其依 ...
随机推荐
- 通过 :hover 伪元素控制其他元素
---代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...
- 最基本PSO算法的C++实现
按照James Kennedy & Russell Eberhart (1995)的版本,算法过程如下: [x*] = PSO() P = Particle_Initialization(); ...
- Asterisk func group
Synopsis Gets, sets or clears the channel group. Each channel can only be member of exactly one grou ...
- poj3070 求斐波那契数列第n项 ——矩阵快速幂
题目:http://poj.org/problem?id=3070 用矩阵快速幂加速递推. 代码如下: #include<iostream> #include<cstdio> ...
- GMap.NET 显示GIF图标的定制
利用System.Drawing.ImageAnimator类实现GIF图标显示 public class GMapMarkerImage : GMapMarker { private Image i ...
- 4.java变量
1.java中如何定义变量的语言 数据类型 变量名:2.如何给变量赋值 语言 变量名=值:3.变量本质是什么. 本质就是内存中的一块空间,这块空间有‘类型’.“名字”.“值” int a;//在内存中 ...
- c++ primer 5th学习时间轴[ 100% ]
学习参考: 1.Mooophy/Cpp-Primer. GitHub上star最多的一个答案,英文版,但是编程用到的单词也不多,查查就懂了.但是到第IV部分,很多题目的没有答案,或者不完整. 2.hu ...
- opencv MatExpr MatOp
opencv提供了很多Mat的操作,其中涉及到两个重要的类:MatOp和MatExpr C++: MatExpr abs(const Mat& m) C++: void absdiff(Inp ...
- 8、html的body内标签之fieldset标签和label标签
一.label标签 <label> 标签为 input 元素定义标签(label). label 元素不会向用户呈现任何特殊的样式.不过,它为鼠标用户改善了可用性,因为如果用户点击 lab ...
- CodeForces - 566D Restructuring Company 并查集的区间合并
Restructuring Company Even the most successful company can go through a crisis period when you have ...