-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg_update_node_ips.py
80 lines (62 loc) · 2.36 KB
/
tg_update_node_ips.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import subprocess
import json
import sys
new_ip_json = sys.argv[1]
command = 'gadmin config dump'
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
p_out = p.stdout.read().decode('utf-8')
p_returncode = p.wait()
if p_returncode != 0:
print(p_out)
config_dict = json.loads(p_out)
print('INFO: Current cluster node IP configuration:')
for h in config_dict['System']['HostList']:
print('ID:', h['ID'], 'Hostname:', h['Hostname'])
with open(new_ip_json, 'r') as fh:
new_ip_dict = json.load(fh)
print('INFO: New cluster node IP configuration:')
for k in new_ip_dict.keys():
print('ID:', k, 'Hostname:', new_ip_dict[k])
# update the HostList
host_cnt = len(config_dict['System']['HostList'])
for i in range(host_cnt):
id = config_dict['System']['HostList'][i]['ID']
if new_ip_dict.get(id):
# this node needs to be updated
config_dict['System']['HostList'][i]['Hostname'] = new_ip_dict[id]
config_str = json.dumps(config_dict['System']['HostList'])
command = "gadmin config set System.HostList '" + config_str + "'"
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
p_out = p.stdout.read().decode('utf-8')
p_returncode = p.wait()
if p_returncode != 0:
print(p_out)
exit(1)
print('INFO: ' + command + ' completed successfully.')
print(' ', p_out)
command = 'gadmin config apply -y'
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
p_out = p.stdout.read().decode('utf-8')
p_returncode = p.wait()
if p_returncode != 0:
print(p_out)
exit(2)
print('INFO: ' + command + ' completed successfully.')
print(' ', p_out)
command = 'gadmin stop all && admin start infra && gadmin start all'
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
p_out = p.stdout.read().decode('utf-8')
p_returncode = p.wait()
if p_returncode != 0:
print(p_out)
exit(3)
print('INFO: ' + command + ' completed successfully.')
print(' ', p_out)
print('INFO: Cluster node IP configuration updated to:')
for h in config_dict['System']['HostList']:
print('ID:', h['ID'], 'Hostname:', h['Hostname'])
print('******************************************')
print('INFO: Rebooting all nodes may be required')
print('******************************************')