今天闲来无事,来介绍一下利用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()