Skip to content

Commit 4e54af0

Browse files
author
Peter Amstutz
committed
Restore --force-docker-pull. Add caching of found docker images.
1 parent 4c74d06 commit 4e54af0

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

cwltool/docker.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import datetime
1313
import requests
1414
from typing import (Dict, List, Text, Any, MutableMapping)
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()
27+
found_images_lock = threading.Lock()
2528

2629
class DockerCommandLineJob(ContainerCommandLineJob):
2730

2831
@staticmethod
29-
def get_image(dockerRequirement, pull_image, dry_run=False):
32+
def get_image(dockerRequirement, pull_image, dry_run=False, force_pull=False):
3033
# type: (Dict[Text, Text], 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,9 +113,13 @@ 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):
122+
def get_from_requirements(self, r, req, pull_image, dry_run=False, force_pull=False):
112123
# type: (Dict[Text, Text], bool, bool, bool) -> Text
113124
if r:
114125
errmsg = None
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121

2222
class SingularityCommandLineJob(ContainerCommandLineJob):
2323
@staticmethod
24-
def get_image(dockerRequirement, pull_image, dry_run=False):
24+
def get_image(dockerRequirement, pull_image, dry_run=False, force_pull=False):
2525
# type: (Dict[Text, Text], bool, bool) -> bool
2626
found = False
2727

28+
if force_pull:
29+
_logger.warn("--force-docker-pull currently not supported for singularity")
30+
2831
if "dockerImageId" not in dockerRequirement and "dockerPull" in dockerRequirement:
2932
match = re.search(pattern=r'([a-z]*://)', string=dockerRequirement["dockerPull"])
3033
if match:

0 commit comments

Comments
 (0)