forked from membase/membase-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlistservers.py
More file actions
128 lines (104 loc) · 3.28 KB
/
listservers.py
File metadata and controls
128 lines (104 loc) · 3.28 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
listservers class
This class implements methods that will list servers within a
membase cluster
"""
import pprint
import restclient
from membase_info import usage
class Listservers:
def __init__(self):
"""
constructor
"""
self.rest_cmd = '/pools/default'
self.method = 'GET'
self.debug = False
self.verbose = False
self.output = 'standard'
self.user = ''
self.password = ''
self.error = ''
def runCmd(
self,
cmd,
server,
port,
user,
password,
opts,
):
"""
runCmd()
This method is the primary method, defined for all command
modules that performs the primary task, in this case, listing
the servers that are part of a Membase cluster
"""
self.server = server
self.port = port
self.user = user
self.password = password
for (o, a) in opts:
if o in ('-o', '--output'):
self.output = a
if o in ('-d', '--debug'):
self.debug = True
if o in ('-v', '--verbose'):
self.verbose = True
data = self.getData(self.server,
self.port,
self.user,
self.password)
if (self.output == 'json'):
print data
else:
if self.verbose:
print 'List of servers within the server %s:%s' % (server, port)
# obtain dict of nodes. If not dict, is error message
nodes = self.getNodes(data)
if type(nodes) == type(list()):
self.printNodes(nodes)
else:
print self.error
def getData(self, server, port, user, password):
"""
getData()
This method obtains the raw json output from the server
The reason for passing arguments which could be obtained
from 'self' is because getData() must be callable externally
"""
self.rest = restclient.RestClient(server,
port,
{'debug':self.debug})
opts = {'error_msg':"Unable to obtain server list"}
data = self.rest.restCmd('GET',
self.rest_cmd,
user,
password)
return data
def getNodes(self, data):
"""
This method deserializes the raw json output and
returns only the nodes
"""
json = self.rest.getJson(data)
#pp = pprint.PrettyPrinter(indent=4)
#pp.pprint(json['nodes'])
# this means an error occured
if type(json) == type(unicode()):
self.error = json
return None
elif type(json) == type(list()):
self.error = json[0]
return None
return json['nodes']
def printNodes(self, nodes):
"""
This method prints the nodes
"""
for node in nodes:
print '%s %s %s' % (node['otpNode'],
node['status'],
node['clusterMembership'])