6 from schema_salad.sourceline import SourceLine
9 from cwltool.errors import WorkflowException
10 import arvados.commands.keepdocker
12 logger = logging.getLogger('arvados.cwl-runner')
15 cached_lookups_lock = threading.Lock()
17 def arv_docker_get_image(api_client, dockerRequirement, pull_image, project_uuid):
18 """Check if a Docker image is available in Keep, if not, upload it using arv-keepdocker."""
20 if "dockerImageId" not in dockerRequirement and "dockerPull" in dockerRequirement:
21 dockerRequirement = copy.deepcopy(dockerRequirement)
22 dockerRequirement["dockerImageId"] = dockerRequirement["dockerPull"]
23 if hasattr(dockerRequirement, 'lc'):
24 dockerRequirement.lc.data["dockerImageId"] = dockerRequirement.lc.data["dockerPull"]
27 global cached_lookups_lock
28 with cached_lookups_lock:
29 if dockerRequirement["dockerImageId"] in cached_lookups:
30 return cached_lookups[dockerRequirement["dockerImageId"]]
32 with SourceLine(dockerRequirement, "dockerImageId", WorkflowException):
33 sp = dockerRequirement["dockerImageId"].split(":")
35 image_tag = sp[1] if len(sp) > 1 else None
37 images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3,
38 image_name=image_name,
42 # Fetch Docker image if necessary.
43 cwltool.docker.get_image(dockerRequirement, pull_image)
45 # Upload image to Arvados
48 args.append("--project-uuid="+project_uuid)
49 args.append(image_name)
51 args.append(image_tag)
52 logger.info("Uploading Docker image %s", ":".join(args[1:]))
54 arvados.commands.keepdocker.main(args, stdout=sys.stderr)
55 except SystemExit as e:
57 raise WorkflowException("keepdocker exited with code %s" % e.code)
59 images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3,
60 image_name=image_name,
64 raise WorkflowException("Could not find Docker image %s:%s" % (image_name, image_tag))
66 pdh = api_client.collections().get(uuid=images[0][0]).execute()["portable_data_hash"]
68 with cached_lookups_lock:
69 cached_lookups[dockerRequirement["dockerImageId"]] = pdh
73 def arv_docker_clear_cache():
75 global cached_lookups_lock
76 with cached_lookups_lock: