6 from cwltool.pack import pack
7 from cwltool.load_tool import fetch_document
8 from cwltool.process import shortname
9 from cwltool.workflow import Workflow, WorkflowException
10 from cwltool.pathmapper import adjustFileObjs, adjustDirObjs
12 import ruamel.yaml as yaml
14 from .runner import upload_docker, upload_dependencies, trim_listing
15 from .arvtool import ArvadosCommandTool
16 from .perf import Perf
18 logger = logging.getLogger('arvados.cwl-runner')
19 metrics = logging.getLogger('arvados.cwl-runner.metrics')
21 def upload_workflow(arvRunner, tool, job_order, project_uuid, uuid=None,
22 submit_runner_ram=0, name=None):
23 upload_docker(arvRunner, tool)
25 document_loader, workflowobj, uri = (tool.doc_loader, tool.doc_loader.fetch(tool.tool["id"]), tool.tool["id"])
27 packed = pack(document_loader, workflowobj, uri, tool.metadata)
29 adjustDirObjs(job_order, trim_listing)
31 main = [p for p in packed["$graph"] if p["id"] == "#main"][0]
32 for inp in main["inputs"]:
33 sn = shortname(inp["id"])
35 inp["default"] = job_order[sn]
38 name = tool.tool.get("label", os.path.basename(tool.tool["id"]))
40 upload_dependencies(arvRunner, name, document_loader,
43 # TODO nowhere for submit_runner_ram to go.
48 "description": tool.tool.get("doc", ""),
49 "definition":yaml.safe_dump(packed)
52 body["workflow"]["owner_uuid"] = project_uuid
55 call = arvRunner.api.workflows().update(uuid=uuid, body=body)
57 call = arvRunner.api.workflows().create(body=body)
58 return call.execute(num_retries=arvRunner.num_retries)["uuid"]
60 class ArvadosWorkflow(Workflow):
61 """Wrap cwltool Workflow to override selected methods."""
63 def __init__(self, arvrunner, toolpath_object, **kwargs):
64 super(ArvadosWorkflow, self).__init__(toolpath_object, **kwargs)
65 self.arvrunner = arvrunner
66 self.work_api = kwargs["work_api"]
68 def job(self, joborder, output_callback, **kwargs):
69 kwargs["work_api"] = self.work_api
70 req, _ = self.get_requirement("http://arvados.org/cwl#RunInSingleContainer")
72 document_loader, workflowobj, uri = (self.doc_loader, self.doc_loader.fetch(self.tool["id"]), self.tool["id"])
74 with Perf(metrics, "subworkflow upload_deps"):
75 workflowobj["requirements"] = self.requirements + workflowobj.get("requirements", [])
76 workflowobj["hints"] = self.hints + workflowobj.get("hints", [])
77 packed = pack(document_loader, workflowobj, uri, self.metadata)
79 upload_dependencies(self.arvrunner,
80 kwargs.get("name", ""),
86 upload_dependencies(self.arvrunner,
87 os.path.basename(joborder.get("id", "#")),
90 joborder.get("id", "#"),
93 with Perf(metrics, "subworkflow adjust"):
94 joborder_keepmount = copy.deepcopy(joborder)
97 if "location" not in obj:
98 raise WorkflowException("%s object is missing required 'location' field: %s" % (obj["class"], obj))
99 if obj["location"].startswith("keep:"):
100 obj["location"] = "/keep/" + obj["location"][5:]
103 elif obj["location"].startswith("_:"):
106 raise WorkflowException("Location is not a keep reference or a literal: '%s'" % obj["location"])
108 adjustFileObjs(joborder_keepmount, keepmount)
109 adjustDirObjs(joborder_keepmount, keepmount)
110 adjustFileObjs(packed, keepmount)
111 adjustDirObjs(packed, keepmount)
114 "class": "CommandLineTool",
115 "baseCommand": "cwltool",
116 "inputs": self.tool["inputs"],
117 "outputs": self.tool["outputs"],
118 "stdout": "cwl.output.json",
119 "requirements": workflowobj["requirements"]+[
121 "class": "InitialWorkDirRequirement",
123 "entryname": "workflow.cwl",
124 "entry": yaml.safe_dump(packed).replace("\\", "\\\\").replace('$(', '\$(').replace('${', '\${')
126 "entryname": "cwl.input.yml",
127 "entry": yaml.safe_dump(joborder_keepmount).replace("\\", "\\\\").replace('$(', '\$(').replace('${', '\${')
130 "hints": workflowobj["hints"],
131 "arguments": ["--no-container", "--move-outputs", "--preserve-entire-environment", "workflow.cwl#main", "cwl.input.yml"]
133 kwargs["loader"] = self.doc_loader
134 kwargs["avsc_names"] = self.doc_schema
135 return ArvadosCommandTool(self.arvrunner, wf_runner, **kwargs).job(joborder, output_callback, **kwargs)
137 return super(ArvadosWorkflow, self).job(joborder, output_callback, **kwargs)