forked from membase/membase-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode.py
289 lines (241 loc) · 9.15 KB
/
node.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""
Node class
This class implements methods that will pertain to
rebalance, add, remove, stop rebalance
"""
import time
import os
import sys
import mbutil
from membase_info import usage
from restclient import *
# Note: I would like to do this differently. I'm thinking
# listservers should be moved into node since it does pertain
# to nodes. TBD
from listservers import *
# the rest commands and associated URIs for various node operations
rest_cmds = {
'rebalance' :'/controller/rebalance',
'rebalance-stop' :'/controller/stopRebalance',
'rebalance-status' :'/pools/default/rebalanceProgress',
'eject-server' :'/controller/ejectNode',
'server-add' :'/controller/addNode',
're-add-server' :'/controller/addNode',
'failover' :'/controller/failOver',
}
server_no_remove = [
'server-add',
're-add-server',
'rebalance-status',
'rebalance-stop',
]
server_no_add = [
'rebalance-status',
'rebalance-stop',
'failover',
]
# Map of operations and the HTTP methods used against the REST interface
methods = {
'rebalance' :'POST',
'rebalance-stop' :'POST',
'rebalance-status' :'GET',
'eject-server' :'POST',
'server-add' :'POST',
'failover' :'POST',
're-add-server' :'POST',
}
# Map of HTTP success code, success message and error message for
# handling HTTP response properly
class Node:
def __init__(self):
"""
constructor
"""
# defaults
self.rest_cmd = rest_cmds['rebalance-status']
self.method = 'GET'
self.debug = False
self.verbose = False
self.server = ''
self.port = ''
self.user = ''
self.password = ''
self.params = {}
self.output = 'standard'
def runCmd(self,
cmd,
server,
port,
user,
password,
opts):
"""
runCmd - perform the operation. This is where all the
functionaly for the given operation is implemented
"""
output_result = ''
self.rest_cmd = rest_cmds[cmd]
self.method = methods[cmd]
self.server = server
self.port = int(port)
self.user = user
self.password = password
servers = self.processOpts(cmd, opts)
if self.debug:
print "servers ", servers
if cmd in ('server-add', 'rebalance'):
self.addServers(servers['add'])
if cmd == 'rebalance':
self.rebalance(servers)
elif cmd == 'rebalance-status':
output_result = self.rebalanceStatus()
print output_result
def processOpts(self, cmd, opts):
"""
processOpts()
This method sets standard opts
note: use of a server key keeps optional
args aligned with server
"""
servers = {}
servers['add'] = {}
servers['remove'] = {}
# don't allow options that don't correspond to given commands
for o, a in opts:
usage_msg = "You cannot specify the %s option with '%s'"\
% (o, cmd)
if o in ( "-r", "--server-remove", "--server-remove-username", \
"--server-remove-password"):
if cmd in server_no_remove:
print "cmd %s usage_msg %s" % (cmd, usage_msg)
usage(usage_msg)
elif o in ( "-a", "--server-add", "--server-add-username", \
"--server-add-password"):
if cmd in server_no_add:
print "cmd %s usage_msg %s" % (cmd, usage_msg)
usage(usage_msg)
for o, a in opts:
if o in ("-a","--server-add"):
server = a
servers['add'][server] = { 'user':'', 'password':''}
elif o in ( "-r", "--server-remove"):
server = a
servers['remove'][server] = { 'user':'', 'password':''}
elif o == "--server-add-username":
servers['add'][server]['user'] = a
elif o == "--server-add-password":
servers['add'][server]['password'] = a
elif o == "--server-remove-username":
servers['remove'][server]['user'] = a
elif o == "--server-remove-password":
servers['remove'][server]['password'] = a
elif o in ('-o', '--output'):
if a == 'json':
self.output = a
elif o in ('-d', '--debug'):
self.debug = True
elif o in ('-v', '--verbose'):
self.verbose = True
return servers
def getEjectList(self, servers):
ejectlist = []
for server in servers['remove']:
ejectlist.append(server)
return ejectlist
def addServers(self, servers):
for server in servers:
user = servers[server]['user']
password = servers[server]['password']
output_result = self.serverAdd(server,
user,
password)
print output_result
def serverAdd(self, server, user='', password=''):
opts = {}
rest = restclient.RestClient(self.server,
self.port,
{'debug':self.debug})
rest.setParam('hostname', server)
if user and password:
rest.setParam('user', user)
rest.setParam('password', password)
opts['error_msg'] = "Unable to add %s" % server
opts['success_msg'] = "Added %s" % server
output_result = rest.restCmd('POST',
rest_cmds['server-add'],
user,
password,
opts)
return output_result
def setNodes(self, ejectlist):
"""
setNodes - this obtains the list of nodes
in order from knownNodes to be passed to a rebalance
"""
known_nodes = ''
eject_nodes = ''
listservers = Listservers()
known_nodes_list = listservers.getNodes(
listservers.getData(self.server,
self.port,
self.user,
self.password))
nodes = []
ejectnodes = []
for node in known_nodes_list:
nodes.append(node['otpNode'])
for ejectee in ejectlist:
host, port = mbutil.hostport(ejectee)
if host == node['hostname']:
ejectnodes.append(node['otpNode'])
eject_nodes = eject_nodes.join(',').join(ejectnodes)
known_nodes = known_nodes.join(',').join(nodes)
# a list of ejectNodes and knownNodes is needed (comma-separated)
return({ 'knownNodes':known_nodes, 'ejectedNodes':eject_nodes})
def rebalance(self, servers):
opts = {}
ejectlist = self.getEjectList(servers)
nodes = self.setNodes(ejectlist)
# POST response will be handled except server-add because that is
# handled in a loop per server in command line list
rest = restclient.RestClient(self.server,
self.port,
{'debug':self.debug})
rest.setParam('knownNodes', nodes['knownNodes'])
rest.setParam('ejectedNodes', nodes['ejectedNodes'])
opts['success_msg'] = 'Rebalanced cluster'
opts['error_msg'] = 'Unable to reblance cluster'
output_result = rest.restCmd('POST',
rest_cmds['rebalance'],
self.user,
self.password,
opts)
if self.debug:
print "rebalance POST response: %s" % output_result
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
print "Start rebalance",
while self.rebalanceStatus() == 'running':
print ".",
time.sleep(1)
print output_result
return 0
def rebalanceStatus(self):
rest = restclient.RestClient(self.server,
self.port,
{'debug':self.debug})
opts = { 'error_msg':'Unable to obtain rebalance status'}
output_result = rest.restCmd('GET',
rest_cmds['rebalance-status'],
self.user,
self.password,
opts)
json = rest.getJson(output_result)
if type(json) == type(list()):
return "ERROR: %s" % json[0]
return json['status']
def setParam(self, param, value):
"""
setParam - sets the param dictionary which
is used for POST
"""
self.params[param] = value