天天看點

python 自動化部署工具-fabri

今天閑來無事,來介紹一下利用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 env
from fapistrano import deploy
from fabric.api import cd
import 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_once
def tar_project():
    with lcd(env.dev_source_path):
        local('tar -czf {0}release.tar.gz .'.format(env.tar_path))
@task
def 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')
@task
def 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))

@task
def main():
     tar_project()
     put_tar_package()
     make_link()           

複制