Put the default arvados-cwl-runner --thread-count back down to 1.
[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     if runtimeContext.project_uuid:
20         cluster_target = runtimeContext.submit_runner_cluster or arvrunner.api._rootDesc["uuidPrefix"]
21         if not runtimeContext.project_uuid.startswith(cluster_target):
22             raise WorkflowException("Project uuid '%s' must be for target cluster '%s'" % (runtimeContext.project_uuid, cluster_target))
23         try:
24             arvrunner.api.groups().get(uuid=runtimeContext.project_uuid).execute()
25         except Exception as e:
26             raise WorkflowException("Invalid project uuid '%s': %s" % (runtimeContext.project_uuid, e))
27
28 def set_cluster_target(tool, arvrunner, builder, runtimeContext):
29     cluster_target_req = None
30     for field in ("hints", "requirements"):
31         if field not in tool:
32             continue
33         for item in tool[field]:
34             if item["class"] == "http://arvados.org/cwl#ClusterTarget":
35                 cluster_target_req = item
36
37     if cluster_target_req is None:
38         return runtimeContext
39
40     with SourceLine(cluster_target_req, None, WorkflowException, runtimeContext.debug):
41         runtimeContext = runtimeContext.copy()
42         runtimeContext.submit_runner_cluster = builder.do_eval(cluster_target_req.get("cluster_id")) or runtimeContext.submit_runner_cluster
43         runtimeContext.project_uuid = builder.do_eval(cluster_target_req.get("project_uuid")) or runtimeContext.project_uuid
44         validate_cluster_target(arvrunner, runtimeContext)
45
46     return runtimeContext
47
48
49 class ArvadosCommandTool(CommandLineTool):
50     """Wrap cwltool CommandLineTool to override selected methods."""
51
52     def __init__(self, arvrunner, toolpath_object, loadingContext):
53         super(ArvadosCommandTool, self).__init__(toolpath_object, loadingContext)
54         self.arvrunner = arvrunner
55
56     def make_job_runner(self, runtimeContext):
57         if runtimeContext.work_api == "containers":
58             return partial(ArvadosContainer, self.arvrunner, runtimeContext)
59         else:
60             raise Exception("Unsupported work_api %s", runtimeContext.work_api)
61
62     def make_path_mapper(self, reffiles, stagedir, runtimeContext, separateDirs):
63         if runtimeContext.work_api == "containers":
64             return ArvPathMapper(self.arvrunner, reffiles+runtimeContext.extra_reffiles, runtimeContext.basedir,
65                                  "/keep/%s",
66                                  "/keep/%s/%s")
67
68     def job(self, joborder, output_callback, runtimeContext):
69         builder = make_builder(joborder, self.hints, self.requirements, runtimeContext, self.metadata)
70         runtimeContext = set_cluster_target(self.tool, self.arvrunner, builder, runtimeContext)
71
72         if runtimeContext.work_api == "containers":
73             dockerReq, is_req = self.get_requirement("DockerRequirement")
74             if dockerReq and dockerReq.get("dockerOutputDirectory"):
75                 runtimeContext.outdir = dockerReq.get("dockerOutputDirectory")
76                 runtimeContext.docker_outdir = dockerReq.get("dockerOutputDirectory")
77             else:
78                 runtimeContext.outdir = "/var/spool/cwl"
79                 runtimeContext.docker_outdir = "/var/spool/cwl"
80         return super(ArvadosCommandTool, self).job(joborder, output_callback, runtimeContext)
81
82 class ArvadosExpressionTool(ExpressionTool):
83     def __init__(self, arvrunner, toolpath_object, loadingContext):
84         super(ArvadosExpressionTool, self).__init__(toolpath_object, loadingContext)
85         self.arvrunner = arvrunner
86
87     def job(self,
88             job_order,         # type: Mapping[Text, Text]
89             output_callback,  # type: Callable[[Any, Any], Any]
90             runtimeContext     # type: RuntimeContext
91            ):
92         return super(ArvadosExpressionTool, self).job(job_order, self.arvrunner.get_wrapped_callback(output_callback), runtimeContext)