...
|
...
|
@@ -8,33 +8,32 @@ import sys |
|
|
import json
|
|
|
import argparse
|
|
|
from switch_lb import LBSwitch
|
|
|
import re
|
|
|
import ConfigParser
|
|
|
|
|
|
|
|
|
|
|
|
class params():
|
|
|
def __init__(self):
|
|
|
self.project_home = '/home/ansible/workdir/'
|
|
|
self.project_home = '/root/workdir/'
|
|
|
self.zone1_yml = '{0}inventories/az1/group_vars/all.yml'.format(self.project_home)
|
|
|
self.zone2_yml = '{0}inventories/az2/group_vars/all.yml'.format(self.project_home)
|
|
|
self.zone3_yml = '{0}inventories/az3/group_vars/all.yml'.format(self.project_home)
|
|
|
self.zone1_tmp_yml = '{0}inventories/az1/group_vars/.switch.yml.tmp'.format(self.project_home)
|
|
|
self.zone2_tmp_yml = '{0}inventories/az2/group_vars/.switch.yml.tmp'.format(self.project_home)
|
|
|
self.zone3_tmp_yml = '{0}inventories/az3/group_vars/.switch.yml.tmp'.format(self.project_home)
|
|
|
self.az1_vars = open(self.zone1_yml).read().strip('\n').split('\n')
|
|
|
self.az2_vars = open(self.zone2_yml).read().strip('\n').split('\n')
|
|
|
self.az3_vars = open(self.zone3_yml).read().strip('\n').split('\n')
|
|
|
self.dns_playbook = '{0}playbooks/yoho.dns.yml'.format(self.project_home)
|
|
|
self.db_playbook = '{0}playbooks/yoho.switch-db.yml'.format(self.project_home)
|
|
|
self.cobar_playbook = '{0}playbooks/yoho.reload-cobar.yml'.format(self.project_home)
|
|
|
self.key_file = '/root/.ssh/id_rsa'
|
|
|
|
|
|
self.key_file = '/root/.ssh/id_dsa'
|
|
|
self.rex_failed_count = re.compile(r'failed=(\d+)')
|
|
|
self.rex_unreachable_count = re.compile(r'unreachable=(\d+)')
|
|
|
|
|
|
|
|
|
class vars_maker():
|
|
|
|
|
|
"""
|
|
|
作用: 一区二区之间进行双中心切换(一区切换至二区)前生成临时的参数文件,用于覆盖原有的DNS配置,最终生成临时文件/home/ansible/yoho-ansible-roles/inventories/.switch.yml
|
|
|
依赖:yoho-ansible-roles
|
|
|
Demo:
|
|
|
demo = vars_maker()
|
|
|
demo.start()
|
...
|
...
|
@@ -89,9 +88,6 @@ class vars_maker(): |
|
|
elif zoneNum == 2:
|
|
|
tmp_yml = params().zone2_tmp_yml
|
|
|
var_rows = params().az2_vars
|
|
|
elif zoneNum == 3:
|
|
|
tmp_yml = params().zone3_tmp_yml
|
|
|
var_rows = params().az3_vars
|
|
|
else:
|
|
|
print 'Wrong zone number! Must be integer in value [1,2,3]!'
|
|
|
return False
|
...
|
...
|
@@ -113,6 +109,7 @@ class vars_maker(): |
|
|
|
|
|
|
|
|
def start(self):
|
|
|
print '\033[1;32m生成临时DNS参数文件...\033[0m'
|
|
|
self.load_vars()
|
|
|
#在一区切换至二区时,一二三区的write dns应当全部解析到二区的read dns
|
|
|
for zone1_dns in self.az1_write_map.keys():
|
...
|
...
|
@@ -121,7 +118,7 @@ class vars_maker(): |
|
|
self.tmp_dns_map[tmp_dns] = tmp_ip
|
|
|
print 'Write DNS after switch:'
|
|
|
print json.dumps(self.tmp_dns_map,indent=2)
|
|
|
for zoneNum in [1,2,3]:
|
|
|
for zoneNum in [1,2]:
|
|
|
self.update_tmp_vars(zoneNum)
|
|
|
return True
|
|
|
|
...
|
...
|
@@ -131,19 +128,27 @@ class dns_handler(): |
|
|
self.zone = zone
|
|
|
|
|
|
def start(self):
|
|
|
print '\033[1;32m开始执行DNS双中心切换...\033[0m'
|
|
|
if self.zone == 'zone1':
|
|
|
for zoneNum in [1,2,3]:
|
|
|
cmd = 'ansible-playbook -i {0}inventories/az{1}/hosts {2} -e confirmation=DNS --key-file={3} --check'.format(params().project_home,zoneNum,params().dns_playbook,params().key_file)
|
|
|
print cmd
|
|
|
for zoneNum in [1,2]:
|
|
|
cmd = 'ansible-playbook -i {0}inventories/az{1}/hosts {2} -e confirmation=DNS --key-file={3}'.format(params().project_home,zoneNum,params().dns_playbook,params().key_file)
|
|
|
print '\033[1;32m{0}\033[0m'.format(cmd)
|
|
|
rsp = os.popen(cmd).read()
|
|
|
print rsp
|
|
|
if int(max(params().rex_failed_count.findall(rsp)))>0 or int(max(params().rex_unreachable_count.findall(rsp)))>0:
|
|
|
print '\033[1;31mPlaybook执行出现错误,后续操作已中断\033[0m'
|
|
|
return False
|
|
|
|
|
|
elif self.zone == 'zone2':
|
|
|
for zoneNum in [1,2,3]:
|
|
|
for zoneNum in [1,2]:
|
|
|
extra_vars = '{0}/inventories/az{1}/group_vars/.switch.yml.tmp'.format(params().project_home,zoneNum)
|
|
|
cmd = 'ansible-playbook -i {0}inventories/az{1}/hosts {2} -e "@{3}" --key-file={4} --check'.format(params().project_home,zoneNum,params().dns_playbook,extra_vars,params().key_file)
|
|
|
print cmd
|
|
|
cmd = 'ansible-playbook -i {0}inventories/az{1}/hosts {2} -e "@{3}" -e confirmation=DNS --key-file={4}'.format(params().project_home,zoneNum,params().dns_playbook,extra_vars,params().key_file)
|
|
|
print '\033[1;32m{0}\033[0m'.format(cmd)
|
|
|
rsp = os.popen(cmd).read()
|
|
|
print rsp
|
|
|
if int(max(params().rex_failed_count.findall(rsp)))>0 or int(max(params().rex_unreachable_count.findall(rsp)))>0:
|
|
|
print '\033[1;31mPlaybook执行出现错误,后续操作已中断\033[0m'
|
|
|
return False
|
|
|
return True
|
|
|
|
|
|
class db_handler():
|
...
|
...
|
@@ -151,20 +156,32 @@ class db_handler(): |
|
|
self.zone = zone
|
|
|
|
|
|
def start(self):
|
|
|
cmd = 'ansible-playbook -i {0}inventories/az1/hosts {1} -e center={2} --key-file={3} --check'.format(params().project_home,params().db_playbook,self.zone,params().key_file)
|
|
|
print cmd
|
|
|
print '\033[1;32m开始执行数据库双中心切换...\033[0m'
|
|
|
db_mgmt = 'db-mgmt-zone1' if self.zone == 'zone1' else 'db-mgmt-zone2'
|
|
|
cmd = 'ansible-playbook -i {0}inventories/az1/hosts {1} -e center={2} -e db_mgmt={3} -e confirmation=DB --key-file={4}'.format(params().project_home,params().db_playbook,self.zone,db_mgmt,params().key_file)
|
|
|
print '\033[1;32m{0}\033[0m'.format(cmd)
|
|
|
rsp = os.popen(cmd).read()
|
|
|
print rsp
|
|
|
if int(max(params().rex_failed_count.findall(rsp)))>0 or int(max(params().rex_unreachable_count.findall(rsp)))>0:
|
|
|
print '\033[1;31mPlaybook执行出现错误,后续操作已中断\033[0m'
|
|
|
return False
|
|
|
return True
|
|
|
|
|
|
|
|
|
class cobar_handler():
|
|
|
def __init__(self,passwd):
|
|
|
self.passwd = passwd
|
|
|
|
|
|
def start(self):
|
|
|
for zoneNum in [1,2,3]:
|
|
|
cmd = 'ansible-playbook -i {0}inventories/az{1}/hosts {2} -e passwd={3} --key-file={4} --check'.format(params().project_home,zoneNum,params().cobar_playbook,self.passwd,params().key_file)
|
|
|
print '\033[1;32m重载cobar中间件...\033[0m'
|
|
|
for zoneNum in [1,2]:
|
|
|
cmd = 'ansible-playbook -i {0}inventories/az{1}/hosts {2} -e passwd={3} -e confirmation=COBAR --key-file={4} --check'.format(params().project_home,zoneNum,params().cobar_playbook,self.passwd,params().key_file)
|
|
|
print '\033[1;32m{0}\033[0m'.format(cmd)
|
|
|
rsp = os.popen(cmd).read()
|
|
|
print rsp
|
|
|
if int(max(params().rex_failed_count.findall(rsp)))>0 or int(max(params().rex_unreachable_count.findall(rsp)))>0:
|
|
|
print '\033[1;31mPlaybook执行出现错误,后续操作已中断\033[0m'
|
|
|
return False
|
|
|
return True
|
|
|
|
|
|
class clb_handler():
|
...
|
...
|
@@ -184,29 +201,36 @@ class clb_handler(): |
|
|
|
|
|
if __name__ == '__main__':
|
|
|
parser = argparse.ArgumentParser()
|
|
|
parser.add_argument('--zone',type=str,help='The zone which will be the new center, must be in [zone1,zone2]')
|
|
|
parser.add_argument('--passwd',type=str,help='Cobar server mysql password')
|
|
|
parser.add_argument('--secretId',type=str,help='Qcloud secret id')
|
|
|
parser.add_argument('--secretKey',type=str,help='Qcloud secret key')
|
|
|
parser.add_argument('--az1_lb_id',type=str,help='Zone1 outer CLB id')
|
|
|
parser.add_argument('--az2_lb_id',type=str,help='Zone2 outer CLB id')
|
|
|
parser.add_argument('--az3_lb_id',type=str,help='Zone3 outer CLB id')
|
|
|
parser.add_argument('--az1_innerlb_id',type=str,help='Zone1 internal CLB id')
|
|
|
parser.add_argument('--config',type=str,help='Center switch configuration file path')
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
def loadConfig():
|
|
|
config = ConfigParser.ConfigParser()
|
|
|
config.readfp(open(args.config))
|
|
|
return config
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
config = loadConfig()
|
|
|
|
|
|
varMan = vars_maker()
|
|
|
varMan.start()
|
|
|
ret=varMan.start()
|
|
|
|
|
|
if not ret:
|
|
|
sys.exit(1)
|
|
|
|
|
|
dbMan = db_handler(config.get("base","zone"))
|
|
|
ret = dbMan.start()
|
|
|
|
|
|
if not ret:
|
|
|
sys.exit(1)
|
|
|
|
|
|
dbMan = db_handler(args.zone)
|
|
|
dbMan.start()
|
|
|
dnsMan = dns_handler(config.get("base","zone"))
|
|
|
ret = dnsMan.start()
|
|
|
|
|
|
dnsMan = dns_handler(args.zone)
|
|
|
dnsMan.start()
|
|
|
if not ret:
|
|
|
sys.exit(1)
|
|
|
|
|
|
cobarMan = cobar_handler(args.passwd)
|
|
|
cobarMan = cobar_handler(config.get("cobar","passwd"))
|
|
|
cobarMan.start()
|
|
|
|
|
|
clbMan = clb_handler(args.secretId, args.secretKey, args.az1_lb_id, args.az2_lb_id, args.az3_lb_id, args.az1_innerlb_id, args.zone)
|
|
|
clbMan = clb_handler(config.get("qcloud","secretId"), config.get("qcloud","secretKey"), config.get("qcloud","az1_lb_id"), config.get("qcloud","az2_lb_id"), config.get("qcloud","az3_lb_id"), config.get("qcloud","az1_innerlb_id"), config.get("base","zone"))
|
|
|
clbMan.start() |
...
|
...
|
|