16169: Monkey patch load_tool.resolve_and_validate_document to fix bug
[arvados.git] / sdk / cwl / arvados_cwl / arvtool.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
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
12
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"):
22         if field not in tool:
23             continue
24         for item in tool[field]:
25             if item["class"] == "http://arvados.org/cwl#ClusterTarget":
26                 cluster_target_req = item
27
28     if cluster_target_req is None:
29         return runtimeContext
30
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)
36
37     return runtimeContext
38
39
40 class ArvadosCommandTool(CommandLineTool):
41     """Wrap cwltool CommandLineTool to override selected methods."""
42
43     def __init__(self, arvrunner, toolpath_object, loadingContext):
44         super(ArvadosCommandTool, self).__init__(toolpath_object, loadingContext)
45         self.arvrunner = arvrunner
46
47     def make_job_runner(self, runtimeContext):
48         if runtimeContext.work_api == "containers":
49             return partial(ArvadosContainer, self.arvrunner, runtimeContext)
50         else:
51             raise Exception("Unsupported work_api %s", runtimeContext.work_api)
52
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,
56                                  "/keep/%s",
57                                  "/keep/%s/%s")
58
59     def job(self, joborder, output_callback, runtimeContext):
60         builder = make_builder(joborder, self.hints, self.requirements, runtimeContext)
61         runtimeContext = set_cluster_target(self.tool, self.arvrunner, builder, runtimeContext)
62
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")
68             else:
69                 runtimeContext.outdir = "/var/spool/cwl"
70                 runtimeContext.docker_outdir = "/var/spool/cwl"
71         return super(ArvadosCommandTool, self).job(joborder, output_callback, runtimeContext)
72
73 class ArvadosExpressionTool(ExpressionTool):
74     def __init__(self, arvrunner, toolpath_object, loadingContext):
75         super(ArvadosExpressionTool, self).__init__(toolpath_object, loadingContext)
76         self.arvrunner = arvrunner
77
78     def job(self,
79             job_order,         # type: Mapping[Text, Text]
80             output_callback,  # type: Callable[[Any, Any], Any]
81             runtimeContext     # type: RuntimeContext
82            ):
83         return super(ArvadosExpressionTool, self).job(job_order, self.arvrunner.get_wrapped_callback(output_callback), runtimeContext)