5 import ruamel.yaml as yaml
7 from cwltool.errors import WorkflowException
8 from cwltool.process import get_feature, UnsupportedRequirement, shortname
9 from cwltool.pathmapper import adjustFiles, adjustDirObjs
10 from cwltool.utils import aslist
12 import arvados.collection
14 from .arvdocker import arv_docker_get_image
16 from .runner import Runner, arvados_jobs_image, packed_workflow, trim_listing
17 from .fsaccess import CollectionFetcher
19 logger = logging.getLogger('arvados.cwl-runner')
21 class ArvadosContainer(object):
22 """Submit and manage a Crunch container request for executing a CWL CommandLineTool."""
24 def __init__(self, runner):
25 self.arvrunner = runner
29 def update_pipeline_component(self, r):
32 def run(self, dry_run=False, pull_image=True, **kwargs):
34 "command": self.command_line,
35 "owner_uuid": self.arvrunner.project_uuid,
37 "output_path": self.outdir,
43 runtime_constraints = {}
49 scheduling_parameters = {}
52 for f in self.pathmapper.files():
53 _, p, tp = self.pathmapper.mapper(f)
54 if tp == "Directory" and '/' not in p[6:]:
57 "portable_data_hash": p[6:]
60 for f in self.pathmapper.files():
61 _, p, tp = self.pathmapper.mapper(f)
62 if p[6:].split("/")[0] not in dirs:
65 "portable_data_hash": p[6:]
68 if self.generatefiles["listing"]:
69 raise UnsupportedRequirement("InitialWorkDirRequirement not supported with --api=containers")
71 container_request["environment"] = {"TMPDIR": self.tmpdir, "HOME": self.outdir}
73 container_request["environment"].update(self.environment)
76 raise UnsupportedRequirement("Stdin redirection currently not suppported")
79 raise UnsupportedRequirement("Stderr redirection currently not suppported")
82 mounts["stdout"] = {"kind": "file",
83 "path": "%s/%s" % (self.outdir, self.stdout)}
85 (docker_req, docker_is_req) = get_feature(self, "DockerRequirement")
87 docker_req = {"dockerImageId": "arvados/jobs"}
89 container_request["container_image"] = arv_docker_get_image(self.arvrunner.api,
92 self.arvrunner.project_uuid)
94 resources = self.builder.resources
95 if resources is not None:
96 runtime_constraints["vcpus"] = resources.get("cores", 1)
97 runtime_constraints["ram"] = resources.get("ram") * 2**20
99 api_req, _ = get_feature(self, "http://arvados.org/cwl#APIRequirement")
101 runtime_constraints["API"] = True
103 runtime_req, _ = get_feature(self, "http://arvados.org/cwl#RuntimeConstraints")
105 if "keep_cache" in runtime_req:
106 runtime_constraints["keep_cache_ram"] = runtime_req["keep_cache"]
108 partition_req, _ = get_feature(self, "http://arvados.org/cwl#PartitionRequirement")
110 scheduling_parameters["partitions"] = aslist(partition_req["partition"])
112 container_request["mounts"] = mounts
113 container_request["runtime_constraints"] = runtime_constraints
114 container_request["use_existing"] = kwargs.get("enable_reuse", True)
115 container_request["scheduling_parameters"] = scheduling_parameters
117 if kwargs.get("runnerjob", "").startswith("arvwf:"):
118 wfuuid = kwargs["runnerjob"][6:kwargs["runnerjob"].index("#")]
119 wfrecord = self.arvrunner.api.workflows().get(uuid=wfuuid).execute(num_retries=self.arvrunner.num_retries)
120 if container_request["name"] == "main":
121 container_request["name"] = wfrecord["name"]
122 container_request["properties"]["template_uuid"] = wfuuid
125 response = self.arvrunner.api.container_requests().create(
126 body=container_request
127 ).execute(num_retries=self.arvrunner.num_retries)
129 self.uuid = response["uuid"]
130 self.arvrunner.processes[self.uuid] = self
132 if response["state"] == "Final":
133 logger.info("%s reused container %s", self.arvrunner.label(self), response["container_uuid"])
136 logger.info("%s %s state is %s", self.arvrunner.label(self), response["uuid"], response["state"])
137 except Exception as e:
138 logger.error("%s got error %s" % (self.arvrunner.label(self), str(e)))
139 self.output_callback({}, "permanentFail")
141 def done(self, record):
143 container = self.arvrunner.api.containers().get(
144 uuid=record["container_uuid"]
145 ).execute(num_retries=self.arvrunner.num_retries)
146 if container["state"] == "Complete":
147 rcode = container["exit_code"]
148 if self.successCodes and rcode in self.successCodes:
149 processStatus = "success"
150 elif self.temporaryFailCodes and rcode in self.temporaryFailCodes:
151 processStatus = "temporaryFail"
152 elif self.permanentFailCodes and rcode in self.permanentFailCodes:
153 processStatus = "permanentFail"
155 processStatus = "success"
157 processStatus = "permanentFail"
159 processStatus = "permanentFail"
161 if processStatus == "permanentFail":
162 logc = arvados.collection.CollectionReader(container["log"],
163 api_client=self.arvrunner.api,
164 keep_client=self.arvrunner.keep_client,
165 num_retries=self.arvrunner.num_retries)
166 done.logtail(logc, logger, "%s error log:" % self.arvrunner.label(self))
169 if container["output"]:
170 outputs = done.done_outputs(self, container, "/tmp", self.outdir, "/keep")
171 except WorkflowException as e:
172 logger.error("%s unable to collect output from %s:\n%s",
173 self.arvrunner.label(self), container["output"], e, exc_info=(e if self.arvrunner.debug else False))
174 processStatus = "permanentFail"
175 except Exception as e:
176 logger.exception("%s while getting output object: %s", self.arvrunner.label(self), e)
177 processStatus = "permanentFail"
179 self.output_callback(outputs, processStatus)
180 if record["uuid"] in self.arvrunner.processes:
181 del self.arvrunner.processes[record["uuid"]]
184 class RunnerContainer(Runner):
185 """Submit and manage a container that runs arvados-cwl-runner."""
187 def arvados_job_spec(self, dry_run=False, pull_image=True, **kwargs):
188 """Create an Arvados container request for this workflow.
190 The returned dict can be used to create a container passed as
191 the +body+ argument to container_requests().create().
194 adjustDirObjs(self.job_order, trim_listing)
197 "owner_uuid": self.arvrunner.project_uuid,
199 "output_path": "/var/spool/cwl",
200 "cwd": "/var/spool/cwl",
202 "state": "Committed",
203 "container_image": arvados_jobs_image(self.arvrunner, self.jobs_image),
205 "/var/lib/cwl/cwl.input.json": {
207 "content": self.job_order
211 "path": "/var/spool/cwl/cwl.output.json"
214 "kind": "collection",
218 "runtime_constraints": {
220 "ram": 1024*1024 * self.submit_runner_ram,
226 if self.tool.tool.get("id", "").startswith("keep:"):
227 sp = self.tool.tool["id"].split('/')
228 workflowcollection = sp[0][5:]
229 workflowname = "/".join(sp[1:])
230 workflowpath = "/var/lib/cwl/workflow/%s" % workflowname
231 container_req["mounts"]["/var/lib/cwl/workflow"] = {
232 "kind": "collection",
233 "portable_data_hash": "%s" % workflowcollection
236 packed = packed_workflow(self.arvrunner, self.tool)
237 workflowpath = "/var/lib/cwl/workflow.json#main"
238 container_req["mounts"]["/var/lib/cwl/workflow.json"] = {
242 if self.tool.tool.get("id", "").startswith("arvwf:"):
243 container_req["properties"]["template_uuid"] = self.tool.tool["id"][6:33]
245 command = ["arvados-cwl-runner", "--local", "--api=containers", "--no-log-timestamps"]
247 command.append("--output-name=" + self.output_name)
248 container_req["output_name"] = self.output_name
251 command.append("--output-tags=" + self.output_tags)
253 if kwargs.get("debug"):
254 command.append("--debug")
256 if self.enable_reuse:
257 command.append("--enable-reuse")
259 command.append("--disable-reuse")
262 command.append("--on-error=" + self.on_error)
264 command.extend([workflowpath, "/var/lib/cwl/cwl.input.json"])
266 container_req["command"] = command
271 def run(self, *args, **kwargs):
272 kwargs["keepprefix"] = "keep:"
273 job_spec = self.arvados_job_spec(*args, **kwargs)
274 job_spec.setdefault("owner_uuid", self.arvrunner.project_uuid)
276 response = self.arvrunner.api.container_requests().create(
278 ).execute(num_retries=self.arvrunner.num_retries)
280 self.uuid = response["uuid"]
281 self.arvrunner.processes[self.uuid] = self
283 logger.info("%s submitted container %s", self.arvrunner.label(self), response["uuid"])
285 if response["state"] == "Final":
288 def done(self, record):
290 container = self.arvrunner.api.containers().get(
291 uuid=record["container_uuid"]
292 ).execute(num_retries=self.arvrunner.num_retries)
293 except Exception as e:
294 logger.exception("%s while getting runner container: %s", self.arvrunner.label(self), e)
295 self.arvrunner.output_callback({}, "permanentFail")
297 super(RunnerContainer, self).done(container)
299 if record["uuid"] in self.arvrunner.processes:
300 del self.arvrunner.processes[record["uuid"]]