10383: Now --update-collection is not mutually exclusive with --resume and --no-resume.
[arvados.git] / sdk / cwl / arvados_cwl / arvdocker.py
1 import logging
2 import sys
3
4 import cwltool.docker
5 from cwltool.errors import WorkflowException
6 import arvados.commands.keepdocker
7
8
9 logger = logging.getLogger('arvados.cwl-runner')
10
11 def arv_docker_get_image(api_client, dockerRequirement, pull_image, project_uuid):
12     """Check if a Docker image is available in Keep, if not, upload it using arv-keepdocker."""
13
14     if "dockerImageId" not in dockerRequirement and "dockerPull" in dockerRequirement:
15         dockerRequirement["dockerImageId"] = dockerRequirement["dockerPull"]
16
17     sp = dockerRequirement["dockerImageId"].split(":")
18     image_name = sp[0]
19     image_tag = sp[1] if len(sp) > 1 else None
20
21     images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3,
22                                                             image_name=image_name,
23                                                             image_tag=image_tag)
24
25     if not images:
26         # Fetch Docker image if necessary.
27         cwltool.docker.get_image(dockerRequirement, pull_image)
28
29         # Upload image to Arvados
30         args = ["--project-uuid="+project_uuid, image_name]
31         if image_tag:
32             args.append(image_tag)
33         logger.info("Uploading Docker image %s", ":".join(args[1:]))
34         try:
35             arvados.commands.keepdocker.main(args, stdout=sys.stderr)
36         except SystemExit:
37             raise WorkflowException()
38
39         images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3,
40                                                                 image_name=image_name,
41                                                                 image_tag=image_tag)
42
43     if not images:
44         raise WorkflowException("Could not find Docker image %s:%s" % (image_name, image_tag))
45
46     pdh = api_client.collections().get(uuid=images[0][0]).execute()["portable_data_hash"]
47     return pdh