deploy.py
4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding:utf-8 -*-
from lib import splitHost
import sys
import os
import argparse
def start(zone,branch):
print '\033[1;32m准备发布yoho-gateway,zone:{0},branch:{1}\n.\n.\n.\033[0m'.format(zone,branch)
#zone must be in [az1,az2,az3]
inventory_zones = ['az1','az2','az3']
deploy_zones = ['qcloud','qcloudaz2','qcloudaz3']
zoneMap = dict(zip(inventory_zones,deploy_zones))
if zone not in inventory_zones:
print 'Invalid zone name! Value must be az1,az2 or az3'
return False
master_home_path = '/home/master'
src_inventory = '{0}/yoho-ansible-roles/inventories/{1}/hosts'.format(master_home_path,zone)
src_iptable = '{0}/auto_deploy/deploy/scripts/iptable/{1}/app-iptable.sh'.format(master_home_path,zoneMap[zone])
inventory_half01 = '{0}/yoho-ansible-roles/inventories/{1}/hosts_gateway_half01'.format(master_home_path,zone)
inventory_half02 = '{0}/yoho-ansible-roles/inventories/{1}/hosts_gateway_half02'.format(master_home_path,zone)
iptables_half01 = '{0}/auto_deploy/deploy/scripts/iptable/{1}/yoho-gateway_half01.sh'.format(master_home_path,zoneMap[zone])
iptables_half02 = '{0}/auto_deploy/deploy/scripts/iptable/{1}/yoho-gateway_half02.sh'.format(master_home_path,zoneMap[zone])
deploy_gateway = '{0}/auto_deploy/deploy/deploy-yoho-gateway.sh'.format(master_home_path)
java_nginx_playbook = '{0}/yoho-ansible-roles/playbooks/java-nginx.yml'.format(master_home_path)
print '\033[1;32m根据java-gateway hosts数量将inventory和iptables文件分别一分为二用于轮循发布\033[0m'
splitHost.do(src_inventory,src_iptable)
print '\033[1;32m临时文件已生成\033[0m'
print '\033[1;32m流量切换至half1...\033[0m'
cmd = 'ansible-playbook -i {0} {1} -e confirmation=YES'.format(inventory_half01,java_nginx_playbook)
print '\033[1;32m{0}\033[0m'.format(cmd)
rsp = os.popen(cmd).read()
print rsp
if not healthCheck(rsp):
print '\033[1;31mnginx配置同步出错,后续操作已中止\033[0m'
return False
print '\033[1;32m流量已切至half1\n.\n.\n.\033[0m'
print '\033[1;32m发布java-gateway到half2...\033[0m'
cmd = '/bin/sh {0} -gyoho30 -p22 -ayoho-gateway -eDeploy -s{1} -b{2} -iyoho-gateway_half02.sh'.format(deploy_gateway,zoneMap[zone],branch)
print '\033[1;32m{0}\033[0m'.format(cmd)
rsp = os.popen(cmd).read()
print rsp
if not healthCheck(rsp):
print '\033[1;31mgateway发布出错,后续操作已中止\033[0m'
return False
print '\033[1;32mjava-gateway已在half2发布成功\033[0m'
print '\033[1;32m流量切换至half2...\033[0m'
cmd = 'ansible-playbook -i {0} {1} -e confirmation=YES'.format(inventory_half02,java_nginx_playbook)
print '\033[1;32m{0}\033[0m'.format(cmd)
rsp = os.popen(cmd).read()
print rsp
if not healthCheck(rsp):
print '\033[1;31mnginx配置同步出错,后续操作已中止\033[0m'
return False
print '\033[1;32m流量已切至half2\n.\n.\n.\033[0m'
print '\033[1;32m发布java-gateway到half1...\033[0m'
cmd = '/bin/sh {0} -gyoho30 -p22 -ayoho-gateway -eDeploy -s{1} -b{2} -iyoho-gateway_half01.sh'.format(deploy_gateway,zoneMap[zone],branch)
print '\033[1;32m{0}\033[0m'.format(cmd)
rsp = os.popen(cmd).read()
print rsp
if not healthCheck(rsp):
print '\033[1;31mgateway发布出错,后续操作已中止\033[0m'
return False
print '\033[1;32mjava-gateway已在half1发布成功\033[0m'
print '\033[1;32m恢复全部流量...\033[0m'
cmd = 'ansible-playbook -i {0} {1} -e confirmation=YES'.format(src_inventory,java_nginx_playbook)
print '\033[1;32m{0}\033[0m'.format(cmd)
rsp = os.popen(cmd).read()
print rsp
if not healthCheck(rsp):
print '\033[1;31mnginx配置同步出错,后续操作已中止\033[0m'
return False
print '\033[1;32m全部流量已恢复\033[0m'
print '\033[1;32m清理临时文件...\033[0m'
print '删除{0}...'.format(inventory_half01)
os.remove(inventory_half01)
print '删除{0}...'.format(inventory_half02)
os.remove(inventory_half02)
print '删除{0}...'.format(iptables_half01)
os.remove(iptables_half01)
print '删除{0}...'.format(iptables_half02)
os.remove(iptables_half02)
print '\033[1;32m全部任务执行完毕\033[0m'
def healthCheck(context):
error_keywords = ['ok=0','error:','No such file or directory','cannot find or open']
for keyword in error_keywords:
if keyword in context:
return False
return True
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--zone',type=str,help='Zone name, must be in [az1,az2,az3]')
parser.add_argument('--branch',type=str,help='Git branch for yoho-gateway, like: master')
args = parser.parse_args()
start(zone=args.zone,branch=args.branch)