1
- from app import app , iam_blueprint
1
+ from app import app , iam_blueprint , iam_base_url
2
2
from flask import json , current_app , render_template , request , redirect , url_for , flash , session
3
3
import requests , json
4
4
import yaml
@@ -29,40 +29,111 @@ def avatar(email, size):
29
29
toscaTemplates .append ( os .path .relpath (os .path .join (path , name ), toscaDir ))
30
30
31
31
orchestratorUrl = app .config .get ('ORCHESTRATOR_URL' )
32
+ slamUrl = app .config .get ('SLAM_URL' )
33
+ cmdbUrl = app .config .get ('CMDB_URL' )
34
+ slam_cert = app .config .get ('SLAM_CERT' )
35
+
36
+ @app .route ('/settings' )
37
+ def show_settings ():
38
+ if not iam_blueprint .session .authorized :
39
+ return redirect (url_for ('login' ))
40
+ return render_template ('settings.html' , orchestrator_url = orchestratorUrl , iam_url = iam_base_url )
32
41
33
- #@app.route('/')
34
42
@app .route ('/login' )
35
43
def login ():
36
44
session .clear ()
37
45
return render_template ('home.html' )
38
46
39
- @app .route ('/dashboard' )
47
+
48
+ def get_sla_extra_info (access_token , service_id ):
49
+ headers = {'Authorization' : 'bearer %s' % (access_token )}
50
+ url = cmdbUrl + "/service/id/" + service_id
51
+ response = requests .get (url , headers = headers , timeout = 20 )
52
+ response .raise_for_status ()
53
+ app .logger .info (json .dumps (response .json ()['data' ]['service_type' ]))
54
+
55
+ service_type = response .json ()['data' ]['service_type' ]
56
+ sitename = response .json ()['data' ]['sitename' ]
57
+ if 'properties' in response .json ()['data' ]:
58
+ if 'gpu_support' in response .json ()['data' ]['properties' ]:
59
+ service_type = service_type + " (gpu_support: " + str (response .json ()['data' ]['properties' ]['gpu_support' ]) + ")"
60
+
61
+ return sitename , service_type
62
+
63
+ def get_slas (access_token ):
64
+
65
+ headers = {'Authorization' : 'bearer %s' % (access_token )}
66
+
67
+ url = slamUrl + "/rest/slam/preferences/" + session ['organisation_name' ]
68
+ verify = True
69
+ if slam_cert :
70
+ verify = slam_cert
71
+ response = requests .get (url , headers = headers , timeout = 20 , verify = verify )
72
+ app .logger .info ("SLA response status: " + str (response .status_code ))
73
+
74
+ response .raise_for_status ()
75
+ app .logger .info ("SLA response: " + json .dumps (response .json ()))
76
+ slas = response .json ()['sla' ]
77
+
78
+ for i in range (len (slas )):
79
+ sitename , service_type = get_sla_extra_info (access_token ,slas [i ]['services' ][0 ]['service_id' ])
80
+ slas [i ]['service_type' ]= service_type
81
+ slas [i ]['sitename' ]= sitename
82
+
83
+ return slas
84
+
85
+ @app .route ('/slas' )
86
+ def getslas ():
87
+
88
+ if not iam_blueprint .session .authorized :
89
+ return redirect (url_for ('login' ))
90
+
91
+ slas = {}
92
+
93
+ try :
94
+ access_token = iam_blueprint .token ['access_token' ]
95
+ slas = get_slas (access_token )
96
+
97
+ except Exception as e :
98
+ flash ("Error retrieving SLAs list: \n " + str (e ), 'warning' )
99
+ return redirect (url_for ('home' ))
100
+
101
+ return render_template ('sla.html' , slas = slas )
102
+
103
+
104
+ @app .route ('/dashboard/<page>' )
105
+ @app .route ('/<page>' )
40
106
@app .route ('/' )
41
- def home ():
107
+ def home (page = 0 ):
42
108
43
- if not iam_blueprint .session .authorized :
44
- return redirect (url_for ('login' ))
45
-
109
+ if not iam_blueprint .session .authorized :
110
+ return redirect (url_for ('login' ))
111
+ try :
46
112
account_info = iam_blueprint .session .get ("/userinfo" )
47
113
48
114
if account_info .ok :
49
115
account_info_json = account_info .json ()
50
116
session ['username' ] = account_info_json ['name' ]
51
117
session ['gravatar' ] = avatar (account_info_json ['email' ], 26 )
118
+ session ['organisation_name' ]= account_info_json ['organisation_name' ]
52
119
access_token = iam_blueprint .token ['access_token' ]
53
120
54
121
headers = {'Authorization' : 'bearer %s' % (access_token )}
55
122
56
- url = orchestratorUrl + "/deployments?createdBy=me"
123
+ url = orchestratorUrl + "/deployments?createdBy=me&page=" + str ( page )
57
124
response = requests .get (url , headers = headers )
58
125
59
126
deployments = {}
60
127
if not response .ok :
61
128
flash ("Error retrieving deployment list: \n " + response .text , 'warning' )
62
129
else :
63
130
deployments = response .json ()["content" ]
64
- return render_template ('deployments.html' , deployments = deployments )
65
-
131
+ pages = response .json ()['page' ]['totalPages' ]
132
+ app .logger .debug (pages )
133
+ return render_template ('deployments.html' , deployments = deployments , tot_pages = pages , current_page = page )
134
+ except Exception :
135
+ app .logger .info ("error" )
136
+ return redirect (url_for ('logout' ))
66
137
67
138
68
139
@app .route ('/template/<depid>' )
@@ -125,7 +196,23 @@ def depcreate():
125
196
description = "N/A"
126
197
if 'description' in template :
127
198
description = template ['description' ]
128
- return render_template ('createdep.html' , templates = toscaTemplates , selectedTemplate = selected_tosca , description = description , inputs = inputs )
199
+
200
+ slas = get_slas (access_token )
201
+ return render_template ('createdep.html' , templates = toscaTemplates , selectedTemplate = selected_tosca , description = description , inputs = inputs , slas = slas )
202
+
203
+ def add_sla_to_template (template , sla_id ):
204
+ # Add the placement policy
205
+
206
+ nodes = template ['topology_template' ]['node_templates' ]
207
+ compute_nodes = []
208
+ # for key, dict in nodes.items():
209
+ # node_type=dict["type"]
210
+ # if node_type == "tosca.nodes.indigo.Compute" or node_type == "tosca.nodes.indigo.Container.Application.Docker.Chronos" :
211
+ # compute_nodes.append(key)
212
+ # template['topology_template']['policies']=[{ "deploy_on_specific_site": { "type": "tosca.policies.Placement", "properties": { "sla_id": sla_id }, "targets": compute_nodes } }]
213
+ template ['topology_template' ]['policies' ]= [{ "deploy_on_specific_site" : { "type" : "tosca.policies.Placement" , "properties" : { "sla_id" : sla_id } } }]
214
+ app .logger .info (yaml .dump (template ,default_flow_style = False ))
215
+ return template
129
216
#
130
217
#
131
218
@app .route ('/submit' , methods = ['POST' ])
@@ -136,23 +223,43 @@ def createdep():
136
223
137
224
access_token = iam_blueprint .session .token ['access_token' ]
138
225
226
+ app .logger .debug ("Form data: " + json .dumps (request .form .to_dict ()))
139
227
140
228
try :
141
- with io .open ( toscaDir + request .args .get ('template' )) as stream :
142
- payload = { "template" : stream .read (), "parameters" : request .form .to_dict () }
143
-
144
- body = json .dumps (payload )
229
+ with io .open ( toscaDir + request .args .get ('template' )) as stream :
230
+ template = yaml .load (stream )
231
+
232
+ form_data = request .form .to_dict ()
233
+
234
+ params = {}
235
+ if 'extra_opts.keepLastAttempt' in form_data :
236
+ params ['keepLastAttempt' ] = 'true'
237
+ else :
238
+ params ['keepLastAttempt' ] = 'false'
239
+
240
+ if form_data ['extra_opts.schedtype' ] == "man" :
241
+ template = add_sla_to_template (template , form_data ['extra_opts.selectedSLA' ])
242
+
243
+ inputs = { k :v for (k ,v ) in form_data .items () if not k .startswith ("extra_opts." ) }
244
+
245
+ app .logger .debug ("Parameters: " + json .dumps (inputs ))
246
+
247
+ payload = { "template" : yaml .dump (template ,default_flow_style = False ), "parameters" : inputs }
248
+
249
+ #body= json.dumps(payload)
145
250
146
251
url = orchestratorUrl + "/deployments/"
147
252
headers = {'Content-Type' : 'application/json' , 'Authorization' : 'bearer %s' % (access_token )}
148
- response = requests .post (url , data = body , headers = headers )
253
+ #response = requests.post(url, data=body, headers=headers)
254
+ response = requests .post (url , json = payload , params = params , headers = headers )
149
255
150
256
if not response .ok :
151
257
flash ("Error submitting deployment: \n " + response .text )
152
-
258
+
153
259
return redirect (url_for ('home' ))
260
+
154
261
except Exception as e :
155
- app . logger . error ("Error submitting deployment:" + str (e ))
262
+ flash ("Error submitting deployment:" + str (e ) + ". Please retry" )
156
263
return redirect (url_for ('home' ))
157
264
158
265
@@ -162,3 +269,5 @@ def logout():
162
269
iam_blueprint .session .get ("/logout" )
163
270
# del iam_blueprint.session.token
164
271
return redirect (url_for ('login' ))
272
+
273
+
0 commit comments