5 from schema_salad.sourceline import SourceLine
8 from cwltool.errors import WorkflowException
9 import arvados.commands.keepdocker
11 logger = logging.getLogger('arvados.cwl-runner')
14 cached_lookups_lock = threading.Lock()
16 def arv_docker_get_image(api_client, dockerRequirement, pull_image, project_uuid):
17 """Check if a Docker image is available in Keep, if not, upload it using arv-keepdocker."""
19 if "dockerImageId" not in dockerRequirement and "dockerPull" in dockerRequirement:
20 dockerRequirement["dockerImageId"] = dockerRequirement["dockerPull"]
21 if hasattr(dockerRequirement, 'lc'):
22 dockerRequirement.lc.data["dockerImageId"] = dockerRequirement.lc.data["dockerPull"]
25 global cached_lookups_lock
26 with cached_lookups_lock:
27 if dockerRequirement["dockerImageId"] in cached_lookups:
28 return cached_lookups[dockerRequirement["dockerImageId"]]
30 with SourceLine(dockerRequirement, "dockerImageId", WorkflowException):
31 sp = dockerRequirement["dockerImageId"].split(":")
33 image_tag = sp[1] if len(sp) > 1 else None
35 images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3,
36 image_name=image_name,
40 # Fetch Docker image if necessary.
41 cwltool.docker.get_image(dockerRequirement, pull_image)
43 # Upload image to Arvados
46 args.append("--project-uuid="+project_uuid)
47 args.append(image_name)
49 args.append(image_tag)
50 logger.info("Uploading Docker image %s", ":".join(args[1:]))
52 arvados.commands.keepdocker.main(args, stdout=sys.stderr)
53 except SystemExit as e:
55 raise WorkflowException("keepdocker exited with code %s" % e.code)
57 images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3,
58 image_name=image_name,
62 raise WorkflowException("Could not find Docker image %s:%s" % (image_name, image_tag))
64 pdh = api_client.collections().get(uuid=images[0][0]).execute()["portable_data_hash"]
66 with cached_lookups_lock:
67 cached_lookups[dockerRequirement["dockerImageId"]] = pdh
71 def arv_docker_clear_cache():
73 global cached_lookups_lock
74 with cached_lookups_lock: