Skip to content

Commit

Permalink
build: use docker_squash lib, not docker-squash script
Browse files Browse the repository at this point in the history
The docker-squash script used to print the squashed <image id> on
stderr, but recently it was changed so the image id is printed to
stdout.  To be able to parse image id from both docker_squash
versions (before and after this change), we'd have to have some
ugly if-fork to detect the docker_squash version (or play with FD
redirection).

So rather let's do as upstream suggested in [1] and use the
docker_squash library directly.

Also add a simple CI test for the ./squash.py file.

[1] goldmann/docker-squash#145

Fixes: sclorg#101
  • Loading branch information
praiskup committed Dec 5, 2018
1 parent 452fee3 commit a4672a4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ test: check

check-failures: check-test-lib
cd tests/failures/check && make tag && ! make check && make clean
cd tests/failures/check && make tag SKIP_SQUASH=0

check: check-failures
check-squash:
./tests/squash/squash.sh

check: check-failures check-squash
TESTED_IMAGES="$(TESTED_IMAGES)" tests/remote-containers.sh
16 changes: 9 additions & 7 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

set -e

script_name=$(readlink -f "$0")
script_dir=$(dirname "$script_name")

OS=${1-$OS}
VERSION=${2-$VERSION}

Expand Down Expand Up @@ -111,13 +114,13 @@ function docker_build_with_version {
clean_image
echo "$IMAGE_ID" > .image-id.raw

squash "${dockerfile}" "$IMAGE_ID"
squash "${dockerfile}"
echo "$IMAGE_ID" > .image-id
}

# squash DOCKERFILE
# -----------------
# Install the docker squashing tool[1] and squash the result image
# Use python library docker_squash[1] and squash the result image
# when necessary.
# [1] https://github.com/goldmann/docker-squash
# Reads:
Expand All @@ -129,8 +132,6 @@ squash ()
local base squashed_from squashed= unsquashed=$IMAGE_ID
test "$SKIP_SQUASH" = 1 && return 0

docker-squash --help &>/dev/null || error "docker-squash is required"

if test -f .image-id.squashed; then
squashed=$(cat .image-id.squashed)
# We (maybe) already have squashed file.
Expand All @@ -139,6 +140,7 @@ squash ()
if test "$squashed_from" = "$IMAGE_ID"; then
# $squashed is up2date
IMAGE_ID=$squashed
echo "Image '$unsquashed' already squashed as '$squashed'"
return 0
fi
fi
Expand All @@ -149,9 +151,9 @@ squash ()
fi

base=$(awk '/^FROM/{print $2}' "$1")
parse_output "docker-squash -f '$base' '$unsquashed'" \
"awk '/New squashed image ID is/{print \$NF}'" \
IMAGE_ID stderr

echo "Squashing the image '$unsquashed' from '$base' layer"
IMAGE_ID=$("${PYTHON-python3}" "$script_dir"/squash.py "$unsquashed" "$base")

echo "$unsquashed" > .image-id.squashed_from
echo "$IMAGE_ID" > .image-id.squashed
Expand Down
23 changes: 23 additions & 0 deletions squash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#! /bin/python

import sys
import logging
from platform import python_version

try:
from docker_squash import squash
except ImportError:
logging.fatal("please install 'docker_squash' for Python {0}".format(python_version()))
sys.exit(1)


if __name__ == "__main__":
if len(sys.argv) != 3:
logging.fatal("%s: %s", sys.argv[0], msg)
sys.exit(1)

print(squash.Squash(
log=logging.getLogger(),
image=sys.argv[1],
from_layer=sys.argv[2]).run()
)
1 change: 1 addition & 0 deletions tests/squash/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dockerfile
18 changes: 18 additions & 0 deletions tests/squash/squash.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#! /bin/sh

set -e

origin=busybox
squash=$(dirname "$(readlink -f "$0")")/../../squash.py
cd "$(dirname "$0")"

cat > Dockerfile <<EOF
FROM $origin
ENV test=test
CMD /bin/echo test
EOF
out=`docker build . | awk '/Successfully built/{print $NF}'`
echo "$out"
squashed=$("${PYTHON-python3}" "$squash" "$out" "$origin")
output=$(docker run --rm $squashed)
test "$output" = "test"

0 comments on commit a4672a4

Please sign in to comment.