-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
231 lines (169 loc) · 7.29 KB
/
main.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
import sys, time, random
# Import pymongo
sys.path.insert(0, 'mongo-python-driver')
from pymongo import ReplicaSetConnection
# Import HA Tools
sys.path.insert(0, 'mongo-python-driver/test/high_availability')
import ha_tools
import tornado.ioloop
import tornado.web
import tornado.template as template
import uuid
import json
new_repl_id = -1
def get_new_repl_id():
global new_repl_id
new_repl_id += 1
return new_repl_id
rs = []
class RsHandler(tornado.web.RequestHandler):
# Reply's tpls extension
_ext = ".json"
# Init template loader with 'tpl' dir
_template = template.Loader('tpl')
def _parse_json(self, data):
try:
json_data = json.loads(data)
except ValueError:
raise tornado.httpserver._BadRequestException(
"Invalid JSON structure."
)
if type(json_data) != dict:
raise tornado.httpserver._BadRequestException(
"We only accept key value objects!"
)
return json_data
def _get_index(self, seq, attribute, value):
return next(index for (index, d) in enumerate(seq) if d[attribute] == value)
def message(self, message):
self.write(self._template.load('message' + self._ext).generate(message=message))
def error(self, message):
raise tornado.httpserver._BadRequestException(
message
)
def get(self):
""" Respond to a GET
"""
self.write('GET')
def post(self, op):
""" Respond to a POST
"""
# Start RS
if op == 'start':
request = self._parse_json(self.request.body)
members = request['members']
rs_id = uuid.uuid4()
rs_name = 'repl' + str(get_new_repl_id())
try:
# start RS and get its' params
res = ha_tools.start_replica_set(members, rs_name=rs_name)
except:
self.error("Couldn't start RS!")
# Let's gather all the info about RS and save it
rs_uri, rs_name, nodes, arbiters = res
# Try to connect to new RS
try:
for _ in range(1 * 60 / 2):
time.sleep(2)
try:
c = ReplicaSetConnection(rs_uri, replicaSet=rs_name)
except:
pass
except:
self.error("Couldn't connect to the new RS: " + rs_uri + ", " + rs_name)
secondaries_uris = []
for secondary in c.secondaries:
secondaries_uris.append('%s:%d' % (secondary))
rs_t = {}
rs_t['id'] = str(rs_id)
rs_t['name'] = rs_name
rs_t['primary'] = rs_uri
rs_t['secondaries'] = secondaries_uris
rs_t['nodes'] = nodes
rs_t['arbiters'] = arbiters
rs.append(rs_t)
self.write(self._template.load(op + self._ext).generate(rs_id=rs_id, rs_uri=rs_uri, rs_name=rs_name))
# FIXME: Some checks should be implemented
# Stop rs
elif op == 'stop':
request = self._parse_json(self.request.body)
rs_id = request['rs']['id']
found_index = self._get_index(rs, 'id', rs_id)
try:
ha_tools.kill_members(rs[found_index]['nodes'].keys(), 2, rs[found_index]['nodes'])
rs_t = rs.pop(found_index)
except:
self.error("Couldn't stop RS!")
rs_id = rs_t['id']
self.write(self._template.load(op + self._ext).generate(rs_id=rs_id))
# Get primary
elif op == 'get_primary':
request = self._parse_json(self.request.body)
rs_id = request['rs']['id']
found_index = self._get_index(rs, 'id', rs_id)
rs_primary_uri = rs[found_index]['primary']
self.write(self._template.load(op + self._ext).generate(rs_id=rs_id, rs_primary_uri=rs_primary_uri))
# Get secondaries
elif op == 'get_secondaries':
request = self._parse_json(self.request.body)
rs_id = request['rs']['id']
found_index = self._get_index(rs, 'id', rs_id)
rs_secondaries_uris = rs[found_index]['secondaries']
self.write(self._template.load(op + self._ext).generate(rs_id=rs_id, rs_secondaries_uris=rs_secondaries_uris))
# Get the arbiters
elif op == 'get_arbiters':
request = self._parse_json(self.request.body)
rs_id = request['rs']['id']
found_index = self._get_index(rs, 'id', rs_id)
rs_arbiters_uris = rs[found_index]['arbiters']
self.write(self._template.load(op + self._ext).generate(rs_id=rs_id, rs_arbiters_uris=rs_arbiters_uris))
# Kill the primary
elif op == 'kill_primary':
request = self._parse_json(self.request.body)
rs_id = request['rs']['id']
found_index = self._get_index(rs, 'id', rs_id)
try:
rs_primary_uri = rs[found_index]['primary']
ha_tools.kill_members([rs_primary_uri], 2, rs[found_index]['nodes'])
del rs[found_index]['primary']
except:
self.error("No primary found for this RS!")
self.write(self._template.load(op + self._ext).generate(rs_id=rs_id, rs_killed_primary_uri=rs_primary_uri))
# Kill the secondary
elif op == 'kill_secondary':
request = self._parse_json(self.request.body)
rs_id = request['rs']['id']
try:
found_index = self._get_index(rs, 'id', rs_id)
except:
self.error("No secondaries found for this RS!")
rs_secondaries_uris = rs[found_index]['secondaries']
try:
# Get a random secondary for this RS
# Get a random index first
rs_secondary_uri_index = random.randrange( len(rs_secondaries_uris) )
# Pop out the element by this random index
rs_secondary_uri = rs[found_index]['secondaries'].pop(rs_secondary_uri_index)
ha_tools.kill_members([rs_secondary_uri], 2, rs[found_index]['nodes'])
del rs[found_index]['nodes'][rs_secondary_uri]
except:
self.error("No secondaries found for this RS!")
self.write(self._template.load(op + self._ext).generate(rs_id=rs_id, rs_killed_secondary_uri=rs_secondary_uri))
# Kill all the secondaries
elif op == 'kill_all_secondaries':
request = self._parse_json(self.request.body)
rs_id = request['rs']['id']
found_index = self._get_index(rs, 'id', rs_id)
try:
rs_secondaries_uris = rs[found_index]['secondaries']
ha_tools.kill_members(rs_secondaries_uris, 2, rs[found_index]['nodes'])
del rs[found_index]['secondaries']
except:
self.error("No secondaries found for this RS!")
self.write(self._template.load(op + self._ext).generate(rs_id=rs_id, rs_killed_secondaries_uris=rs_secondaries_uris))
application = tornado.web.Application([
(r"/rs/([_a-z]*)", RsHandler)
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()