首页 > 代码库 > 用Fabric实现小批量的自动化上线

用Fabric实现小批量的自动化上线

大家在平时的开发中应该时常遇到代码上线的问题,一般来说存在以下几个头疼的问题:
<ul>
<li>主机数量较多,但不是特别多(1~100)</li>
<li>上线步骤繁琐,容易出错</li>
<li>可能需要sudo,需要多次输入sudo密码</li>
</ul>
一般来说你有两个选择:
<ul>
<li><a href="http://www.fabfile.org/" target="_blank">fabric</a> Python写的</li>
<li><a href="http://capistranorb.com/" target="_blank">capistrano</a> Ruby写的</li>
</ul>
然后就选择了fabric,直接上代码

<pre class="lang:python">
# -*- coding=utf-8 -*-

from fabric.api import *
from fabric.contrib.project import rsync_project

env.roledefs = {
‘liantong‘:[ #电信机房
‘xxx@w01v.add.xxx.com‘,
‘xxx@w02v.add.xxx.com‘,
],
‘dianxin‘:[ #联通机房
‘xxx@w01v.add.xxx.com‘,
‘xxx@w02v.add.xxx.com‘,
],
}

@roles(‘liantong‘, ‘dianxin‘) #这么写装饰器是表示电信,联通都要执行这个函数
def check():
run(‘ps aux | grep uwsgi | grep -v grep‘)
run(‘ls -l /home/system/service/‘)

def initFiles():
execute(liantong_conf) #执行 liantong_conf()
execute(dianxin_conf) #执行 dianxin_conf()

@roles(‘dianxin‘) #电信
def dianxin_conf():
initFiles1()
sudo("cd /home/system/service && mv settings_online_dianxin.py settings.py")
initFiles2()

@roles(‘liantong‘) #联通
def liantong_conf():
initFiles1()
sudo("cd /home/system/service && mv settings_online_liantong.py settings.py")
initFiles2()

@roles(‘liantong‘, ‘dianxin‘)
def initFiles1():
sudo("chmod 777 /home/system/service")
sudo("chmod 777 /home/system/service/orm")
sudo("chown -R auxten:auxten /home/system/service")

rsync_project( # 调用rsync
remote_dir=‘/home/system/service/‘,
local_dir=‘/Users/auxten/Codes/Web/flow-web/*‘,
exclude=[‘python2.7‘,‘*.pyc‘,‘*.log‘,‘.svn‘,‘.idea‘,‘logs‘,‘*pull.sh‘,‘*push.sh‘,‘settings.py‘]
)

@roles(‘liantong‘, ‘dianxin‘)
def initFiles2():
sudo("chown -R apache:apache /home/system/service")

@roles(‘liantong‘, ‘dianxin‘)
def restart():
sudo("sh /home/system/service/run.sh")
run(‘ps aux | grep uwsgi | grep -v grep‘)

@roles(‘liantong‘, ‘dianxin‘)
def checkLog():
sudo(‘tail -50 /home/system/service/uwsgi.log‘)
</pre>


<pre class="lang:bash">
############
#上线就只需执行
fab -f webDeploy.py initFiles restart #即可,登陆密码和sudo密码都只会问一遍
</pre>

更多功能等待大家去挖掘,我也是刚刚用,欢迎留言交流心得

用Fabric实现小批量的自动化上线