Merge branch '13627-set-work-api' refs #13627
[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 "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"]
29
30     global cached_lookups
31     global cached_lookups_lock
32     with cached_lookups_lock:
33         if dockerRequirement["dockerImageId"] in cached_lookups:
34             return dockerRequirement["dockerImageId"]
35
36     with SourceLine(dockerRequirement, "dockerImageId", WorkflowException, logger.isEnabledFor(logging.DEBUG)):
37         sp = dockerRequirement["dockerImageId"].split(":")
38         image_name = sp[0]
39         image_tag = sp[1] if len(sp) > 1 else "latest"
40
41         images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3,
42                                                                 image_name=image_name,
43                                                                 image_tag=image_tag)
44
45         if not images:
46             # Fetch Docker image if necessary.
47             try:
48                 cwltool.docker.DockerCommandLineJob.get_image(dockerRequirement, pull_image)
49             except OSError as e:
50                 raise WorkflowException("While trying to get Docker image '%s', failed to execute 'docker': %s" % (dockerRequirement["dockerImageId"], e))
51
52             # Upload image to Arvados
53             args = []
54             if project_uuid:
55                 args.append("--project-uuid="+project_uuid)
56             args.append(image_name)
57             args.append(image_tag)
58             logger.info("Uploading Docker image %s:%s", image_name, image_tag)
59             try:
60                 arvados.commands.put.api_client = api_client
61                 arvados.commands.keepdocker.main(args, stdout=sys.stderr, install_sig_handlers=False, api=api_client)
62             except SystemExit as e:
63                 if e.code:
64                     raise WorkflowException("keepdocker exited with code %s" % e.code)
65
66             images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3,
67                                                                     image_name=image_name,
68                                                                     image_tag=image_tag)
69
70         if not images:
71             raise WorkflowException("Could not find Docker image %s:%s" % (image_name, image_tag))
72
73         with cached_lookups_lock:
74             cached_lookups[dockerRequirement["dockerImageId"]] = True
75
76     return dockerRequirement["dockerImageId"]
77
78 def arv_docker_clear_cache():
79     global cached_lookups
80     global cached_lookups_lock
81     with cached_lookups_lock:
82         cached_lookups = {}