5 import ruamel.yaml as yaml
7 from cwltool.errors import WorkflowException
8 from cwltool.process import get_feature, UnsupportedRequirement, shortname
9 from cwltool.pathmapper import adjustFiles, adjustDirObjs
10 from cwltool.utils import aslist
12 import arvados.collection
14 from .arvdocker import arv_docker_get_image
16 from .runner import Runner, arvados_jobs_image, packed_workflow, trim_listing
17 from .fsaccess import CollectionFetcher
18 from .pathmapper import NoFollowPathMapper
19 from .perf import Perf
21 logger = logging.getLogger('arvados.cwl-runner')
22 metrics = logging.getLogger('arvados.cwl-runner.metrics')
24 class ArvadosContainer(object):
25 """Submit and manage a Crunch container request for executing a CWL CommandLineTool."""
27 def __init__(self, runner):
28 self.arvrunner = runner
32 def update_pipeline_component(self, r):
35 def run(self, dry_run=False, pull_image=True, **kwargs):
37 "command": self.command_line,
38 "owner_uuid": self.arvrunner.project_uuid,
40 "output_path": self.outdir,
46 runtime_constraints = {}
52 scheduling_parameters = {}
55 for f in self.pathmapper.files():
56 pdh, p, tp = self.pathmapper.mapper(f)
57 if tp == "Directory" and '/' not in pdh:
60 "portable_data_hash": pdh[5:]
64 for f in self.pathmapper.files():
65 res, p, tp = self.pathmapper.mapper(f)
66 if res.startswith("keep:"):
68 elif res.startswith("/keep/"):
72 sp = res.split("/", 1)
77 "portable_data_hash": pdh
80 mounts[p]["path"] = sp[1]
82 with Perf(metrics, "generatefiles %s" % self.name):
83 if self.generatefiles["listing"]:
84 vwd = arvados.collection.Collection(api_client=self.arvrunner.api,
85 keep_client=self.arvrunner.keep_client,
86 num_retries=self.arvrunner.num_retries)
87 generatemapper = NoFollowPathMapper([self.generatefiles], "", "",
90 with Perf(metrics, "createfiles %s" % self.name):
91 for f, p in generatemapper.items():
94 elif p.type in ("File", "Directory"):
95 source, path = self.arvrunner.fs_access.get_collection(p.resolved)
96 vwd.copy(path, p.target, source_collection=source)
97 elif p.type == "CreateFile":
98 with vwd.open(p.target, "w") as n:
99 n.write(p.resolved.encode("utf-8"))
101 with Perf(metrics, "generatefiles.save_new %s" % self.name):
104 for f, p in generatemapper.items():
107 mountpoint = "%s/%s" % (self.outdir, p.target)
108 mounts[mountpoint] = {"kind": "collection",
109 "portable_data_hash": vwd.portable_data_hash(),
112 container_request["environment"] = {"TMPDIR": self.tmpdir, "HOME": self.outdir}
114 container_request["environment"].update(self.environment)
117 sp = self.stdin[6:].split("/", 1)
118 mounts["stdin"] = {"kind": "collection",
119 "portable_data_hash": sp[0],
123 mounts["stderr"] = {"kind": "file",
124 "path": "%s/%s" % (self.outdir, self.stderr)}
127 mounts["stdout"] = {"kind": "file",
128 "path": "%s/%s" % (self.outdir, self.stdout)}
130 (docker_req, docker_is_req) = get_feature(self, "DockerRequirement")
132 docker_req = {"dockerImageId": "arvados/jobs"}
134 container_request["container_image"] = arv_docker_get_image(self.arvrunner.api,
137 self.arvrunner.project_uuid)
139 resources = self.builder.resources
140 if resources is not None:
141 runtime_constraints["vcpus"] = resources.get("cores", 1)
142 runtime_constraints["ram"] = resources.get("ram") * 2**20
144 api_req, _ = get_feature(self, "http://arvados.org/cwl#APIRequirement")
146 runtime_constraints["API"] = True
148 runtime_req, _ = get_feature(self, "http://arvados.org/cwl#RuntimeConstraints")
150 if "keep_cache" in runtime_req:
151 runtime_constraints["keep_cache_ram"] = runtime_req["keep_cache"] * 2**20
153 partition_req, _ = get_feature(self, "http://arvados.org/cwl#PartitionRequirement")
155 scheduling_parameters["partitions"] = aslist(partition_req["partition"])
157 container_request["mounts"] = mounts
158 container_request["runtime_constraints"] = runtime_constraints
159 container_request["use_existing"] = kwargs.get("enable_reuse", True)
160 container_request["scheduling_parameters"] = scheduling_parameters
162 if kwargs.get("runnerjob", "").startswith("arvwf:"):
163 wfuuid = kwargs["runnerjob"][6:kwargs["runnerjob"].index("#")]
164 wfrecord = self.arvrunner.api.workflows().get(uuid=wfuuid).execute(num_retries=self.arvrunner.num_retries)
165 if container_request["name"] == "main":
166 container_request["name"] = wfrecord["name"]
167 container_request["properties"]["template_uuid"] = wfuuid
170 response = self.arvrunner.api.container_requests().create(
171 body=container_request
172 ).execute(num_retries=self.arvrunner.num_retries)
174 self.uuid = response["uuid"]
175 self.arvrunner.processes[self.uuid] = self
177 if response["state"] == "Final":
178 logger.info("%s reused container %s", self.arvrunner.label(self), response["container_uuid"])
181 logger.info("%s %s state is %s", self.arvrunner.label(self), response["uuid"], response["state"])
182 except Exception as e:
183 logger.error("%s got error %s" % (self.arvrunner.label(self), str(e)))
184 self.output_callback({}, "permanentFail")
186 def done(self, record):
188 container = self.arvrunner.api.containers().get(
189 uuid=record["container_uuid"]
190 ).execute(num_retries=self.arvrunner.num_retries)
191 if container["state"] == "Complete":
192 rcode = container["exit_code"]
193 if self.successCodes and rcode in self.successCodes:
194 processStatus = "success"
195 elif self.temporaryFailCodes and rcode in self.temporaryFailCodes:
196 processStatus = "temporaryFail"
197 elif self.permanentFailCodes and rcode in self.permanentFailCodes:
198 processStatus = "permanentFail"
200 processStatus = "success"
202 processStatus = "permanentFail"
204 processStatus = "permanentFail"
206 if processStatus == "permanentFail":
207 logc = arvados.collection.CollectionReader(container["log"],
208 api_client=self.arvrunner.api,
209 keep_client=self.arvrunner.keep_client,
210 num_retries=self.arvrunner.num_retries)
211 done.logtail(logc, logger, "%s error log:" % self.arvrunner.label(self))
214 if container["output"]:
215 outputs = done.done_outputs(self, container, "/tmp", self.outdir, "/keep")
216 except WorkflowException as e:
217 logger.error("%s unable to collect output from %s:\n%s",
218 self.arvrunner.label(self), container["output"], e, exc_info=(e if self.arvrunner.debug else False))
219 processStatus = "permanentFail"
220 except Exception as e:
221 logger.exception("%s while getting output object: %s", self.arvrunner.label(self), e)
222 processStatus = "permanentFail"
224 self.output_callback(outputs, processStatus)
225 if record["uuid"] in self.arvrunner.processes:
226 del self.arvrunner.processes[record["uuid"]]
229 class RunnerContainer(Runner):
230 """Submit and manage a container that runs arvados-cwl-runner."""
232 def arvados_job_spec(self, dry_run=False, pull_image=True, **kwargs):
233 """Create an Arvados container request for this workflow.
235 The returned dict can be used to create a container passed as
236 the +body+ argument to container_requests().create().
239 adjustDirObjs(self.job_order, trim_listing)
242 "owner_uuid": self.arvrunner.project_uuid,
244 "output_path": "/var/spool/cwl",
245 "cwd": "/var/spool/cwl",
247 "state": "Committed",
248 "container_image": arvados_jobs_image(self.arvrunner, self.jobs_image),
250 "/var/lib/cwl/cwl.input.json": {
252 "content": self.job_order
256 "path": "/var/spool/cwl/cwl.output.json"
259 "kind": "collection",
263 "runtime_constraints": {
265 "ram": 1024*1024 * self.submit_runner_ram,
271 if self.tool.tool.get("id", "").startswith("keep:"):
272 sp = self.tool.tool["id"].split('/')
273 workflowcollection = sp[0][5:]
274 workflowname = "/".join(sp[1:])
275 workflowpath = "/var/lib/cwl/workflow/%s" % workflowname
276 container_req["mounts"]["/var/lib/cwl/workflow"] = {
277 "kind": "collection",
278 "portable_data_hash": "%s" % workflowcollection
281 packed = packed_workflow(self.arvrunner, self.tool)
282 workflowpath = "/var/lib/cwl/workflow.json#main"
283 container_req["mounts"]["/var/lib/cwl/workflow.json"] = {
287 if self.tool.tool.get("id", "").startswith("arvwf:"):
288 container_req["properties"]["template_uuid"] = self.tool.tool["id"][6:33]
290 command = ["arvados-cwl-runner", "--local", "--api=containers", "--no-log-timestamps"]
292 command.append("--output-name=" + self.output_name)
293 container_req["output_name"] = self.output_name
296 command.append("--output-tags=" + self.output_tags)
298 if kwargs.get("debug"):
299 command.append("--debug")
301 if self.enable_reuse:
302 command.append("--enable-reuse")
304 command.append("--disable-reuse")
307 command.append("--on-error=" + self.on_error)
309 command.extend([workflowpath, "/var/lib/cwl/cwl.input.json"])
311 container_req["command"] = command
316 def run(self, *args, **kwargs):
317 kwargs["keepprefix"] = "keep:"
318 job_spec = self.arvados_job_spec(*args, **kwargs)
319 job_spec.setdefault("owner_uuid", self.arvrunner.project_uuid)
321 response = self.arvrunner.api.container_requests().create(
323 ).execute(num_retries=self.arvrunner.num_retries)
325 self.uuid = response["uuid"]
326 self.arvrunner.processes[self.uuid] = self
328 logger.info("%s submitted container %s", self.arvrunner.label(self), response["uuid"])
330 if response["state"] == "Final":
333 def done(self, record):
335 container = self.arvrunner.api.containers().get(
336 uuid=record["container_uuid"]
337 ).execute(num_retries=self.arvrunner.num_retries)
338 except Exception as e:
339 logger.exception("%s while getting runner container: %s", self.arvrunner.label(self), e)
340 self.arvrunner.output_callback({}, "permanentFail")
342 super(RunnerContainer, self).done(container)
344 if record["uuid"] in self.arvrunner.processes:
345 del self.arvrunner.processes[record["uuid"]]