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

http://linrc.com/fabric_deploy/


大家在平时的开发中应该时常遇到代码上线的问题,一般来说存在以下几个头疼的问题:

  • 主机数量较多,但不是特别多(1~100)

  • 上线步骤繁琐,容易出错

  • 可能需要sudo,需要多次输入sudo密码

一般来说你有两个选择:

  • fabric Python写的

  • capistrano Ruby写的

然后就选择了fabric,直接上代


# -*- coding=utf-8 -*-

 

from fabric.api import *

from fabric.contrib.project import rsync_project

 

env.roledefs = {

    'liantong':[ #电信机房

        '[email protected]',

        '[email protected]',

    ],

    'dianxin':[ #联通机房

        '[email protected]',

        '[email protected]',

    ],

}

 

@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')









############

#上线就只需执行

fab -f webDeploy.py initFiles restart #即可,登陆密码和sudo密码都只会问一遍



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

你可能感兴趣的:(python,自动化上线)