1
1
import arvados
2
+ import arvados .collection
3
+ import os
4
+ import connexion
5
+ import json
6
+ import subprocess
7
+ import tempfile
8
+
9
+ def get_api ():
10
+ return arvados .api_from_config (version = "v1" , apiconfig = {
11
+ "ARVADOS_API_HOST" : os .environ ["ARVADOS_API_HOST" ],
12
+ "ARVADOS_API_TOKEN" : connexion .request .headers ['Authorization' ],
13
+ "ARVADOS_API_HOST_INSECURE" : os .environ .get ("ARVADOS_API_HOST_INSECURE" , "false" ),
14
+ })
15
+
16
+
17
+ statemap = {
18
+ "Queued" : "Queued" ,
19
+ "Locked" : "Initializing" ,
20
+ "Running" : "Running" ,
21
+ "Complete" : "Complete" ,
22
+ "Cancelled" : "Canceled"
23
+ }
24
+
2
25
3
26
def GetServiceInfo ():
4
27
return {
@@ -12,31 +35,89 @@ def GetServiceInfo():
12
35
"key_values" : {}
13
36
}
14
37
15
- def ListWorkflows (body ):
38
+ def ListWorkflows (body = None ):
16
39
# body["page_size"]
17
40
# body["page_token"]
18
41
# body["key_value_search"]
19
42
20
- wf = []
21
- for l in os .listdir (os .path .join (os .getcwd (), "workflows" )):
22
- if os .path .isdir (os .path .join (os .getcwd (), "workflows" , l )):
23
- wf .append (Workflow (l ))
43
+ api = get_api ()
44
+
45
+ requests = api .container_requests ().list (filters = [["requesting_container_uuid" , "=" , None ]]).execute ()
46
+ containers = api .containers ().list (filters = [["uuid" , "in" , [w ["container_uuid" ] for w in requests ["items" ]]]]).execute ()
47
+
48
+ uuidmap = {c ["uuid" ]: statemap [c ["state" ]] for c in containers ["items" ]}
49
+
24
50
return {
25
- "workflows" : [{"workflow_id" : w .workflow_id , "state" : w .getstate ()} for w in wf ],
51
+ "workflows" : [{"workflow_id" : cr ["uuid" ],
52
+ "state" : uuidmap [cr ["container_uuid" ]]}
53
+ for cr in requests ["items" ]
54
+ if cr ["command" ][0 ] == "arvados-cwl-runner" ],
26
55
"next_page_token" : ""
27
56
}
28
57
29
58
def RunWorkflow (body ):
30
59
if body ["workflow_type" ] != "CWL" or body ["workflow_type_version" ] != "v1.0" :
31
60
return
32
- workflow_id = uuid .uuid4 ().hex
33
- job = Workflow (workflow_id )
34
- job .run (body )
61
+
62
+ env = {
63
+ "ARVADOS_API_HOST" : os .environ ["ARVADOS_API_HOST" ],
64
+ "ARVADOS_API_TOKEN" : connexion .request .headers ['Authorization' ],
65
+ "ARVADOS_API_HOST_INSECURE" : os .environ .get ("ARVADOS_API_HOST_INSECURE" , "false" )
66
+ }
67
+ with tempfile .NamedTemporaryFile () as inputtemp :
68
+ json .dump (request ["workflow_params" ], inputtemp )
69
+ workflow_id = subprocess .check_output (["arvados-cwl-runner" , "--submit" , "--no-wait" ,
70
+ request .get ("workflow_url" ), inputtemp .name ], env = env )
35
71
return {"workflow_id" : workflow_id }
36
72
73
+ def visit (d , op ):
74
+ op (d )
75
+ if isinstance (d , list ):
76
+ for i in d :
77
+ visit (i , op )
78
+ elif isinstance (d , dict ):
79
+ for i in d .itervalues ():
80
+ visit (i , op )
81
+
37
82
def GetWorkflowLog (workflow_id ):
38
- job = Workflow (workflow_id )
39
- return job .getlog ()
83
+ api = get_api ()
84
+
85
+ request = api .container_requests ().get (uuid = workflow_id ).execute ()
86
+ container = api .containers ().get (uuid = request ["container_uuid" ]).execute ()
87
+
88
+ outputobj = {}
89
+ if request ["output_uuid" ]:
90
+ c = arvados .collection .CollectionReader (request ["output_uuid" ])
91
+ with c .open ("cwl.output.json" ) as f :
92
+ outputobj = json .load (f )
93
+ def keepref (d ):
94
+ if isinstance (d , dict ) and "location" in d :
95
+ d ["location" ] = "keep:%s/%s" % (c .portable_data_hash (), d ["location" ])
96
+ visit (outputobj , keepref )
97
+
98
+ stderr = ""
99
+ if request ["log_uuid" ]:
100
+ c = arvados .collection .CollectionReader (request ["log_uuid" ])
101
+ if "stderr.txt" in c :
102
+ with c .open ("stderr.txt" ) as f :
103
+ stderr = f .read ()
104
+
105
+ return {
106
+ "workflow_id" : request ["uuid" ],
107
+ "request" : {},
108
+ "state" : statemap [container ["state" ]],
109
+ "workflow_log" : {
110
+ "cmd" : ["" ],
111
+ "startTime" : "" ,
112
+ "endTime" : "" ,
113
+ "stdout" : "" ,
114
+ "stderr" : stderr ,
115
+ "exitCode" : container ["exit_code" ]
116
+ },
117
+ "task_logs" : [],
118
+ "outputs" : outputobj
119
+ }
120
+
40
121
41
122
def CancelJob (workflow_id ):
42
123
job = Workflow (workflow_id )
0 commit comments