Merge master to output-tags branch and resolve conflict
[arvados.git] / sdk / cwl / arvados_cwl / __init__.py
1 #!/usr/bin/env python
2
3 # Implement cwl-runner interface for submitting and running work on Arvados, using
4 # either the Crunch jobs API or Crunch containers API.
5
6 import argparse
7 import logging
8 import os
9 import sys
10 import threading
11 import hashlib
12 import copy
13 import json
14 from functools import partial
15 import pkg_resources  # part of setuptools
16
17 from cwltool.errors import WorkflowException
18 import cwltool.main
19 import cwltool.workflow
20 import schema_salad
21
22 import arvados
23 import arvados.config
24
25 from .arvcontainer import ArvadosContainer, RunnerContainer
26 from .arvjob import ArvadosJob, RunnerJob, RunnerTemplate
27 from. runner import Runner, upload_instance
28 from .arvtool import ArvadosCommandTool
29 from .arvworkflow import ArvadosWorkflow, upload_workflow
30 from .fsaccess import CollectionFsAccess
31 from .perf import Perf
32 from .pathmapper import FinalOutputPathMapper
33 from ._version import __version__
34
35 from cwltool.pack import pack
36 from cwltool.process import shortname, UnsupportedRequirement, getListing
37 from cwltool.pathmapper import adjustFileObjs, adjustDirObjs
38 from cwltool.draft2tool import compute_checksums
39 from arvados.api import OrderedJsonModel
40
41 logger = logging.getLogger('arvados.cwl-runner')
42 metrics = logging.getLogger('arvados.cwl-runner.metrics')
43 logger.setLevel(logging.INFO)
44
45
46 class ArvCwlRunner(object):
47     """Execute a CWL tool or workflow, submit work (using either jobs or
48     containers API), wait for them to complete, and report output.
49
50     """
51
52     def __init__(self, api_client, work_api=None, keep_client=None, output_name=None, output_tags=None):
53         self.api = api_client
54         self.processes = {}
55         self.lock = threading.Lock()
56         self.cond = threading.Condition(self.lock)
57         self.final_output = None
58         self.final_status = None
59         self.uploaded = {}
60         self.num_retries = 4
61         self.uuid = None
62         self.stop_polling = threading.Event()
63         self.poll_api = None
64         self.pipeline = None
65         self.final_output_collection = None
66         self.output_name = output_name
67         self.output_tags = output_tags
68         self.project_uuid = None
69
70         if keep_client is not None:
71             self.keep_client = keep_client
72         else:
73             self.keep_client = arvados.keep.KeepClient(api_client=self.api, num_retries=self.num_retries)
74
75         for api in ["jobs", "containers"]:
76             try:
77                 methods = self.api._rootDesc.get('resources')[api]['methods']
78                 if ('httpMethod' in methods['create'] and
79                     (work_api == api or work_api is None)):
80                     self.work_api = api
81                     break
82             except KeyError:
83                 pass
84         if not self.work_api:
85             if work_api is None:
86                 raise Exception("No supported APIs")
87             else:
88                 raise Exception("Unsupported API '%s'" % work_api)
89
90     def arv_make_tool(self, toolpath_object, **kwargs):
91         kwargs["work_api"] = self.work_api
92         if "class" in toolpath_object and toolpath_object["class"] == "CommandLineTool":
93             return ArvadosCommandTool(self, toolpath_object, **kwargs)
94         elif "class" in toolpath_object and toolpath_object["class"] == "Workflow":
95             return ArvadosWorkflow(self, toolpath_object, **kwargs)
96         else:
97             return cwltool.workflow.defaultMakeTool(toolpath_object, **kwargs)
98
99     def output_callback(self, out, processStatus):
100         if processStatus == "success":
101             logger.info("Overall process status is %s", processStatus)
102             if self.pipeline:
103                 self.api.pipeline_instances().update(uuid=self.pipeline["uuid"],
104                                                      body={"state": "Complete"}).execute(num_retries=self.num_retries)
105         else:
106             logger.warn("Overall process status is %s", processStatus)
107             if self.pipeline:
108                 self.api.pipeline_instances().update(uuid=self.pipeline["uuid"],
109                                                      body={"state": "Failed"}).execute(num_retries=self.num_retries)
110         self.final_status = processStatus
111         self.final_output = out
112
113     def on_message(self, event):
114         if "object_uuid" in event:
115             if event["object_uuid"] in self.processes and event["event_type"] == "update":
116                 if event["properties"]["new_attributes"]["state"] == "Running" and self.processes[event["object_uuid"]].running is False:
117                     uuid = event["object_uuid"]
118                     with self.lock:
119                         j = self.processes[uuid]
120                         logger.info("Job %s (%s) is Running", j.name, uuid)
121                         j.running = True
122                         j.update_pipeline_component(event["properties"]["new_attributes"])
123                 elif event["properties"]["new_attributes"]["state"] in ("Complete", "Failed", "Cancelled"):
124                     uuid = event["object_uuid"]
125                     try:
126                         self.cond.acquire()
127                         j = self.processes[uuid]
128                         txt = self.work_api[0].upper() + self.work_api[1:-1]
129                         logger.info("%s %s (%s) is %s", txt, j.name, uuid, event["properties"]["new_attributes"]["state"])
130                         with Perf(metrics, "done %s" % j.name):
131                             j.done(event["properties"]["new_attributes"])
132                         self.cond.notify()
133                     finally:
134                         self.cond.release()
135
136     def poll_states(self):
137         """Poll status of jobs or containers listed in the processes dict.
138
139         Runs in a separate thread.
140         """
141
142         while True:
143             self.stop_polling.wait(15)
144             if self.stop_polling.is_set():
145                 break
146             with self.lock:
147                 keys = self.processes.keys()
148             if not keys:
149                 continue
150
151             if self.work_api == "containers":
152                 table = self.poll_api.containers()
153             elif self.work_api == "jobs":
154                 table = self.poll_api.jobs()
155
156             try:
157                 proc_states = table.list(filters=[["uuid", "in", keys]]).execute(num_retries=self.num_retries)
158             except Exception as e:
159                 logger.warn("Error checking states on API server: %s", e)
160                 continue
161
162             for p in proc_states["items"]:
163                 self.on_message({
164                     "object_uuid": p["uuid"],
165                     "event_type": "update",
166                     "properties": {
167                         "new_attributes": p
168                     }
169                 })
170
171     def get_uploaded(self):
172         return self.uploaded.copy()
173
174     def add_uploaded(self, src, pair):
175         self.uploaded[src] = pair
176
177     def check_writable(self, obj):
178         if isinstance(obj, dict):
179             if obj.get("writable"):
180                 raise UnsupportedRequirement("InitialWorkDir feature 'writable: true' not supported")
181             for v in obj.itervalues():
182                 self.check_writable(v)
183         if isinstance(obj, list):
184             for v in obj:
185                 self.check_writable(v)
186
187     def make_output_collection(self, name, tagsString, outputObj):
188         outputObj = copy.deepcopy(outputObj)
189
190         files = []
191         def capture(fileobj):
192             files.append(fileobj)
193
194         adjustDirObjs(outputObj, capture)
195         adjustFileObjs(outputObj, capture)
196
197         generatemapper = FinalOutputPathMapper(files, "", "", separateDirs=False)
198
199         final = arvados.collection.Collection(api_client=self.api,
200                                               keep_client=self.keep_client,
201                                               num_retries=self.num_retries)
202
203         srccollections = {}
204         for k,v in generatemapper.items():
205             if k.startswith("_:"):
206                 if v.type == "Directory":
207                     continue
208                 if v.type == "CreateFile":
209                     with final.open(v.target, "wb") as f:
210                         f.write(v.resolved.encode("utf-8"))
211                     continue
212
213             if not k.startswith("keep:"):
214                 raise Exception("Output source is not in keep or a literal")
215             sp = k.split("/")
216             srccollection = sp[0][5:]
217             if srccollection not in srccollections:
218                 try:
219                     srccollections[srccollection] = arvados.collection.CollectionReader(
220                         srccollection,
221                         api_client=self.api,
222                         keep_client=self.keep_client,
223                         num_retries=self.num_retries)
224                 except arvados.errors.ArgumentError as e:
225                     logger.error("Creating CollectionReader for '%s' '%s': %s", k, v, e)
226                     raise
227             reader = srccollections[srccollection]
228             try:
229                 srcpath = "/".join(sp[1:]) if len(sp) > 1 else "."
230                 final.copy(srcpath, v.target, source_collection=reader, overwrite=False)
231             except IOError as e:
232                 logger.warn("While preparing output collection: %s", e)
233
234         def rewrite(fileobj):
235             fileobj["location"] = generatemapper.mapper(fileobj["location"]).target
236             for k in ("basename", "listing", "contents"):
237                 if k in fileobj:
238                     del fileobj[k]
239
240         adjustDirObjs(outputObj, rewrite)
241         adjustFileObjs(outputObj, rewrite)
242
243         with final.open("cwl.output.json", "w") as f:
244             json.dump(outputObj, f, sort_keys=True, indent=4, separators=(',',': '))
245
246         final.save_new(name=name, owner_uuid=self.project_uuid, ensure_unique_name=True)
247
248         logger.info("Final output collection %s \"%s\" (%s)", final.portable_data_hash(),
249                     final.api_response()["name"],
250                     final.manifest_locator())
251
252         final_uuid = final.manifest_locator()
253         tags = tagsString.split(',')
254         for tag in tags:
255              self.api.links().create(body={
256                 "head_uuid": final_uuid, "link_class": "tag", "name": tag
257                 }).execute(num_retries=self.num_retries)
258
259         def finalcollection(fileobj):
260             fileobj["location"] = "keep:%s/%s" % (final.portable_data_hash(), fileobj["location"])
261
262         adjustDirObjs(outputObj, finalcollection)
263         adjustFileObjs(outputObj, finalcollection)
264
265         return (outputObj, final)
266
267     def set_crunch_output(self):
268         if self.work_api == "containers":
269             try:
270                 current = self.api.containers().current().execute(num_retries=self.num_retries)
271                 self.api.containers().update(uuid=current['uuid'],
272                                              body={
273                                                  'output': self.final_output_collection.portable_data_hash(),
274                                              }).execute(num_retries=self.num_retries)
275             except Exception as e:
276                 logger.info("Setting container output: %s", e)
277         elif self.work_api == "jobs" and "TASK_UUID" in os.environ:
278             self.api.job_tasks().update(uuid=os.environ["TASK_UUID"],
279                                    body={
280                                        'output': self.final_output_collection.portable_data_hash(),
281                                        'success': self.final_status == "success",
282                                        'progress':1.0
283                                    }).execute(num_retries=self.num_retries)
284
285     def arv_executor(self, tool, job_order, **kwargs):
286         self.debug = kwargs.get("debug")
287
288         tool.visit(self.check_writable)
289
290         useruuid = self.api.users().current().execute()["uuid"]
291         self.project_uuid = kwargs.get("project_uuid") if kwargs.get("project_uuid") else useruuid
292         self.pipeline = None
293         make_fs_access = kwargs.get("make_fs_access") or partial(CollectionFsAccess,
294                                                                  api_client=self.api,
295                                                                  keep_client=self.keep_client)
296         self.fs_access = make_fs_access(kwargs["basedir"])
297
298         if kwargs.get("create_template"):
299             tmpl = RunnerTemplate(self, tool, job_order, kwargs.get("enable_reuse"))
300             tmpl.save()
301             # cwltool.main will write our return value to stdout.
302             return tmpl.uuid
303
304         if kwargs.get("create_workflow") or kwargs.get("update_workflow"):
305             return upload_workflow(self, tool, job_order, self.project_uuid, kwargs.get("update_workflow"))
306
307         self.ignore_docker_for_reuse = kwargs.get("ignore_docker_for_reuse")
308
309         kwargs["make_fs_access"] = make_fs_access
310         kwargs["enable_reuse"] = kwargs.get("enable_reuse")
311         kwargs["use_container"] = True
312         kwargs["tmpdir_prefix"] = "tmp"
313         kwargs["on_error"] = "continue"
314         kwargs["compute_checksum"] = kwargs.get("compute_checksum")
315
316         if self.work_api == "containers":
317             kwargs["outdir"] = "/var/spool/cwl"
318             kwargs["docker_outdir"] = "/var/spool/cwl"
319             kwargs["tmpdir"] = "/tmp"
320             kwargs["docker_tmpdir"] = "/tmp"
321         elif self.work_api == "jobs":
322             kwargs["outdir"] = "$(task.outdir)"
323             kwargs["docker_outdir"] = "$(task.outdir)"
324             kwargs["tmpdir"] = "$(task.tmpdir)"
325
326         upload_instance(self, shortname(tool.tool["id"]), tool, job_order)
327
328         runnerjob = None
329         if kwargs.get("submit"):
330             if self.work_api == "containers":
331                 if tool.tool["class"] == "CommandLineTool":
332                     runnerjob = tool.job(job_order,
333                                          self.output_callback,
334                                          **kwargs).next()
335                 else:
336                     runnerjob = RunnerContainer(self, tool, job_order, kwargs.get("enable_reuse"), self.output_name, self.output_tags)
337             else:
338                 runnerjob = RunnerJob(self, tool, job_order, kwargs.get("enable_reuse"), self.output_name, self.output_tags)
339
340         if not kwargs.get("submit") and "cwl_runner_job" not in kwargs and not self.work_api == "containers":
341             # Create pipeline for local run
342             self.pipeline = self.api.pipeline_instances().create(
343                 body={
344                     "owner_uuid": self.project_uuid,
345                     "name": shortname(tool.tool["id"]),
346                     "components": {},
347                     "state": "RunningOnClient"}).execute(num_retries=self.num_retries)
348             logger.info("Pipeline instance %s", self.pipeline["uuid"])
349
350         if runnerjob and not kwargs.get("wait"):
351             runnerjob.run(wait=kwargs.get("wait"))
352             return runnerjob.uuid
353
354         self.poll_api = arvados.api('v1')
355         self.polling_thread = threading.Thread(target=self.poll_states)
356         self.polling_thread.start()
357
358         if runnerjob:
359             jobiter = iter((runnerjob,))
360         else:
361             if "cwl_runner_job" in kwargs:
362                 self.uuid = kwargs.get("cwl_runner_job").get('uuid')
363             jobiter = tool.job(job_order,
364                                self.output_callback,
365                                **kwargs)
366
367         try:
368             self.cond.acquire()
369             # Will continue to hold the lock for the duration of this code
370             # except when in cond.wait(), at which point on_message can update
371             # job state and process output callbacks.
372
373             loopperf = Perf(metrics, "jobiter")
374             loopperf.__enter__()
375             for runnable in jobiter:
376                 loopperf.__exit__()
377                 if runnable:
378                     with Perf(metrics, "run"):
379                         runnable.run(**kwargs)
380                 else:
381                     if self.processes:
382                         self.cond.wait(1)
383                     else:
384                         logger.error("Workflow is deadlocked, no runnable jobs and not waiting on any pending jobs.")
385                         break
386                 loopperf.__enter__()
387             loopperf.__exit__()
388
389             while self.processes:
390                 self.cond.wait(1)
391
392         except UnsupportedRequirement:
393             raise
394         except:
395             if sys.exc_info()[0] is KeyboardInterrupt:
396                 logger.error("Interrupted, marking pipeline as failed")
397             else:
398                 logger.error("Caught unhandled exception, marking pipeline as failed.  Error was: %s", sys.exc_info()[1], exc_info=(sys.exc_info()[1] if self.debug else False))
399             if self.pipeline:
400                 self.api.pipeline_instances().update(uuid=self.pipeline["uuid"],
401                                                      body={"state": "Failed"}).execute(num_retries=self.num_retries)
402             if runnerjob and runnerjob.uuid and self.work_api == "containers":
403                 self.api.container_requests().update(uuid=runnerjob.uuid,
404                                                      body={"priority": "0"}).execute(num_retries=self.num_retries)
405         finally:
406             self.cond.release()
407             self.stop_polling.set()
408             self.polling_thread.join()
409
410         if self.final_status == "UnsupportedRequirement":
411             raise UnsupportedRequirement("Check log for details.")
412
413         if self.final_output is None:
414             raise WorkflowException("Workflow did not return a result.")
415
416         if kwargs.get("submit") and isinstance(runnerjob, Runner):
417             logger.info("Final output collection %s", runnerjob.final_output)
418         else:
419             if self.output_name is None:
420                 self.output_name = "Output of %s" % (shortname(tool.tool["id"]))
421             if self.output_tags is None:
422                 self.output_tags = ""
423             self.final_output, self.final_output_collection = self.make_output_collection(self.output_name, self.output_tags, self.final_output)
424             self.set_crunch_output()
425
426         if self.final_status != "success":
427             raise WorkflowException("Workflow failed.")
428
429         if kwargs.get("compute_checksum"):
430             adjustDirObjs(self.final_output, partial(getListing, self.fs_access))
431             adjustFileObjs(self.final_output, partial(compute_checksums, self.fs_access))
432
433         return self.final_output
434
435
436 def versionstring():
437     """Print version string of key packages for provenance and debugging."""
438
439     arvcwlpkg = pkg_resources.require("arvados-cwl-runner")
440     arvpkg = pkg_resources.require("arvados-python-client")
441     cwlpkg = pkg_resources.require("cwltool")
442
443     return "%s %s %s, %s %s, %s %s" % (sys.argv[0], __version__, arvcwlpkg[0].version,
444                                     "arvados-python-client", arvpkg[0].version,
445                                     "cwltool", cwlpkg[0].version)
446
447
448 def arg_parser():  # type: () -> argparse.ArgumentParser
449     parser = argparse.ArgumentParser(description='Arvados executor for Common Workflow Language')
450
451     parser.add_argument("--basedir", type=str,
452                         help="Base directory used to resolve relative references in the input, default to directory of input object file or current directory (if inputs piped/provided on command line).")
453     parser.add_argument("--outdir", type=str, default=os.path.abspath('.'),
454                         help="Output directory, default current directory")
455
456     parser.add_argument("--eval-timeout",
457                         help="Time to wait for a Javascript expression to evaluate before giving an error, default 20s.",
458                         type=float,
459                         default=20)
460     parser.add_argument("--version", action="store_true", help="Print version and exit")
461
462     exgroup = parser.add_mutually_exclusive_group()
463     exgroup.add_argument("--verbose", action="store_true", help="Default logging")
464     exgroup.add_argument("--quiet", action="store_true", help="Only print warnings and errors.")
465     exgroup.add_argument("--debug", action="store_true", help="Print even more logging")
466
467     parser.add_argument("--metrics", action="store_true", help="Print timing metrics")
468
469     parser.add_argument("--tool-help", action="store_true", help="Print command line help for tool")
470
471     exgroup = parser.add_mutually_exclusive_group()
472     exgroup.add_argument("--enable-reuse", action="store_true",
473                         default=True, dest="enable_reuse",
474                         help="")
475     exgroup.add_argument("--disable-reuse", action="store_false",
476                         default=True, dest="enable_reuse",
477                         help="")
478
479     parser.add_argument("--project-uuid", type=str, metavar="UUID", help="Project that will own the workflow jobs, if not provided, will go to home project.")
480     parser.add_argument("--output-name", type=str, help="Name to use for collection that stores the final output.", default=None)
481     parser.add_argument("--output-tags", type=str, help="Tags for the final output collection separated by commas, e.g., '--output-tags tag0,tag1,tag2'.", default=None)
482     parser.add_argument("--ignore-docker-for-reuse", action="store_true",
483                         help="Ignore Docker image version when deciding whether to reuse past jobs.",
484                         default=False)
485
486     exgroup = parser.add_mutually_exclusive_group()
487     exgroup.add_argument("--submit", action="store_true", help="Submit workflow to run on Arvados.",
488                         default=True, dest="submit")
489     exgroup.add_argument("--local", action="store_false", help="Run workflow on local host (submits jobs to Arvados).",
490                         default=True, dest="submit")
491     exgroup.add_argument("--create-template", action="store_true", help="Create an Arvados pipeline template.")
492     exgroup.add_argument("--create-workflow", action="store_true", help="Create an Arvados workflow.")
493     exgroup.add_argument("--update-workflow", type=str, metavar="UUID", help="Update existing Arvados workflow with uuid.")
494
495     exgroup = parser.add_mutually_exclusive_group()
496     exgroup.add_argument("--wait", action="store_true", help="After submitting workflow runner job, wait for completion.",
497                         default=True, dest="wait")
498     exgroup.add_argument("--no-wait", action="store_false", help="Submit workflow runner job and exit.",
499                         default=True, dest="wait")
500
501     parser.add_argument("--api", type=str,
502                         default=None, dest="work_api",
503                         help="Select work submission API, one of 'jobs' or 'containers'. Default is 'jobs' if that API is available, otherwise 'containers'.")
504
505     parser.add_argument("--compute-checksum", action="store_true", default=False,
506                         help="Compute checksum of contents while collecting outputs",
507                         dest="compute_checksum")
508
509     parser.add_argument("workflow", type=str, nargs="?", default=None, help="The workflow to execute")
510     parser.add_argument("job_order", nargs=argparse.REMAINDER, help="The input object to the workflow.")
511
512     return parser
513
514 def add_arv_hints():
515     cache = {}
516     res = pkg_resources.resource_stream(__name__, 'arv-cwl-schema.yml')
517     cache["http://arvados.org/cwl"] = res.read()
518     res.close()
519     document_loader, cwlnames, _, _ = cwltool.process.get_schema("v1.0")
520     _, extnames, _, _ = schema_salad.schema.load_schema("http://arvados.org/cwl", cache=cache)
521     for n in extnames.names:
522         if not cwlnames.has_name("http://arvados.org/cwl#"+n, ""):
523             cwlnames.add_name("http://arvados.org/cwl#"+n, "", extnames.get_name(n, ""))
524         document_loader.idx["http://arvados.org/cwl#"+n] = {}
525
526 def main(args, stdout, stderr, api_client=None, keep_client=None):
527     parser = arg_parser()
528
529     job_order_object = None
530     arvargs = parser.parse_args(args)
531     if (arvargs.create_template or arvargs.create_workflow or arvargs.update_workflow) and not arvargs.job_order:
532         job_order_object = ({}, "")
533
534     add_arv_hints()
535
536     try:
537         if api_client is None:
538             api_client=arvados.api('v1', model=OrderedJsonModel())
539         runner = ArvCwlRunner(api_client, work_api=arvargs.work_api, keep_client=keep_client, output_name=arvargs.output_name, output_tags=arvargs.output_tags)
540     except Exception as e:
541         logger.error(e)
542         return 1
543
544     if arvargs.debug:
545         logger.setLevel(logging.DEBUG)
546
547     if arvargs.quiet:
548         logger.setLevel(logging.WARN)
549         logging.getLogger('arvados.arv-run').setLevel(logging.WARN)
550
551     if arvargs.metrics:
552         metrics.setLevel(logging.DEBUG)
553         logging.getLogger("cwltool.metrics").setLevel(logging.DEBUG)
554
555     arvargs.conformance_test = None
556     arvargs.use_container = True
557
558     return cwltool.main.main(args=arvargs,
559                              stdout=stdout,
560                              stderr=stderr,
561                              executor=runner.arv_executor,
562                              makeTool=runner.arv_make_tool,
563                              versionfunc=versionstring,
564                              job_order_object=job_order_object,
565                              make_fs_access=partial(CollectionFsAccess, api_client=api_client))