Authored by root

Init deploy gateway test

# -*- 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} --key-file=/root/.ssh/id_dsa -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
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
print '\033[1;32mjava-gateway已在half2发布成功\033[0m'
print '\033[1;32m流量切换至half2...\033[0m'
cmd = 'ansible-playbook -i {0} {1} --key-file=/root/.ssh/id_dsa -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
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
print '\033[1;32mjava-gateway已在half1发布成功\033[0m'
print '\033[1;32m恢复全部流量...\033[0m'
cmd = 'ansible-playbook -i {0} {1} --key-file=/root/.ssh/id_dsa -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
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):
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)
... ...
import re
import sys
def do(src_inventory,src_iptable):
#Split Inventory
rex_gateway_block = re.compile(r'(\[java-gateway\][\s*\d+\.]+)')
rex_ip = re.compile(r'(\d+\.\d+\.\d+\.\d+)')
#Find java-gateway block from inventory file
src_inventory_data = open(src_inventory).read()
#Figure out ip addresses
gateway_block = rex_gateway_block.findall(src_inventory_data)[0].strip('\n')
gateway_hosts = rex_ip.findall(gateway_block)
#Cut one group into two
gateway_group_half01,gateway_group_half02 = cutList(gateway_hosts)
gateway_inventory_01 = src_inventory+'_gateway_half01'
gateway_inventory_02 = src_inventory+'_gateway_half02'
#Make new inventorys
with open(gateway_inventory_01,'w') as f:
new_gateway_block = '[java-gateway]\n'+'\n'.join(gateway_group_half01)
f.write(src_inventory_data.replace(gateway_block,new_gateway_block))
f.close()
with open(gateway_inventory_02,'w') as f:
new_gateway_block = '[java-gateway]\n'+'\n'.join(gateway_group_half02)
f.write(src_inventory_data.replace(gateway_block,new_gateway_block))
f.close()
#Split Iptable
rex_gateway_entry = re.compile(r'(yoho_gateway_qcloud\S+?=.*)') #Match whole entry start with yoho_gateway until \n
src_iptable_data = open(src_iptable).read()
gateway_entry = rex_gateway_entry.findall(src_iptable_data)[0]
gateway_iptable_01 = src_iptable.replace('app-iptable.sh','yoho-gateway_half01.sh')
gateway_iptable_02 = src_iptable.replace('app-iptable.sh','yoho-gateway_half02.sh')
#Make new iptables
with open(gateway_iptable_01,'w') as f:
new_gateway_entry = 'yoho_gateway_qcloud_ips=({0})'.format(' '.join(['master@'+ip for ip in gateway_group_half01]))
f.write(src_iptable_data.replace(gateway_entry,new_gateway_entry))
f.close()
with open(gateway_iptable_02,'w') as f:
new_gateway_entry = 'yoho_gateway_qcloud_ips=({0})'.format(' '.join(['master@'+ip for ip in gateway_group_half02]))
f.write(src_iptable_data.replace(gateway_entry,new_gateway_entry))
f.close()
return 0
def cutList(src_list):
#Cut list into two
mid_index = len(src_list)/2
return src_list[:mid_index],src_list[mid_index:]
if __name__ == '__main__':
do(sys.argv[1],sys.argv[2])
... ...