-
Notifications
You must be signed in to change notification settings - Fork 3
/
taverna.py
562 lines (388 loc) · 20.4 KB
/
taverna.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
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
557
558
559
560
# Copyright (C) 2012 SCS srl <[email protected]>
""" This module defines the TavernaServerConnector a wrapper for the Taverna Server functions."""
import httplib
import base64
import thread
import requests
import forward
class TavernaServerConnector():
""" The TavernaServerConnector allows the Workflow Manager to contact the Taverna Server application, directly
trough http or tunneled trough a ssh connection.
Arguments:
tunneling (boolean): True if connection goes trough a ssh tunnel
url (string): the server url
localPort (int): the local port number
remoteHost (string): the remoteHost name
remotePort (int): the remote port number
username (string): the username to access to the remote system
password (string): the password to access to the remote system
"""
def __init__(self, tunneling, url, localPort=8080, remoteHost='', remotePort=8080, username='', password='', maxAttempts = 10):
""" initialize the connector with the given taverna server url
"""
if tunneling:
self.tunneling = True
self.tunneler = thread.start_new_thread(forward.start,
(localPort, remoteHost, remotePort, username, password))
self.server_url = '127.0.0.1:%s' % localPort
else:
self.tunneling = False
self.server_url = url
self.service_url = "/taverna-server/rest/runs"
self.userAndPass = base64.b64encode(username + ":" + password)
self.CREATE_WORKFLOW_MAX_NUMBER_OF_ATTEMPTS = maxAttempts
def createWorkflow(self, workflowDefinition):
""" Create a new workflow according to the given definition string.
Arguments:
workflowDefinition (string): the workflow definition file string buffer
Returns:
dictionary::
Success -- {'workflowId':'a string value'}
Failure -- {'workflowId':'', 'error.description':'', error.code:''}
"""
self.connection = httplib.HTTPSConnection(self.server_url)
ret = {}
wf = workflowDefinition
# if not wf.count("""<workflow xmlns="http://ns.taverna.org.uk/2010/xml/server/">"""):
# wf = """<workflow xmlns="http://ns.taverna.org.uk/2010/xml/server/"> %s </workflow>""" % wf
workflowCreatedSucessfully = False
counter = 0
while (not workflowCreatedSucessfully) and counter < self.CREATE_WORKFLOW_MAX_NUMBER_OF_ATTEMPTS:
try:
# increase attempt counter
counter = counter + 1
# post workflow definition file
#Maybe is better to change all http call with requests library,it is easier to use and faster.
#response = requests.post('https://'+self.server_url+self.service_url, data=wf, headers={"content-type":"application/vnd.taverna.t2flow+xml", 'Authorization' : 'Basic %s' % self.userAndPass}, verify=False)
headers = {"Content-type": "application/vnd.taverna.t2flow+xml" , 'Authorization' : 'Basic %s' % self.userAndPass}
self.connection.request("POST", self.service_url, wf, headers)
# get and handle response
response = self.connection.getresponse()
o = response.read()
if response.status == 201:
workflowCreatedSucessfully = True
# workflow has been correctly created
ret["workflowId"] = response.msg["Location"].split("/")[-1]
# get brand new created workflow information
info = self.getWorkflowInformation(ret["workflowId"])
ret.update(info)
else:
ret["workflowId"] = ""
ret["error.description"] = "Error Creating Workflow: " + o
if counter == self.CREATE_WORKFLOW_MAX_NUMBER_OF_ATTEMPTS:
ret["error.description"] = ret["error.description"] + ". Maximum number of attempts reached !"
ret["error.code"] = "%s %s" % ( response.status, response.reason)
except Exception as e:
ret["workflowId"] = ""
ret["error.description"] = "Error Creating Workflow! %s" % str(e)
ret["error.code"] = ""
# close previous connection
self.connection.close()
return ret
def setServerURL(self, url):
""" Sets the base URL of the Taverna Server
Arguments:
url (string): the base URL of the Taverna Server
"""
self.server_url = url
def setServicePath(self, path):
""" Sets the path to the Taverna Server, with respect to the base URL
Arguments:
url (string): the path to the Taverna Server, with respect to the base URL
"""
self.service_url = path
def setPlugins(self, workflowId, pluginDefinition):
""" Takes the contents of a plugin.xml file, and then creates this file in the server
Arguments:
workflowId (string): the workflow unique identifier
pluginDefinition (string): the plugin definition file string buffer
Returns:
dictionary::
Success -- {'workflowId':'a string value'}
Failure -- {'workflowId':'', 'error.description':'', error.code:''}
"""
self.connection = httplib.HTTPSConnection(self.server_url)
ret = {}
plugins = """<t2sr:upload t2sr:name="plugins.xml" xmlns:t2sr="http://ns.taverna.org.uk/2010/xml/server/rest/">%s</t2sr:upload>""" % base64.b64encode(pluginDefinition)
try:
ret["workflowId"] = workflowId
# POST plugin.xml file
headers = {"Content-type": "application/xml" , 'Authorization' : 'Basic %s' % self.userAndPass}
self.connection.request('POST',
"%s/%s/wd/plugins" % (self.service_url, workflowId),
plugins,
headers)
response = self.connection.getresponse()
o = response.read()
if response.status != 201:
ret["workflowId"] = ""
ret["error.description"] = "Error Creating Plugin File!"
ret["error.code"] = "%s %s" % (response.status, response.reason)
except Exception as e:
ret["workflowId"] = ""
ret["error.description"] = "Error Creating Plugin File!"
ret["error.code"] = "500 Internal Server Error"
self.connection.close()
return ret
def setPluginProperties(self, workflowId, propertiesFileName, propertiesDefinition):
""" Creates a properties file with the specified filename and content, and uploads the file to the server
Arguments:
workflowId (string): the workflow unique identifier
propertiesFileName (string): the name of the properties file to be created
propertiesDefinition (string): the content of the properties file
Returns:
dictionary::
Success -- {'workflowId':'a string value'}
Failure -- {'workflowId':'', 'error.description':'', error.code:''}
"""
self.connection = httplib.HTTPSConnection(self.server_url)
ret = {}
properties = """<t2sr:upload t2sr:name="%s" xmlns:t2sr="http://ns.taverna.org.uk/2010/xml/server/rest/">%s</t2sr:upload>""" % (propertiesFileName,base64.b64encode(propertiesDefinition))
try:
ret["workflowId"] = workflowId
# POST properties file
headers = {"Content-type": "application/xml" , 'Authorization' : 'Basic %s' % self.userAndPass}
self.connection.request('POST',
"%s/%s/wd/conf" % (self.service_url, workflowId),
properties,
headers)
response = self.connection.getresponse()
o = response.read()
if response.status != 201:
ret["workflowId"] = ""
ret["error.description"] = "Error Creating Properties File!"
ret["error.code"] = "%s %s" % (response.status, response.reason)
except Exception as e:
ret["workflowId"] = ""
ret["error.description"] = "Error Creating Properties File!"
ret["error.code"] = "500 Internal Server Error"
self.connection.close()
return ret
def setTicket(self, workflowId, ticket):
""" Stores the specified ticket in the working directory of the workflow with the specified id
Arguments:
workflowId (string): the workflow unique identifier
ticket (string): a valid authentication ticket
Returns:
dictionary::
Success -- {'workflowId':'a string value'}
Failure -- {'workflowId':'', 'error.description':'', error.code:''}
"""
self.connection = httplib.HTTPSConnection(self.server_url)
ret = {}
credential = """<t2sr:upload t2sr:name="ticket" xmlns:t2sr="http://ns.taverna.org.uk/2010/xml/server/rest/">"""
credential = credential + base64.b64encode(ticket)
credential = credential + """</t2sr:upload>"""
try:
ret["workflowId"] = workflowId
# POST credentials
headers = {"Content-type": "application/xml" , 'Authorization' : 'Basic %s' % self.userAndPass}
self.connection.request('POST',
"%s/%s/wd/conf" % (self.service_url, workflowId),
credential,
headers)
response = self.connection.getresponse()
o = response.read()
if response.status != 201:
ret["workflowId"] = ""
ret["error.description"] = "Error setting authentication ticket in workflow working directory!"
ret["error.code"] = "%s %s" % (response.status, response.reason)
except Exception as e:
ret["workflowId"] = ""
ret["error.description"] = "Error setting authentication ticket in workflow working directory!"
ret["error.code"] = "500 Internal Server Error"
self.connection.close()
return ret
def setTrustedIdentity(self, workflowId, identityFileName, identityDefinition):
""" Takes the name of the certificate file, reads the contents of the file and submits the file contents to the server
Arguments:
workflowId (string): the workflow unique identifier
identityFileName (string): name of the certificate file
identityDefinition (string): contents of the certificate file
Returns:
dictionary::
Success -- {'workflowId':'a string value'}
Failure -- {'workflowId':'', 'error.description':'', error.code:''}
"""
self.connection = httplib.HTTPSConnection(self.server_url)
ret = {}
identity = """<t2sr:trustedIdentity xmlns:t2sr="http://ns.taverna.org.uk/2010/xml/server/" xmlns:t2s="http://ns.taverna.org.uk/2010/xml/server/">"""
identity = identity + """<t2s:certificateFile>%s</t2s:certificateFile>""" % base64.b64encode(identityFileName)
identity = identity + """<t2s:certificateBytes>%s</t2s:certificateBytes></t2sr:trustedIdentity>""" % base64.b64encode(identityDefinition)
try:
ret["workflowId"] = workflowId
# POST identity file
headers = {"Content-type": "application/xml" , 'Authorization' : 'Basic %s' % self.userAndPass}
self.connection.request('POST',
"%s/%s/security/trusts" % (self.service_url, workflowId),
identity,
headers)
response = self.connection.getresponse()
o = response.read()
if response.status != 201:
ret["workflowId"] = ""
ret["error.description"] = "Error Creating Trusted Identity!"
ret["error.code"] = "%s %s" % (response.status, response.reason)
except Exception as e:
ret["workflowId"] = ""
ret["error.description"] = "Error Creating Trusted Identity!"
ret["error.code"] = "500 Internal Server Error"
self.connection.close()
return ret
def setWorkflowInputs(self, workflowId, inputDefinition):
""" Take the inputs definition string and create a baclava.xml file for the workflow with the given id.
Arguments:
workflowId (string): the workflowd unique identifier
workflowDefinition (string): the workflow definition file string buffer
inputDefinition (string): the input definition file string buffer:param workflowId: the id unique identifier
defaultInputMap (map): a map with the default inputs to be added to all (i.e. defaultInputMap = {'sessionTicket': <sessionTicket<, 'workflowId': >workflowId>, ...}
Returns:
dictionary::
Success -- {'workflowId':'a string value'}
Failure -- {'workflowId':'', 'error.description':'', error.code:''}
"""
ret = {}
self.connection = httplib.HTTPSConnection(self.server_url)
baclava = """<t2sr:upload xmlns:t2sr="http://ns.taverna.org.uk/2010/xml/server/rest/" t2sr:name="baclava.xml">%s</t2sr:upload>""" % base64.b64encode(inputDefinition)
headers = {"Content-type": "application/xml" , 'Authorization' : 'Basic %s' % self.userAndPass}
self.connection.request('POST',
"%s/%s/wd" % (self.service_url, workflowId),
baclava,
headers)
response = self.connection.getresponse()
o = response.read()
try:
ret["workflowId"] = workflowId
# PUT baclava
headers = {"Content-type": "text/plain" , 'Authorization' : 'Basic %s' % self.userAndPass}
self.connection.request('PUT',
"%s/%s/input/baclava" % (self.service_url, workflowId ),
"baclava.xml",
headers)
response = self.connection.getresponse()
o = response.read()
if response.status != 200:
ret["workflowId"] = ""
ret["error.description"] = "Error Creating Input File!"
ret["error.code"] = "%s %s" % (response.status, response.reason)
except Exception as e:
ret["workflowId"] = ""
ret["error.description"] = "Error Creating Input File!"
ret["error.code"] = "500 Internal Server Error"
self.connection.close()
return ret
def getWorkflowInputs(self, workflowId):
"""
Retrieve the workflow inputs file from taverna server
:param workflowId:
:return:
"""
self.connection = httplib.HTTPSConnection(self.server_url)
headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Authorization' : 'Basic %s' % self.userAndPass}
self.connection.request("GET", "%s/%s/wd/baclava.xml" % (self.service_url, workflowId), "", headers)
response = self.connection.getresponse()
ret = response.read()
self.connection.close()
return ret
def getWorkflowDefinition(self, workflowId):
"""
Retrieve the workflow file from taverna server
:param workflowId:
:return:
"""
self.connection = httplib.HTTPSConnection(self.server_url)
headers = {"Content-type": "text/plain" , 'Authorization' : 'Basic %s' % self.userAndPass}
self.connection.request("GET", "%s/%s/workflow" % (self.service_url, workflowId), "", headers)
response = self.connection.getresponse()
ret = response.read()
self.connection.close()
return ret
def getWorkflowInformation(self, workflowId):
""" return the basic information associated to the given workflow id
Arguments:
workflowId (string): the workflow unique identifier
Returns:
dictionary::
Success -- {'workflowId':'a string value', 'info01':'a string value', 'info02':'a string value',..}
Failure -- {'workflowId':'', 'error.description':'', error.code:''}
"""
ret = {'workflowId': workflowId}
infos = ["status", "createTime", "expiry", "startTime", "finishTime"]
headers = {"Content-type": "text/plain" , 'Authorization' : 'Basic %s' % self.userAndPass}
for info in infos:
try:
ret[info] = self.getWorkflowInfo(workflowId, info, headers)
except Exception as e:
ret[info] = ""
ret["error.description"] = "Error Getting Workflow Information ( %s ) " % info
ret["error.code"] = type(e)
additional_info = {'stderr': "listeners/io/properties/stderr",
'stdout': "listeners/io/properties/stdout",
'exitcode': "listeners/io/properties/exitcode"}
for info in additional_info.keys():
try:
ret[info] = self.getWorkflowInfo(workflowId, additional_info[info], headers)
except Exception as e:
ret[info] = ""
ret["error.description"] = "Error Getting Workflow Information ( %s ) " % info
ret["error.code"] = type(e)
try:
headers = {"Content-type": "text/plain" , 'Authorization' : 'Basic %s' % self.userAndPass, 'Accept': 'application/xml'}
ret["output"] = self.getWorkflowInfo(workflowId, "output", headers)
except Exception as e:
ret[info] = ""
ret["error.description"] = "Error Getting Workflow Information ( %s ) " % info
ret["error.code"] = type(e)
return ret
def getWorkflowInfo(self, workflowId, info, headers):
""" return the workflow id requested info as a string
Arguments:
workflowId (string): the workflow unique identifier
Returns:
string. The requested info as a string
"""
response = requests.get('https://%s/%s/%s'%(self.server_url+self.service_url,workflowId, info), headers=headers, verify=False)
ret = response.content
return ret
def startWorkflow(self, workflowId):
""" start the workflow with the given workflow id
Arguments:
workflowId (string): the workflow unique identifier
Returns:
dictionary::
Success -- {'workflowId':'a string value', 'info01':'a string value', 'info02':'a string value',..}
Failure -- {'workflowId':'', 'error.description':'', error.code:''}
"""
self.connection = httplib.HTTPSConnection(self.server_url)
headers = {"Content-type": "text/plain" , 'Authorization' : 'Basic %s' % self.userAndPass}
self.connection.request('PUT',
"%s/%s/status" % (self.service_url, workflowId ),
"Operating",
headers)
result = self.connection.getresponse()
result.read()
self.connection.close()
return self.getWorkflowInformation(workflowId)
def deleteWorkflow(self, workflowId):
""" delete the workflow with the given workflow id
Arguments:
workflowId (string): the workflow unique identifier
Returns:
dictionary::
Success -- {'workflowId':'a string value', 'info01':'a string value', 'info02':'a string value',..}
Failure -- {'workflowId':'', 'error.description':'', error.code:''}
"""
# recover information
info = self.getWorkflowInformation(workflowId)
# send delete commmand
self.connection = httplib.HTTPSConnection(self.server_url)
headers = {"Content-type": "text/plain" , 'Authorization' : 'Basic %s' % self.userAndPass}
self.connection.request('DELETE',
"%s/%s" % (self.service_url, workflowId),
"",
headers)
response = self.connection.getresponse()
response.read()
self.connection.close()
info['status'] = 'Deleted'
return info