splitHost.py
2.27 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
48
49
50
51
52
53
54
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])