mysql_failover.py
1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import yaml
import sys
import os
def get_db(key):
return key[:key.rfind('_')]
def get_extra_vars( orig_master_ip, new_master_ip, az = 'aws',):
extra_vars = {}
group_var_file = "%s/inventories/%s/group_vars/all.yml" %(os.getcwd(),az)
with open(group_var_file, 'r') as stream:
g_vars = yaml.safe_load(stream)
# find db key
dbs = []
for k, v in g_vars.items():
if k.startswith('db') and v == orig_master_ip:
dbs.append(get_db(k))
# find all key need to replace
for k, v in g_vars.items():
if get_db(k) in dbs:
extra_vars[k] = new_master_ip
return extra_vars
if __name__ == '__main__':
# read arg
orig_master_ip = sys.argv[1]
new_master_ip = sys.argv[2]
az = sys.argv[3]
msg = ''
extra_vars = get_extra_vars(orig_master_ip, new_master_ip, az)
for key, value in extra_vars.items():
msg = msg + " " + key + "=" + value
print(msg.strip())
sys.exit(0)