1 # Crunch script integration for running arvados-cwl-runner (importing
2 # arvados_cwl module) inside a crunch job.
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.
11 import arvados.collection
21 from arvados.api import OrderedJsonModel
22 from cwltool.process import shortname, adjustFileObjs, adjustDirObjs, getListing, normalizeFilesDirs
23 from cwltool.load_tool import load_tool
25 logger = logging.getLogger('arvados.cwl-runner')
28 # Print package versions
29 logger.info(arvados_cwl.versionstring())
31 api = arvados.api("v1")
33 arvados_cwl.add_arv_hints()
36 job_order_object = arvados.current_job()['script_parameters']
38 pdh_path = re.compile(r'^[0-9a-f]{32}\+\d+(/.+)?$')
47 v["location"] = keeppath(v["location"])
49 job_order_object["cwl:tool"] = "file://%s/%s" % (os.environ['TASK_KEEPMOUNT'], job_order_object["cwl:tool"])
51 for k,v in job_order_object.items():
52 if isinstance(v, basestring) and arvados.util.keep_locator_pattern.match(v):
53 job_order_object[k] = {
55 "location": "keep:%s" % v
58 adjustFileObjs(job_order_object, keeppathObj)
59 adjustDirObjs(job_order_object, keeppathObj)
60 normalizeFilesDirs(job_order_object)
61 adjustDirObjs(job_order_object, functools.partial(getListing, arvados_cwl.fsaccess.CollectionFsAccess("", api_client=api)))
64 if "arv:output_name" in job_order_object:
65 output_name = job_order_object["arv:output_name"]
66 del job_order_object["arv:output_name"]
68 runner = arvados_cwl.ArvCwlRunner(api_client=arvados.api('v1', model=OrderedJsonModel()),
69 output_name=output_name)
71 t = load_tool(job_order_object, runner.arv_make_tool)
73 args = argparse.Namespace()
74 args.project_uuid = arvados.current_job()["owner_uuid"]
75 args.enable_reuse = True
79 args.ignore_docker_for_reuse = False
80 args.basedir = os.getcwd()
81 args.cwl_runner_job={"uuid": arvados.current_job()["uuid"], "state": arvados.current_job()["state"]}
82 outputObj = runner.arv_executor(t, job_order_object, **vars(args))
84 if runner.final_output_collection:
85 outputCollection = runner.final_output_collection.portable_data_hash()
87 outputCollection = None
89 api.job_tasks().update(uuid=arvados.current_task()['uuid'],
91 'output': outputCollection,
95 except Exception as e:
96 logging.exception("Unhandled exception")
97 api.job_tasks().update(uuid=arvados.current_task()['uuid'],