Skip to content

Commit

Permalink
Don't use cachefrom if sources don't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
avirshup committed Nov 3, 2017
1 parent 3862e29 commit bb2c175
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
8 changes: 5 additions & 3 deletions dockermake/step.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def build(self, client, pull=False, usecache=True):
usecache = False

if not usecache:
print(' INFO: Docker caching disabled - forcing rebuild')
cprint(' Build cache disabled - this image will be rebuilt from scratch',
'yellow')

dockerfile = u'\n'.join(self.dockerfile_lines)

Expand All @@ -103,8 +104,8 @@ def build(self, client, pull=False, usecache=True):
nocache=not usecache,
decode=True, rm=True)

if usecache and self.cache_from:
build_args['cache_from'] = self.cache_from
if usecache:
utils.set_build_cachefrom(self.cache_from, build_args, client)

if self.build_dir is not None:
tempdir = self.write_dockerfile(dockerfile)
Expand Down Expand Up @@ -150,6 +151,7 @@ def build(self, client, pull=False, usecache=True):
os.unlink(os.path.join(tempdir, 'Dockerfile'))
os.rmdir(tempdir)


def write_dockerfile(self, dockerfile):
tempdir = os.path.abspath(os.path.join(self.build_dir, DOCKER_TMPDIR))
temp_df = os.path.join(tempdir, 'Dockerfile')
Expand Down
18 changes: 18 additions & 0 deletions dockermake/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ def get_console_width():

SHOWSIZE = set(('Pushing', 'Pulling', 'Pulled', 'Downloaded', 'Downloading'))


def _show_xfer_state(pullstats, item):
imgid = item['id']
stat = item['status']
Expand All @@ -232,3 +233,20 @@ def _show_xfer_state(pullstats, item):
return toprint
else:
return None


def set_build_cachefrom(cache_from, buildargs, client):
if cache_from: # use cachefrom only if at least one of the images exists
for image in cache_from:
try:
client.images.get(image)
except docker.errors.ImageNotFound:
pass
else:
cprint(" Build cache sources: %s" % cache_from,
'blue')
buildargs['cache_from'] = cache_from
return
else:
cprint(" No build cache sources present; ignoring --cache-repo and --cache-tag",
'blue')
14 changes: 12 additions & 2 deletions test/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ def test_no_cache(twin_simple_targets):
image1, image2 = twin_simple_targets
assert image1.id != image2.id


def test_explicit_cache_from(twin_simple_targets, docker_client):
clean8 = creates_images('img1repo/simple-target:img1tag',
'img2repo/simple-target:img2tag')
def test_explicit_cache_from(twin_simple_targets, docker_client, clean8):
image1, image2 = twin_simple_targets
image1.tag('img1repo/simple-target', tag='img1tag')
image2.tag('img2repo/simple-target', tag='img2tag')
Expand All @@ -88,6 +89,15 @@ def test_explicit_cache_from(twin_simple_targets, docker_client):
assert final_image.id == image1.id


def test_cache_fallback(twin_simple_targets, docker_client):
image1, image2 = twin_simple_targets

run_docker_make('-f data/simple.yml simple-target'
' --cache-repo fakerepo --cache-tag faketag')
final_image = docker_client.images.get('simple-target')
assert final_image.id == image2.id


def _check_files(img, **present):
for f, record in _FILES.items():
if not present.get(f, True):
Expand Down

0 comments on commit bb2c175

Please sign in to comment.