需求:测试环境高可用部署,更换程序包时需要多个台服务器一起进行更换,更换后自动启动程序

    1. from fabric.api import *
    2. from fabric.contrib.console import confirm
    3. from fabric.utils import abort
    4. from fabric.colors import *
    5. import time,os
    6. host_yq1 = '192.128.138.129'
    7. host_yq2 = '192.128.138.175'
    8. host_yq3 = '192.128.138.49'
    9. host_hj1 = '192.128.138.192'
    10. host_hj2 = '192.128.138.222'
    11. host_hj3 = '192.128.138.156'
    12. host_wl1 = '192.135.8.135'
    13. host_wl2 = '192.135.8.136'
    14. host_wl3 = '192.135.8.137'
    15. env.roledefs = {
    16. 'yq': [host_yq1, host_yq2, host_yq3],
    17. 'hj': [host_hj1, host_hj2, host_hj3],
    18. 'wl': [host_wl1, host_wl2, host_wl3]
    19. }
    20. env.user = ""
    21. env.password = ""
    22. bak_time = time.strftime("%Y%m%d%H%M", time.localtime(int(time.time())))
    23. @roles('wl')
    24. @task
    25. def replace_cwos():
    26. '''
    27. 1、打开对应文件夹;
    28. 2、杀掉对应的程序;
    29. 3、删除对用的程序包;
    30. 4、上传对应的程序包;
    31. 5、启动程序
    32. :return:
    33. '''
    34. with settings(hide('output')):
    35. with cd('/home/data/cwos/cwos-portal-V1.9.0.200303'):
    36. run('systemctl stop cwos-portal-V1.9.0.200303.service')
    37. park_bak = "cwos-portal-V1.9.0.200303.jar" + bak_time
    38. bak_cmd = "mv cwos-portal-V1.9.0.200303.jar " + park_bak
    39. run(bak_cmd)
    40. run('rm -rf cwos-portal-V1.9.0.200303.jar')
    41. try:
    42. put(local_path="E:/重庆国网/cwos-portal-V1.9.0.200303.jar",remote_path="/home/data/cwos/cwos-portal-V1.9.0.200303/")
    43. except SystemExit as e:
    44. print("上传文件失败失败的原因是:%s" %e)
    45. run('./stop.sh')
    46. res = run('./start.sh',pty=False)
    47. if res.return_code == 0:
    48. puts(green("success"))
    49. else:
    50. puts(red("Fail"))
    51. @task
    52. def install():
    53. execute(replace_cwos)