Skip to content

Commit a0ef554

Browse files
authored
Merge pull request #707 from common-workflow-language/fix-force-docker-pull
Restore --force-docker-pull. Add caching of found docker images.
2 parents 0c8e2ee + a19877e commit a0ef554

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

cwltool/docker.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
import datetime
1313
import requests
14-
from typing import (Dict, List, Text, Any, MutableMapping)
14+
from typing import (Dict, List, Text, Any, MutableMapping, Set)
15+
import threading
1516

1617
from .docker_id import docker_vm_id
1718
from .errors import WorkflowException
@@ -22,17 +23,23 @@
2223

2324
_logger = logging.getLogger("cwltool")
2425

26+
found_images = set() # type: Set[Text]
27+
found_images_lock = threading.Lock()
2528

2629
class DockerCommandLineJob(ContainerCommandLineJob):
2730

2831
@staticmethod
29-
def get_image(dockerRequirement, pull_image, dry_run=False):
30-
# type: (Dict[Text, Text], bool, bool) -> bool
32+
def get_image(dockerRequirement, pull_image, dry_run=False, force_pull=False):
33+
# type: (Dict[Text, Text], bool, bool, bool) -> bool
3134
found = False
3235

3336
if "dockerImageId" not in dockerRequirement and "dockerPull" in dockerRequirement:
3437
dockerRequirement["dockerImageId"] = dockerRequirement["dockerPull"]
3538

39+
with found_images_lock:
40+
if dockerRequirement["dockerImageId"] in found_images:
41+
return True
42+
3643
for ln in subprocess.check_output(
3744
["docker", "images", "--no-trunc", "--all"]).decode('utf-8').splitlines():
3845
try:
@@ -58,7 +65,7 @@ def get_image(dockerRequirement, pull_image, dry_run=False):
5865
except ValueError:
5966
pass
6067

61-
if not found and pull_image:
68+
if (force_pull or not found) and pull_image:
6269
cmd = [] # type: List[Text]
6370
if "dockerPull" in dockerRequirement:
6471
cmd = ["docker", "pull", str(dockerRequirement["dockerPull"])]
@@ -106,10 +113,14 @@ def get_image(dockerRequirement, pull_image, dry_run=False):
106113
subprocess.check_call(cmd, stdout=sys.stderr)
107114
found = True
108115

116+
if found:
117+
with found_images_lock:
118+
found_images.add(dockerRequirement["dockerImageId"])
119+
109120
return found
110121

111-
def get_from_requirements(self, r, req, pull_image, dry_run=False):
112-
# type: (Dict[Text, Text], bool, bool, bool) -> Text
122+
def get_from_requirements(self, r, req, pull_image, dry_run=False, force_pull=False):
123+
# type: (Dict[Text, Text], bool, bool, bool, bool) -> Text
113124
if r:
114125
errmsg = None
115126
try:
@@ -125,7 +136,7 @@ def get_from_requirements(self, r, req, pull_image, dry_run=False):
125136
else:
126137
return None
127138

128-
if self.get_image(r, pull_image, dry_run):
139+
if self.get_image(r, pull_image, dry_run, force_pull=force_pull):
129140
return r["dockerImageId"]
130141
else:
131142
if req:

cwltool/job.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ class ContainerCommandLineJob(JobBase):
337337
__metaclass__ = ABCMeta
338338

339339
@abstractmethod
340-
def get_from_requirements(self, r, req, pull_image, dry_run=False):
341-
# type: (Dict[Text, Text], bool, bool, bool) -> Text
340+
def get_from_requirements(self, r, req, pull_image, dry_run=False, force_pull=False):
341+
# type: (Dict[Text, Text], bool, bool, bool, bool) -> Text
342342
pass
343343

344344
@abstractmethod
@@ -373,7 +373,7 @@ def run(self, pull_image=True, rm_container=True,
373373
try:
374374
env = cast(MutableMapping[Text, Text], os.environ)
375375
if docker_req and kwargs.get("use_container"):
376-
img_id = str(self.get_from_requirements(docker_req, True, pull_image))
376+
img_id = str(self.get_from_requirements(docker_req, True, pull_image, force_pull=kwargs.get("force_docker_pull")))
377377
if img_id is None:
378378
if self.builder.find_default_container:
379379
default_container = self.builder.find_default_container()

cwltool/singularity.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,13 @@ def get_image(dockerRequirement, pull_image, dry_run=False):
6969

7070
return found
7171

72-
def get_from_requirements(self, r, req, pull_image, dry_run=False):
73-
# type: (Dict[Text, Text], bool, bool, bool) -> Text
72+
def get_from_requirements(self, r, req, pull_image, dry_run=False, force_pull=False):
73+
# type: (Dict[Text, Text], bool, bool, bool, bool) -> Text
7474
# returns the filename of the Singularity image (e.g. hello-world-latest.img)
75+
76+
if force_pull:
77+
_logger.warn("--force-docker-pull currently not supported for singularity")
78+
7579
if r:
7680
errmsg = None
7781
try:

0 commit comments

Comments
 (0)