项目发布和运维的工作相当机械,频率还蛮高,导致时间浪费在敲大量重复的命令上。

修复bug什么的,测试,提交版本库(2分钟),ssh到测试环境pull部署(2分钟),rsync到线上机器A,B,C,D,E(1分钟),分别ssh到ABCDE五台机器,逐一重启(8-10分钟) = 13-15分钟

其中郁闷的是,每次操作都是相同的,命令一样,要命的是在多个机器上,很难在本机一个脚本搞定,主要时间都浪费在ssh,敲命令上了,写成脚本,完全可以一键执行,花两分钟看下执行结果。

安装

pip install fabric

入门示例

#fabfile.py
from fabric.api import run def host_type():
run('uname -s')

启动

itcast@ubuntu:~/tmp/fab$ fab -H 127.0.0.1 host_type
[127.0.0.1] Executing task 'host_type'
[127.0.0.1] run: uname -s
[127.0.0.1] Login password for 'itcast':
[127.0.0.1] out: Linux
[127.0.0.1] out: Done.
Disconnecting from 127.0.0.1... done.
itcast@ubuntu:~/tmp/fab$ fab -H 127.0.0.1 host_type
[127.0.0.1] Executing task 'host_type'
[127.0.0.1] run: uname -s
[127.0.0.1] Login password for 'itcast':
[127.0.0.1] out: Linux
[127.0.0.1] out:

fabric常用参数

  • -l : 显示定义好的任务函数名
  • -f : 指定fab入口文件,默认入口文件名为fabfile.py
  • -H : 指定目标主机,多台主机用","号分割

fabric常用API

  • local : 执行本地命令,如:local('uname -s')
  • lcd : 切换本地目录,如:lcd('/home')
  • cd : 切换远程目录,如:cd('/etc')
  • run : 执行远程命令,如:run('free -m')
  • sudo : sudo方式执行远程命令,如:sudo('touch /abc')
  • put : 上传本地文件到远程主机,如:put('/hello', '/home/itcast/hello')
  • get : 从远程主机下载文件到本地,如:get('/home/python/world', '/home/itcast/world')
  • reboot : 重启远程主机,如:reboot()
  • @task : 函数装饰器,标识的函数为fab可调用的,非标记的对fab不可见,纯业务逻辑
  • @runs_once : 函数装饰器,标识的函数只会执行一次,不受多台主机影响

fabric全局属性设定

  • env.host : 定义目标主机,如:env.host=['192.168.17.192', '192.168.17.193']
  • env.user : 定义用户名,如:env.user="root"
  • env.port : 定义目标主机端口,默认为22,如:env.port="22"
  • env.password : 定义密码,如:env.password="chuanzhi"
  • env.passwords : 不同的主机不同的密码,如:env.passwords={'itcast@192.168.17.192:22':'chuanzhi', 'itcast@192.168.17.193:22':'python'}

示例1:动态获取远程目录列表

from fabric.api import *

env.hosts=['192.168.17.192', '192.168.17.193']
#env.password='python'
env.passwords = {
'itcast@192.168.17.192:22':'python',
'itcast@192.168.17.193:22':'python',
} @runs_once
def input_raw():
return prompt("please input directory name:", default="/home") def workask(dirname):
run('ls -l ' + dirname) @task
def go():
print('start ...')
getdirname = input_raw()
workask(getdirname)
print('end ...')

示例2:上传文件并执行

from fabric.api import *

env.user = 'itcast'
env.hosts = ['192.168.17.192', '192.168.17.193']
env.password = 'python' @task
@runs_once
def tar_task():
with lcd('/home/itcast/testdemo'):
local('tar zcvf demo.tar.gz demo.py') @task
def put_task():
run('mkdir -p /home/itcast/testdemo')
with cd('/home/itcast/testdemo'):
put('/home/itcast/testdemo/demo.tar.gz', '/home/itcast/testdemo/demo.tar.gz') @task
def check_task():
lmd5 = local('md5sum /home/itcast/testdemo/demo.tar.gz', capture=True).split(' ')[0]
rmd5 = run('md5sum /home/itcast/testdemo/demo.tar.gz').split(' ')[0]
if lmd5 == rmd5:
print('OK ...')
else:
print('ERROR ...') @task
def run_task():
with cd('/home/itcast/testdemo'):
run('tar zxvf demo.tar.gz')
run('python demo.py') @task
def go():
tar_task()
put_task()
check_task()
run_task()

代码自动化部署

from fabric.api import *

env.user = 'itcast'
env.hosts = ['192.168.17.192', '192.168.17.193']
env.password = 'python' @runs_once
@task
def local_update():
with lcd("/home/itcast/tmp/itcasthello"):
local("git add -A")
local("git commit -m 'update'")
local("git pull origin master")
local("git push origin master") @task
def remote_update():
with cd("/home/itcast/tmp/itcasthello"):
run("git checkout master")
run("git pull origin master") @task
def deploy():
local_update()
remote_update()

fabric 更详尽的用法的更多相关文章

  1. DDGScreenShot--iOS 图片裁剪,切圆角,加边框,你还用cornerRadius,还有更高级的用法

    写在前面 我们肯定做过这样的需求,给一个图片切圆角, 当然我们大多采用简单粗暴的方法 myIcon.layer.cornerRadius = 16.5 myIcon.layer.masksToBoun ...

  2. 关于el-dialog,我更推荐的用法

    最近的项目里用上了vue和element-ui.vue这种轻量级渐进式框架的舒适自不必说,但一直困扰着我的,是如何方便又优雅的弹出模态dialog... 对于我这种在jquery出现之前就用docum ...

  3. MySql 比Replace Into更适合的用法,外加SqlServer的方式。

    Mysql: INSERT INTO `his_examine_result` (Mid,His_Examine_Mid, His_File_Mid, ResultType, His_Employee ...

  4. C#进阶系列——WebApi 异常处理解决方案

    前言:上篇C#进阶系列——WebApi接口传参不再困惑:传参详解介绍了WebApi参数的传递,这篇来看看WebApi里面异常的处理.关于异常处理,作为程序员的我们肯定不陌生,记得在介绍 AOP 的时候 ...

  5. Android APP测试的日志文件抓取

         1    log文件分类简介 实时打印的主要有:logcat main,logcat radio,logcat events,tcpdump,还有高通平台的还会有QXDM日志 状态信息的有: ...

  6. 微软极品工具箱-Sysinternals Suite

    工具包由来 Sysinternals Suite是微软发布的一套非常强大的免费工具程序集,一共包括74个windows工具.Sysinternals是Winternals公司提供的免费工具,Winte ...

  7. WebApi异常

    WebApi异常处理解决方案   前言:上篇C#进阶系列——WebApi接口传参不再困惑:传参详解介绍了WebApi参数的传递,这篇来看看WebApi里面异常的处理.关于异常处理,作为程序员的我们肯定 ...

  8. Android测试日志文件抓取与分析

    1.log文件分类简介 实时打印的主要有:logcat main,logcat radio,logcat events,tcpdump,还有高通平台的还会有QXDM日志 状态信息的有:adb shel ...

  9. 【android】[转]Android软件测试的日志文件抓取简介

    1    log文件分类简介 实时打印的主要有:logcat main,logcat radio,logcat events,tcpdump,还有高通平台的还会有QXDM日志 状态信息的有:adb s ...

随机推荐

  1. PHP正则验证类

    项目中经常使用到的验证,很使用的.查看与下载<?php /** * 验证类 * * @lastmodify 2015-12-19 * @author wuheng */ class Verify ...

  2. WordPress中默认文本编辑器替换成百度UEditor编辑器

    1.下载 下载地址: http://pan.baidu.com/s/1geNk19L 2.解压放到plugins目录下 3.插件启用

  3. Linux date 命令

    date命令用于打印或设置系统日期和时间,常见用法如下: [root@localhost ~]# date //查看当前时间 [root@localhost ~]# date +"%Y-%m ...

  4. vs启动出错(chenlu-1):参数“basePath”不能是相对路径

    参数“basePath”不能是相对路径 原因: 1.调试路径下没有exe文件.没有生成exe文件. 2.项目属性->配置属性->调试->命令中的参数被设置为相对路径.

  5. 获取请求IP

    服务器获取客户端或者网页的请求,获取IP时需要注意,并不是直接 request.getRemoteAddr(); 就可以了,因为一个请求到达服务器之前,一般都会经过一层或者多层代理服务器,比如反向代理 ...

  6. 使用PsExec获取shell执行命令

    PsExec PsExec是pstools工具组套件的一部分,确成为了渗透利器,下载地址:点击这里下载 连接shell 我的Windows Server 2012默认打开域网络防火墙的时候,是不能连接 ...

  7. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  8. 把Oracle由归档模式改为非归档模式

    把Oracle由归档模式改为非归档模式 开始–>运行命令cmd进入命令行模式 1. 使用命令sqlplus以无日志形式打开如下: sqlplus /nolog; 2. 连接数据库dev.worl ...

  9. 微信小程序之wx.showmodal

    1. . wx.showModal({ title: "2222步", content: currentCache ? "确定为自己城市添加步数吗" : &qu ...

  10. jQuery 中的 39 个技巧【申明:来源于网络】

    jQuery 中的 39 个技巧[申明:来源于网络] 地址:http://blog.csdn.net/zhongqi2513/article/details/53704812?ref=myread