This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbioexcel.py
331 lines (309 loc) · 13.5 KB
/
bioexcel.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
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python3
from __future__ import print_function
import time
import ecp
import sys
import argparse
import json
import threading
import yaml
import jwt
class BIEXCEL:
users = []
sessions = None
bioexceltools = {}
nfsclienttools = {}
launcher = {}
deployconf = {}
destroyconf = {}
nfsserver = None
status = {}
deploymentstatus = {}
ownertoken = None
jsondir = None
def __init__(self):
self.nfsserver = ""
self.nfsremotedir = "/var/nfs"
self.sessions = 1
self.ownertoken = "owner.txt"
self.jsondir = "json/"
self.inputjson = self.jsondir+"config/input.json";
def login(self, username, password, ecpcli):
login = ecpcli.login(username, password)
if "401" in login:
print("Login failed! Invalid username or password given.")
sys.exit(0)
else:
print("Login success!!")
def check_shared_config(self, ecpcli):
print("Checking shared configuration..")
bioexcelconfig = False
res = ecpcli.make_request('get', 'sharedconfig', '')
resp = res.json()
if '_embedded' in resp:
for config in resp['_embedded']['configurationResourceList']:
if config['name'] == "BioExcel CPMD license on Embassy Cloud":
bioexcelconfig = True
if bioexcelconfig:
print("Configurations shared with user.")
return True
else:
print("Configuration not shared with user.")
ecpowner = ecp.ECP()
ecpowner.get_token(self.ownertoken)
email = self.get_email(ecpcli)
bioexcelconfig = self.join_team("BioExcel Embassy", email, ecpowner)
if bioexcelconfig:
print("Adding to team success !!")
return True
else:
print("Adding to team failed !!")
sys.exit(0)
def get_email(self, ecpcli):
token = ecpcli.get_session_token()
decoded = jwt.decode(token, verify=False)
email = decoded['email']
return email
def join_team(self, teamname, email, ecpowner):
data = {"name": "" + teamname + "", "memberAccountEmails": ["" + email + ""]}
datajson = json.dumps(data)
response = ecpowner.make_request('create', 'jointeam', '', datajson)
if response.status_code == 200:
return True
else:
return False
def get_users(self):
datafh = open(self.jsondir+'user.json', 'r')
data = datafh.read()
datafh.close()
userjson = json.loads(data)
self.sessions = int(userjson['user-sessions'])
self.users = userjson['users']
def get_tools_config(self):
datafh = open(self.inputjson, 'r')
data = datafh.read()
datafh.close()
jsondata = json.loads(data)
self.nfsserver = jsondata['nfs_server']
self.nfsremotedir = jsondata['nfs_remote_folder']
bioexcel = jsondata['bioexcel']
for apps in bioexcel:
self.bioexceltools[apps['application_name'] + "-url"] = apps['image_source_url']
self.bioexceltools[apps['application_name'] + "-config"] = apps['configuration_name']
nfsclient = jsondata['nfsclient']
for apps in nfsclient:
self.nfsclienttools[apps['application_name'] + "-url"] = apps['image_source_url']
self.nfsclienttools[apps['application_name'] + "-config"] = apps['configuration_name']
def get_launcher_data(self):
file = self.jsondir+'launcher/bioexcel.json'
datafh = open(file, 'r')
data = datafh.read()
self.launcher['bioexcel'] = json.loads(data)
file = self.jsondir+'launcher/nfsclient.json'
datafh = open(file, 'r')
data = datafh.read()
self.launcher['nfsclient'] = json.loads(data)
file = self.jsondir+'launcher/ecpimage.json'
datafh = open(file, 'r')
data = datafh.read()
datafh.close()
self.launcher['ecpimage'] = json.loads(data)
file = self.jsondir + 'launcher/ecpapplication.json'
datafh = open(file, 'r')
data = datafh.read()
datafh.close()
self.launcher['ecpapplication'] = json.loads(data)
def get_deploy_config(self):
datafh = open(self.jsondir+'deploy.json', 'r')
data = datafh.read()
datafh.close()
jsondata = json.loads(data)
self.deployConf = jsondata['deployments']
def get_destroy_config(self):
datafh = open(self.jsondir+'destroy.json', 'r')
data = datafh.read()
datafh.close()
jsondata = json.loads(data)
self.deployments = jsondata['deployments']
def get_json_data(self, launcher, toolname, configname):
jsonData = self.launcher[launcher]
if launcher == 'bioexcel':
inputs = [{"inputName": "application_name", "assignedValue": toolname},
{"inputName": "image_source_url", "assignedValue": self.bioexceltools.get(toolname + "-url")},
{"inputName": "image_disk_type", "assignedValue": "BioExcel_Embassy_VM"}]
if configname == '':
jsonData["configurationName"] = self.bioexceltools.get(toolname + "-config")
else:
jsonData["configurationName"] = configname
elif launcher == 'nfsclient':
inputs = [{"inputName": "nfs_server_host", "assignedValue": self.nfsserver},
{"inputName": "remote_folder", "assignedValue": self.nfsremotedir},
{"inputName": "application_name", "assignedValue": toolname}]
if configname == '':
jsonData["configurationName"] = self.nfsclienttools.get(toolname + "-config")
else:
jsonData["configurationName"] = configname
elif launcher == 'ecpimage':
inputs = [{"inputName": "application_name", "assignedValue": toolname}]
jsonData["configurationName"] = configname
else:
jsonData["applicationName"] = toolname
jsonData["configurationName"] = configname
inputs = []
jsondumps = json.dumps(inputs)
inputjson = json.loads(jsondumps)
jsonData["assignedInputs"] = inputjson
return json.dumps(jsonData)
def deploy(self, ecpcli, reqid):
for dep in self.deployConf:
toolname = dep['application_name']
launcher = dep['launcher']
configname = dep['config_name']
teamname = dep['team_name']
data = self.get_json_data(launcher, toolname, configname)
print(yaml.safe_dump(data, indent=2, default_flow_style=False))
if launcher == 'ecpapplication':
response = ecpcli.make_request('create', 'deployment?teamName', teamname, data)
else:
response = ecpcli.make_request('create', 'deployment', '', data)
start = time.time()
res = response.json()
try:
reference = res['reference']
except:
print("Exception while creating deployment!")
print("Response status : ", response.status_code)
print("Response from server : ", res)
return "NONE|CREATION_FAILED"
print("Deployment process {0} started. Reference :- {1}".format(reqid, reference))
print("Deployment logs : ")
logexLen = 0
while True:
logs = ecpcli.make_request('get', 'logs', reference, '')
logText = (logs.text).rstrip('\r\n')
logTextLen = len(logText)
logText1 = logText[logexLen:logTextLen].rstrip('\r\n')
if len(logText1) > 0:
print("----Request ID {0} ; log for reference {1} ".format(reqid, reference))
print(logText1)
logexLen = logTextLen
if ('external_ip' in logText):
done = time.time()
elapsed = done - start
print("Request ID {0} Deployment [{1}] started Running!".format(reqid, reference))
self.deploymentstatus[reqid] = "{0} : RUNNING :{1:5.4f}".format(reference, elapsed)
break
elif ('error(s) occurred' in logText):
done = time.time()
elapsed = done - start
print("Request ID {0} ; Deployment [{1}] starting failed!".format(reqid, reference))
self.deploymentstatus[reqid] = "{0} : FAILED :{1:5.4f}".format(reference, elapsed)
break
else:
time.sleep(5)
def destroy(self, reference, ecpcli):
response = ecpcli.make_request('stop', 'deployment', reference, None)
if response.status_code == 200:
print("Destroy logs : ")
logexLen = 0
count = 0
while True:
logs = ecpcli.make_request('get', 'destroylogs', reference, '')
logText = (logs.text).rstrip('\r\n')
logTextLen = len(logText)
logText1 = logText[logexLen:logTextLen].rstrip('\r\n')
if len(logText1) > 0:
print(logText1)
logexLen = logTextLen
count += 1
if ('Destroy complete' in logText):
return reference + "|DESTROYED"
break
elif ('error(s) occurred' in logText):
return reference + "|FAILED"
break
else:
if count == 60:
return reference + "|TIME_OUT"
break
else:
time.sleep(5)
else:
return reference + "|FAILED"
def main(self, argv):
parser = argparse.ArgumentParser(description='Bioexcel Cloud Portal e2e test Client')
parser.add_argument('action', help='Action to perform : deploy/destroy')
parser.add_argument('--token', help='File contains JWT identity token, Optional.')
parser.add_argument('--json', help='directory contains all config jsons, Optional, '
'it will look up "json" folder in current directory by default.')
parser.add_argument('--owner', help='File contains "BioExcel Embassy" team owner account JWT ,'
'it will look up "owner.txt" in current directory by default.')
args = parser.parse_args()
if args.owner is not None:
self.ownertoken = args.owner
if args.json is not None:
self.jsondir = args.json+"/"
self.get_users()
if args.action == 'deploy':
self.get_tools_config()
self.get_deploy_config()
self.get_launcher_data()
if args.token is None:
threads = list()
usercount = 0
for user in self.users:
ecpcli = ecp.ECP()
usercount += 1
self.login(user['username'], user['password'], ecpcli)
self.check_shared_config(ecpcli)
x = threading.Thread(target=self.deploy, args=(ecpcli, usercount), daemon=True)
threads.append(x)
x.start()
else:
ecpcli = ecp.ECP()
ecpcli.get_token(args.token)
self.check_shared_config(ecpcli)
sessiontoken = ecpcli.get_session_token()
threads = list()
for i in range(self.sessions):
ecpclisession = ecp.ECP()
ecpclisession.set_token(sessiontoken)
x = threading.Thread(target=self.deploy, args=(ecpclisession, i), daemon=True)
threads.append(x)
x.start()
threadcount = 0
for index, thread in enumerate(threads):
thread.join()
threadcount += 1
if threadcount == self.sessions:
print(" Results : ")
print("No REFERENCE STATUS ELAPSED")
print(yaml.safe_dump(self.deploymentstatus, indent=2, default_flow_style=False))
elif args.action == 'destroy':
ecpcli = ecp.ECP()
if args.token is None:
self.login(self.users[0]['username'], self.users[0]['password'], ecpcli)
else:
ecpcli.get_token(args.token)
self.get_destroy_config()
count = 0
for deployment in self.deployments:
reference = deployment['reference']
self.status[count] = self.destroy(reference, ecpcli)
count += 1
print("RESULT : ")
print("---------------------------------------")
print("- REFERENCE - STATUS -")
print("---------------------------------------")
for i in range(len(self.status)):
statusspl = self.status[i].split("|")
print("- " + statusspl[0] + " - " + statusspl[1] + " -")
print("- - -")
print("---------------------------------------")
def run():
"""Run bioexcel cli"""
bioexcel = BIEXCEL()
sys.exit(bioexcel.main(sys.argv))
if __name__ == "__main__":
bioexcel = BIEXCEL()
bioexcel.main(sys.argv)