Merge branch '9831-faster-unique-name'
[arvados.git] / sdk / cwl / arvados_cwl / arvdocker.py
1 import logging
2 import sys
3 import threading
4
5 from schema_salad.sourceline import SourceLine
6
7 import cwltool.docker
8 from cwltool.errors import WorkflowException
9 import arvados.commands.keepdocker
10
11 logger = logging.getLogger('arvados.cwl-runner')
12
13 cached_lookups = {}
14 cached_lookups_lock = threading.Lock()
15
16 def arv_docker_get_image(api_client, dockerRequirement, pull_image, project_uuid):
17     """Check if a Docker image is available in Keep, if not, upload it using arv-keepdocker."""
18
19     if "dockerImageId" not in dockerRequirement and "dockerPull" in dockerRequirement:
20         dockerRequirement["dockerImageId"] = dockerRequirement["dockerPull"]
21         if hasattr(dockerRequirement, 'lc'):
22             dockerRequirement.lc.data["dockerImageId"] = dockerRequirement.lc.data["dockerPull"]
23
24     global cached_lookups
25     global cached_lookups_lock
26     with cached_lookups_lock:
27         if dockerRequirement["dockerImageId"] in cached_lookups:
28             return cached_lookups[dockerRequirement["dockerImageId"]]
29
30     with SourceLine(dockerRequirement, "dockerImageId", WorkflowException):
31         sp = dockerRequirement["dockerImageId"].split(":")
32         image_name = sp[0]
33         image_tag = sp[1] if len(sp) > 1 else None
34
35         images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3,
36                                                                 image_name=image_name,
37                                                                 image_tag=image_tag)
38
39         if not images:
40             # Fetch Docker image if necessary.
41             cwltool.docker.get_image(dockerRequirement, pull_image)
42
43             # Upload image to Arvados
44             args = []
45             if project_uuid:
46                 args.append("--project-uuid="+project_uuid)
47             args.append(image_name)
48             if image_tag:
49                 args.append(image_tag)
50             logger.info("Uploading Docker image %s", ":".join(args[1:]))
51             try:
52                 arvados.commands.keepdocker.main(args, stdout=sys.stderr)
53             except SystemExit as e:
54                 if e.code:
55                     raise WorkflowException("keepdocker exited with code %s" % e.code)
56
57             images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3,
58                                                                     image_name=image_name,
59                                                                     image_tag=image_tag)
60
61         if not images:
62             raise WorkflowException("Could not find Docker image %s:%s" % (image_name, image_tag))
63
64         pdh = api_client.collections().get(uuid=images[0][0]).execute()["portable_data_hash"]
65
66         with cached_lookups_lock:
67             cached_lookups[dockerRequirement["dockerImageId"]] = pdh
68
69         return pdh
70
71 def arv_docker_clear_cache():
72     global cached_lookups
73     global cached_lookups_lock
74     with cached_lookups_lock:
75         cached_lookups = {}