需求:测试环境高可用部署,更换程序包时需要多个台服务器一起进行更换,更换后自动启动程序
from fabric.api import *from fabric.contrib.console import confirmfrom fabric.utils import abortfrom fabric.colors import *import time,oshost_yq1 = '192.128.138.129'host_yq2 = '192.128.138.175'host_yq3 = '192.128.138.49'host_hj1 = '192.128.138.192'host_hj2 = '192.128.138.222'host_hj3 = '192.128.138.156'host_wl1 = '192.135.8.135'host_wl2 = '192.135.8.136'host_wl3 = '192.135.8.137'env.roledefs = {'yq': [host_yq1, host_yq2, host_yq3],'hj': [host_hj1, host_hj2, host_hj3],'wl': [host_wl1, host_wl2, host_wl3]}env.user = ""env.password = ""bak_time = time.strftime("%Y%m%d%H%M", time.localtime(int(time.time())))@roles('wl')@taskdef replace_cwos():'''1、打开对应文件夹;2、杀掉对应的程序;3、删除对用的程序包;4、上传对应的程序包;5、启动程序:return:'''with settings(hide('output')):with cd('/home/data/cwos/cwos-portal-V1.9.0.200303'):run('systemctl stop cwos-portal-V1.9.0.200303.service')park_bak = "cwos-portal-V1.9.0.200303.jar" + bak_timebak_cmd = "mv cwos-portal-V1.9.0.200303.jar " + park_bakrun(bak_cmd)run('rm -rf cwos-portal-V1.9.0.200303.jar')try:put(local_path="E:/重庆国网/cwos-portal-V1.9.0.200303.jar",remote_path="/home/data/cwos/cwos-portal-V1.9.0.200303/")except SystemExit as e:print("上传文件失败失败的原因是:%s" %e)run('./stop.sh')res = run('./start.sh',pty=False)if res.return_code == 0:puts(green("success"))else:puts(red("Fail"))@taskdef install():execute(replace_cwos)
