python 自动化部署工具-fabric

来源:互联网 发布:windows qt embedded 编辑:程序博客网 时间:2024/05/22 12:38

今天闲来无事,来介绍一下利用fabric 来部署代码包。

安装 pip install fabric

fabric 默认引用fafile.py,指定执行文件加参数-f,如:fab -H 127.0.0.1 -f fabtest.py test

生产环境的代码发布需要具备以下几点:打包,发布,切换,回滚,版本管理

# -*- coding: utf-8 -*-from fabric.api import *from fabric.state import envfrom fapistrano import deployfrom fabric.api import cdimport time env.user='deploy'env.host=['192.168.1.10','192.168.1.10']env.password='testabc123'env.dev_source_path = '/data/workspace'env.tar_path='/tmp/release/'env.pro_project_path='/home/workspace/'env.version = time.strftime("%Y%m%d")+"v1"@task@runs_oncedef tar_project():    with lcd(env.dev_source_path):        local('tar -czf {0}release.tar.gz .'.format(env.tar_path))@taskdef put_tar_package():    with cd (env.pro_project_path+'release'):        run('mkdirenv.version')        with settings(warn_only=True):        result = put(env.tar_path+'release.tar.gz' env.pro_project_path+'release'+env.version)       if result.failed and no("put file failed. Continue[Y/N]")                 abort("put tar failed")      with cd( )      run('tar -zxvf release.tar.gz')      run('rm -rf   release.tar.gz')@taskdef make_link():    with settings(warn_only=true):        current_path =env.pro_project_path + 'current'         run('rm -rf  {0}'.format(current_path))        run('ln -s {0} {1}'.foramt(env.pro_project_path+'release'+env.version,current_path))@taskdef main():     tar_project()     put_tar_package()     make_link()