-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
321 lines (251 loc) · 9.35 KB
/
app.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from threading import Thread
from functools import wraps
from quart import Quart, request
from quart_cors import cors
from bootnode import Bootnode
from util import to_nodes, jsonify
from pymongo import MongoClient
import datetime
import asyncio
import requests_async as requests
import datetime
loop = asyncio.new_event_loop()
asyncio.get_child_watcher().attach_loop(loop)
app = Quart(__name__)
cors(app)
SUPPORTED_PROVIDERS = ['private-cloud', 'google']
SUPPORTED_ZONES = {
'google': ['us-central1-a', 'europe-west6-a', 'asia-east2-a'],
'private-cloud': ['london1', 'munich1', 'oslo1', 'tokyo1'],
}
# connect to mongo and set up database vars
mongo_client = MongoClient()
bootnode_db = mongo_client.bootnode
nodes_collection = bootnode_db.nodes
updates_collection = bootnode_db.updates
node_statuses = bootnode_db.node_statuses
# set up system update loop
async def update_nodes_lambda(date, zone, provider):
print('updating', date, zone, provider)
print('-------- Getting ' + provider + ' nodes in zone: ' + zone + ' --------')
bootnode = Bootnode('casper', 'testnet', provider, zone)
deployments = [d.to_dict() for d in await bootnode.list_deployments()]
services = [s.to_dict() for s in await bootnode.list_services()]
pods = [p.to_dict() for p in await bootnode.list_pods()]
nodes = to_nodes(deployments, services, pods, zone)
for node in nodes:
node['lastUpdated'] = date
if node.get('blockchain', None) == 'casper' and node.get('ip', None) is not None:
try:
ip = node['ip']
port = 9001
if provider == 'private-cloud':
for p in node['ports']:
if p['port'] == 9001:
port = p['nodePort']
node['provider'] = provider
print('Pod', zone + ' ' + node['id'] + ' ' + node['ip'])
start = datetime.datetime.now()
reqs = [
requests.put('https://{0}:{1}/show/blocks'.format(ip, port),
json={'depth': 1},
verify=False),
requests.put('https://{0}:{1}/show/dag'.format(ip,
port),
json={'depth': 10,
'showJustifications':
True}, verify=False)
]
ress = await asyncio.gather(*reqs)
blockdata = ress[0]
dag = ress[1]
end = datetime.datetime.now()
node['metadata'] = {
'block': blockdata.json()[0],
'dag': dag.json(),
}
node['latencyMillis'] = (end - start).microseconds / 1000
except Exception as e:
print('cannot get metadata for ' + node['id'] + ': ' +
str(e))
nodes_collection.insert_one(node)
# except Exception as e:
# print('update nodes loop error: ' + str(e))
# finally:
# function to spin off thread
async def update_nodes_loop():
while True:
try:
date = datetime.datetime.utcnow()
updates = []
for provider in SUPPORTED_PROVIDERS:
zones = SUPPORTED_ZONES[provider]
for zone in zones:
updates.append(update_nodes_lambda(date, zone, provider))
await asyncio.gather(*updates)
updates_collection.update_one(
{
'name': 'nodes',
},
{
'$set': {
'date': date
},
},
True
)
await asyncio.sleep(1)
except Exception as e:
print('update_nodes_loop error' + str(e))
def update_nodes_thread():
print('starting update thread')
asyncio.set_event_loop(loop)
loop.run_until_complete(update_nodes_loop())
Thread(target=update_nodes_thread).start()
def auth_required(fn):
@wraps(fn)
async def wrapped_fn(*args, **kwargs):
# print(request.headers.get('Authorization'))
if request.headers.get('Authorization') != 'Bearer fLcLu7OLD81aR9jf':
return jsonify({
'status': 'failed',
'error': 'authorization required',
})
return await fn(*args, **kwargs)
return wrapped_fn
@app.route('/login', methods=['POST'])
async def login():
try:
json = await request.get_json()
print('login', json)
if json['email'] == '[email protected]' and json['password'] == 'testtest':
return jsonify({'token': 'fLcLu7OLD81aR9jf'})
return jsonify({
'status': 'failed',
'error': 'incorrect username or password'
})
except Exception as e:
return jsonify({
'status': 'failed',
'error': 'could not get login: ' + str(e),
})
@app.route('/nodes', methods=['GET'])
@auth_required
async def get_nodes():
try:
update = updates_collection.find_one({ 'name': 'nodes' })
# print(update)
# print('getting node data as of ' + str(update['date']))
nodes = nodes_collection.find({'lastUpdated': update['date']})
ns = []
for node in nodes:
node.pop('_id')
ns.append(node)
return jsonify(ns)
except Exception as e:
return jsonify({
'status': 'failed',
'error': 'could not get nodes: ' + str(e),
})
@app.route('/nodes', methods=['DELETE'])
@auth_required
async def delete_nodes():
try:
print('deleting everything!')
update = updates_collection.find_one({ 'name': 'nodes' })
nodes = nodes_collection.find({'lastUpdated': update['date']})
dns = []
for node in nodes:
dns.append(delete_node(node['id'], node['provider'], node['zone']))
await asyncio.gather(*dns)
print('deleted ' + str(len(dns)) + ' nodes')
return jsonify({
'status': 'success',
})
except Exception as e:
return jsonify({
'status': 'failed',
'error': 'could not delete a nodes: ' + str(e)
})
@app.route('/nodes', methods=['PUT'])
@auth_required
async def put_node():
try:
json = await request.get_json()
print('launching ' + str(json['number']) + ' nodes in ' + str(json['zone']))
provider = json['provider']
if provider not in SUPPORTED_PROVIDERS:
return jsonify({
'status': 'failed',
'error': provider + ' is not a valid provider',
})
zone = json['zone']
if zone not in SUPPORTED_ZONES[provider]:
return jsonify({
'status': 'failed',
'error': zone + ' is not a valid zone',
})
bootnode = Bootnode('casper', 'testnet', provider, zone)
number = 1
if json['number'] is not None:
number = int(json['number'])
nodes = []
ds = []
for i in range(number):
async def create_deployment():
data = await bootnode.create_deployment()
# print('deployment created', data.deployment)
ds.append(create_deployment())
await asyncio.gather(*ds)
return jsonify({
'status': 'success',
'nodes-starting': number,
})
except Exception as e:
return jsonify({
'status': 'failed',
'error': 'could not create a node: ' + str(e)
})
@app.route('/nodes/<node_id>', methods=['GET'])
@auth_required
async def get_node(node_id, provider=None, zone=None):
try:
json = await request.get_json()
if provider is None:
provider = json['provider']
if zone is None:
zone = json['zone']
bootnode = Bootnode('casper', 'testnet', provider, zone)
deployment = await bootnode.get_deployment(node_id)
service = await bootnode.get_service(node_id)
pods = [p.to_dict() for p in await bootnode.list_pods(label_selector='app=' + node_id)]
return jsonify(to_nodes([deployment.to_dict()],
[service.to_dict()], pods, zone))
except Exception as e:
return jsonify({
'status': 'failed',
'error': 'node not found: ' + str(e)
})
@app.route('/nodes/<node_id>', methods=['DELETE'])
@auth_required
async def delete_node(node_id, provider=None, zone=None):
is_called = provider is not None
try:
print('deleting ' + str(node_id) + ' from provider ' + str(provider) + ' and zone ' + str(zone))
if provider is None:
json = await request.get_json()
provider = json['provider']
zone = json['zone']
bootnode = Bootnode('casper', 'testnet', provider, zone)
await bootnode.delete_deployment(node_id)
return jsonify({
'status': 'ok',
})
except Exception as e:
print('delete_node error', e)
return jsonify({
'status': 'failed',
'error': 'could not delete: ' + str(e)
})
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port='4000')