16306: Move nginx temp dirs into a subdir.
[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     """Check if a Docker image is available in Keep, if not, upload it using arv-keepdocker."""
23
24     if "http://arvados.org/cwl#dockerCollectionPDH" in dockerRequirement:
25         return dockerRequirement["http://arvados.org/cwl#dockerCollectionPDH"]
26
27     if "dockerImageId" not in dockerRequirement and "dockerPull" in dockerRequirement:
28         dockerRequirement = copy.deepcopy(dockerRequirement)
29         dockerRequirement["dockerImageId"] = dockerRequirement["dockerPull"]
30         if hasattr(dockerRequirement, 'lc'):
31             dockerRequirement.lc.data["dockerImageId"] = dockerRequirement.lc.data["dockerPull"]
32
33     global cached_lookups
34     global cached_lookups_lock
35     with cached_lookups_lock:
36         if dockerRequirement["dockerImageId"] in cached_lookups:
37             return cached_lookups[dockerRequirement["dockerImageId"]]
38
39     with SourceLine(dockerRequirement, "dockerImageId", WorkflowException, logger.isEnabledFor(logging.DEBUG)):
40         sp = dockerRequirement["dockerImageId"].split(":")
41         image_name = sp[0]
42         image_tag = sp[1] if len(sp) > 1 else "latest"
43
44         images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3,
45                                                                 image_name=image_name,
46                                                                 image_tag=image_tag)
47
48         if not images:
49             # Fetch Docker image if necessary.
50             try:
51                 cwltool.docker.DockerCommandLineJob.get_image(dockerRequirement, pull_image)
52             except OSError as e:
53                 raise WorkflowException("While trying to get Docker image '%s', failed to execute 'docker': %s" % (dockerRequirement["dockerImageId"], e))
54
55             # Upload image to Arvados
56             args = []
57             if project_uuid:
58                 args.append("--project-uuid="+project_uuid)
59             args.append(image_name)
60             args.append(image_tag)
61             logger.info("Uploading Docker image %s:%s", image_name, image_tag)
62             try:
63                 arvados.commands.put.api_client = api_client
64                 arvados.commands.keepdocker.main(args, stdout=sys.stderr, install_sig_handlers=False, api=api_client)
65             except SystemExit as e:
66                 # If e.code is None or zero, then keepdocker exited normally and we can continue
67                 if e.code:
68                     raise WorkflowException("keepdocker exited with code %s" % e.code)
69
70             images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3,
71                                                                     image_name=image_name,
72                                                                     image_tag=image_tag)
73
74         if not images:
75             raise WorkflowException("Could not find Docker image %s:%s" % (image_name, image_tag))
76
77         pdh = api_client.collections().get(uuid=images[0][0]).execute()["portable_data_hash"]
78
79         with cached_lookups_lock:
80             cached_lookups[dockerRequirement["dockerImageId"]] = pdh
81
82     return pdh
83
84 def arv_docker_clear_cache():
85     global cached_lookups
86     global cached_lookups_lock
87     with cached_lookups_lock:
88         cached_lookups = {}