This repository was archived by the owner on Jul 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
419 lines (337 loc) · 11.3 KB
/
main.py
File metadata and controls
419 lines (337 loc) · 11.3 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
"""
Module that contains start of the program, tick scheduler and web APIs
"""
import logging
import json
from flask import Flask, send_file, request, make_response
from flask_restful import Api
from environment import (
GEN_REPOSITORY,
MONGO_DB_HOST,
MONGO_DB_PASSWORD,
MONGO_DB_PORT,
MONGO_DB_USER,
TICK_INTERVAL,
REPOSITORY_UPDATE_INTERVAL,
DEBUG,
HOST,
PORT,
)
from src.tools.scheduler import Scheduler
from src.controller import Controller, Gridpack, Database
from src.tools.user import User
from src.tools.utils import include_gridpack_ids
app = Flask(__name__, static_folder="./frontend/static", template_folder="./frontend")
api = Api(app)
scheduler = Scheduler()
controller = None # pylint: disable=invalid-name
@app.route("/")
def index_page():
"""
Return index.html
"""
return send_file("frontend/index.html")
def output_text(data, code=200, headers=None):
"""
Makes a Flask response with a plain text encoded body
"""
if data and isinstance(data, (dict, list)):
data = json.dumps(data, indent=1, sort_keys=True)
resp = make_response(data, code)
resp.headers.extend(headers or {})
resp.headers["Content-Type"] = "application/json"
resp.headers["Access-Control-Allow-Origin"] = "*"
return resp
@app.route("/api/tick")
def api_tick():
"""
API to trigger a controller tick
"""
if not is_user_authorized():
return output_text({"message": "Unauthorized"}, code=403)
tick()
return output_text({"message": "OK"})
@app.route("/api/tick_repository")
def api_tick_repository():
"""
API to trigger a controller tick for repository data
"""
if not is_user_authorized():
return output_text({"message": "Unauthorized"}, code=403)
tick_repository()
return output_text({"message": "OK"})
@app.route("/api/user")
def user_info():
"""
API for user info
"""
return output_text(user_info_dict())
@app.route("/api/system_info")
def system_info():
"""
API for system info
"""
return output_text(
{
"last_tick": controller.last_tick,
"last_repository_tick": controller.last_repository_tick,
"options": controller.repository_tree,
"gen_repository": GEN_REPOSITORY,
"job_cores": controller.job_cores,
"job_memory": controller.job_memory,
}
)
@app.route("/api/mcm", methods=["POST"])
def force_mcm_request():
"""
This endpoint forces the creation for a request in McM
for a completed Gridpack
"""
if not is_user_authorized():
return output_text({"message": "Unauthorized"}, code=403)
gridpack_id: str = request.args.get("gridpack_id", "")
if not gridpack_id:
return output_text(
{"message": 'Please choose a Gridpack via request parameter "gridpack_id"'},
code=400,
)
force_status = controller.force_request_for_gridpack(gridpack_id=gridpack_id)
if isinstance(force_status, dict):
return output_text(data=force_status, code=400)
return output_text({"message": f"Request forced for {gridpack_id}"})
@app.route("/api/create", methods=["PUT"])
def create_gridpack():
"""
API to create a gridpack or list of gridpacks
"""
if not is_user_authorized():
return output_text({"message": "Unauthorized"}, code=403)
logging.info("DATA %s", request.data.decode("utf-8"))
gridpacks = json.loads(request.data.decode("utf-8"))
if not isinstance(gridpacks, list):
gridpacks = [gridpacks]
gridpack_ids = []
for gridpack_dict in gridpacks:
try:
gridpack = Gridpack.make(gridpack_dict)
except Exception as ex:
return output_text({"message": str(ex)}, code=400)
error = gridpack.validate()
if error:
return output_text({"message": error}, code=400)
gridpack_id = controller.create(gridpack)
gridpack_ids.append(gridpack_id)
scheduler.notify()
return output_text({"message": gridpack_ids})
@app.route("/api/create_approve", methods=["PUT"])
def create_approve_gridpack():
"""
API to create and approve a gridpack or list of gridpacks
"""
if not is_user_authorized():
return output_text({"message": "Unauthorized"}, code=403)
logging.info("DATA %s", request.data.decode("utf-8"))
gridpacks = json.loads(request.data.decode("utf-8"))
if not isinstance(gridpacks, list):
gridpacks = [gridpacks]
gridpack_ids = []
for gridpack_dict in gridpacks:
try:
gridpack = Gridpack.make(gridpack_dict)
except Exception as ex:
return output_text({"message": str(ex)}, code=400)
error = gridpack.validate()
if error:
return output_text({"message": error}, code=400)
gridpack_id = controller.create(gridpack)
controller.approve(gridpack_id)
gridpack_ids.append(gridpack_id)
scheduler.notify()
return output_text({"message": gridpack_ids})
@app.route("/api/approve", methods=["POST"])
def approve_gridpack():
"""
API to approve a gridpack
"""
if not is_user_authorized():
return output_text({"message": "Unauthorized"}, code=403)
gridpack_dict = json.loads(request.data.decode("utf-8"))
gridpack_id = gridpack_dict.get("_id")
if not gridpack_id:
return output_text({"message": "No ID"})
controller.approve(gridpack_id)
scheduler.notify()
return output_text({"message": "OK"})
@app.route("/api/reset", methods=["POST"])
def reset_gridpack():
"""
API to reset a gridpack
"""
if not is_user_authorized():
return output_text({"message": "Unauthorized"}, code=403)
gridpack_dict = json.loads(request.data.decode("utf-8"))
gridpack_id = gridpack_dict.get("_id")
if not gridpack_id:
return output_text({"message": "No ID"})
controller.reset(gridpack_id)
scheduler.notify()
return output_text({"message": "OK"})
@app.route("/api/create_request", methods=["POST"])
def create_request():
"""
API to create a request in McM
"""
if not is_user_authorized():
return output_text({"message": "Unauthorized"}, code=403)
gridpack_dict = json.loads(request.data.decode("utf-8"))
gridpack_id = gridpack_dict.get("_id")
if not gridpack_id:
return output_text({"message": "No ID"})
controller.create_request(gridpack_id)
scheduler.notify()
return output_text({"message": "OK"})
@app.route("/api/delete", methods=["DELETE"])
def delete_gridpack():
"""
API to delete a gridpack
"""
if not is_user_authorized():
return output_text({"message": "Unauthorized"}, code=403)
gridpack_dict = json.loads(request.data.decode("utf-8"))
gridpack_id = gridpack_dict.get("_id")
if not gridpack_id:
return output_text({"message": "No ID"})
controller.delete(gridpack_id)
scheduler.notify()
return output_text({"message": "OK"})
@app.route("/api/get")
def get_gridpacks():
"""
API to fetch gridpacks from database
"""
database = Database()
gridpacks, count = database.get_gridpacks()
return output_text([gridpacks, count])
@app.route("/api/get_fragment/<string:gridpack_id>")
def get_fragment(gridpack_id):
"""
API to get gridpack's fragment
"""
database = Database()
gridpack_json = database.get_gridpack(gridpack_id)
if not gridpack_json:
return output_text({"message": "Gridpack not found"}, code=404)
gridpack = Gridpack.make(gridpack_json)
fragment, _ = controller.get_fragment(gridpack)
return output_text(fragment, headers={"Content-Type": "text/plain"})
@app.route("/api/get_run_card/<string:gridpack_id>")
def get_run_card(gridpack_id):
"""
API to get gridpack's run card
"""
try:
gridpack: Gridpack = controller.get_original_gridpack(gridpack_id=gridpack_id)
content = include_gridpack_ids(
gridpack_id=gridpack_id,
effective_gridpack_id=gridpack.get_id(),
content=gridpack.get_run_card(),
)
return output_text(content, headers={"Content-Type": "text/plain"})
except ValueError:
return output_text({"message": "Gridpack not found"}, code=404)
except AssertionError as a:
return output_text({"message": str(a)}, code=400)
except Exception:
logging.error(
"Unable to retrieve `run_card` for Gridpack: %s", gridpack, exc_info=True
)
return output_text({"message": "Unable to retrieve the element"}, code=400)
@app.route("/api/get_customize_card/<string:gridpack_id>")
def get_customize_card(gridpack_id):
"""
API to get gridpack's fragment
"""
try:
gridpack: Gridpack = controller.get_original_gridpack(gridpack_id=gridpack_id)
content = include_gridpack_ids(
gridpack_id=gridpack_id,
effective_gridpack_id=gridpack.get_id(),
content=gridpack.get_customize_card(),
)
return output_text(content, headers={"Content-Type": "text/plain"})
except ValueError:
return output_text({"message": "Gridpack not found"}, code=404)
except AssertionError as a:
return output_text({"message": str(a)}, code=400)
except Exception:
logging.error(
"Unable to retrieve `customize_card` for Gridpack: %s",
gridpack,
exc_info=True,
)
return output_text({"message": "Unable to retrieve the element"}, code=400)
def user_info_dict():
"""
Get user name, login, email and authorized flag from request headers
"""
return User().get_user_info()
def is_user_authorized():
"""
Return whether user is a member of administrators e-group
"""
return User().is_authorized()
def tick():
"""
Trigger controller to perform a tick
"""
if controller:
controller.tick()
def tick_repository():
"""
Trigger controller to perform a tick on repository data
"""
if controller:
controller.update_repository_tree()
def setup_console_logging(debug):
"""
Setup logging to console
"""
logging.basicConfig(
format="[%(asctime)s][%(levelname)s] %(message)s",
level=logging.DEBUG if debug else logging.INFO,
)
def set_scheduler():
"""
Set the automatic jobs for the application scheduler.
"""
logger = logging.getLogger()
logger.info("Adding machine tick with interval %ss", TICK_INTERVAL)
scheduler.add_job(tick, TICK_INTERVAL)
logger.info(
"Adding repository update with interval %ss", REPOSITORY_UPDATE_INTERVAL
)
scheduler.add_job(tick_repository, REPOSITORY_UPDATE_INTERVAL)
def set_app():
"""
Set the required configuration to start.
"""
global controller # pylint: disable=global-statement
setup_console_logging(DEBUG)
Database.set_credentials(MONGO_DB_USER, MONGO_DB_PASSWORD)
Database.set_host_port(MONGO_DB_HOST, MONGO_DB_PORT)
controller = Controller()
def main():
"""
Main function, parse arguments, create a controller and start Flask web server
"""
set_app()
set_scheduler()
scheduler.start()
logger = logging.getLogger()
logger.info("Will run on %s:%s", HOST, PORT)
try:
app.run(host=HOST, port=PORT, debug=DEBUG, use_reloader=False, threaded=True)
finally:
scheduler.stop()
if __name__ == "__main__":
main()