-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddNode.py
66 lines (56 loc) · 2.15 KB
/
addNode.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
__author__ = 'tatroc'
import requests
import json
import time
from datetime import datetime
import syslog
def add(hostname, knownNodes, cuser, cpassword, a):
# Add node to cluster
payload = {'hostname': hostname}
try:
# find member of cluster
for servers in a:
if servers.hostname != hostname:
cluster_member = servers.hostname
print cluster_member
break
url = 'http://' + hostname + ':8091/controller/addNode'
resp = requests.post('http://' + cluster_member + ':8091/controller/addNode', data=payload, auth=(cuser, cpassword))
except Exception as e:
msg = "Adding node to cluster failed"
print msg
syslog.syslog(syslog.LOG_ERR, msg)
# re-balance cluster
payload = {'knownNodes': knownNodes}
try:
url = 'http://' + hostname + ':8091/controller/rebalance'
r2 = requests.post(url, data=payload, auth=(cuser, cpassword))
except Exception as e:
msg = "Cluster rebalance operation failed"
print msg
syslog.syslog(syslog.LOG_ERR, msg)
# get re-balance progress
try:
url = 'http://' + hostname + ':8091/pools/default/rebalanceProgress'
rebalanceprogress = requests.get(url, auth=(cuser, cpassword))
except Exception as e:
msg = "Connection failed"
print msg
syslog.syslog(syslog.LOG_ERR, msg)
json_data = json.loads(rebalanceprogress.text)
while json_data["status"] == "running":
time.sleep(5)
msg = str(datetime.now()) + " : node " + hostname + " added to cluster, re-balance running"
print msg
syslog.syslog(syslog.LOG_INFO, msg)
try:
url = 'http://' + hostname + ':8091/pools/default/rebalanceProgress'
rebalanceprogress = requests.get(url, auth=(cuser, cpassword))
json_data = json.loads(rebalanceprogress.text)
except Exception as e:
msg = "Connection failed"
print msg
syslog.syslog(syslog.LOG_ERR, msg)
msg = str(datetime.now()) + " : Node added and re-balance complete"
print msg
syslog.syslog(syslog.LOG_INFO, msg)