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 ._version import __version__
10 from functools import partial
11 from schema_salad.sourceline import SourceLine
12 from cwltool.errors import WorkflowException
14 def validate_cluster_target(arvrunner, runtimeContext):
15 if (runtimeContext.submit_runner_cluster and
16 runtimeContext.submit_runner_cluster not in arvrunner.api._rootDesc["remoteHosts"] and
17 runtimeContext.submit_runner_cluster != arvrunner.api._rootDesc["uuidPrefix"]):
18 raise WorkflowException("Unknown or invalid cluster id '%s' known remote clusters are %s" % (runtimeContext.submit_runner_cluster,
19 ", ".join(list(arvrunner.api._rootDesc["remoteHosts"].keys()))))
20 if runtimeContext.project_uuid:
21 cluster_target = runtimeContext.submit_runner_cluster or arvrunner.api._rootDesc["uuidPrefix"]
22 if not runtimeContext.project_uuid.startswith(cluster_target):
23 raise WorkflowException("Project uuid '%s' should start with id of target cluster '%s'" % (runtimeContext.project_uuid, cluster_target))
26 if runtimeContext.project_uuid[5:12] == '-tpzed-':
27 arvrunner.api.users().get(uuid=runtimeContext.project_uuid).execute()
29 proj = arvrunner.api.groups().get(uuid=runtimeContext.project_uuid).execute()
30 if proj["group_class"] != "project":
31 raise Exception("not a project, group_class is '%s'" % (proj["group_class"]))
32 except Exception as e:
33 raise WorkflowException("Invalid project uuid '%s': %s" % (runtimeContext.project_uuid, e))
35 def set_cluster_target(tool, arvrunner, builder, runtimeContext):
36 cluster_target_req = None
37 for field in ("hints", "requirements"):
40 for item in tool[field]:
41 if item["class"] == "http://arvados.org/cwl#ClusterTarget":
42 cluster_target_req = item
44 if cluster_target_req is None:
47 with SourceLine(cluster_target_req, None, WorkflowException, runtimeContext.debug):
48 runtimeContext = runtimeContext.copy()
49 runtimeContext.submit_runner_cluster = builder.do_eval(cluster_target_req.get("cluster_id")) or runtimeContext.submit_runner_cluster
50 runtimeContext.project_uuid = builder.do_eval(cluster_target_req.get("project_uuid")) or runtimeContext.project_uuid
51 validate_cluster_target(arvrunner, runtimeContext)
56 class ArvadosCommandTool(CommandLineTool):
57 """Wrap cwltool CommandLineTool to override selected methods."""
59 def __init__(self, arvrunner, toolpath_object, loadingContext):
60 super(ArvadosCommandTool, self).__init__(toolpath_object, loadingContext)
62 (docker_req, docker_is_req) = self.get_requirement("DockerRequirement")
64 self.hints.append({"class": "DockerRequirement",
65 "dockerPull": "arvados/jobs:"+__version__})
67 self.arvrunner = arvrunner
69 def make_job_runner(self, runtimeContext):
70 if runtimeContext.work_api == "containers":
71 return partial(ArvadosContainer, self.arvrunner, runtimeContext)
73 raise Exception("Unsupported work_api %s", runtimeContext.work_api)
75 def make_path_mapper(self, reffiles, stagedir, runtimeContext, separateDirs):
76 if runtimeContext.work_api == "containers":
77 return ArvPathMapper(self.arvrunner, reffiles+runtimeContext.extra_reffiles, runtimeContext.basedir,
81 def job(self, joborder, output_callback, runtimeContext):
82 builder = make_builder(joborder, self.hints, self.requirements, runtimeContext, self.metadata)
83 runtimeContext = set_cluster_target(self.tool, self.arvrunner, builder, runtimeContext)
85 if runtimeContext.work_api == "containers":
86 dockerReq, is_req = self.get_requirement("DockerRequirement")
87 if dockerReq and dockerReq.get("dockerOutputDirectory"):
88 runtimeContext.outdir = dockerReq.get("dockerOutputDirectory")
89 runtimeContext.docker_outdir = dockerReq.get("dockerOutputDirectory")
91 runtimeContext.outdir = "/var/spool/cwl"
92 runtimeContext.docker_outdir = "/var/spool/cwl"
93 return super(ArvadosCommandTool, self).job(joborder, output_callback, runtimeContext)
95 class ArvadosExpressionTool(ExpressionTool):
96 def __init__(self, arvrunner, toolpath_object, loadingContext):
97 super(ArvadosExpressionTool, self).__init__(toolpath_object, loadingContext)
98 self.arvrunner = arvrunner
101 job_order, # type: Mapping[Text, Text]
102 output_callback, # type: Callable[[Any, Any], Any]
103 runtimeContext # type: RuntimeContext
105 return super(ArvadosExpressionTool, self).job(job_order, self.arvrunner.get_wrapped_callback(output_callback), runtimeContext)