10383: Now --update-collection is not mutually exclusive with --resume and --no-resume.
[arvados.git] / sdk / cwl / arvados_cwl / crunch_script.py
1 # Crunch script integration for running arvados-cwl-runner (importing
2 # arvados_cwl module) inside a crunch job.
3 #
4 # This gets the job record, transforms the script parameters into a valid CWL
5 # input object, then executes the CWL runner to run the underlying workflow or
6 # tool.  When the workflow completes, record the output object in an output
7 # collection for this runner job.
8
9 import arvados
10 import arvados_cwl
11 import arvados.collection
12 import arvados.util
13 import cwltool.main
14 import logging
15 import os
16 import json
17 import argparse
18 import re
19 import functools
20
21 from arvados.api import OrderedJsonModel
22 from cwltool.process import shortname, adjustFileObjs, adjustDirObjs, getListing, normalizeFilesDirs
23 from cwltool.load_tool import load_tool
24 from cwltool.errors import WorkflowException
25
26 logger = logging.getLogger('arvados.cwl-runner')
27
28 def run():
29     # Print package versions
30     logger.info(arvados_cwl.versionstring())
31
32     api = arvados.api("v1")
33
34     arvados_cwl.add_arv_hints()
35
36     runner = None
37     try:
38         job_order_object = arvados.current_job()['script_parameters']
39
40         pdh_path = re.compile(r'^[0-9a-f]{32}\+\d+(/.+)?$')
41
42         def keeppath(v):
43             if pdh_path.match(v):
44                 return "keep:%s" % v
45             else:
46                 return v
47
48         def keeppathObj(v):
49             v["location"] = keeppath(v["location"])
50
51         job_order_object["cwl:tool"] = "file://%s/%s" % (os.environ['TASK_KEEPMOUNT'], job_order_object["cwl:tool"])
52
53         for k,v in job_order_object.items():
54             if isinstance(v, basestring) and arvados.util.keep_locator_pattern.match(v):
55                 job_order_object[k] = {
56                     "class": "File",
57                     "location": "keep:%s" % v
58                 }
59
60         adjustFileObjs(job_order_object, keeppathObj)
61         adjustDirObjs(job_order_object, keeppathObj)
62         normalizeFilesDirs(job_order_object)
63         adjustDirObjs(job_order_object, functools.partial(getListing, arvados_cwl.fsaccess.CollectionFsAccess("", api_client=api)))
64
65         output_name = None
66         if "arv:output_name" in job_order_object:
67             output_name = job_order_object["arv:output_name"]
68             del job_order_object["arv:output_name"]
69
70         runner = arvados_cwl.ArvCwlRunner(api_client=arvados.api('v1', model=OrderedJsonModel()),
71                                           output_name=output_name)
72
73         t = load_tool(job_order_object, runner.arv_make_tool)
74
75         args = argparse.Namespace()
76         args.project_uuid = arvados.current_job()["owner_uuid"]
77         args.enable_reuse = True
78         args.submit = False
79         args.debug = True
80         args.quiet = False
81         args.ignore_docker_for_reuse = False
82         args.basedir = os.getcwd()
83         args.cwl_runner_job={"uuid": arvados.current_job()["uuid"], "state": arvados.current_job()["state"]}
84         outputObj = runner.arv_executor(t, job_order_object, **vars(args))
85     except Exception as e:
86         if isinstance(e, WorkflowException):
87             logging.info("Workflow error %s", e)
88         else:
89             logging.exception("Unhandled exception")
90         if runner and runner.final_output_collection:
91             outputCollection = runner.final_output_collection.portable_data_hash()
92         else:
93             outputCollection = None
94         api.job_tasks().update(uuid=arvados.current_task()['uuid'],
95                                              body={
96                                                  'output': outputCollection,
97                                                  'success': False,
98                                                  'progress':1.0
99                                              }).execute()