Init deploy gateway test
Showing
3 changed files
with
149 additions
and
0 deletions
scripts/deploy_gateway/deploy.py
0 → 100644
1 | +# -*- coding:utf-8 -*- | ||
2 | + | ||
3 | +from lib import splitHost | ||
4 | +import sys | ||
5 | +import os | ||
6 | +import argparse | ||
7 | + | ||
8 | +def start(zone,branch): | ||
9 | + | ||
10 | + print '\033[1;32m准备发布yoho-gateway,zone:{0},branch:{1}\n.\n.\n.\033[0m'.format(zone,branch) | ||
11 | + #zone must be in [az1,az2,az3] | ||
12 | + inventory_zones = ['az1','az2','az3'] | ||
13 | + deploy_zones = ['qcloud','qcloudaz2','qcloudaz3'] | ||
14 | + zoneMap = dict(zip(inventory_zones,deploy_zones)) | ||
15 | + if zone not in inventory_zones: | ||
16 | + print 'Invalid zone name! Value must be az1,az2 or az3' | ||
17 | + return False | ||
18 | + | ||
19 | + master_home_path = '/home/master' | ||
20 | + src_inventory = '{0}/yoho-ansible-roles/inventories/{1}/hosts'.format(master_home_path,zone) | ||
21 | + src_iptable = '{0}/auto_deploy/deploy/scripts/iptable/{1}/app-iptable.sh'.format(master_home_path,zoneMap[zone]) | ||
22 | + | ||
23 | + inventory_half01 = '{0}/yoho-ansible-roles/inventories/{1}/hosts_gateway_half01'.format(master_home_path,zone) | ||
24 | + inventory_half02 = '{0}/yoho-ansible-roles/inventories/{1}/hosts_gateway_half02'.format(master_home_path,zone) | ||
25 | + | ||
26 | + iptables_half01 = '{0}/auto_deploy/deploy/scripts/iptable/{1}/yoho-gateway_half01.sh'.format(master_home_path,zoneMap[zone]) | ||
27 | + iptables_half02 = '{0}/auto_deploy/deploy/scripts/iptable/{1}/yoho-gateway_half02.sh'.format(master_home_path,zoneMap[zone]) | ||
28 | + | ||
29 | + deploy_gateway = '{0}/auto_deploy/deploy/deploy-yoho-gateway.sh'.format(master_home_path) | ||
30 | + | ||
31 | + java_nginx_playbook = '{0}/yoho-ansible-roles/playbooks/java-nginx.yml'.format(master_home_path) | ||
32 | + | ||
33 | + print '\033[1;32m根据java-gateway hosts数量将inventory和iptables文件分别一分为二用于轮循发布\033[0m' | ||
34 | + splitHost.do(src_inventory,src_iptable) | ||
35 | + print '\033[1;32m临时文件已生成\033[0m' | ||
36 | + | ||
37 | + print '\033[1;32m流量切换至half1...\033[0m' | ||
38 | + cmd = 'ansible-playbook -i {0} {1} --key-file=/root/.ssh/id_dsa -e confirmation=YES'.format(inventory_half01,java_nginx_playbook) | ||
39 | + print '\033[1;32m{0}\033[0m'.format(cmd) | ||
40 | + rsp = os.popen(cmd).read() | ||
41 | + print rsp | ||
42 | + print '\033[1;32m流量已切至half1\n.\n.\n.\033[0m' | ||
43 | + | ||
44 | + print '\033[1;32m发布java-gateway到half2...\033[0m' | ||
45 | + cmd = '/bin/sh {0} -gyoho30 -p22 -ayoho-gateway -eDeploy -s{1} -b{2} -iyoho-gateway_half02.sh'.format(deploy_gateway,zoneMap[zone],branch) | ||
46 | + print '\033[1;32m{0}\033[0m'.format(cmd) | ||
47 | + rsp = os.popen(cmd).read() | ||
48 | + print rsp | ||
49 | + print '\033[1;32mjava-gateway已在half2发布成功\033[0m' | ||
50 | + | ||
51 | + print '\033[1;32m流量切换至half2...\033[0m' | ||
52 | + cmd = 'ansible-playbook -i {0} {1} --key-file=/root/.ssh/id_dsa -e confirmation=YES'.format(inventory_half02,java_nginx_playbook) | ||
53 | + print '\033[1;32m{0}\033[0m'.format(cmd) | ||
54 | + rsp = os.popen(cmd).read() | ||
55 | + print rsp | ||
56 | + print '\033[1;32m流量已切至half2\n.\n.\n.\033[0m' | ||
57 | + | ||
58 | + print '\033[1;32m发布java-gateway到half1...\033[0m' | ||
59 | + cmd = '/bin/sh {0} -gyoho30 -p22 -ayoho-gateway -eDeploy -s{1} -b{2} -iyoho-gateway_half01.sh'.format(deploy_gateway,zoneMap[zone],branch) | ||
60 | + print '\033[1;32m{0}\033[0m'.format(cmd) | ||
61 | + rsp = os.popen(cmd).read() | ||
62 | + print rsp | ||
63 | + print '\033[1;32mjava-gateway已在half1发布成功\033[0m' | ||
64 | + | ||
65 | + print '\033[1;32m恢复全部流量...\033[0m' | ||
66 | + cmd = 'ansible-playbook -i {0} {1} --key-file=/root/.ssh/id_dsa -e confirmation=YES'.format(src_inventory,java_nginx_playbook) | ||
67 | + print '\033[1;32m{0}\033[0m'.format(cmd) | ||
68 | + rsp = os.popen(cmd).read() | ||
69 | + print rsp | ||
70 | + print '\033[1;32m全部流量已恢复\033[0m' | ||
71 | + | ||
72 | + print '\033[1;32m清理临时文件...\033[0m' | ||
73 | + print '删除{0}...'.format(inventory_half01) | ||
74 | + os.remove(inventory_half01) | ||
75 | + print '删除{0}...'.format(inventory_half02) | ||
76 | + os.remove(inventory_half02) | ||
77 | + print '删除{0}...'.format(iptables_half01) | ||
78 | + os.remove(iptables_half01) | ||
79 | + print '删除{0}...'.format(iptables_half02) | ||
80 | + os.remove(iptables_half02) | ||
81 | + print '\033[1;32m全部任务执行完毕\033[0m' | ||
82 | + | ||
83 | + | ||
84 | +def healthCheck(context): | ||
85 | + return True | ||
86 | + | ||
87 | + | ||
88 | + | ||
89 | + | ||
90 | +if __name__ == '__main__': | ||
91 | + parser = argparse.ArgumentParser() | ||
92 | + parser.add_argument('--zone',type=str,help='Zone name, must be in [az1,az2,az3]') | ||
93 | + parser.add_argument('--branch',type=str,help='Git branch for yoho-gateway, like: master') | ||
94 | + args = parser.parse_args() | ||
95 | + start(zone=args.zone,branch=args.branch) |
scripts/deploy_gateway/lib/__init__.py
0 → 100644
scripts/deploy_gateway/lib/splitHost.py
0 → 100644
1 | +import re | ||
2 | +import sys | ||
3 | + | ||
4 | +def do(src_inventory,src_iptable): | ||
5 | + #Split Inventory | ||
6 | + rex_gateway_block = re.compile(r'(\[java-gateway\][\s*\d+\.]+)') | ||
7 | + rex_ip = re.compile(r'(\d+\.\d+\.\d+\.\d+)') | ||
8 | + #Find java-gateway block from inventory file | ||
9 | + src_inventory_data = open(src_inventory).read() | ||
10 | + #Figure out ip addresses | ||
11 | + gateway_block = rex_gateway_block.findall(src_inventory_data)[0].strip('\n') | ||
12 | + gateway_hosts = rex_ip.findall(gateway_block) | ||
13 | + #Cut one group into two | ||
14 | + gateway_group_half01,gateway_group_half02 = cutList(gateway_hosts) | ||
15 | + gateway_inventory_01 = src_inventory+'_gateway_half01' | ||
16 | + gateway_inventory_02 = src_inventory+'_gateway_half02' | ||
17 | + | ||
18 | + #Make new inventorys | ||
19 | + with open(gateway_inventory_01,'w') as f: | ||
20 | + new_gateway_block = '[java-gateway]\n'+'\n'.join(gateway_group_half01) | ||
21 | + f.write(src_inventory_data.replace(gateway_block,new_gateway_block)) | ||
22 | + f.close() | ||
23 | + with open(gateway_inventory_02,'w') as f: | ||
24 | + new_gateway_block = '[java-gateway]\n'+'\n'.join(gateway_group_half02) | ||
25 | + f.write(src_inventory_data.replace(gateway_block,new_gateway_block)) | ||
26 | + f.close() | ||
27 | + | ||
28 | + #Split Iptable | ||
29 | + rex_gateway_entry = re.compile(r'(yoho_gateway_qcloud\S+?=.*)') #Match whole entry start with yoho_gateway until \n | ||
30 | + src_iptable_data = open(src_iptable).read() | ||
31 | + gateway_entry = rex_gateway_entry.findall(src_iptable_data)[0] | ||
32 | + gateway_iptable_01 = src_iptable.replace('app-iptable.sh','yoho-gateway_half01.sh') | ||
33 | + gateway_iptable_02 = src_iptable.replace('app-iptable.sh','yoho-gateway_half02.sh') | ||
34 | + | ||
35 | + #Make new iptables | ||
36 | + with open(gateway_iptable_01,'w') as f: | ||
37 | + new_gateway_entry = 'yoho_gateway_qcloud_ips=({0})'.format(' '.join(['master@'+ip for ip in gateway_group_half01])) | ||
38 | + f.write(src_iptable_data.replace(gateway_entry,new_gateway_entry)) | ||
39 | + f.close() | ||
40 | + with open(gateway_iptable_02,'w') as f: | ||
41 | + new_gateway_entry = 'yoho_gateway_qcloud_ips=({0})'.format(' '.join(['master@'+ip for ip in gateway_group_half02])) | ||
42 | + f.write(src_iptable_data.replace(gateway_entry,new_gateway_entry)) | ||
43 | + f.close() | ||
44 | + return 0 | ||
45 | + | ||
46 | + | ||
47 | +def cutList(src_list): | ||
48 | + #Cut list into two | ||
49 | + mid_index = len(src_list)/2 | ||
50 | + return src_list[:mid_index],src_list[mid_index:] | ||
51 | + | ||
52 | + | ||
53 | +if __name__ == '__main__': | ||
54 | + do(sys.argv[1],sys.argv[2]) |
-
Please register or login to post a comment