程序生产环境的发布是业务上线最后一个环境,要求具备源码打包、发布、切换、回滚、版本管理等功能,本实例实现了一整套流程,其中版本切换与回滚使用了Linux的软链接来实现。具体代码如下:
-
# -*- coding:UTF-8 -*-
-
'''
-
Created on 2018年1月12日
-
-
@author: liuyazhuang
-
'''
-
-
from fabric.api import *
-
from fabric.colors import *
-
from fabric.context_managers import *
-
from fabric.contrib.console import confirm
-
import time
-
-
env.user = 'root'
-
env.hosts = ['192.168.209.121', '192.168.209.122']
-
env.password = '密码'
-
-
#开发机项目主目录
-
env.project_dev_source = '/data/dev/source/'
-
#开发机项目压缩包存储目录
-
env.project_tar_source = "/data/dev/releases/"
-
#项目压缩包前缀,文件名为release.tar.gz
-
env.project_pack_name='release'
-
-
#项目生产环境主目录
-
env.deploy_project_root = "/data/application/"
-
#项目发布目录,位于主目录下面
-
env.deploy_release_dir = 'releases'
-
#对外服务的当前版本软链接
-
env.deploy_current_dir = 'current'
-
#版本号
-
env.deploy_version=time.strftime("%Y%m%d") + "v2"
-
-
-
def input_versionid(): #获得用户输入的版本号,以便做版本回滚操作
-
return prompt("please input project rollback version ID:", default="")
-
-
-
-
def tar_source(): #打包本地项目主目录,并将压缩包存储到本地压缩包目录
-
print yellow("Creating source package...")
-
with lcd(env.project_dev_source):
-
local("tar -czf %s.tar.gz." %(env.project_tar_source + env.project_pack_name))
-
print green("Creating source package success!")
-
-
-
def put_package(): #上传任务函数
-
#开始打印
-
print yellow("Start put package...")
-
with settings(warn_only = True):
-
with cd(env.deploy_project_root + env.deploy_release_dir):
-
#创建版本目录
-
run("mkdir %s" % (env.deploy_version))
-
env.deplog_full_path = env.deploy_project_root + env.deploy_release_dir + "/" + env.deploy_version
-
-
#上传项目压缩包至此目录
-
with settings(warn_only = True):
-
result = put(env.project_tar_source + env.project_pack_name + ".tar.gz", env.deplog_full_path)
-
if result.failed and not("put file failed, Continue[Y/N]?"):
-
abort("Aborting file put task!")
-
-
#成功解压后删除压缩包
-
with cd(env.deplog_full_path):
-
run("tar -zxvf %s.tar.gz" % (env.project_pack_name))
-
run("rm -rf %s.tar.gz" % (env.project_pack_name))
-
-
print green("put & untar package success!")
-
-
-
-
def make_symlink(): #为当前版本目录做链接
-
print yellow("update current symlink")
-
env.deploy_full_path = env.deploy_project_root + env.deploy_release_dir + "/" + env.deploy_version
-
#删除软链接,重新创建并制定软链接源目录,新版本生效
-
with settings(warn_only = True):
-
run("rm -rf %s" % (env.deploy_project_root + env.deploy_current_dir))
-
run("ln -s %s %s" % (env.deploy_full_path, env.deploy_project_root + env.deploy_current_dir))
-
print green("make symlink success!")
-
-
-
def rollback(): #版本回滚任务函数
-
print yellow("rollback project version")
-
#获得用户输入的回滚版本号
-
versionid = input_versionid()
-
if versionid == '':
-
abort("Please version ID error, abort!")
-
-
env.deploy_full_path = env.deploy_project_root + env.deploy_release_dir + "/" + versionid
-
#删除软链接,重新创建并制定软链接目录,新版本生效
-
run("rm -f %s" % env.deploy_project_root + env.deploy_current_dir)
-
run("ln -s %s %s" % (env.deploy_full_path, env.deploy_project_root + env.deploy_current_dir))
-
print green("rollback success!")
-
-
-
def go(): #自动化程序版本发布入口函数
-
tar_source()
-
put_package()
-
make_symlink()