17417: Merge branch 'main' into 17417-add-arm64
[arvados.git] / sdk / cwl / arvados_cwl / arvdocker.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 import logging
6 import sys
7 import threading
8 import copy
9
10 from schema_salad.sourceline import SourceLine
11
12 import cwltool.docker
13 from cwltool.errors import WorkflowException
14 import arvados.commands.keepdocker
15
16 logger = logging.getLogger('arvados.cwl-runner')
17
18 cached_lookups = {}
19 cached_lookups_lock = threading.Lock()
20
21 def arv_docker_get_image(api_client, dockerRequirement, pull_image, project_uuid,
22                          force_pull, tmp_outdir_prefix):
23     """Check if a Docker image is available in Keep, if not, upload it using arv-keepdocker."""
24
25     if "http://arvados.org/cwl#dockerCollectionPDH" in dockerRequirement:
26         return dockerRequirement["http://arvados.org/cwl#dockerCollectionPDH"]
27
28     if "dockerImageId" not in dockerRequirement and "dockerPull" in dockerRequirement:
29         dockerRequirement = copy.deepcopy(dockerRequirement)
30         dockerRequirement["dockerImageId"] = dockerRequirement["dockerPull"]
31         if hasattr(dockerRequirement, 'lc'):
32             dockerRequirement.lc.data["dockerImageId"] = dockerRequirement.lc.data["dockerPull"]
33
34     global cached_lookups
35     global cached_lookups_lock
36     with cached_lookups_lock:
37         if dockerRequirement["dockerImageId"] in cached_lookups:
38             return cached_lookups[dockerRequirement["dockerImageId"]]
39
40     with SourceLine(dockerRequirement, "dockerImageId", WorkflowException, logger.isEnabledFor(logging.DEBUG)):
41         sp = dockerRequirement["dockerImageId"].split(":")
42         image_name = sp[0]
43         image_tag = sp[1] if len(sp) > 1 else "latest"
44
45         images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3,
46                                                                 image_name=image_name,
47                                                                 image_tag=image_tag)
48
49         if not images:
50             # Fetch Docker image if necessary.
51             try:
52                 result = cwltool.docker.DockerCommandLineJob.get_image(dockerRequirement, pull_image,
53                                                               force_pull, tmp_outdir_prefix)
54                 if not result:
55                     raise WorkflowException("Docker image '%s' not available" % dockerRequirement["dockerImageId"])
56             except OSError as e:
57                 raise WorkflowException("While trying to get Docker image '%s', failed to execute 'docker': %s" % (dockerRequirement["dockerImageId"], e))
58
59             # Upload image to Arvados
60             args = []
61             if project_uuid:
62                 args.append("--project-uuid="+project_uuid)
63             args.append(image_name)
64             args.append(image_tag)
65             logger.info("Uploading Docker image %s:%s", image_name, image_tag)
66             try:
67                 arvados.commands.put.api_client = api_client
68                 arvados.commands.keepdocker.main(args, stdout=sys.stderr, install_sig_handlers=False, api=api_client)
69             except SystemExit as e:
70                 # If e.code is None or zero, then keepdocker exited normally and we can continue
71                 if e.code:
72                     raise WorkflowException("keepdocker exited with code %s" % e.code)
73
74             images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3,
75                                                                     image_name=image_name,
76                                                                     image_tag=image_tag)
77
78         if not images:
79             raise WorkflowException("Could not find Docker image %s:%s" % (image_name, image_tag))
80
81         pdh = api_client.collections().get(uuid=images[0][0]).execute()["portable_data_hash"]
82
83         with cached_lookups_lock:
84             cached_lookups[dockerRequirement["dockerImageId"]] = pdh
85
86     return pdh
87
88 def arv_docker_clear_cache():
89     global cached_lookups
90     global cached_lookups_lock
91     with cached_lookups_lock:
92         cached_lookups = {}