6 from schema_salad.sourceline import SourceLine, cmap
8 from cwltool.pack import pack
9 from cwltool.load_tool import fetch_document
10 from cwltool.process import shortname
11 from cwltool.workflow import Workflow, WorkflowException
12 from cwltool.pathmapper import adjustFileObjs, adjustDirObjs
14 import ruamel.yaml as yaml
16 from .runner import upload_dependencies, packed_workflow, upload_workflow_collection, trim_anonymous_location
17 from .pathmapper import trim_listing
18 from .arvtool import ArvadosCommandTool
19 from .perf import Perf
21 logger = logging.getLogger('arvados.cwl-runner')
22 metrics = logging.getLogger('arvados.cwl-runner.metrics')
24 def upload_workflow(arvRunner, tool, job_order, project_uuid, uuid=None,
25 submit_runner_ram=0, name=None):
27 packed = packed_workflow(arvRunner, tool)
29 adjustDirObjs(job_order, trim_listing)
30 adjustFileObjs(job_order, trim_anonymous_location)
31 adjustDirObjs(job_order, trim_anonymous_location)
33 main = [p for p in packed["$graph"] if p["id"] == "#main"][0]
34 for inp in main["inputs"]:
35 sn = shortname(inp["id"])
37 inp["default"] = job_order[sn]
40 name = tool.tool.get("label", os.path.basename(tool.tool["id"]))
42 upload_dependencies(arvRunner, name, tool.doc_loader,
43 packed, tool.tool["id"], False)
45 # TODO nowhere for submit_runner_ram to go.
50 "description": tool.tool.get("doc", ""),
51 "definition":yaml.round_trip_dump(packed)
54 body["workflow"]["owner_uuid"] = project_uuid
57 call = arvRunner.api.workflows().update(uuid=uuid, body=body)
59 call = arvRunner.api.workflows().create(body=body)
60 return call.execute(num_retries=arvRunner.num_retries)["uuid"]
64 for r in reversed(reqs):
65 if r["class"] not in dedup and not r["class"].startswith("http://arvados.org/cwl#"):
67 return [dedup[r] for r in sorted(dedup.keys())]
69 class ArvadosWorkflow(Workflow):
70 """Wrap cwltool Workflow to override selected methods."""
72 def __init__(self, arvrunner, toolpath_object, **kwargs):
73 super(ArvadosWorkflow, self).__init__(toolpath_object, **kwargs)
74 self.arvrunner = arvrunner
75 self.work_api = kwargs["work_api"]
78 def job(self, joborder, output_callback, **kwargs):
79 kwargs["work_api"] = self.work_api
80 req, _ = self.get_requirement("http://arvados.org/cwl#RunInSingleContainer")
82 with SourceLine(self.tool, None, WorkflowException):
83 if "id" not in self.tool:
84 raise WorkflowException("%s object must have 'id'" % (self.tool["class"]))
85 document_loader, workflowobj, uri = (self.doc_loader, self.doc_loader.fetch(self.tool["id"]), self.tool["id"])
87 with Perf(metrics, "subworkflow upload_deps"):
88 upload_dependencies(self.arvrunner,
89 os.path.basename(joborder.get("id", "#")),
92 joborder.get("id", "#"),
95 if self.wf_pdh is None:
96 workflowobj["requirements"] = dedup_reqs(self.requirements)
97 workflowobj["hints"] = dedup_reqs(self.hints)
99 packed = pack(document_loader, workflowobj, uri, self.metadata)
101 upload_dependencies(self.arvrunner,
102 kwargs.get("name", ""),
108 with Perf(metrics, "subworkflow adjust"):
109 joborder_keepmount = copy.deepcopy(joborder)
112 with SourceLine(obj, None, WorkflowException):
113 if "location" not in obj:
114 raise WorkflowException("%s object is missing required 'location' field: %s" % (obj["class"], obj))
115 with SourceLine(obj, "location", WorkflowException):
116 if obj["location"].startswith("keep:"):
117 obj["location"] = "/keep/" + obj["location"][5:]
120 elif obj["location"].startswith("_:"):
123 raise WorkflowException("Location is not a keep reference or a literal: '%s'" % obj["location"])
125 adjustFileObjs(joborder_keepmount, keepmount)
126 adjustDirObjs(joborder_keepmount, keepmount)
128 if self.wf_pdh is None:
129 adjustFileObjs(packed, keepmount)
130 adjustDirObjs(packed, keepmount)
131 self.wf_pdh = upload_workflow_collection(self.arvrunner, shortname(self.tool["id"]), packed)
134 "class": "CommandLineTool",
135 "baseCommand": "cwltool",
136 "inputs": self.tool["inputs"],
137 "outputs": self.tool["outputs"],
138 "stdout": "cwl.output.json",
139 "requirements": workflowobj["requirements"]+[
141 "class": "InitialWorkDirRequirement",
143 "entryname": "workflow.cwl",
146 "location": "keep:%s/workflow.cwl" % self.wf_pdh
149 "entryname": "cwl.input.yml",
150 "entry": json.dumps(joborder_keepmount, indent=2, sort_keys=True, separators=(',',': ')).replace("\\", "\\\\").replace('$(', '\$(').replace('${', '\${')
153 "hints": workflowobj["hints"],
154 "arguments": ["--no-container", "--move-outputs", "--preserve-entire-environment", "workflow.cwl#main", "cwl.input.yml"]
156 kwargs["loader"] = self.doc_loader
157 kwargs["avsc_names"] = self.doc_schema
158 return ArvadosCommandTool(self.arvrunner, wf_runner, **kwargs).job(joborder, output_callback, **kwargs)
160 return super(ArvadosWorkflow, self).job(joborder, output_callback, **kwargs)