7 from cwltool.process import get_feature, shortname
8 from cwltool.errors import WorkflowException
9 from cwltool.draft2tool import revmap_file, CommandLineTool
10 from cwltool.load_tool import fetch_document
11 from cwltool.builder import Builder
13 import arvados.collection
15 from .arvdocker import arv_docker_get_image
16 from .runner import Runner, arvados_jobs_image
17 from .pathmapper import InitialWorkDirPathMapper
18 from .perf import Perf
20 from ._version import __version__
22 logger = logging.getLogger('arvados.cwl-runner')
23 metrics = logging.getLogger('arvados.cwl-runner.metrics')
25 crunchrunner_re = re.compile(r"^\S+ \S+ \d+ \d+ stderr \S+ \S+ crunchrunner: \$\(task\.(tmpdir|outdir|keep)\)=(.*)")
27 crunchrunner_git_commit = 'a3f2cb186e437bfce0031b024b2157b73ed2717d'
29 class ArvadosJob(object):
30 """Submit and manage a Crunch job for executing a CWL CommandLineTool."""
32 def __init__(self, runner):
33 self.arvrunner = runner
37 def run(self, dry_run=False, pull_image=True, **kwargs):
39 "command": self.command_line
41 runtime_constraints = {}
43 with Perf(metrics, "generatefiles %s" % self.name):
44 if self.generatefiles["listing"]:
45 vwd = arvados.collection.Collection(api_client=self.arvrunner.api,
46 keep_client=self.arvrunner.keep_client,
47 num_retries=self.arvrunner.num_retries)
48 script_parameters["task.vwd"] = {}
49 generatemapper = InitialWorkDirPathMapper([self.generatefiles], "", "",
52 with Perf(metrics, "createfiles %s" % self.name):
53 for f, p in generatemapper.items():
54 if p.type == "CreateFile":
55 with vwd.open(p.target, "w") as n:
56 n.write(p.resolved.encode("utf-8"))
58 with Perf(metrics, "generatefiles.save_new %s" % self.name):
61 for f, p in generatemapper.items():
63 script_parameters["task.vwd"][p.target] = p.resolved
64 if p.type == "CreateFile":
65 script_parameters["task.vwd"][p.target] = "$(task.keep)/%s/%s" % (vwd.portable_data_hash(), p.target)
67 script_parameters["task.env"] = {"TMPDIR": self.tmpdir, "HOME": self.outdir}
69 script_parameters["task.env"].update(self.environment)
72 script_parameters["task.stdin"] = self.stdin
75 script_parameters["task.stdout"] = self.stdout
78 script_parameters["task.stderr"] = self.stderr
81 script_parameters["task.successCodes"] = self.successCodes
82 if self.temporaryFailCodes:
83 script_parameters["task.temporaryFailCodes"] = self.temporaryFailCodes
84 if self.permanentFailCodes:
85 script_parameters["task.permanentFailCodes"] = self.permanentFailCodes
87 with Perf(metrics, "arv_docker_get_image %s" % self.name):
88 (docker_req, docker_is_req) = get_feature(self, "DockerRequirement")
89 if docker_req and kwargs.get("use_container") is not False:
90 if docker_req.get("dockerOutputDirectory"):
91 raise SourceLine(docker_req, "dockerOutputDirectory", UnsupportedRequirement).makeError(
92 "Option 'dockerOutputDirectory' of DockerRequirement not supported.")
93 runtime_constraints["docker_image"] = arv_docker_get_image(self.arvrunner.api, docker_req, pull_image, self.arvrunner.project_uuid)
95 runtime_constraints["docker_image"] = arvados_jobs_image(self.arvrunner)
97 resources = self.builder.resources
98 if resources is not None:
99 runtime_constraints["min_cores_per_node"] = resources.get("cores", 1)
100 runtime_constraints["min_ram_mb_per_node"] = resources.get("ram")
101 runtime_constraints["min_scratch_mb_per_node"] = resources.get("tmpdirSize", 0) + resources.get("outdirSize", 0)
103 runtime_req, _ = get_feature(self, "http://arvados.org/cwl#RuntimeConstraints")
105 if "keep_cache" in runtime_req:
106 runtime_constraints["keep_cache_mb_per_task"] = runtime_req["keep_cache"]
107 if "outputDirType" in runtime_req:
108 if runtime_req["outputDirType"] == "local_output_dir":
109 script_parameters["task.keepTmpOutput"] = False
110 elif runtime_req["outputDirType"] == "keep_output_dir":
111 script_parameters["task.keepTmpOutput"] = True
113 filters = [["repository", "=", "arvados"],
114 ["script", "=", "crunchrunner"],
115 ["script_version", "in git", crunchrunner_git_commit]]
116 if not self.arvrunner.ignore_docker_for_reuse:
117 filters.append(["docker_image_locator", "in docker", runtime_constraints["docker_image"]])
120 with Perf(metrics, "create %s" % self.name):
121 response = self.arvrunner.api.jobs().create(
123 "owner_uuid": self.arvrunner.project_uuid,
124 "script": "crunchrunner",
125 "repository": "arvados",
126 "script_version": "master",
127 "minimum_script_version": crunchrunner_git_commit,
128 "script_parameters": {"tasks": [script_parameters]},
129 "runtime_constraints": runtime_constraints
132 find_or_create=kwargs.get("enable_reuse", True)
133 ).execute(num_retries=self.arvrunner.num_retries)
135 self.arvrunner.processes[response["uuid"]] = self
137 self.update_pipeline_component(response)
139 logger.info("Job %s (%s) is %s", self.name, response["uuid"], response["state"])
141 if response["state"] in ("Complete", "Failed", "Cancelled"):
142 with Perf(metrics, "done %s" % self.name):
144 except Exception as e:
145 logger.exception("Job %s error" % (self.name))
146 self.output_callback({}, "permanentFail")
148 def update_pipeline_component(self, record):
149 if self.arvrunner.pipeline:
150 self.arvrunner.pipeline["components"][self.name] = {"job": record}
151 with Perf(metrics, "update_pipeline_component %s" % self.name):
152 self.arvrunner.pipeline = self.arvrunner.api.pipeline_instances().update(uuid=self.arvrunner.pipeline["uuid"],
154 "components": self.arvrunner.pipeline["components"]
155 }).execute(num_retries=self.arvrunner.num_retries)
156 if self.arvrunner.uuid:
158 job = self.arvrunner.api.jobs().get(uuid=self.arvrunner.uuid).execute()
160 components = job["components"]
161 components[self.name] = record["uuid"]
162 self.arvrunner.api.jobs().update(uuid=self.arvrunner.uuid,
164 "components": components
165 }).execute(num_retries=self.arvrunner.num_retries)
166 except Exception as e:
167 logger.info("Error adding to components: %s", e)
169 def done(self, record):
171 self.update_pipeline_component(record)
176 if record["state"] == "Complete":
177 processStatus = "success"
179 processStatus = "permanentFail"
184 with Perf(metrics, "inspect log %s" % self.name):
185 logc = arvados.collection.CollectionReader(record["log"],
186 api_client=self.arvrunner.api,
187 keep_client=self.arvrunner.keep_client,
188 num_retries=self.arvrunner.num_retries)
189 log = logc.open(logc.keys()[0])
195 # Determine the tmpdir, outdir and keepdir paths from
196 # the job run. Unfortunately, we can't take the first
197 # values we find (which are expected to be near the
198 # top) and stop scanning because if the node fails and
199 # the job restarts on a different node these values
200 # will different runs, and we need to know about the
201 # final run that actually produced output.
202 g = crunchrunner_re.match(l)
204 dirs[g.group(1)] = g.group(2)
206 with Perf(metrics, "output collection %s" % self.name):
207 outputs = done.done(self, record, dirs["tmpdir"],
208 dirs["outdir"], dirs["keep"])
209 except WorkflowException as e:
210 logger.error("Error while collecting output for job %s:\n%s", self.name, e, exc_info=(e if self.arvrunner.debug else False))
211 processStatus = "permanentFail"
212 except Exception as e:
213 logger.exception("Got unknown exception while collecting output for job %s:", self.name)
214 processStatus = "permanentFail"
216 # Note: Currently, on error output_callback is expecting an empty dict,
217 # anything else will fail.
218 if not isinstance(outputs, dict):
219 logger.error("Unexpected output type %s '%s'", type(outputs), outputs)
221 processStatus = "permanentFail"
223 self.output_callback(outputs, processStatus)
225 del self.arvrunner.processes[record["uuid"]]
228 class RunnerJob(Runner):
229 """Submit and manage a Crunch job that runs crunch_scripts/cwl-runner."""
231 def arvados_job_spec(self, dry_run=False, pull_image=True, **kwargs):
232 """Create an Arvados job specification for this workflow.
234 The returned dict can be used to create a job (i.e., passed as
235 the +body+ argument to jobs().create()), or as a component in
236 a pipeline template or pipeline instance.
239 workflowmapper = super(RunnerJob, self).arvados_job_spec(dry_run=dry_run, pull_image=pull_image, **kwargs)
241 self.job_order["cwl:tool"] = workflowmapper.mapper(self.tool.tool["id"]).target[5:]
244 self.job_order["arv:output_name"] = self.output_name
247 self.job_order["arv:output_tags"] = self.output_tags
249 self.job_order["arv:enable_reuse"] = self.enable_reuse
252 "script": "cwl-runner",
253 "script_version": "master",
254 "minimum_script_version": "570509ab4d2ef93d870fd2b1f2eab178afb1bad9",
255 "repository": "arvados",
256 "script_parameters": self.job_order,
257 "runtime_constraints": {
258 "docker_image": arvados_jobs_image(self.arvrunner),
259 "min_ram_mb_per_node": self.submit_runner_ram
263 def run(self, *args, **kwargs):
264 job_spec = self.arvados_job_spec(*args, **kwargs)
266 job_spec.setdefault("owner_uuid", self.arvrunner.project_uuid)
268 job = self.arvrunner.api.jobs().create(
270 find_or_create=self.enable_reuse
271 ).execute(num_retries=self.arvrunner.num_retries)
273 for k,v in job_spec["script_parameters"].items():
274 if v is False or v is None or isinstance(v, dict):
275 job_spec["script_parameters"][k] = {"value": v}
277 del job_spec["owner_uuid"]
278 job_spec["job"] = job
279 self.arvrunner.pipeline = self.arvrunner.api.pipeline_instances().create(
281 "owner_uuid": self.arvrunner.project_uuid,
283 "components": {"cwl-runner": job_spec },
284 "state": "RunningOnServer"}).execute(num_retries=self.arvrunner.num_retries)
285 logger.info("Created pipeline %s", self.arvrunner.pipeline["uuid"])
287 if kwargs.get("wait") is False:
288 self.uuid = self.arvrunner.pipeline["uuid"]
291 self.uuid = job["uuid"]
292 self.arvrunner.processes[self.uuid] = self
294 if job["state"] in ("Complete", "Failed", "Cancelled"):
298 class RunnerTemplate(object):
299 """An Arvados pipeline template that invokes a CWL workflow."""
301 type_to_dataclass = {
302 'boolean': 'boolean',
304 'Directory': 'Collection',
310 def __init__(self, runner, tool, job_order, enable_reuse, uuid,
311 submit_runner_ram=0, name=None):
314 self.job = RunnerJob(
318 enable_reuse=enable_reuse,
321 submit_runner_ram=submit_runner_ram,
325 def pipeline_component_spec(self):
326 """Return a component that Workbench and a-r-p-i will understand.
328 Specifically, translate CWL input specs to Arvados pipeline
329 format, like {"dataclass":"File","value":"xyz"}.
332 spec = self.job.arvados_job_spec()
334 # Most of the component spec is exactly the same as the job
335 # spec (script, script_version, etc.).
336 # spec['script_parameters'] isn't right, though. A component
337 # spec's script_parameters hash is a translation of
338 # self.tool.tool['inputs'] with defaults/overrides taken from
339 # the job order. So we move the job parameters out of the way
340 # and build a new spec['script_parameters'].
341 job_params = spec['script_parameters']
342 spec['script_parameters'] = {}
344 for param in self.tool.tool['inputs']:
345 param = copy.deepcopy(param)
347 # Data type and "required" flag...
348 types = param['type']
349 if not isinstance(types, list):
351 param['required'] = 'null' not in types
352 non_null_types = set(types) - set(['null'])
353 if len(non_null_types) == 1:
354 the_type = [c for c in non_null_types][0]
355 dataclass = self.type_to_dataclass.get(the_type)
357 param['dataclass'] = dataclass
358 # Note: If we didn't figure out a single appropriate
359 # dataclass, we just left that attribute out. We leave
360 # the "type" attribute there in any case, which might help
363 # Title and description...
364 title = param.pop('label', '')
365 descr = param.pop('doc', '').rstrip('\n')
367 param['title'] = title
369 param['description'] = descr
371 # Fill in the value from the current job order, if any.
372 param_id = shortname(param.pop('id'))
373 value = job_params.get(param_id)
376 elif not isinstance(value, dict):
377 param['value'] = value
378 elif param.get('dataclass') in ('File', 'Collection') and value.get('location'):
379 param['value'] = value['location'][5:]
381 spec['script_parameters'][param_id] = param
382 spec['script_parameters']['cwl:tool'] = job_params['cwl:tool']
388 self.job.name: self.pipeline_component_spec(),
390 "name": self.job.name,
392 if self.runner.project_uuid:
393 body["owner_uuid"] = self.runner.project_uuid
395 self.runner.api.pipeline_templates().update(
396 uuid=self.uuid, body=body).execute(
397 num_retries=self.runner.num_retries)
398 logger.info("Updated template %s", self.uuid)
400 self.uuid = self.runner.api.pipeline_templates().create(
401 body=body, ensure_unique_name=True).execute(
402 num_retries=self.runner.num_retries)['uuid']
403 logger.info("Created template %s", self.uuid)