1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
10 from schema_salad.sourceline import SourceLine
13 from cwltool.errors import WorkflowException
14 import arvados.commands.keepdocker
16 logger = logging.getLogger('arvados.cwl-runner')
19 cached_lookups_lock = threading.Lock()
21 def arv_docker_get_image(api_client, dockerRequirement, pull_image, project_uuid):
22 """Check if a Docker image is available in Keep, if not, upload it using arv-keepdocker."""
24 if "dockerImageId" not in dockerRequirement and "dockerPull" in dockerRequirement:
25 dockerRequirement = copy.deepcopy(dockerRequirement)
26 dockerRequirement["dockerImageId"] = dockerRequirement["dockerPull"]
27 if hasattr(dockerRequirement, 'lc'):
28 dockerRequirement.lc.data["dockerImageId"] = dockerRequirement.lc.data["dockerPull"]
31 global cached_lookups_lock
32 with cached_lookups_lock:
33 if dockerRequirement["dockerImageId"] in cached_lookups:
34 return dockerRequirement["dockerImageId"]
36 with SourceLine(dockerRequirement, "dockerImageId", WorkflowException):
37 sp = dockerRequirement["dockerImageId"].split(":")
39 image_tag = sp[1] if len(sp) > 1 else "latest"
41 images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3,
42 image_name=image_name,
46 # Fetch Docker image if necessary.
47 cwltool.docker.get_image(dockerRequirement, pull_image)
49 # Upload image to Arvados
52 args.append("--project-uuid="+project_uuid)
53 args.append(image_name)
54 args.append(image_tag)
55 logger.info("Uploading Docker image %s:%s", image_name, image_tag)
57 arvados.commands.keepdocker.main(args, stdout=sys.stderr)
58 except SystemExit as e:
60 raise WorkflowException("keepdocker exited with code %s" % e.code)
62 images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3,
63 image_name=image_name,
67 raise WorkflowException("Could not find Docker image %s:%s" % (image_name, image_tag))
69 with cached_lookups_lock:
70 cached_lookups[dockerRequirement["dockerImageId"]] = True
72 return dockerRequirement["dockerImageId"]
74 def arv_docker_clear_cache():
76 global cached_lookups_lock
77 with cached_lookups_lock: