5
5
import json
6
6
import subprocess
7
7
import tempfile
8
+ from wes_service .util import visit , WESBackend
8
9
9
10
def get_api ():
10
11
return arvados .api_from_config (version = "v1" , apiconfig = {
@@ -22,109 +23,112 @@ def get_api():
22
23
"Cancelled" : "Canceled"
23
24
}
24
25
25
-
26
- def GetServiceInfo ():
27
- return {
28
- "workflow_type_versions" : {
29
- "CWL" : ["v1.0" ]
30
- },
31
- "supported_wes_versions" : "0.1.0" ,
32
- "supported_filesystem_protocols" : ["file" ],
33
- "engine_versions" : "cwl-runner" ,
34
- "system_state_counts" : {},
35
- "key_values" : {}
36
- }
37
-
38
- def ListWorkflows (body = None ):
39
- # body["page_size"]
40
- # body["page_token"]
41
- # body["key_value_search"]
42
-
43
- api = get_api ()
44
-
45
- requests = api .container_requests ().list (filters = [["requesting_container_uuid" , "=" , None ]],
46
- select = ["uuid" , "command" , "container_uuid" ]).execute ()
47
- containers = api .containers ().list (filters = [["uuid" , "in" , [w ["container_uuid" ] for w in requests ["items" ]]]],
48
- select = ["uuid" , "state" ]).execute ()
49
-
50
- uuidmap = {c ["uuid" ]: statemap [c ["state" ]] for c in containers ["items" ]}
51
-
52
- return {
53
- "workflows" : [{"workflow_id" : cr ["uuid" ],
54
- "state" : uuidmap [cr ["container_uuid" ]]}
55
- for cr in requests ["items" ]
56
- if cr ["command" ][0 ] == "arvados-cwl-runner" ],
57
- "next_page_token" : ""
58
- }
59
-
60
- def RunWorkflow (body ):
61
- if body ["workflow_type" ] != "CWL" or body ["workflow_type_version" ] != "v1.0" :
62
- return
63
-
64
- env = {
65
- "PATH" : os .environ ["PATH" ],
66
- "ARVADOS_API_HOST" : os .environ ["ARVADOS_API_HOST" ],
67
- "ARVADOS_API_TOKEN" : connexion .request .headers ['Authorization' ],
68
- "ARVADOS_API_HOST_INSECURE" : os .environ .get ("ARVADOS_API_HOST_INSECURE" , "false" )
69
- }
70
- with tempfile .NamedTemporaryFile () as inputtemp :
71
- json .dump (body ["workflow_params" ], inputtemp )
72
- inputtemp .flush ()
73
- workflow_id = subprocess .check_output (["arvados-cwl-runner" , "--submit" , "--no-wait" , "--api=containers" ,
74
- body .get ("workflow_url" ), inputtemp .name ], env = env ).strip ()
75
- return {"workflow_id" : workflow_id }
76
-
77
-
78
- def GetWorkflowLog (workflow_id ):
79
- api = get_api ()
80
-
81
- request = api .container_requests ().get (uuid = workflow_id ).execute ()
82
- container = api .containers ().get (uuid = request ["container_uuid" ]).execute ()
83
-
84
- outputobj = {}
85
- if request ["output_uuid" ]:
86
- c = arvados .collection .CollectionReader (request ["output_uuid" ])
87
- with c .open ("cwl.output.json" ) as f :
88
- outputobj = json .load (f )
89
- def keepref (d ):
90
- if isinstance (d , dict ) and "location" in d :
91
- d ["location" ] = "keep:%s/%s" % (c .portable_data_hash (), d ["location" ])
92
- visit (outputobj , keepref )
93
-
94
- stderr = ""
95
- if request ["log_uuid" ]:
96
- c = arvados .collection .CollectionReader (request ["log_uuid" ])
97
- if "stderr.txt" in c :
98
- with c .open ("stderr.txt" ) as f :
99
- stderr = f .read ()
100
-
101
- r = {
102
- "workflow_id" : request ["uuid" ],
103
- "request" : {},
104
- "state" : statemap [container ["state" ]],
105
- "workflow_log" : {
106
- "cmd" : ["" ],
107
- "startTime" : "" ,
108
- "endTime" : "" ,
109
- "stdout" : "" ,
110
- "stderr" : stderr
111
- },
112
- "task_logs" : [],
113
- "outputs" : outputobj
114
- }
115
- if container ["exit_code" ] is not None :
116
- r ["workflow_log" ]["exitCode" ] = container ["exit_code" ]
117
- return r
118
-
119
-
120
- def CancelJob (workflow_id ):
121
- api = get_api ()
122
- request = api .container_requests ().update (body = {"priority" : 0 }).execute ()
123
- return {"workflow_id" : request ["uuid" ]}
124
-
125
- def GetWorkflowStatus (workflow_id ):
126
- api = get_api ()
127
- request = api .container_requests ().get (uuid = workflow_id ).execute ()
128
- container = api .containers ().get (uuid = request ["container_uuid" ]).execute ()
129
- return {"workflow_id" : request ["uuid" ],
130
- "state" : statemap [container ["state" ]]}
26
+ class ArvadosBackend (WESBackend ):
27
+ def GetServiceInfo (self ):
28
+ return {
29
+ "workflow_type_versions" : {
30
+ "CWL" : ["v1.0" ]
31
+ },
32
+ "supported_wes_versions" : "0.1.0" ,
33
+ "supported_filesystem_protocols" : ["file" ],
34
+ "engine_versions" : "cwl-runner" ,
35
+ "system_state_counts" : {},
36
+ "key_values" : {}
37
+ }
38
+
39
+ def ListWorkflows (self , body = None ):
40
+ # body["page_size"]
41
+ # body["page_token"]
42
+ # body["key_value_search"]
43
+
44
+ api = get_api ()
45
+
46
+ requests = api .container_requests ().list (filters = [["requesting_container_uuid" , "=" , None ]],
47
+ select = ["uuid" , "command" , "container_uuid" ]).execute ()
48
+ containers = api .containers ().list (filters = [["uuid" , "in" , [w ["container_uuid" ] for w in requests ["items" ]]]],
49
+ select = ["uuid" , "state" ]).execute ()
50
+
51
+ uuidmap = {c ["uuid" ]: statemap [c ["state" ]] for c in containers ["items" ]}
52
+
53
+ return {
54
+ "workflows" : [{"workflow_id" : cr ["uuid" ],
55
+ "state" : uuidmap [cr ["container_uuid" ]]}
56
+ for cr in requests ["items" ]
57
+ if cr ["command" ][0 ] == "arvados-cwl-runner" ],
58
+ "next_page_token" : ""
59
+ }
60
+
61
+ def RunWorkflow (self , body ):
62
+ if body ["workflow_type" ] != "CWL" or body ["workflow_type_version" ] != "v1.0" :
63
+ return
64
+
65
+ env = {
66
+ "PATH" : os .environ ["PATH" ],
67
+ "ARVADOS_API_HOST" : os .environ ["ARVADOS_API_HOST" ],
68
+ "ARVADOS_API_TOKEN" : connexion .request .headers ['Authorization' ],
69
+ "ARVADOS_API_HOST_INSECURE" : os .environ .get ("ARVADOS_API_HOST_INSECURE" , "false" )
70
+ }
71
+ with tempfile .NamedTemporaryFile () as inputtemp :
72
+ json .dump (body ["workflow_params" ], inputtemp )
73
+ inputtemp .flush ()
74
+ workflow_id = subprocess .check_output (["arvados-cwl-runner" , "--submit" , "--no-wait" , "--api=containers" ,
75
+ body .get ("workflow_url" ), inputtemp .name ], env = env ).strip ()
76
+ return {"workflow_id" : workflow_id }
77
+
78
+
79
+ def GetWorkflowLog (self , workflow_id ):
80
+ api = get_api ()
81
+
82
+ request = api .container_requests ().get (uuid = workflow_id ).execute ()
83
+ container = api .containers ().get (uuid = request ["container_uuid" ]).execute ()
84
+
85
+ outputobj = {}
86
+ if request ["output_uuid" ]:
87
+ c = arvados .collection .CollectionReader (request ["output_uuid" ])
88
+ with c .open ("cwl.output.json" ) as f :
89
+ outputobj = json .load (f )
90
+ def keepref (d ):
91
+ if isinstance (d , dict ) and "location" in d :
92
+ d ["location" ] = "keep:%s/%s" % (c .portable_data_hash (), d ["location" ])
93
+ visit (outputobj , keepref )
94
+
95
+ stderr = ""
96
+ if request ["log_uuid" ]:
97
+ c = arvados .collection .CollectionReader (request ["log_uuid" ])
98
+ if "stderr.txt" in c :
99
+ with c .open ("stderr.txt" ) as f :
100
+ stderr = f .read ()
101
+
102
+ r = {
103
+ "workflow_id" : request ["uuid" ],
104
+ "request" : {},
105
+ "state" : statemap [container ["state" ]],
106
+ "workflow_log" : {
107
+ "cmd" : ["" ],
108
+ "startTime" : "" ,
109
+ "endTime" : "" ,
110
+ "stdout" : "" ,
111
+ "stderr" : stderr
112
+ },
113
+ "task_logs" : [],
114
+ "outputs" : outputobj
115
+ }
116
+ if container ["exit_code" ] is not None :
117
+ r ["workflow_log" ]["exitCode" ] = container ["exit_code" ]
118
+ return r
119
+
120
+
121
+ def CancelJob (self , workflow_id ):
122
+ api = get_api ()
123
+ request = api .container_requests ().update (body = {"priority" : 0 }).execute ()
124
+ return {"workflow_id" : request ["uuid" ]}
125
+
126
+ def GetWorkflowStatus (self , workflow_id ):
127
+ api = get_api ()
128
+ request = api .container_requests ().get (uuid = workflow_id ).execute ()
129
+ container = api .containers ().get (uuid = request ["container_uuid" ]).execute ()
130
+ return {"workflow_id" : request ["uuid" ],
131
+ "state" : statemap [container ["state" ]]}
132
+
133
+ def create_backend (opts ):
134
+ return ArvadosBackend (optdict )
0 commit comments