-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
556 lines (488 loc) · 21.6 KB
/
app.py
File metadata and controls
556 lines (488 loc) · 21.6 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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#!/usr/bin/python3
"""
USER MANAGEMENT MODULE & LIFECYCLE MANAGER - REST API
This is being developed for the MF2C Project: http://www.mf2c-project.eu/
Copyright: Roi Sucasas Font, Atos Research and Innovation, 2017.
This code is licensed under an Apache 2.0 license. Please, refer to the LICENSE.TXT file for more information
Created on 18 oct. 2018
@author: Roi Sucasas - ATOS
"""
import config as cfg
from lifecycle import app_funcs as lm
from lifecycle.data import data_adapter as data_adapter
from lifecycle import init_config as lm_init_config
from lifecycle.logs import LOG
# ext
from flask_cors import CORS
from flask import Flask, request, Response, json
from flask_restful import Resource, Api
from flask_restful_swagger import swagger
'''
REST API
Routes:
Root:
/api/v2
GET: get rest api service status
Lifecycle:
/lm
POST: SLA / UM / QoS notifications
/lm/agent-config
GET: get agent's lifecycle configuration: docker, docker-swarm, kubernetes, ...
/lm/check-agent-um
(*) GET: checks if device can run more apps - UP & SM policies (from 'local' User Management module)
/lm/check-agent-swarm
(*) GET: checks if device can run warm apps (from 'local' User Management module)
/lm/agent-um
GET: get agent's current user-profile and sharing-model (from 'local' User Management module)
PUT: updates user-profile current number of applications running
/lm/service-instances/<string:service_instance_id>
(*) GET: get service instance / all service instances (from cimi)
(*) PUT: Starts / stops / restarts a service instance // starts a job in COMPSs
(*) DELETE: terminates a service instance; deletes service instance (from cimi)
/lm/service-instances/<string:service_instance_id>/der
(*) PUT: starts a job in COMPSs
/lm/service-instances/<string:service_instance_id>/report
(*) GET: get service instance report
/lm/service
POST: Submits a service and gets a service instance (new version)
/lm/service-instance-int
POST: Submits /lm/service-instance-inta service in a mF2C agent
PUT: starts / stops ... a service in a mF2C agent; start-job
'''
try:
lm_init_config.init()
# APP
app = Flask(__name__)
CORS(app)
# API DOC
api = swagger.docs(Api(app),
apiVersion='1.2.0',
api_spec_url=cfg.dic['API_DOC_URL'],
produces=["application/json", "text/html"],
swaggerVersion="1.2",
description='mF2C - Lifecycle Management REST API - version ' + cfg.dic['VERSION'],
basePath='http://localhost:' + str(cfg.dic['SERVER_PORT']),
resourcePath='/')
except ValueError:
LOG.error('[app] Exception: Error while initializing app / api')
########################################################################################################################
### LIFECYCLE MANAGER
########################################################################################################################
#
# API 'home' Route
#
# '/api/v2/'
#
# GET: get rest api service status
#
@app.route('/api/v2', methods=['GET'])
@app.route('/api/v2/', methods=['GET'])
def default_route():
data = {
'app': "Lifecycle Management REST API",
'status': "Running",
'api_doc_json': "http://" + data_adapter.get_host_ip() + ":" + str(cfg.dic['SERVER_PORT']) + cfg.dic['API_DOC_URL'],
'api_doc_html': "http://" + data_adapter.get_host_ip() + ":" + str(cfg.dic['SERVER_PORT']) + cfg.dic['API_DOC_URL'] + ".html#!/spec"
}
resp = Response(json.dumps(data), status=200, mimetype='application/json')
return resp
#
# Service instance route: status, service events handler
#
# '/api/v2/lm/agent-config'
#
# GET: get agent's lifecycle configuration: docker, docker-swarm, kubernetes, ...
#
class InstanceConfig(Resource):
# GET /api/v2/lm/agent-config
@swagger.operation(
summary="get agent's lifecycle configuration: docker, docker-swarm, kubernetes, ...",
notes="get agent's lifecycle configuration: docker, docker-swarm, kubernetes, ...",
produces=["application/json"],
authorizations=[],
parameters=[],
responseMessages=[{
"code": 500,
"message": "Exception processing request"
}])
def get(self):
return lm.getAgentConfig()
api.add_resource(InstanceConfig, '/api/v2/lm/agent-config')
#
#
# '/api/v2/lm/check-agent-um'
#
# GET: checks if device can run more apps - UP & SM policies (from 'local' User Management module)
#
class CheckAgentUM(Resource):
# GET /api/v2/lm/check-agent-um
@swagger.operation(
summary="checks if device can run more apps - UP & SM policies (from 'local' User Management module)",
notes="checks if device can run more apps - UP & SM policies (from 'local' User Management module)",
produces=["application/json"],
authorizations=[],
parameters=[],
responseMessages=[{
"code": 500,
"message": "Exception processing request"
}])
def get(self):
return lm.getCheckAgentUMInfo()
api.add_resource(CheckAgentUM, '/api/v2/lm/check-agent-um')
#
#
# '/api/v2/lm/check-agent-swarm'
#
# GET: checks if device can run warm apps (from 'local' User Management module)
#
class CheckAgentSwarm(Resource):
# GET /api/v2/lm/check-agent-swarm
@swagger.operation(
summary="checks if device can run warm apps (from 'local' User Management module)",
notes="checks if device can run warm apps (from 'local' User Management module)",
produces=["application/json"],
authorizations=[],
parameters=[],
responseMessages=[{
"code": 500,
"message": "Exception processing request"
}])
def get(self):
return lm.getCheckAgentSwarm()
api.add_resource(CheckAgentSwarm, '/api/v2/lm/check-agent-swarm')
#
# Service instance route: status, service events handler
#
# '/api/v2/lm/agent-um'
#
# GET: get agent's current user-profile and sharing-model (from 'local' User Management module)
# PUT: updates user-profile current number of applications running
#
class AgentUM(Resource):
# GET /api/v2/lm/agent-um
@swagger.operation(
summary="get agent's current user-profile and sharing-model (from 'local' User Management module)",
notes="get agent's current user-profile and sharing-model (from 'local' User Management module)",
produces=["application/json"],
authorizations=[],
parameters=[],
responseMessages=[{
"code": 500,
"message": "Exception processing request"
}])
def get(self):
return lm.getAgentUMInfo()
api.add_resource(AgentUM, '/api/v2/lm/agent-um')
#
# Healthcheck service: UM and LM
#
# '/api/v2/lm/isumalive'
#
# GET: checks if UM is alive
#
class AgentUMIsAlive(Resource):
# GET /api/v2/lm/isumalive
@swagger.operation(
summary="checks if UM is alive",
notes="checks if UM is alive",
produces=["application/json"],
authorizations=[],
parameters=[],
responseMessages=[{
"code": 500,
"message": "Exception processing request"
}])
def get(self):
return lm.isUMAlive()
api.add_resource(AgentUMIsAlive, '/api/v2/lm/isumalive')
#
# Service instance route:
#
# '/api/v2/lm/service-instances/<string:service_instance_id>'
#
# GET: get service instance / all service instances
# PUT: Starts / stops / restarts a service instance // starts a job in COMPSs
# DELETE: Terminate service, Deallocate service's resources
#
class ServiceInstance(Resource):
# GET: get service instance
# GET /api/v2/lm/service-instance/<string:service_instance_id>
@swagger.operation(
summary="Get service instance",
notes="Gets a (all) service instance(s)",
produces=["application/json"],
authorizations=[],
parameters=[{
"name": "service_instance_id",
"description": "Service instance ID or 'all'.<br/>Example: <br/>b08ee389-36c0-45f0-8684-61baf6e03da8",
"required": True,
"paramType": "path",
"type": "string"
}],
responseMessages=[{
"code": 500,
"message": "Exception processing request"
}])
def get(self, service_instance_id):
return lm.getServiceInstance(service_instance_id)
# PUT: Starts / stops / restarts ... a service and returns a JSON object with the result / status of the operation.
# start-job ... starts a job in COMPSs
# PUT /api/v2/lm/service-instance/<string:service_instance_id>
@swagger.operation(
summary="start, stop, restart a <b>service instance</b>",
notes="Available operations:<br/>"
"<b>'start / stop / restart'</b> ... service instance operations<br/>",
produces=["application/json"],
authorizations=[],
parameters=[{
"name": "service_instance_id",
"description": "Service instance ID or 'all'.<br/>Example: <br/>b08ee389-36c0-45f0-8684-61baf6e03da8",
"required": True,
"paramType": "path",
"type": "string"
}, {
"name": "body",
"description": "Parameters in JSON format.<br/>"
"{<br/><font color='blue'>\"operation\":</font>\"start/restart/stop\"<br/>}",
"required": True,
"paramType": "body",
"type": "string"
}],
responseMessages=[{
"code": 406,
"message": "('operation') Parameter not found"
}, {
"code": 500,
"message": "Exception processing request"
}])
def put(self, service_instance_id):
return lm.putServiceInstance(request, service_instance_id)
# DELETE: Terminate service, Deallocate service's resources
# DELETE /api/v2/lm/service-instance/<string:service_instance_id>
@swagger.operation(
summary="Terminates a service instance and deallocates the service's resources",
notes="Terminates a service instance, and deallocates all the resources.",
produces=["application/json"],
authorizations=[],
parameters=[{
"name": "service_instance_id",
"description": "Service instance ID or 'all'.<br/>Example: <br/>b08ee389-36c0-45f0-8684-61baf6e03da8",
"required": True,
"paramType": "path",
"type": "string"
}],
responseMessages=[{
"code": 406,
"message": "'service_instance_id' parameter not found"
}, {
"code": 500,
"message": "Exception processing request"
}])
def delete(self, service_instance_id):
return lm.deleteServiceInstance(service_instance_id)
api.add_resource(ServiceInstance, '/api/v2/lm/service-instances/<string:service_instance_id>')
#
# Service instance COMPSs route:
#
# /lm/service-instance/<string:service_instance_id>/der
# PUT: starts a job in COMPSs
#
class ServiceInstanceCOMPSs(Resource):
# PUT: Starts / stops / restarts ... a service and returns a JSON object with the result / status of the operation.
# start-job ... starts a job in COMPSs
# PUT /api/v2/lm/service-instance/<string:service_instance_id>
@swagger.operation(
summary="start a <b>COMPSs job</b>",
notes="Executes a COMPSs job in the DER.<br/>"
"<hr/><br/>Example:<br/>"
"{<br/><font color='blue'>\"operation\":</font>\"start-job\",<br/>"
"<font color='blue'>\"ceiClass\":</font>\"es.bsc.compss.agent.test.TestItf\",<br/>"
"<font color='blue'>\"className\":</font>\"es.bsc.compss.agent.test.Test\",<br/>"
"<font color='blue'>\"hasResult\":</font>false,<br/>"
"<font color='blue'>\"methodName\":</font>\"main\",<br/>"
"<font color='blue'>\"parameters\":</font>\""
" <params paramId=\\\"0\\\">"
" <direction>IN</direction>"
" <stream>UNSPECIFIED</stream>"
" <type>OBJECT_T</type>"
" <array paramId=\\\"0\\\">"
" <componentClassname>java.lang.String</componentClassname>"
" <values>"
" <element paramId=\\\"0\\\">"
" <className>java.lang.String</className>"
" <value xmlns:xsi=\\\"http://www.w3.org/2001/XMLSchema-instance\\\" "
" xmlns:xs=\\\"http://www.w3.org/2001/XMLSchema\\\" xsi:type=\\\"xs:string\\\">3</value>"
" </element>"
" </values>"
" </array>"
" </params>\" <br/>}",
produces=["application/json"],
authorizations=[],
parameters=[{
"name": "service_instance_id", "description": "Service instance ID or 'all'",
"required": True,
"paramType": "path",
"type": "string"
}, {
"name": "body",
"description": "Parameters in JSON format",
"required": True,
"paramType": "body",
"type": "json"
}], responseMessages=[{
"code": 406, "message": "('operation' / 'parameters') Parameter not found"
}, {
"code": 500, "message": "Exception processing request"
}])
def put(self, service_instance_id):
return lm.putServiceInstanceCOMPSs(request, service_instance_id)
api.add_resource(ServiceInstanceCOMPSs, '/api/v2/lm/service-instances/<string:service_instance_id>/der')
#
# Service instance route:
#
# '/api/v2/lm/service-instances/<string:service_instance_id>/report'
#
# GET: get service instance report
#
class ServiceInstanceReport(Resource):
# GET: get service instance report
# GET /api/v2/lm/service-instance/<string:service_instance_id>/report
@swagger.operation(
summary="Returns the service instance operation report",
notes="Gets the service instance operation report",
produces=["application/json"],
authorizations=[],
parameters=[{
"name": "service_instance_id",
"description": "Service instance ID or 'all'.<br/>Example: <br/>b08ee389-36c0-45f0-8684-61baf6e03da8",
"required": True,
"paramType": "path",
"type": "string"
}],
responseMessages=[{
"code": 500,
"message": "Exception processing request"
}])
def get(self, service_instance_id):
return lm.getServiceInstanceReport(service_instance_id)
api.add_resource(ServiceInstanceReport, '/api/v2/lm/service-instances/<string:service_instance_id>/report')
#
# Service route (v2): submits a service ==> service instance is created
#
# '/api/v2/lm/service'
#
# POST: Submits a service; gets a service instance
#
class Service(Resource):
# POST: Submits a service
# POST /api/v2/lm/service
@swagger.operation(
summary="Submits a <b>service</b> (deployment phase)</b>",
notes="Submits a service and returns a json with the content of a service instance. Types of services that can be deployed and managed:<br/>"
" <b>'docker'</b> ... deploy a docker image<br/>"
" <b>'docker-compose'</b> ... deploy a docker compose service<br/>"
" <b>'docker-swarm'</b> ... deploy a docker swarm service<br/>"
" <b>'compss'</b> ... deploy a docker COMPSs image<br/><br/>"
"Body content:<br/>"
"{<br/>"
" <b>service_id</b>: string,<br/>"
" <b>sla_template</b>: string,<br/>"
" <b>service</b>: json (only if <i>service_id</i> is not defined),<br/>"
" <b>service_instance_id</b>: json (only used for internal calls - request forwarding),<br/>"
" <b>agents_list</b>: list,<br/>}<br/><hr/>"
"Example 1:<br/>"
"{<br/>"
" \"service_id\": \"service/74dd7176-111e-412a-98ce-a6409d58b3ca\",<br/>"
" \"sla_template\": \"sla-template/12932af0ef123\"<br/>}<br/><br/>"
"Example 2:<br/>"
"{<br/>"
" \"service_id\": \"service/74dd7176-111e-412a-98ce-a6409d58b3ca\",<br/>"
" \"sla_template\": \"sla-template/12932af0ef123\",<br/>"
" \"agents_list\": [{\"agent_ip\": \"192.168.131.02\"}, {\"agent_ip\": \"192.168.131.03\"}]<br/>}<br/><br/>"
"Example 3:<br/>"
"{<br/>"
" \"service\": {<br/> \"name\": \"nginx\", <br/> \"exec\": \"nginx\", <br/> \"num_agents\": 5, <br/> \"exec_type\": \"docker\", <br/> \"exec_ports\": [80], <br/> \"category\": 2, <br/> \"sla_templates\": [\"sla-template/c2843afa-0fb0-4366-87e9-303c6a961be2\"]},<br/>"
" \"sla_template\": \"sla-template/12932af0ef123\",<br/>"
" \"agents_list\": [{\"agent_ip\": \"192.168.131.02\"}, {\"agent_ip\": \"192.168.131.03\"}]<br/>}<br/><br/>",
produces=["application/json"],
authorizations=[],
parameters=[{
"name": "body",
"description": "Parameters in JSON format",
"required": True,
"paramType": "body",
"type": "json"
}],
responseMessages=[{
"code": 406,
"message": "'service_id' / 'sla_template' parameter not found"
}, {
"code": 500,
"message": "Error processing request"
}])
def post(self):
return lm.postService(request)
api.add_resource(Service, '/api/v2/lm/service')
#
# Service instance (only calls between LMs - LMs internal communication) route: deploy a service, start, stop, ...
#
# '/api/v2/lm/service-instance-int'
#
# POST: Submits a service in a mF2C agent
# PUT: starts / stops ... a service in a mF2C agent
#
class ServiceInstanceInt(Resource):
# POST: Submits a service in an agent
# POST /api/v2/lm/service-instance-int
def post(self):
return lm.postServiceInt(request)
# PUT: Starts / stops / restarts ... a service in an agent, and returns a JSON object with the result / status of the operation.
# PUT /api/v2/lm/service-instance-int
def put(self):
return lm.putServiceInt(request)
api.add_resource(ServiceInstanceInt, '/api/v2/lm/service-instance-int')
#
# Service instance route: service events handler
#
# '/api/v2/lm'
#
# POST: SLA / UM notifications
#
class LmEvents(Resource):
# POST: process warnings and notifications
# POST /api/v2/lm
@swagger.operation(
summary="Process warnings and notifications",
notes="Process warnings and notifications from other components.",
produces=["application/json"],
authorizations=[],
parameters=[{
"name": "body",
"description": "Parameters in JSON format.<br/>Example: <br/>"
"{\"type\":\"sla_notification / um_warning / qos_enforcement\", <br/>\"data\":{}}",
"required": True,
"paramType": "body",
"type": "string"
}], responseMessages=[{
"code": 406, "message": "'type' / 'data' parameter not found"
}, {
"code": 500, "message": "Exception processing request"
}, {
"code": 501, "message": "Operation not defined / implemented"
}])
def post(self):
return lm.postServiceInstanceEvent(request)
api.add_resource(LmEvents, '/api/v2/lm')
########################################################################################################################
# MAIN
def main():
LOG.info("[app] Starting Lifecycle Management application [version=" + str(cfg.dic['VERSION']) + "] ...")
LOG.info("[app] Swagger running on http://" + data_adapter.get_host_ip() + ":" + str(cfg.dic['SERVER_PORT']) + cfg.dic['API_DOC_URL'] + ".html")
LOG.info("[app] REST API running on http://" + data_adapter.get_host_ip() + ":" + str(cfg.dic['SERVER_PORT']) + cfg.dic['API_DOC_URL'])
# START (SSL) SERVER
# context = ('PATH_TO_CERT_CRT', 'PATH_TO_CERT_KEY')
# app.run(host='0.0.0.0', port=config.dic['SERVER_PORT'], ssl_context=context, threaded=True, debug=False)
# START SERVER
app.run(host='0.0.0.0', port=cfg.dic['SERVER_PORT'], threaded=True, debug=False)
if __name__ == "__main__":
main()