1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 from future import standard_library
6 standard_library.install_aliases()
7 from builtins import str
12 import urllib.request, urllib.parse, urllib.error
19 import arvados_cwl.util
20 import ruamel.yaml as yaml
22 from cwltool.errors import WorkflowException
23 from cwltool.process import UnsupportedRequirement, shortname
24 from cwltool.pathmapper import adjustFileObjs, adjustDirObjs, visit_class
25 from cwltool.utils import aslist
26 from cwltool.job import JobBase
28 import arvados.collection
30 from .arvdocker import arv_docker_get_image
32 from .runner import Runner, arvados_jobs_image, packed_workflow, trim_anonymous_location, remove_redundant_fields
33 from .fsaccess import CollectionFetcher
34 from .pathmapper import NoFollowPathMapper, trim_listing
35 from .perf import Perf
36 from ._version import __version__
38 logger = logging.getLogger('arvados.cwl-runner')
39 metrics = logging.getLogger('arvados.cwl-runner.metrics')
41 class ArvadosContainer(JobBase):
42 """Submit and manage a Crunch container request for executing a CWL CommandLineTool."""
44 def __init__(self, runner, job_runtime,
45 builder, # type: Builder
46 joborder, # type: Dict[Text, Union[Dict[Text, Any], List, Text]]
47 make_path_mapper, # type: Callable[..., PathMapper]
48 requirements, # type: List[Dict[Text, Text]]
49 hints, # type: List[Dict[Text, Text]]
52 super(ArvadosContainer, self).__init__(builder, joborder, make_path_mapper, requirements, hints, name)
53 self.arvrunner = runner
54 self.job_runtime = job_runtime
58 def update_pipeline_component(self, r):
61 def run(self, runtimeContext):
62 # ArvadosCommandTool subclasses from cwltool.CommandLineTool,
63 # which calls makeJobRunner() to get a new ArvadosContainer
64 # object. The fields that define execution such as
65 # command_line, environment, etc are set on the
66 # ArvadosContainer object by CommandLineTool.job() before
69 runtimeContext = self.job_runtime
72 "command": self.command_line,
74 "output_path": self.outdir,
76 "priority": runtimeContext.priority,
80 runtime_constraints = {}
82 if runtimeContext.project_uuid:
83 container_request["owner_uuid"] = runtimeContext.project_uuid
85 if self.arvrunner.secret_store.has_secret(self.command_line):
86 raise WorkflowException("Secret material leaked on command line, only file literals may contain secrets")
88 if self.arvrunner.secret_store.has_secret(self.environment):
89 raise WorkflowException("Secret material leaked in environment, only file literals may contain secrets")
91 resources = self.builder.resources
92 if resources is not None:
93 runtime_constraints["vcpus"] = math.ceil(resources.get("cores", 1))
94 runtime_constraints["ram"] = math.ceil(resources.get("ram") * 2**20)
99 "capacity": math.ceil(resources.get("outdirSize", 0) * 2**20)
103 "capacity": math.ceil(resources.get("tmpdirSize", 0) * 2**20)
107 scheduling_parameters = {}
109 rf = [self.pathmapper.mapper(f) for f in self.pathmapper.referenced_files]
110 rf.sort(key=lambda k: k.resolved)
112 for resolved, target, tp, stg in rf:
115 if prevdir and target.startswith(prevdir):
117 if tp == "Directory":
120 targetdir = os.path.dirname(target)
121 sp = resolved.split("/", 1)
122 pdh = sp[0][5:] # remove "keep:"
123 mounts[targetdir] = {
124 "kind": "collection",
125 "portable_data_hash": pdh
127 if pdh in self.pathmapper.pdh_to_uuid:
128 mounts[targetdir]["uuid"] = self.pathmapper.pdh_to_uuid[pdh]
130 if tp == "Directory":
133 path = os.path.dirname(sp[1])
134 if path and path != "/":
135 mounts[targetdir]["path"] = path
136 prevdir = targetdir + "/"
138 with Perf(metrics, "generatefiles %s" % self.name):
139 if self.generatefiles["listing"]:
140 vwd = arvados.collection.Collection(api_client=self.arvrunner.api,
141 keep_client=self.arvrunner.keep_client,
142 num_retries=self.arvrunner.num_retries)
143 generatemapper = NoFollowPathMapper(self.generatefiles["listing"], "", "",
146 sorteditems = sorted(generatemapper.items(), key=lambda n: n[1].target)
148 logger.debug("generatemapper is %s", sorteditems)
150 with Perf(metrics, "createfiles %s" % self.name):
151 for f, p in sorteditems:
155 if p.target.startswith("/"):
156 dst = p.target[len(self.outdir)+1:] if p.target.startswith(self.outdir+"/") else p.target[1:]
160 if p.type in ("File", "Directory", "WritableFile", "WritableDirectory"):
161 if p.resolved.startswith("_:"):
164 source, path = self.arvrunner.fs_access.get_collection(p.resolved)
165 vwd.copy(path or ".", dst, source_collection=source)
166 elif p.type == "CreateFile":
167 if self.arvrunner.secret_store.has_secret(p.resolved):
168 mountpoint = p.target if p.target.startswith("/") else os.path.join(self.outdir, p.target)
169 secret_mounts[mountpoint] = {
171 "content": self.arvrunner.secret_store.retrieve(p.resolved)
174 with vwd.open(dst, "w") as n:
177 def keepemptydirs(p):
178 if isinstance(p, arvados.collection.RichCollectionBase):
180 p.open(".keep", "w").close()
187 if not runtimeContext.current_container:
188 runtimeContext.current_container = arvados_cwl.util.get_current_container(self.arvrunner.api, self.arvrunner.num_retries, logger)
189 info = arvados_cwl.util.get_intermediate_collection_info(self.name, runtimeContext.current_container, runtimeContext.intermediate_output_ttl)
190 vwd.save_new(name=info["name"],
191 owner_uuid=runtimeContext.project_uuid,
192 ensure_unique_name=True,
193 trash_at=info["trash_at"],
194 properties=info["properties"])
197 for f, p in sorteditems:
198 if (not p.target or self.arvrunner.secret_store.has_secret(p.resolved) or
199 (prev is not None and p.target.startswith(prev))):
201 if p.target.startswith("/"):
202 dst = p.target[len(self.outdir)+1:] if p.target.startswith(self.outdir+"/") else p.target[1:]
205 mountpoint = p.target if p.target.startswith("/") else os.path.join(self.outdir, p.target)
206 mounts[mountpoint] = {"kind": "collection",
207 "portable_data_hash": vwd.portable_data_hash(),
209 if p.type.startswith("Writable"):
210 mounts[mountpoint]["writable"] = True
211 prev = p.target + "/"
213 container_request["environment"] = {"TMPDIR": self.tmpdir, "HOME": self.outdir}
215 container_request["environment"].update(self.environment)
218 sp = self.stdin[6:].split("/", 1)
219 mounts["stdin"] = {"kind": "collection",
220 "portable_data_hash": sp[0],
224 mounts["stderr"] = {"kind": "file",
225 "path": "%s/%s" % (self.outdir, self.stderr)}
228 mounts["stdout"] = {"kind": "file",
229 "path": "%s/%s" % (self.outdir, self.stdout)}
231 (docker_req, docker_is_req) = self.get_requirement("DockerRequirement")
233 docker_req = {"dockerImageId": "arvados/jobs:"+__version__}
235 container_request["container_image"] = arv_docker_get_image(self.arvrunner.api,
237 runtimeContext.pull_image,
238 runtimeContext.project_uuid)
240 network_req, _ = self.get_requirement("NetworkAccess")
242 runtime_constraints["API"] = network_req["networkAccess"]
244 api_req, _ = self.get_requirement("http://arvados.org/cwl#APIRequirement")
246 runtime_constraints["API"] = True
248 runtime_req, _ = self.get_requirement("http://arvados.org/cwl#RuntimeConstraints")
250 if "keep_cache" in runtime_req:
251 runtime_constraints["keep_cache_ram"] = math.ceil(runtime_req["keep_cache"] * 2**20)
252 if "outputDirType" in runtime_req:
253 if runtime_req["outputDirType"] == "local_output_dir":
254 # Currently the default behavior.
256 elif runtime_req["outputDirType"] == "keep_output_dir":
257 mounts[self.outdir]= {
258 "kind": "collection",
262 partition_req, _ = self.get_requirement("http://arvados.org/cwl#PartitionRequirement")
264 scheduling_parameters["partitions"] = aslist(partition_req["partition"])
266 intermediate_output_req, _ = self.get_requirement("http://arvados.org/cwl#IntermediateOutput")
267 if intermediate_output_req:
268 self.output_ttl = intermediate_output_req["outputTTL"]
270 self.output_ttl = self.arvrunner.intermediate_output_ttl
272 if self.output_ttl < 0:
273 raise WorkflowException("Invalid value %d for output_ttl, cannot be less than zero" % container_request["output_ttl"])
275 if self.timelimit is not None and self.timelimit > 0:
276 scheduling_parameters["max_run_time"] = self.timelimit
278 extra_submit_params = {}
279 if runtimeContext.submit_runner_cluster:
280 extra_submit_params["cluster_id"] = runtimeContext.submit_runner_cluster
282 container_request["output_name"] = "Output for step %s" % (self.name)
283 container_request["output_ttl"] = self.output_ttl
284 container_request["mounts"] = mounts
285 container_request["secret_mounts"] = secret_mounts
286 container_request["runtime_constraints"] = runtime_constraints
287 container_request["scheduling_parameters"] = scheduling_parameters
289 enable_reuse = runtimeContext.enable_reuse
291 reuse_req, _ = self.get_requirement("WorkReuse")
293 enable_reuse = reuse_req["enableReuse"]
294 reuse_req, _ = self.get_requirement("http://arvados.org/cwl#ReuseRequirement")
296 enable_reuse = reuse_req["enableReuse"]
297 container_request["use_existing"] = enable_reuse
299 if runtimeContext.runnerjob.startswith("arvwf:"):
300 wfuuid = runtimeContext.runnerjob[6:runtimeContext.runnerjob.index("#")]
301 wfrecord = self.arvrunner.api.workflows().get(uuid=wfuuid).execute(num_retries=self.arvrunner.num_retries)
302 if container_request["name"] == "main":
303 container_request["name"] = wfrecord["name"]
304 container_request["properties"]["template_uuid"] = wfuuid
306 self.output_callback = self.arvrunner.get_wrapped_callback(self.output_callback)
309 if runtimeContext.submit_request_uuid:
310 response = self.arvrunner.api.container_requests().update(
311 uuid=runtimeContext.submit_request_uuid,
312 body=container_request,
313 **extra_submit_params
314 ).execute(num_retries=self.arvrunner.num_retries)
316 response = self.arvrunner.api.container_requests().create(
317 body=container_request,
318 **extra_submit_params
319 ).execute(num_retries=self.arvrunner.num_retries)
321 self.uuid = response["uuid"]
322 self.arvrunner.process_submitted(self)
324 if response["state"] == "Final":
325 logger.info("%s reused container %s", self.arvrunner.label(self), response["container_uuid"])
327 logger.info("%s %s state is %s", self.arvrunner.label(self), response["uuid"], response["state"])
328 except Exception as e:
329 logger.exception("%s error submitting container\n%s", self.arvrunner.label(self), e)
330 logger.debug("Container request was %s", container_request)
331 self.output_callback({}, "permanentFail")
333 def done(self, record):
336 container = self.arvrunner.api.containers().get(
337 uuid=record["container_uuid"]
338 ).execute(num_retries=self.arvrunner.num_retries)
339 if container["state"] == "Complete":
340 rcode = container["exit_code"]
341 if self.successCodes and rcode in self.successCodes:
342 processStatus = "success"
343 elif self.temporaryFailCodes and rcode in self.temporaryFailCodes:
344 processStatus = "temporaryFail"
345 elif self.permanentFailCodes and rcode in self.permanentFailCodes:
346 processStatus = "permanentFail"
348 processStatus = "success"
350 processStatus = "permanentFail"
352 processStatus = "permanentFail"
354 if processStatus == "permanentFail" and record["log_uuid"]:
355 logc = arvados.collection.CollectionReader(record["log_uuid"],
356 api_client=self.arvrunner.api,
357 keep_client=self.arvrunner.keep_client,
358 num_retries=self.arvrunner.num_retries)
359 label = self.arvrunner.label(self)
362 "%s (%s) error log:" % (label, record["uuid"]), maxlen=40)
364 if record["output_uuid"]:
365 if self.arvrunner.trash_intermediate or self.arvrunner.intermediate_output_ttl:
366 # Compute the trash time to avoid requesting the collection record.
367 trash_at = ciso8601.parse_datetime_as_naive(record["modified_at"]) + datetime.timedelta(0, self.arvrunner.intermediate_output_ttl)
368 aftertime = " at %s" % trash_at.strftime("%Y-%m-%d %H:%M:%S UTC") if self.arvrunner.intermediate_output_ttl else ""
369 orpart = ", or" if self.arvrunner.trash_intermediate and self.arvrunner.intermediate_output_ttl else ""
370 oncomplete = " upon successful completion of the workflow" if self.arvrunner.trash_intermediate else ""
371 logger.info("%s Intermediate output %s (%s) will be trashed%s%s%s." % (
372 self.arvrunner.label(self), record["output_uuid"], container["output"], aftertime, orpart, oncomplete))
373 self.arvrunner.add_intermediate_output(record["output_uuid"])
375 if container["output"]:
376 outputs = done.done_outputs(self, container, "/tmp", self.outdir, "/keep")
377 except WorkflowException as e:
378 # Only include a stack trace if in debug mode.
379 # A stack trace may obfuscate more useful output about the workflow.
380 logger.error("%s unable to collect output from %s:\n%s",
381 self.arvrunner.label(self), container["output"], e, exc_info=(e if self.arvrunner.debug else False))
382 processStatus = "permanentFail"
384 logger.exception("%s while getting output object:", self.arvrunner.label(self))
385 processStatus = "permanentFail"
387 self.output_callback(outputs, processStatus)
390 class RunnerContainer(Runner):
391 """Submit and manage a container that runs arvados-cwl-runner."""
393 def arvados_job_spec(self, runtimeContext):
394 """Create an Arvados container request for this workflow.
396 The returned dict can be used to create a container passed as
397 the +body+ argument to container_requests().create().
400 adjustDirObjs(self.job_order, trim_listing)
401 visit_class(self.job_order, ("File", "Directory"), trim_anonymous_location)
402 visit_class(self.job_order, ("File", "Directory"), remove_redundant_fields)
405 for param in sorted(self.job_order.keys()):
406 if self.secret_store.has_secret(self.job_order[param]):
407 mnt = "/secrets/s%d" % len(secret_mounts)
408 secret_mounts[mnt] = {
410 "content": self.secret_store.retrieve(self.job_order[param])
412 self.job_order[param] = {"$include": mnt}
416 "output_path": "/var/spool/cwl",
417 "cwd": "/var/spool/cwl",
418 "priority": self.priority,
419 "state": "Committed",
420 "container_image": arvados_jobs_image(self.arvrunner, self.jobs_image),
422 "/var/lib/cwl/cwl.input.json": {
424 "content": self.job_order
428 "path": "/var/spool/cwl/cwl.output.json"
431 "kind": "collection",
435 "secret_mounts": secret_mounts,
436 "runtime_constraints": {
437 "vcpus": math.ceil(self.submit_runner_cores),
438 "ram": 1024*1024 * (math.ceil(self.submit_runner_ram) + math.ceil(self.collection_cache_size)),
441 "use_existing": False, # Never reuse the runner container - see #15497.
445 if self.embedded_tool.tool.get("id", "").startswith("keep:"):
446 sp = self.embedded_tool.tool["id"].split('/')
447 workflowcollection = sp[0][5:]
448 workflowname = "/".join(sp[1:])
449 workflowpath = "/var/lib/cwl/workflow/%s" % workflowname
450 container_req["mounts"]["/var/lib/cwl/workflow"] = {
451 "kind": "collection",
452 "portable_data_hash": "%s" % workflowcollection
455 packed = packed_workflow(self.arvrunner, self.embedded_tool, self.merged_map)
456 workflowpath = "/var/lib/cwl/workflow.json#main"
457 container_req["mounts"]["/var/lib/cwl/workflow.json"] = {
461 if self.embedded_tool.tool.get("id", "").startswith("arvwf:"):
462 container_req["properties"]["template_uuid"] = self.embedded_tool.tool["id"][6:33]
465 # --local means execute the workflow instead of submitting a container request
466 # --api=containers means use the containers API
467 # --no-log-timestamps means don't add timestamps (the logging infrastructure does this)
468 # --disable-validate because we already validated so don't need to do it again
469 # --eval-timeout is the timeout for javascript invocation
470 # --parallel-task-count is the number of threads to use for job submission
471 # --enable/disable-reuse sets desired job reuse
472 # --collection-cache-size sets aside memory to store collections
473 command = ["arvados-cwl-runner",
476 "--no-log-timestamps",
477 "--disable-validate",
479 "--eval-timeout=%s" % self.arvrunner.eval_timeout,
480 "--thread-count=%s" % self.arvrunner.thread_count,
481 "--enable-reuse" if self.enable_reuse else "--disable-reuse",
482 "--collection-cache-size=%s" % self.collection_cache_size]
485 command.append("--output-name=" + self.output_name)
486 container_req["output_name"] = self.output_name
489 command.append("--output-tags=" + self.output_tags)
491 if runtimeContext.debug:
492 command.append("--debug")
494 if runtimeContext.storage_classes != "default":
495 command.append("--storage-classes=" + runtimeContext.storage_classes)
498 command.append("--on-error=" + self.on_error)
500 if self.intermediate_output_ttl:
501 command.append("--intermediate-output-ttl=%d" % self.intermediate_output_ttl)
503 if self.arvrunner.trash_intermediate:
504 command.append("--trash-intermediate")
506 if self.arvrunner.project_uuid:
507 command.append("--project-uuid="+self.arvrunner.project_uuid)
510 command.append("--enable-dev")
512 command.extend([workflowpath, "/var/lib/cwl/cwl.input.json"])
514 container_req["command"] = command
519 def run(self, runtimeContext):
520 runtimeContext.keepprefix = "keep:"
521 job_spec = self.arvados_job_spec(runtimeContext)
522 if self.arvrunner.project_uuid:
523 job_spec["owner_uuid"] = self.arvrunner.project_uuid
525 extra_submit_params = {}
526 if runtimeContext.submit_runner_cluster:
527 extra_submit_params["cluster_id"] = runtimeContext.submit_runner_cluster
529 if runtimeContext.submit_request_uuid:
530 if "cluster_id" in extra_submit_params:
531 # Doesn't make sense for "update" and actually fails
532 del extra_submit_params["cluster_id"]
533 response = self.arvrunner.api.container_requests().update(
534 uuid=runtimeContext.submit_request_uuid,
536 **extra_submit_params
537 ).execute(num_retries=self.arvrunner.num_retries)
539 response = self.arvrunner.api.container_requests().create(
541 **extra_submit_params
542 ).execute(num_retries=self.arvrunner.num_retries)
544 self.uuid = response["uuid"]
545 self.arvrunner.process_submitted(self)
547 logger.info("%s submitted container_request %s", self.arvrunner.label(self), response["uuid"])
549 def done(self, record):
551 container = self.arvrunner.api.containers().get(
552 uuid=record["container_uuid"]
553 ).execute(num_retries=self.arvrunner.num_retries)
554 container["log"] = record["log_uuid"]
556 logger.exception("%s while getting runner container", self.arvrunner.label(self))
557 self.arvrunner.output_callback({}, "permanentFail")
559 super(RunnerContainer, self).done(container)