Authored by chunhua.zhang

删除switch脚本

---
# dns server reconfigure to match: mysql master switch to az2
- hosts: nameserver-master
vars_prompt:
- name: "confirmation"
prompt: "Sync and reload dns server. Please Enter DNS to continue."
private: no
remote_user: root
vars:
db_cms_write: 10.66.106.11
db_passport_write: 10.66.106.8
db_shops_write: 10.66.106.3
db_order_write: 10.66.106.13
db_message_write: 10.66.106.5
db_uic_write: 10.66.106.12
db_ordersplit_1_write: 10.66.106.6
db_ordersplit_2_write: 10.66.106.9
db_ordersplit_3_write: 10.66.106.15
db_ordersplit_4_write: 10.66.106.16
roles:
- yoho.dns
... ...
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# author tiexin.yang@yoho.cn
import os
import sys
sys.path.append('../')
import json
import argparse
from switch_lb import LBSwitch
import re
import ConfigParser
from vars import vars_maker
from db import db_handler
from cobar import cobar_handler
from clb import clb_handler
from dns import dns_handler
class switch_handler():
def __init__(self,config_file='switch.ini'):
config = ConfigParser.ConfigParser()
config.readfp(open(config_file))
#Load Base params
self.project_home = config.get('base','project_home').rstrip('/')
self.key_file = config.get('base','key_file')
self.zone = config.get('base','zone')
#Load DB params
self.db_playbook = '{0}/playbooks/yoho.switch-db.yml'.format(self.project_home)
#Load Cobar params
self.passwd = config.get('cobar','passwd')
self.cobar_playbook = '{0}/playbooks/yoho.reload-cobar.yml'.format(self.project_home)
#Load CLB params
self.secretId = config.get('qcloud','secretId')
self.secretKey = config.get('qcloud','secretKey')
self.az1_lb_id = config.get('qcloud','az1_lb_id')
self.az2_lb_id = config.get('qcloud','az2_lb_id')
self.az3_lb_id = config.get('qcloud','az3_lb_id')
self.az1_innerlb_id = config.get('qcloud','az1_innerlb_id')
#Load DNS params
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)
def run(self):
os.chdir(self.project_home)
varsMarker = vars_maker(self.az1_vars,self.az2_vars,self.az3_vars,self.zone1_tmp_yml,self.zone2_tmp_yml,self.zone3_tmp_yml)
dbHandler = db_handler(self.zone,self.project_home,self.db_playbook,self.key_file)
dnsHandler = dns_handler(self.zone,self.project_home,self.dns_playbook,self.key_file)
cobarHandler = cobar_handler(self.zone,self.project_home,self.cobar_playbook,self.key_file,self.passwd)
clbHandler = clb_handler(self.secretId,self.secretKey,self.az1_lb_id,self.az2_lb_id,self.az3_lb_id,self.az1_innerlb_id,self.zone)
#Switch order: vars, db, dns, cobar, clb
#if varsMarker.start() and dbHandler.start() and dnsHandler.start() and cobarHandler.start() and clbHandler.start():
if varsMarker.start() and dbHandler.start() and dnsHandler.start():
print '\033[1;32m双中心切换任务全部正常结束\033[0m'
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--config',type=str,help='Center switch configuration file path')
args = parser.parse_args()
switchHandler = switch_handler(config_file=args.config)
switchHandler.run()
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# author tiexin.yang@yoho.cn
import os
import sys
import json
import argparse
from switch_lb import LBSwitch
class clb_handler():
def __init__(self, secretId, secretKey, az1_lb_id, az2_lb_id, az3_lb_id, az1_innerlb_id, zone):
self.zone = zone
self.clbClient = LBSwitch(secretId, secretKey, az1_lb_id, az2_lb_id, az3_lb_id, az1_innerlb_id)
def start(self):
if self.zone == 'zone1':
ret = self.clbClient.all_to_az1()
elif self.zone == 'zone2':
ret = self.clbClient.all_to_az2()
else:
print 'Zone name {0} can not be recongnized'.format(self.zone)
return True
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# author tiexin.yang@yoho.cn
import os
import sys
import json
import argparse
import re
class cobar_handler():
def __init__(self,zone,project_home,cobar_playbook,key_file,passwd):
self.zone = zone
self.rex_failed_count = re.compile(r'failed=(\d+)')
self.rex_unreachable_count = re.compile(r'unreachable=(\d+)')
self.project_home = project_home
self.cobar_playbook = cobar_playbook
self.key_file = key_file
self.passwd = passwd
def start(self):
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(self.project_home,zoneNum,self.cobar_playbook,self.passwd,self.key_file)
print '\033[1;32m{0}\033[0m'.format(cmd)
rsp = os.popen(cmd).read()
print rsp
if int(max(self.rex_failed_count.findall(rsp)))>0 or int(max(self.rex_unreachable_count.findall(rsp)))>0:
print '\033[1;31mPlaybook执行出现错误,后续操作已中断\033[0m'
return False
return True
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# author tiexin.yang@yoho.cn
import os
import sys
import json
import argparse
import re
class db_handler():
def __init__(self,zone,project_home,db_playbook,key_file):
self.zone = zone
self.rex_failed_count = re.compile(r'failed=(\d+)')
self.rex_unreachable_count = re.compile(r'unreachable=(\d+)')
self.project_home = project_home
self.db_playbook = db_playbook
self.key_file = key_file
def start(self):
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(self.project_home,self.db_playbook,self.zone,db_mgmt,self.key_file)
print '\033[1;32m{0}\033[0m'.format(cmd)
rsp = os.popen(cmd).read()
print rsp
if int(max(self.rex_failed_count.findall(rsp)))>0 or int(max(self.rex_unreachable_count.findall(rsp)))>0:
print '\033[1;31mPlaybook执行出现错误,后续操作已中断\033[0m'
return False
return True
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# author tiexin.yang@yoho.cn
import os
import sys
import json
import argparse
import re
class dns_handler():
def __init__(self,zone,project_home,dns_playbook,key_file):
self.zone = zone
self.rex_failed_count = re.compile(r'failed=(\d+)')
self.rex_unreachable_count = re.compile(r'unreachable=(\d+)')
self.project_home = project_home
self.dns_playbook = dns_playbook
self.key_file = key_file
def start(self):
print '\033[1;32m开始执行DNS双中心切换...\033[0m'
if self.zone == 'zone1':
for zoneNum in [1,2]:
cmd = 'ansible-playbook -i {0}/inventories/az{1}/hosts {2} -e confirmation=DNS --key-file={3}'.format(self.project_home,zoneNum,self.dns_playbook,self.key_file)
print '\033[1;32m{0}\033[0m'.format(cmd)
rsp = os.popen(cmd).read()
print rsp
if int(max(self.rex_failed_count.findall(rsp)))>0 or int(max(self.rex_unreachable_count.findall(rsp)))>0:
print '\033[1;31mPlaybook执行出现错误,后续操作已中断\033[0m'
return False
elif self.zone == 'zone2':
for zoneNum in [1,2]:
extra_vars = '{0}/inventories/az{1}/group_vars/.switch.yml.tmp'.format(self.project_home,zoneNum)
cmd = 'ansible-playbook -i {0}/inventories/az{1}/hosts {2} -e "@{3}" -e confirmation=DNS --key-file={4}'.format(self.project_home,zoneNum,self.dns_playbook,extra_vars,self.key_file)
print '\033[1;32m{0}\033[0m'.format(cmd)
rsp = os.popen(cmd).read()
print rsp
if int(max(self.rex_failed_count.findall(rsp)))>0 or int(max(self.rex_unreachable_count.findall(rsp)))>0:
print '\033[1;31mPlaybook执行出现错误,后续操作已中断\033[0m'
return False
return True
# YOHO Center Switch
## 准备`ansible`的环境
>1.创建用户: `useradd ansible -g root -d /home/ansible`.
>
>2.安装`ansible`
>
>3.将被管理的服务器的`private key` copy 到 `~/.ssh/`
>
>4.`git clone` 本项目即可
## 依赖:
>仅支持大于等于2.7.3并小于3.0的python版本
>
>python模块: qcloud、ansible
>
>安装python模块: pip install qcloud ansible
>
>依赖脚本: switch_lb.py (http://git.yoho.cn/ops/yoho-ansible-roles/blob/master/scripts/switch_lb.py) clone本项目即可无需单独下载
### `switch.ini`: `switch.ini`的配置
>参考scripts/switch/switch.ini
## How to Run
>根据需要修改本地配置文件switch.ini(http://git.yoho.cn/ops/yoho-ansible-roles/blob/master/scripts/switch/switch.ini)
>
>需在配置文件中准确填写相关的密钥、密码、负载均衡等信息
>
>需进入脚本所在目录下执行脚本(/home/ansible/yoho-ansible-roles/scripts/switch)
```
cd /home/ansible/yoho-ansible-roles/scripts/switch
python center_switch.py --config=switch.ini
```
[base]
#Must be zone1 or zone2
zone = ZONE_NAME
project_home = /home/ansible/yoho-ansible-roles/
#Private key file to connect to each host
key_file = /root/.ssh/id_rsa
[qcloud]
#Qcloud Secret ID
secretId = *******************
#Qcloud Secret Key
secretKey = *************
#app-java CLB id in zone1
az1_lb_id = AZ1_CLB_ID
#app-java-az2 CLB id in zone2
az2_lb_id = AZ2_CLB_ID
#app-java-az3-灰度 CLB id in zone3
az3_lb_id = AZ3_CLB_ID
#yoho-inner-lb-az1 CLBin zone1
az1_innerlb_id = AZ1_INNER_CLB_ID
[cobar]
#Cobar mysql password
passwd = *****
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# author tiexin.yang@yoho.cn
import os
import sys
import json
import argparse
import re
class vars_maker():
"""
作用: 一区二区之间进行双中心切换(一区切换至二区)前生成临时的参数文件,用于覆盖原有的DNS配置,最终生成临时文件/home/ansible/yoho-ansible-roles/inventories/.switch.yml
Demo:
demo = vars_maker()
demo.start()
"""
def __init__(self, az1_vars, az2_vars, az3_vars, zone1_tmp_yml, zone2_tmp_yml, zone3_tmp_yml):
self.az1_write_map = {}
self.az2_read_map = {}
self.tmp_dns_map = {}
self.az1_vars = az1_vars
self.az2_vars = az2_vars
self.az3_vars = az3_vars
self.zone1_tmp_yml = zone1_tmp_yml
self.zone2_tmp_yml = zone2_tmp_yml
self.zone3_tmp_yml = zone3_tmp_yml
self.target_dns = [
"db_cms_write",
"db_passport_write",
"db_shops_write",
"db_order_write",
"db_uic_write",
"db_ordersplit_1_write",
"db_ordersplit_2_write",
"db_ordersplit_3_write",
"db_ordersplit_4_write"
]
def splitVar(self,entry):
#将参数配置文件根据格式dns:ip分割成key,value形式
ip = entry.split(':')[-1].strip()
dns = entry.split(':')[0].strip().replace('_write','').replace('_read','')
return dns,ip
def load_vars(self):
#加载一区二区的yml参数文件并录入字典对象
for entry in self.az1_vars:
if 'write' in entry:
dns,ip = self.splitVar(entry)
self.az1_write_map[dns] = ip
for entry in self.az2_vars:
if 'read' in entry:
dns,ip = self.splitVar(entry)
self.az2_read_map[dns] = ip
print 'Write DNS:'
print json.dumps(self.az1_write_map,indent=4)
print 'Read DNS:'
print json.dumps(self.az2_read_map,indent=4)
return True
def update_tmp_vars(self,zoneNum=1):
#创建临时切换参数文件
if zoneNum == 1:
tmp_yml = self.zone1_tmp_yml
var_rows = self.az1_vars
elif zoneNum == 2:
tmp_yml = self.zone2_tmp_yml
var_rows = self.az2_vars
elif zoneNum == 3:
tmp_yml = self.zone3_tmp_yml
var_rows = self.az3_vars
else:
print 'Wrong zone number! Must be integer in value [1,2,3]!'
return False
with open(tmp_yml,'w') as f:
for row in var_rows:
write_matched = False
for write_dns in self.tmp_dns_map:
if write_dns in row and write_dns in self.target_dns:
new_dns_entry = '{0}: {1}\n'.format(write_dns,self.tmp_dns_map[write_dns])
f.write(new_dns_entry)
write_matched = True
break
if write_matched:
continue
else:
f.write(row+'\n')
return True
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():
tmp_dns = '{0}_write'.format(zone1_dns)
tmp_ip = self.az2_read_map[zone1_dns]
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]:
self.update_tmp_vars(zoneNum)
return True