1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 from cwltool.command_line_tool import CommandLineTool, ExpressionTool
6 from .arvcontainer import ArvadosContainer
7 from .pathmapper import ArvPathMapper
8 from .runner import make_builder
9 from functools import partial
10 from schema_salad.sourceline import SourceLine
11 from cwltool.errors import WorkflowException
13 def validate_cluster_target(arvrunner, runtimeContext):
14 if (runtimeContext.submit_runner_cluster and
15 runtimeContext.submit_runner_cluster not in arvrunner.api._rootDesc["remoteHosts"] and
16 runtimeContext.submit_runner_cluster != arvrunner.api._rootDesc["uuidPrefix"]):
17 raise WorkflowException("Unknown or invalid cluster id '%s' known remote clusters are %s" % (runtimeContext.submit_runner_cluster,
18 ", ".join(list(arvrunner.api._rootDesc["remoteHosts"].keys()))))
19 def set_cluster_target(tool, arvrunner, builder, runtimeContext):
20 cluster_target_req = None
21 for field in ("hints", "requirements"):
24 for item in tool[field]:
25 if item["class"] == "http://arvados.org/cwl#ClusterTarget":
26 cluster_target_req = item
28 if cluster_target_req is None:
31 with SourceLine(cluster_target_req, None, WorkflowException, runtimeContext.debug):
32 runtimeContext = runtimeContext.copy()
33 runtimeContext.submit_runner_cluster = builder.do_eval(cluster_target_req.get("cluster_id")) or runtimeContext.submit_runner_cluster
34 runtimeContext.project_uuid = builder.do_eval(cluster_target_req.get("project_uuid")) or runtimeContext.project_uuid
35 validate_cluster_target(arvrunner, runtimeContext)
40 class ArvadosCommandTool(CommandLineTool):
41 """Wrap cwltool CommandLineTool to override selected methods."""
43 def __init__(self, arvrunner, toolpath_object, loadingContext):
44 super(ArvadosCommandTool, self).__init__(toolpath_object, loadingContext)
45 self.arvrunner = arvrunner
47 def make_job_runner(self, runtimeContext):
48 if runtimeContext.work_api == "containers":
49 return partial(ArvadosContainer, self.arvrunner, runtimeContext)
51 raise Exception("Unsupported work_api %s", runtimeContext.work_api)
53 def make_path_mapper(self, reffiles, stagedir, runtimeContext, separateDirs):
54 if runtimeContext.work_api == "containers":
55 return ArvPathMapper(self.arvrunner, reffiles+runtimeContext.extra_reffiles, runtimeContext.basedir,
59 def job(self, joborder, output_callback, runtimeContext):
60 builder = make_builder(joborder, self.hints, self.requirements, runtimeContext, self.metadata)
61 runtimeContext = set_cluster_target(self.tool, self.arvrunner, builder, runtimeContext)
63 if runtimeContext.work_api == "containers":
64 dockerReq, is_req = self.get_requirement("DockerRequirement")
65 if dockerReq and dockerReq.get("dockerOutputDirectory"):
66 runtimeContext.outdir = dockerReq.get("dockerOutputDirectory")
67 runtimeContext.docker_outdir = dockerReq.get("dockerOutputDirectory")
69 runtimeContext.outdir = "/var/spool/cwl"
70 runtimeContext.docker_outdir = "/var/spool/cwl"
71 return super(ArvadosCommandTool, self).job(joborder, output_callback, runtimeContext)
73 class ArvadosExpressionTool(ExpressionTool):
74 def __init__(self, arvrunner, toolpath_object, loadingContext):
75 super(ArvadosExpressionTool, self).__init__(toolpath_object, loadingContext)
76 self.arvrunner = arvrunner
79 job_order, # type: Mapping[Text, Text]
80 output_callback, # type: Callable[[Any, Any], Any]
81 runtimeContext # type: RuntimeContext
83 return super(ArvadosExpressionTool, self).job(job_order, self.arvrunner.get_wrapped_callback(output_callback), runtimeContext)