Showing
10 changed files
with
22 additions
and
400 deletions
playbooks/yoho.dns.master_to_az2.yml
0 → 100644
1 | +--- | ||
2 | + | ||
3 | +# dns server reconfigure to match: mysql master switch to az2 | ||
4 | +- hosts: nameserver-master | ||
5 | + vars_prompt: | ||
6 | + - name: "confirmation" | ||
7 | + prompt: "Sync and reload dns server. Please Enter DNS to continue." | ||
8 | + private: no | ||
9 | + remote_user: root | ||
10 | + vars: | ||
11 | + db_cms_write: 10.66.106.11 | ||
12 | + db_passport_write: 10.66.106.8 | ||
13 | + db_shops_write: 10.66.106.3 | ||
14 | + db_order_write: 10.66.106.13 | ||
15 | + db_message_write: 10.66.106.5 | ||
16 | + db_uic_write: 10.66.106.12 | ||
17 | + db_ordersplit_1_write: 10.66.106.6 | ||
18 | + db_ordersplit_2_write: 10.66.106.9 | ||
19 | + db_ordersplit_3_write: 10.66.106.15 | ||
20 | + db_ordersplit_4_write: 10.66.106.16 | ||
21 | + roles: | ||
22 | + - yoho.dns |
scripts/switch/__init__.py
deleted
100644 → 0
scripts/switch/center_switch.py
deleted
100644 → 0
1 | -#!/usr/bin/python | ||
2 | -# -*- coding: UTF-8 -*- | ||
3 | -# author tiexin.yang@yoho.cn | ||
4 | - | ||
5 | - | ||
6 | -import os | ||
7 | -import sys | ||
8 | -sys.path.append('../') | ||
9 | -import json | ||
10 | -import argparse | ||
11 | -from switch_lb import LBSwitch | ||
12 | -import re | ||
13 | -import ConfigParser | ||
14 | -from vars import vars_maker | ||
15 | -from db import db_handler | ||
16 | -from cobar import cobar_handler | ||
17 | -from clb import clb_handler | ||
18 | -from dns import dns_handler | ||
19 | - | ||
20 | - | ||
21 | - | ||
22 | -class switch_handler(): | ||
23 | - def __init__(self,config_file='switch.ini'): | ||
24 | - config = ConfigParser.ConfigParser() | ||
25 | - config.readfp(open(config_file)) | ||
26 | - | ||
27 | - #Load Base params | ||
28 | - self.project_home = config.get('base','project_home').rstrip('/') | ||
29 | - self.key_file = config.get('base','key_file') | ||
30 | - self.zone = config.get('base','zone') | ||
31 | - | ||
32 | - #Load DB params | ||
33 | - self.db_playbook = '{0}/playbooks/yoho.switch-db.yml'.format(self.project_home) | ||
34 | - | ||
35 | - #Load Cobar params | ||
36 | - self.passwd = config.get('cobar','passwd') | ||
37 | - self.cobar_playbook = '{0}/playbooks/yoho.reload-cobar.yml'.format(self.project_home) | ||
38 | - | ||
39 | - #Load CLB params | ||
40 | - self.secretId = config.get('qcloud','secretId') | ||
41 | - self.secretKey = config.get('qcloud','secretKey') | ||
42 | - self.az1_lb_id = config.get('qcloud','az1_lb_id') | ||
43 | - self.az2_lb_id = config.get('qcloud','az2_lb_id') | ||
44 | - self.az3_lb_id = config.get('qcloud','az3_lb_id') | ||
45 | - self.az1_innerlb_id = config.get('qcloud','az1_innerlb_id') | ||
46 | - | ||
47 | - #Load DNS params | ||
48 | - self.zone1_yml = '{0}/inventories/az1/group_vars/all.yml'.format(self.project_home) | ||
49 | - self.zone2_yml = '{0}/inventories/az2/group_vars/all.yml'.format(self.project_home) | ||
50 | - self.zone3_yml = '{0}/inventories/az3/group_vars/all.yml'.format(self.project_home) | ||
51 | - self.zone1_tmp_yml = '{0}/inventories/az1/group_vars/.switch.yml.tmp'.format(self.project_home) | ||
52 | - self.zone2_tmp_yml = '{0}/inventories/az2/group_vars/.switch.yml.tmp'.format(self.project_home) | ||
53 | - self.zone3_tmp_yml = '{0}/inventories/az3/group_vars/.switch.yml.tmp'.format(self.project_home) | ||
54 | - self.az1_vars = open(self.zone1_yml).read().strip('\n').split('\n') | ||
55 | - self.az2_vars = open(self.zone2_yml).read().strip('\n').split('\n') | ||
56 | - self.az3_vars = open(self.zone3_yml).read().strip('\n').split('\n') | ||
57 | - self.dns_playbook = '{0}/playbooks/yoho.dns.yml'.format(self.project_home) | ||
58 | - | ||
59 | - | ||
60 | - def run(self): | ||
61 | - os.chdir(self.project_home) | ||
62 | - varsMarker = vars_maker(self.az1_vars,self.az2_vars,self.az3_vars,self.zone1_tmp_yml,self.zone2_tmp_yml,self.zone3_tmp_yml) | ||
63 | - dbHandler = db_handler(self.zone,self.project_home,self.db_playbook,self.key_file) | ||
64 | - dnsHandler = dns_handler(self.zone,self.project_home,self.dns_playbook,self.key_file) | ||
65 | - cobarHandler = cobar_handler(self.zone,self.project_home,self.cobar_playbook,self.key_file,self.passwd) | ||
66 | - 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) | ||
67 | - | ||
68 | - #Switch order: vars, db, dns, cobar, clb | ||
69 | - #if varsMarker.start() and dbHandler.start() and dnsHandler.start() and cobarHandler.start() and clbHandler.start(): | ||
70 | - if varsMarker.start() and dbHandler.start() and dnsHandler.start(): | ||
71 | - print '\033[1;32m双中心切换任务全部正常结束\033[0m' | ||
72 | - | ||
73 | -if __name__ == '__main__': | ||
74 | - parser = argparse.ArgumentParser() | ||
75 | - parser.add_argument('--config',type=str,help='Center switch configuration file path') | ||
76 | - args = parser.parse_args() | ||
77 | - switchHandler = switch_handler(config_file=args.config) | ||
78 | - switchHandler.run() |
scripts/switch/clb.py
deleted
100644 → 0
1 | -#!/usr/bin/python | ||
2 | -# -*- coding: UTF-8 -*- | ||
3 | -# author tiexin.yang@yoho.cn | ||
4 | - | ||
5 | - | ||
6 | -import os | ||
7 | -import sys | ||
8 | -import json | ||
9 | -import argparse | ||
10 | -from switch_lb import LBSwitch | ||
11 | - | ||
12 | - | ||
13 | -class clb_handler(): | ||
14 | - def __init__(self, secretId, secretKey, az1_lb_id, az2_lb_id, az3_lb_id, az1_innerlb_id, zone): | ||
15 | - self.zone = zone | ||
16 | - self.clbClient = LBSwitch(secretId, secretKey, az1_lb_id, az2_lb_id, az3_lb_id, az1_innerlb_id) | ||
17 | - | ||
18 | - def start(self): | ||
19 | - if self.zone == 'zone1': | ||
20 | - ret = self.clbClient.all_to_az1() | ||
21 | - elif self.zone == 'zone2': | ||
22 | - ret = self.clbClient.all_to_az2() | ||
23 | - else: | ||
24 | - print 'Zone name {0} can not be recongnized'.format(self.zone) | ||
25 | - return True |
scripts/switch/cobar.py
deleted
100644 → 0
1 | -#!/usr/bin/python | ||
2 | -# -*- coding: UTF-8 -*- | ||
3 | -# author tiexin.yang@yoho.cn | ||
4 | - | ||
5 | - | ||
6 | -import os | ||
7 | -import sys | ||
8 | -import json | ||
9 | -import argparse | ||
10 | -import re | ||
11 | - | ||
12 | - | ||
13 | -class cobar_handler(): | ||
14 | - def __init__(self,zone,project_home,cobar_playbook,key_file,passwd): | ||
15 | - self.zone = zone | ||
16 | - self.rex_failed_count = re.compile(r'failed=(\d+)') | ||
17 | - self.rex_unreachable_count = re.compile(r'unreachable=(\d+)') | ||
18 | - self.project_home = project_home | ||
19 | - self.cobar_playbook = cobar_playbook | ||
20 | - self.key_file = key_file | ||
21 | - self.passwd = passwd | ||
22 | - | ||
23 | - def start(self): | ||
24 | - print '\033[1;32m重载cobar中间件...\033[0m' | ||
25 | - for zoneNum in [1,2]: | ||
26 | - 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) | ||
27 | - print '\033[1;32m{0}\033[0m'.format(cmd) | ||
28 | - rsp = os.popen(cmd).read() | ||
29 | - print rsp | ||
30 | - if int(max(self.rex_failed_count.findall(rsp)))>0 or int(max(self.rex_unreachable_count.findall(rsp)))>0: | ||
31 | - print '\033[1;31mPlaybook执行出现错误,后续操作已中断\033[0m' | ||
32 | - return False | ||
33 | - return True |
scripts/switch/db.py
deleted
100644 → 0
1 | -#!/usr/bin/python | ||
2 | -# -*- coding: UTF-8 -*- | ||
3 | -# author tiexin.yang@yoho.cn | ||
4 | - | ||
5 | - | ||
6 | -import os | ||
7 | -import sys | ||
8 | -import json | ||
9 | -import argparse | ||
10 | -import re | ||
11 | - | ||
12 | - | ||
13 | -class db_handler(): | ||
14 | - def __init__(self,zone,project_home,db_playbook,key_file): | ||
15 | - self.zone = zone | ||
16 | - self.rex_failed_count = re.compile(r'failed=(\d+)') | ||
17 | - self.rex_unreachable_count = re.compile(r'unreachable=(\d+)') | ||
18 | - self.project_home = project_home | ||
19 | - self.db_playbook = db_playbook | ||
20 | - self.key_file = key_file | ||
21 | - | ||
22 | - | ||
23 | - def start(self): | ||
24 | - print '\033[1;32m开始执行数据库双中心切换...\033[0m' | ||
25 | - db_mgmt = 'db-mgmt-zone1' if self.zone == 'zone1' else 'db-mgmt-zone2' | ||
26 | - 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) | ||
27 | - print '\033[1;32m{0}\033[0m'.format(cmd) | ||
28 | - rsp = os.popen(cmd).read() | ||
29 | - print rsp | ||
30 | - if int(max(self.rex_failed_count.findall(rsp)))>0 or int(max(self.rex_unreachable_count.findall(rsp)))>0: | ||
31 | - print '\033[1;31mPlaybook执行出现错误,后续操作已中断\033[0m' | ||
32 | - return False | ||
33 | - return True |
scripts/switch/dns.py
deleted
100644 → 0
1 | -#!/usr/bin/python | ||
2 | -# -*- coding: UTF-8 -*- | ||
3 | -# author tiexin.yang@yoho.cn | ||
4 | - | ||
5 | - | ||
6 | -import os | ||
7 | -import sys | ||
8 | -import json | ||
9 | -import argparse | ||
10 | -import re | ||
11 | - | ||
12 | - | ||
13 | -class dns_handler(): | ||
14 | - def __init__(self,zone,project_home,dns_playbook,key_file): | ||
15 | - self.zone = zone | ||
16 | - self.rex_failed_count = re.compile(r'failed=(\d+)') | ||
17 | - self.rex_unreachable_count = re.compile(r'unreachable=(\d+)') | ||
18 | - self.project_home = project_home | ||
19 | - self.dns_playbook = dns_playbook | ||
20 | - self.key_file = key_file | ||
21 | - | ||
22 | - def start(self): | ||
23 | - print '\033[1;32m开始执行DNS双中心切换...\033[0m' | ||
24 | - if self.zone == 'zone1': | ||
25 | - for zoneNum in [1,2]: | ||
26 | - 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) | ||
27 | - print '\033[1;32m{0}\033[0m'.format(cmd) | ||
28 | - rsp = os.popen(cmd).read() | ||
29 | - print rsp | ||
30 | - if int(max(self.rex_failed_count.findall(rsp)))>0 or int(max(self.rex_unreachable_count.findall(rsp)))>0: | ||
31 | - print '\033[1;31mPlaybook执行出现错误,后续操作已中断\033[0m' | ||
32 | - return False | ||
33 | - | ||
34 | - elif self.zone == 'zone2': | ||
35 | - for zoneNum in [1,2]: | ||
36 | - extra_vars = '{0}/inventories/az{1}/group_vars/.switch.yml.tmp'.format(self.project_home,zoneNum) | ||
37 | - 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) | ||
38 | - print '\033[1;32m{0}\033[0m'.format(cmd) | ||
39 | - rsp = os.popen(cmd).read() | ||
40 | - print rsp | ||
41 | - if int(max(self.rex_failed_count.findall(rsp)))>0 or int(max(self.rex_unreachable_count.findall(rsp)))>0: | ||
42 | - print '\033[1;31mPlaybook执行出现错误,后续操作已中断\033[0m' | ||
43 | - return False | ||
44 | - return True |
scripts/switch/readme.md
deleted
100644 → 0
1 | -# YOHO Center Switch | ||
2 | - | ||
3 | - | ||
4 | -## 准备`ansible`的环境 | ||
5 | - | ||
6 | - | ||
7 | ->1.创建用户: `useradd ansible -g root -d /home/ansible`. | ||
8 | -> | ||
9 | ->2.安装`ansible` | ||
10 | -> | ||
11 | ->3.将被管理的服务器的`private key` copy 到 `~/.ssh/` | ||
12 | -> | ||
13 | ->4.`git clone` 本项目即可 | ||
14 | - | ||
15 | - | ||
16 | -## 依赖: | ||
17 | - | ||
18 | - | ||
19 | ->仅支持大于等于2.7.3并小于3.0的python版本 | ||
20 | -> | ||
21 | ->python模块: qcloud、ansible | ||
22 | -> | ||
23 | ->安装python模块: pip install qcloud ansible | ||
24 | -> | ||
25 | ->依赖脚本: switch_lb.py (http://git.yoho.cn/ops/yoho-ansible-roles/blob/master/scripts/switch_lb.py) clone本项目即可无需单独下载 | ||
26 | - | ||
27 | - | ||
28 | -### `switch.ini`: `switch.ini`的配置 | ||
29 | - | ||
30 | ->参考scripts/switch/switch.ini | ||
31 | - | ||
32 | - | ||
33 | - | ||
34 | - | ||
35 | - | ||
36 | - | ||
37 | - | ||
38 | -## How to Run | ||
39 | - | ||
40 | - | ||
41 | ->根据需要修改本地配置文件switch.ini(http://git.yoho.cn/ops/yoho-ansible-roles/blob/master/scripts/switch/switch.ini) | ||
42 | -> | ||
43 | ->需在配置文件中准确填写相关的密钥、密码、负载均衡等信息 | ||
44 | -> | ||
45 | ->需进入脚本所在目录下执行脚本(/home/ansible/yoho-ansible-roles/scripts/switch) | ||
46 | - | ||
47 | -``` | ||
48 | - | ||
49 | -cd /home/ansible/yoho-ansible-roles/scripts/switch | ||
50 | -python center_switch.py --config=switch.ini | ||
51 | -``` |
scripts/switch/switch.ini
deleted
100644 → 0
1 | -[base] | ||
2 | -#Must be zone1 or zone2 | ||
3 | -zone = ZONE_NAME | ||
4 | -project_home = /home/ansible/yoho-ansible-roles/ | ||
5 | -#Private key file to connect to each host | ||
6 | -key_file = /root/.ssh/id_rsa | ||
7 | - | ||
8 | -[qcloud] | ||
9 | -#Qcloud Secret ID | ||
10 | -secretId = ******************* | ||
11 | -#Qcloud Secret Key | ||
12 | -secretKey = ************* | ||
13 | -#app-java CLB id in zone1 | ||
14 | -az1_lb_id = AZ1_CLB_ID | ||
15 | -#app-java-az2 CLB id in zone2 | ||
16 | -az2_lb_id = AZ2_CLB_ID | ||
17 | -#app-java-az3-灰度 CLB id in zone3 | ||
18 | -az3_lb_id = AZ3_CLB_ID | ||
19 | -#yoho-inner-lb-az1 CLBin zone1 | ||
20 | -az1_innerlb_id = AZ1_INNER_CLB_ID | ||
21 | - | ||
22 | -[cobar] | ||
23 | -#Cobar mysql password | ||
24 | -passwd = ***** |
scripts/switch/vars.py
deleted
100644 → 0
1 | -#!/usr/bin/python | ||
2 | -# -*- coding: UTF-8 -*- | ||
3 | -# author tiexin.yang@yoho.cn | ||
4 | - | ||
5 | - | ||
6 | -import os | ||
7 | -import sys | ||
8 | -import json | ||
9 | -import argparse | ||
10 | -import re | ||
11 | - | ||
12 | -class vars_maker(): | ||
13 | - | ||
14 | - """ | ||
15 | - 作用: 一区二区之间进行双中心切换(一区切换至二区)前生成临时的参数文件,用于覆盖原有的DNS配置,最终生成临时文件/home/ansible/yoho-ansible-roles/inventories/.switch.yml | ||
16 | - Demo: | ||
17 | - demo = vars_maker() | ||
18 | - demo.start() | ||
19 | - """ | ||
20 | - def __init__(self, az1_vars, az2_vars, az3_vars, zone1_tmp_yml, zone2_tmp_yml, zone3_tmp_yml): | ||
21 | - self.az1_write_map = {} | ||
22 | - self.az2_read_map = {} | ||
23 | - self.tmp_dns_map = {} | ||
24 | - | ||
25 | - self.az1_vars = az1_vars | ||
26 | - self.az2_vars = az2_vars | ||
27 | - self.az3_vars = az3_vars | ||
28 | - self.zone1_tmp_yml = zone1_tmp_yml | ||
29 | - self.zone2_tmp_yml = zone2_tmp_yml | ||
30 | - self.zone3_tmp_yml = zone3_tmp_yml | ||
31 | - | ||
32 | - self.target_dns = [ | ||
33 | - "db_cms_write", | ||
34 | - "db_passport_write", | ||
35 | - "db_shops_write", | ||
36 | - "db_order_write", | ||
37 | - "db_uic_write", | ||
38 | - "db_ordersplit_1_write", | ||
39 | - "db_ordersplit_2_write", | ||
40 | - "db_ordersplit_3_write", | ||
41 | - "db_ordersplit_4_write" | ||
42 | - ] | ||
43 | - | ||
44 | - def splitVar(self,entry): | ||
45 | - #将参数配置文件根据格式dns:ip分割成key,value形式 | ||
46 | - ip = entry.split(':')[-1].strip() | ||
47 | - dns = entry.split(':')[0].strip().replace('_write','').replace('_read','') | ||
48 | - return dns,ip | ||
49 | - | ||
50 | - def load_vars(self): | ||
51 | - #加载一区二区的yml参数文件并录入字典对象 | ||
52 | - for entry in self.az1_vars: | ||
53 | - if 'write' in entry: | ||
54 | - dns,ip = self.splitVar(entry) | ||
55 | - self.az1_write_map[dns] = ip | ||
56 | - | ||
57 | - for entry in self.az2_vars: | ||
58 | - if 'read' in entry: | ||
59 | - dns,ip = self.splitVar(entry) | ||
60 | - self.az2_read_map[dns] = ip | ||
61 | - | ||
62 | - print 'Write DNS:' | ||
63 | - print json.dumps(self.az1_write_map,indent=4) | ||
64 | - | ||
65 | - print 'Read DNS:' | ||
66 | - print json.dumps(self.az2_read_map,indent=4) | ||
67 | - return True | ||
68 | - | ||
69 | - def update_tmp_vars(self,zoneNum=1): | ||
70 | - #创建临时切换参数文件 | ||
71 | - if zoneNum == 1: | ||
72 | - tmp_yml = self.zone1_tmp_yml | ||
73 | - var_rows = self.az1_vars | ||
74 | - elif zoneNum == 2: | ||
75 | - tmp_yml = self.zone2_tmp_yml | ||
76 | - var_rows = self.az2_vars | ||
77 | - elif zoneNum == 3: | ||
78 | - tmp_yml = self.zone3_tmp_yml | ||
79 | - var_rows = self.az3_vars | ||
80 | - else: | ||
81 | - print 'Wrong zone number! Must be integer in value [1,2,3]!' | ||
82 | - return False | ||
83 | - | ||
84 | - with open(tmp_yml,'w') as f: | ||
85 | - for row in var_rows: | ||
86 | - write_matched = False | ||
87 | - for write_dns in self.tmp_dns_map: | ||
88 | - if write_dns in row and write_dns in self.target_dns: | ||
89 | - new_dns_entry = '{0}: {1}\n'.format(write_dns,self.tmp_dns_map[write_dns]) | ||
90 | - f.write(new_dns_entry) | ||
91 | - write_matched = True | ||
92 | - break | ||
93 | - if write_matched: | ||
94 | - continue | ||
95 | - else: | ||
96 | - f.write(row+'\n') | ||
97 | - return True | ||
98 | - | ||
99 | - | ||
100 | - def start(self): | ||
101 | - print '\033[1;32m生成临时DNS参数文件...\033[0m' | ||
102 | - self.load_vars() | ||
103 | - #在一区切换至二区时,一二三区的write dns应当全部解析到二区的read dns | ||
104 | - for zone1_dns in self.az1_write_map.keys(): | ||
105 | - tmp_dns = '{0}_write'.format(zone1_dns) | ||
106 | - tmp_ip = self.az2_read_map[zone1_dns] | ||
107 | - self.tmp_dns_map[tmp_dns] = tmp_ip | ||
108 | - print 'Write DNS after switch:' | ||
109 | - print json.dumps(self.tmp_dns_map,indent=2) | ||
110 | - for zoneNum in [1,2,3]: | ||
111 | - self.update_tmp_vars(zoneNum) | ||
112 | - return True |
-
Please register or login to post a comment