2 # Copyright (C) The Arvados Authors. All rights reserved.
4 # SPDX-License-Identifier: Apache-2.0
6 # Crunch script integration for running arvados-cwl-runner inside a crunch job.
12 # Use the crunch script defined in the arvados_cwl package. This helps
13 # prevent the crunch script from going out of sync with the rest of the
14 # arvados_cwl package.
15 import arvados_cwl.crunch_script
16 arvados_cwl.crunch_script.run()
21 # When running against an older arvados-cwl-runner package without
22 # arvados_cwl.crunch_script, fall back to the old code.
25 # This gets the job record, transforms the script parameters into a valid CWL
26 # input object, then executes the CWL runner to run the underlying workflow or
27 # tool. When the workflow completes, record the output object in an output
28 # collection for this runner job.
31 import arvados.collection
41 from arvados.api import OrderedJsonModel
42 from cwltool.process import shortname, adjustFileObjs, adjustDirObjs, getListing, normalizeFilesDirs
43 from cwltool.load_tool import load_tool
45 # Print package versions
46 logging.info(cwltool.main.versionstring())
48 api = arvados.api("v1")
51 job_order_object = arvados.current_job()['script_parameters']
53 pdh_path = re.compile(r'^[0-9a-f]{32}\+\d+(/.+)?$')
62 v["location"] = keeppath(v["location"])
64 job_order_object["cwl:tool"] = "file://%s/%s" % (os.environ['TASK_KEEPMOUNT'], job_order_object["cwl:tool"])
66 for k,v in job_order_object.items():
67 if isinstance(v, basestring) and arvados.util.keep_locator_pattern.match(v):
68 job_order_object[k] = {
70 "location": "keep:%s" % v
73 adjustFileObjs(job_order_object, keeppathObj)
74 adjustDirObjs(job_order_object, keeppathObj)
75 normalizeFilesDirs(job_order_object)
76 adjustDirObjs(job_order_object, functools.partial(getListing, arvados_cwl.fsaccess.CollectionFsAccess("", api_client=api)))
79 if "arv:output_name" in job_order_object:
80 output_name = job_order_object["arv:output_name"]
81 del job_order_object["arv:output_name"]
83 runner = arvados_cwl.ArvCwlRunner(api_client=arvados.api('v1', model=OrderedJsonModel()),
84 output_name=output_name)
86 t = load_tool(job_order_object, runner.arv_make_tool)
88 args = argparse.Namespace()
89 args.project_uuid = arvados.current_job()["owner_uuid"]
90 args.enable_reuse = True
94 args.ignore_docker_for_reuse = False
95 args.basedir = os.getcwd()
96 args.cwl_runner_job={"uuid": arvados.current_job()["uuid"], "state": arvados.current_job()["state"]}
97 outputObj = runner.arv_executor(t, job_order_object, **vars(args))
99 if runner.final_output_collection:
100 outputCollection = runner.final_output_collection.portable_data_hash()
102 outputCollection = None
104 api.job_tasks().update(uuid=arvados.current_task()['uuid'],
106 'output': outputCollection,
110 except Exception as e:
111 logging.exception("Unhandled exception")
112 api.job_tasks().update(uuid=arvados.current_task()['uuid'],