fabric 快速部署,回滾,打包,上傳
阿新 • • 發佈:2019-01-04
#!/usr/bin/env python #--coding:utf8-- ''' http://www.cnblogs.com/onlyfu/p/4926351.html ''' from fabric.api import * from fabric.colors import * from fabric.context_managers import * from fabric.contrib.console import confirm import time #environment value env.user ='root' env.hosts=['192.168.1.86','192.168.1.116'] env.password='pass_key' env.project_dev_source='/var/www/html/'#開發機專案主目錄 env.project_tar_source='/var/www/releases/'#開發機壓縮包目錄 env.project_pack_name='release'#專案壓縮包名release.tar.gz env.deploy_project_root='/var/www/html/'#生產環境主目錄 env.deploy_release_dir='releases'#專案釋出目錄:/var/www/html/releases/下 env.deploy_current_dir='current' #對外服務當前版本軟連線 env.deploy_version=time.strftime("%Y%m%d")+"v2"#版本號 env.deploy_full_path="" #版本號,迴歸操作 @runs_once def input_versionid(): return prompt("input project roollback version ID:",default="") #打包本地專案 @task @runs_once def tar_source(): print yellow("creating source package...") with lcd(env.project_dev_source): local("tar -zcf %s.tar.gz ."%(env.project_tar_source+env.project_pack_name)) print green("creating source package success!") #上傳任務 @task def put_package(): print yellow("start put package...") with settings(warn_only=True): with cd(env.deploy_project_root+env.deploy_release_dir): #cd /var/www/html/releases/ run("mkdir %s"%(env.deploy_version))#建立版本目錄 env.deploy_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.deploy_full_path) if result.failed and not("put file failed,Continue[Y/N]:?"): abort("aborting file put task!") with cd(env.deploy_full_path):#解壓後刪除壓縮包 run("tar -zxcf %s.tar.gz"%(env.project_pack_name)) run("rm -rf %s.tar.gz"%(env.project_pack_name)) print green("put & untar package success!") #當前版本目錄軟連線 @task 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!") #版本回滾任務 @task def rollback(): print yellow("rollback project version") versionid= input_versionid() #獲得使用者輸入回滾版本號 if versionid == '': abort("project version ID error,abort!") env.deploy_full_path = env.deploy_project_root+env.deploy_release_dir+"/"+versionid 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("rollback success!")) @task def go(): tar_source() put_package() make_symlink()