fabric 自动化部署
fabric
项目发布和运维的工作相当机械,频率还蛮高,导致时间浪费在敲大量重复的命令上。
修复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 自动化部署的更多相关文章
- fabric自动化部署django
使用fabric部署django应用 使用fabric部署django应用 本文是我的网站易读中文网自动化部署的脚本实现,以下代码在ubuntu和debian中测试通过 由于网站使用的是python技 ...
- 使用 Fabric 自动化部署 Django 项目
作者:HelloGitHub-追梦人物 文中涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 在上一篇教程中,我们通过手工方式将代码部署到了服务器.整个过程涉及到十几条命令,输了 ...
- 用 Fabric 实现自动化部署
自动化部署代码 http://liyangliang.me/posts/2015/06/deploy-applications-using-fabric/ http://fabric-docs-cn. ...
- Django 1.6 最佳实践: django项目的服务器自动化部署(转)
原文:http://www.weiguda.com/blog/41/ 当我们设置服务器时, 不应该每次都使用ssh登录服务器, 再按照记忆一步一步的配置. 因为这样实在是太容易忘记某些步骤了. 服务器 ...
- 使用Fabric自动化你的任务
Fabric是一个Python库,可以通过SSH在多个host上批量执行任务.你可以编写任务脚本,然后通过Fabric在本地就可以使用SSH在大量远程服务器上自动运行.这些功能非常适合应用的自动化部署 ...
- Linux 自动化部署
1.pexpect Pexpect 是 Don Libes 的 Expect 语言的一个 Python 实现,是一个用来启动子程序,并使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的 Py ...
- 转:使用Fabric自动化你的任务
转:http://www.cnblogs.com/holbrook/archive/2012/03/05/2380398.html fabric是什么? Fabric是一个Python库,可以通过SS ...
- [转]使用Fabric自动化你的任务
fabric是什么? Fabric是一个Python库,可以通过SSH在多个host上批量执行任务.你可以编写任务脚本,然后通过Fabric在本地就可以使用SSH在大量远程服务器上自动运行.这些功能非 ...
- Python学习笔记—自动化部署【新手必学】
前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:唯恋殊雨 目录 pexpect fabric pexpect P ...
随机推荐
- VSM(Virtual Storage Manager For Ceph)安装教程
转载注明出处,陈小跑 http://www.cnblogs.com/chenxianpao/p/5770271.html 一.安装环境 OS:CentOS7.2 VSM:v2.1 released 二 ...
- Thinkphp2.1爆出重大安全漏洞
thinkphp 2.1的版本 我们来分析下漏洞吧 官方发布了一个安全补丁 表述是:该URL安全漏洞会造成用户在客户端伪造URL,执行非法代码. 官方的补丁: /trunk/ThinkPHP/Lib/ ...
- 怎样制作一个横版格斗过关游戏 Cocos2d-x 2.0.4
本文实践自 Allen Tan 的文章<How To Make A Side-Scrolling Beat 'Em Up Game Like Scott Pilgrim with Coco ...
- mssql性能优化
总结下SQL SERVER数据库性能优化相关的注意事项,在网上搜索了一下,发现很多文章,有的都列出了上百条,但是仔细看发现,有很多似是而非或者过时(可能对SQL SERVER6.5以前的版本或者ORA ...
- 开源知识库管理系统选型 centos6.4 搭建knowlededgeroot-1.0.4知识库平台
开源知识库管理系统选型,除了使用wiki外,还有下面可选: http://www.knowledgebase-script.com/ https://github.com/lordlamer/know ...
- centos7,py2和py3共存
1.查看是否已经安装Python CentOS 7.2 默认安装了python2.7.5 因为一些命令要用它比如yum 它使用的是python2.7.5. 使用 python -V 命令查看一下是否安 ...
- 一个下载git库代码的脚本
由于每日构建需求, 须要用脚本下载代码, 实现自己主动化编译, 这个脚本是整个系统的一小块功能 #!/bin/bash #@author Liuyang #@date 2015-06-23 funct ...
- jquery淡入淡出无延迟代码
<!DOCTYPE html> <html> <head> <script src="jquery.js"></script& ...
- How to learn a new technology
是什么?为什么会出现? 这一阶段主要是对该技术有一个整体了解,他所解决的是什么问题,他的整体结构等. 怎么做? 最简单的是找一个上手视频,因为视频是非常直观的展示了技术的使用.先学会用是最根本的,对于 ...
- 控制应用程序重启,外部程序C# 实例
第一步:新建一个控制台项目,作为关闭当前应用程序的调用程序. using System; using System.Configuration; using System.Diagnostics; n ...