d5295afc23a4d8511569091ef96e94a8c7a6268c
[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 import re
10 import subprocess
11
12 from schema_salad.sourceline import SourceLine
13
14 import cwltool.docker
15 from cwltool.errors import WorkflowException
16 import arvados.commands.keepdocker
17
18 logger = logging.getLogger('arvados.cwl-runner')
19
20 cached_lookups = {}
21 cached_lookups_lock = threading.Lock()
22
23 def determine_image_id(dockerImageId):
24     for line in (
25         subprocess.check_output(  # nosec
26             ["docker", "images", "--no-trunc", "--all"]
27         )
28         .decode("utf-8")
29         .splitlines()
30     ):
31         try:
32             match = re.match(r"^([^ ]+)\s+([^ ]+)\s+([^ ]+)", line)
33             split = dockerImageId.split(":")
34             if len(split) == 1:
35                 split.append("latest")
36             elif len(split) == 2:
37                 #  if split[1] doesn't  match valid tag names, it is a part of repository
38                 if not re.match(r"[\w][\w.-]{0,127}", split[1]):
39                     split[0] = split[0] + ":" + split[1]
40                     split[1] = "latest"
41             elif len(split) == 3:
42                 if re.match(r"[\w][\w.-]{0,127}", split[2]):
43                     split[0] = split[0] + ":" + split[1]
44                     split[1] = split[2]
45                     del split[2]
46
47             # check for repository:tag match or image id match
48             if match and (
49                 (split[0] == match.group(1) and split[1] == match.group(2))
50                 or dockerImageId == match.group(3)
51             ):
52                 return match.group(3)
53         except ValueError:
54             pass
55
56     return None
57
58
59 def arv_docker_get_image(api_client, dockerRequirement, pull_image, project_uuid,
60                          force_pull, tmp_outdir_prefix, match_local_docker):
61     """Check if a Docker image is available in Keep, if not, upload it using arv-keepdocker."""
62
63     if "http://arvados.org/cwl#dockerCollectionPDH" in dockerRequirement:
64         return dockerRequirement["http://arvados.org/cwl#dockerCollectionPDH"]
65
66     if "dockerImageId" not in dockerRequirement and "dockerPull" in dockerRequirement:
67         dockerRequirement = copy.deepcopy(dockerRequirement)
68         dockerRequirement["dockerImageId"] = dockerRequirement["dockerPull"]
69         if hasattr(dockerRequirement, 'lc'):
70             dockerRequirement.lc.data["dockerImageId"] = dockerRequirement.lc.data["dockerPull"]
71
72     global cached_lookups
73     global cached_lookups_lock
74     with cached_lookups_lock:
75         if dockerRequirement["dockerImageId"] in cached_lookups:
76             return cached_lookups[dockerRequirement["dockerImageId"]]
77
78     with SourceLine(dockerRequirement, "dockerImageId", WorkflowException, logger.isEnabledFor(logging.DEBUG)):
79         sp = dockerRequirement["dockerImageId"].split(":")
80         image_name = sp[0]
81         image_tag = sp[1] if len(sp) > 1 else "latest"
82
83         out_of_project_images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3,
84                                                                 image_name=image_name,
85                                                                 image_tag=image_tag,
86                                                                 project_uuid=None)
87
88         images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3,
89                                                                 image_name=image_name,
90                                                                 image_tag=image_tag,
91                                                                 project_uuid=project_uuid)
92
93         if match_local_docker:
94             local_image_id = determine_image_id(dockerRequirement["dockerImageId"])
95             if local_image_id:
96                 # find it in the list
97                 found = False
98                 for i in images:
99                     if i[1]["dockerhash"] == local_image_id:
100                         found = True
101                         images = [i]
102                         break
103                 if not found:
104                     # force re-upload.
105                     images = []
106
107                 for i in out_of_project_images:
108                     if i[1]["dockerhash"] == local_image_id:
109                         found = True
110                         out_of_project_images = [i]
111                         break
112                 if not found:
113                     # force re-upload.
114                     out_of_project_images = []
115
116         if not images:
117             if not out_of_project_images:
118                 # Fetch Docker image if necessary.
119                 try:
120                     result = cwltool.docker.DockerCommandLineJob.get_image(dockerRequirement, pull_image,
121                                                                   force_pull, tmp_outdir_prefix)
122                     if not result:
123                         raise WorkflowException("Docker image '%s' not available" % dockerRequirement["dockerImageId"])
124                 except OSError as e:
125                     raise WorkflowException("While trying to get Docker image '%s', failed to execute 'docker': %s" % (dockerRequirement["dockerImageId"], e))
126
127             # Upload image to Arvados
128             args = []
129             if project_uuid:
130                 args.append("--project-uuid="+project_uuid)
131             args.append(image_name)
132             args.append(image_tag)
133             logger.info("Uploading Docker image %s:%s", image_name, image_tag)
134             try:
135                 arvados.commands.put.api_client = api_client
136                 arvados.commands.keepdocker.main(args, stdout=sys.stderr, install_sig_handlers=False, api=api_client)
137             except SystemExit as e:
138                 # If e.code is None or zero, then keepdocker exited normally and we can continue
139                 if e.code:
140                     raise WorkflowException("keepdocker exited with code %s" % e.code)
141
142             images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3,
143                                                                     image_name=image_name,
144                                                                     image_tag=image_tag,
145                                                                     project_uuid=project_uuid)
146
147         if not images:
148             raise WorkflowException("Could not find Docker image %s:%s" % (image_name, image_tag))
149
150         pdh = api_client.collections().get(uuid=images[0][0]).execute()["portable_data_hash"]
151
152         with cached_lookups_lock:
153             cached_lookups[dockerRequirement["dockerImageId"]] = pdh
154
155     return pdh
156
157 def arv_docker_clear_cache():
158     global cached_lookups
159     global cached_lookups_lock
160     with cached_lookups_lock:
161         cached_lookups = {}