/
buckets.py
169 lines (152 loc) · 6.07 KB
/
buckets.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from usage import usage
import restclient
rest_cmds = {
'bucket-list': '/pools/default/buckets',
'bucket-flush': '/pools/default/buckets/',
'bucket-delete': '/pools/default/buckets/',
'bucket-create': '/pools/default/buckets/',
'bucket-edit': '/pools/default/buckets/',
'bucket-get': '/pools/default/buckets',
'bucket-stats': '/pools/default/buckets/{0}/stats?zoom=hour',
'bucket-node-stats': '/pools/default/buckets/{0}/stats/{1}?zoom={2}'
}
methods = {
'bucket-list': 'GET',
'bucket-delete': 'DELETE',
'bucket-create': 'POST',
'bucket-edit': 'POST',
'bucket-flush': 'POST',
'bucket-get': 'GET',
'bucket-stats': 'GET',
'bucket-node-stats': 'GET',
}
class Buckets:
def __init__(self):
self.debug = False
self.rest_cmd = rest_cmds['bucket-list']
self.method = 'GET'
def runCmd(self, cmd, server, port,
user, password, opts):
self.user = user
self.password = password
bucketname = ''
buckettype = ''
authtype = 'sasl'
bucketport = '11211'
bucketpassword = ''
bucketramsize = ''
bucketreplication = '1'
output = 'default'
for (o, a) in opts:
if o == '-b' or o == '--bucket':
bucketname = a
if o == '--bucket-type':
buckettype = a
if o == '--bucket-port':
bucketport = a
if o == '--bucket-password':
bucketpassword = a
if o == '--bucket-ramsize':
bucketramsize = a
if o == '--bucket-replica':
bucketreplication = a
if o == '-d' or o == '--debug':
self.debug = True
if o in ('-o', '--output'):
output = a
self.rest_cmd = rest_cmds[cmd]
rest = restclient.RestClient(server, port, {'debug':self.debug})
# get the parameters straight
if cmd in ('bucket-create', 'bucket-edit'):
if bucketname:
rest.setParam('name', bucketname)
if bucketname == "default":
if bucketport and bucketport != "11211":
usage("default bucket must be on port 11211.")
if bucketpassword:
usage("default bucket should only have empty password.")
authtype = 'sasl'
else:
if bucketport == "11211":
authtype = 'sasl'
else:
authtype = 'none'
if bucketpassword:
usage("a sasl bucket is supported only on port 11211.")
if buckettype:
rest.setParam('bucketType', buckettype)
if authtype:
rest.setParam('authType', authtype)
if bucketport:
rest.setParam('proxyPort', bucketport)
if bucketpassword:
rest.setParam('saslPassword', bucketpassword)
if bucketramsize:
rest.setParam('ramQuotaMB', bucketramsize)
if bucketreplication:
rest.setParam('replicaNumber', bucketreplication)
if cmd in ('bucket-delete', 'bucket-flush', 'bucket-edit'):
self.rest_cmd = self.rest_cmd + bucketname
if cmd == 'bucket-flush':
self.rest_cmd = self.rest_cmd + '/controller/doFlush'
opts = {}
opts['error_msg'] = "unable to %s; please check your username (-u) and password (-p);" % cmd
opts['success_msg'] = "%s" % cmd
data = rest.restCmd(methods[cmd], self.rest_cmd,
self.user, self.password, opts)
if cmd in ("bucket-get", "bucket-stats", "bucket-node-stats"):
return rest.getJson(data)
elif cmd == "bucket-list":
if output == 'json':
print data
else:
json = rest.getJson(data)
for bucket in json:
print '%s' % bucket['name']
print ' bucketType: %s' % bucket['bucketType']
print ' authType: %s' % bucket['authType']
if bucket['authType'] == "sasl":
print ' saslPassword: %s' % bucket['saslPassword']
else:
print ' proxyPort: %s' % bucket['proxyPort']
print ' numReplicas: %s' % bucket['replicaNumber']
print ' ramQuota: %s' % bucket['quota']['ram']
print ' ramUsed: %s' % bucket['basicStats']['memUsed']
else:
if output == 'json':
print rest.jsonMessage(data)
else:
print data
class BucketStats:
def __init__(self, bucket_name):
self.debug = False
self.rest_cmd = rest_cmds['bucket-stats'].format(bucket_name)
self.method = 'GET'
def runCmd(self, cmd, server, port,
user, password, opts):
opts = {}
opts['error_msg'] = "unable to %s" % cmd
opts['success_msg'] = "%s" % cmd
#print server, port, cmd, self.rest_cmd
rest = restclient.RestClient(server, port, {'debug':self.debug})
data = rest.restCmd(methods[cmd], self.rest_cmd,
user, password, opts)
return rest.getJson(data)
class BucketNodeStats:
def __init__(self, bucket_name, stat_name, scale):
self.debug = False
self.rest_cmd = rest_cmds['bucket-node-stats'].format(bucket_name, stat_name, scale)
self.method = 'GET'
#print self.rest_cmd
def runCmd(self, cmd, server, port,
user, password, opts):
opts = {}
opts['error_msg'] = "unable to %s" % cmd
opts['success_msg'] = "%s" % cmd
#print server, port, cmd, self.rest_cmd
rest = restclient.RestClient(server, port, {'debug':self.debug})
data = rest.restCmd(methods[cmd], self.rest_cmd,
user, password, opts)
return rest.getJson(data)