• 用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>

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

  • 相关阅读:
    DSP 数学工具回顾:从无穷级数 到 快速傅立叶变换
    用c++设计音效插件 : 10 基础DSP理论 (第一部分)
    用c++设计音效插件 : 10 基础DSP理论 (第二部分)
    用c++设计音效插件 :6 ASPiK Programming Guide
    python 如何在多层循环中使用break/continue
    李沐 如何判断(你自己的)研究工作的价值
    SpringBoot 2.x集成Elasticsearch
    SpringBoot 2.x集成AWS S3对象存储
    关于pycharm一直index这件事
    docker 安装 nacos
  • 原文地址:https://www.cnblogs.com/51reboot/p/4005553.html
Copyright © 2020-2023  润新知