docker: rebuild image if 'extra files' checksum does not match

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
This commit is contained in:
Philippe Mathieu-Daudé 2017-06-02 15:56:09 -03:00 committed by Alex Bennée
parent 4c84f662c2
commit 438d116872
1 changed files with 13 additions and 3 deletions

View File

@ -38,6 +38,9 @@ def _text_checksum(text):
"""Calculate a digest string unique to the text content"""
return hashlib.sha1(text).hexdigest()
def _file_checksum(filename):
return _text_checksum(open(filename, 'rb').read())
def _guess_docker_command():
""" Guess a working docker command or raise exception if not found"""
commands = [["docker"], ["sudo", "-n", "docker"]]
@ -154,7 +157,7 @@ class Docker(object):
return labels.get("com.qemu.dockerfile-checksum", "")
def build_image(self, tag, docker_dir, dockerfile,
quiet=True, user=False, argv=None):
quiet=True, user=False, argv=None, extra_files_cksum=[]):
if argv == None:
argv = []
@ -170,7 +173,8 @@ class Docker(object):
tmp_df.write("\n")
tmp_df.write("LABEL com.qemu.dockerfile-checksum=%s" %
_text_checksum(dockerfile))
_text_checksum("\n".join([dockerfile] +
extra_files_cksum)))
tmp_df.flush()
self._do(["build", "-t", tag, "-f", tmp_df.name] + argv + \
@ -276,16 +280,22 @@ class BuildCommand(SubCommand):
# Copy any extra files into the Docker context. These can be
# included by the use of the ADD directive in the Dockerfile.
cksum = []
if args.include_executable:
# FIXME: there is no checksum of this executable and the linked
# libraries, once the image built any change of this executable
# or any library won't trigger another build.
_copy_binary_with_libs(args.include_executable, docker_dir)
for filename in args.extra_files or []:
_copy_with_mkdir(filename, docker_dir)
cksum += [_file_checksum(filename)]
argv += ["--build-arg=" + k.lower() + "=" + v
for k, v in os.environ.iteritems()
if k.lower() in FILTERED_ENV_NAMES]
dkr.build_image(tag, docker_dir, dockerfile,
quiet=args.quiet, user=args.user, argv=argv)
quiet=args.quiet, user=args.user, argv=argv,
extra_files_cksum=cksum)
rmtree(docker_dir)