Docker tweaks for TCG testing
cover Docker tweaks for TCG testing
Hi Fam,
These are the docker specific changes from my current TCG testing branch:
https://github.com/stsquad/qemu/tree/testing/tcg-tests-revival-v8
Once the CI tests have worked their way through I'm hoping to submit the pull-req to get this merged. However I've been hacking about in docker.py and rather than sending the whole 50+ series I've split them out for easy review. The ones that currently need review are:
: patch 0005/docker docker.py use ps to probe usage.patch : patch 0006/docker docker.py don t conflate checksums for ext.patch : patch 0007/docker docker.py add check sub command.patch : patch 0008/docker docker.py adding age check command.patch
If you get a chance could you have a quick look through?
Alex Bennée (8):
docker: Add "cc" subcommand
docker: extend "cc" command to accept compiler
docker: allow "cc" command to run in user context
docker: Makefile.include introduce DOCKER_SCRIPT
docker: docker.py use "ps" to probe usage
docker: docker.py don't conflate checksums for extra_files
docker: docker.py add check sub-command
docker: docker.py adding age check command
tests/docker/Makefile.include | 10 ++-- tests/docker/docker.py | 105 ++++++++++++++++++++++++++++++++-- 2 files changed, 106 insertions(+), 9 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/8 docker: Add "cc" subcommand
---
v2
- add if args.paths check to avoid iterating null argument
--- tests/docker/docker.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
tests/docker/docker.py +25 -0
@@ -409,6 +409,31 @@ class ProbeCommand(SubCommand): return +class CcCommand(SubCommand): + """Compile sources with cc in images""" + name = "cc" + + def args(self, parser): + parser.add_argument("--image", "-i", required=True, + help="The docker image in which to run cc") + parser.add_argument("--source-path", "-s", nargs="*", dest="paths", + help="""Extra paths to (ro) mount into container for + reading sources""") + + def run(self, args, argv): + if argv and argv[0] == "--": + argv = argv[1:] + cwd = os.getcwd() + cmd = ["--rm", "-w", cwd, + "-v", "%s:%s:rw" % (cwd, cwd)] + if args.paths: + for p in args.paths: + cmd += ["-v", "%s:%s:ro,z" % (p, p)] + cmd += [args.image, "cc"] + cmd += argv + return Docker().command("run", cmd, args.quiet) + + def main(): parser = argparse.ArgumentParser(description="A Docker helper", usage="%s <subcommand> ..." % os.path.basename(sys.argv[0]))
Signature · 1 line
2.17.1
PATCH 2/8 docker: extend "cc" command to accept compiler
In reply to Alex BennéeWhen calling our cross-compilation images we want to call something other than the default cc.
---
v2
- use arg.cc default to simplify logic
--- tests/docker/docker.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
tests/docker/docker.py +3 -1
@@ -416,6 +416,8 @@ class CcCommand(SubCommand): def args(self, parser): parser.add_argument("--image", "-i", required=True, help="The docker image in which to run cc") + parser.add_argument("--cc", default="cc", + help="The compiler executable to call") parser.add_argument("--source-path", "-s", nargs="*", dest="paths", help="""Extra paths to (ro) mount into container for reading sources""")
@@ -429,7 +431,7 @@ class CcCommand(SubCommand): if args.paths: for p in args.paths: cmd += ["-v", "%s:%s:ro,z" % (p, p)] - cmd += [args.image, "cc"] + cmd += [args.image, args.cc] cmd += argv return Docker().command("run", cmd, args.quiet)
Signature · 1 line
2.17.1
PATCH 3/8 docker: allow "cc" command to run in user context
In reply to Alex Bennée--- tests/docker/docker.py | 4 ++++ 1 file changed, 4 insertions(+)
tests/docker/docker.py +4 -0
@@ -418,6 +418,8 @@ class CcCommand(SubCommand): help="The docker image in which to run cc") parser.add_argument("--cc", default="cc", help="The compiler executable to call") + parser.add_argument("--user", + help="The user-id to run under") parser.add_argument("--source-path", "-s", nargs="*", dest="paths", help="""Extra paths to (ro) mount into container for reading sources""")
@@ -431,6 +433,8 @@ class CcCommand(SubCommand): if args.paths: for p in args.paths: cmd += ["-v", "%s:%s:ro,z" % (p, p)] + if args.user: + cmd += ["-u", args.user] cmd += [args.image, args.cc] cmd += argv return Docker().command("run", cmd, args.quiet)
Signature · 1 line
2.17.1
PATCH 4/8 docker: Makefile.include introduce DOCKER_SCRIPT
In reply to Alex BennéeDefine this in one place to make it easy to re-use.
--- tests/docker/Makefile.include | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
tests/docker/Makefile.include +6 -4
@@ -15,6 +15,8 @@ DOCKER_TESTS := $(notdir $(shell \ DOCKER_TOOLS := travis +DOCKER_SCRIPT=$(SRC_PATH)/tests/docker/docker.py + TESTS ?= % IMAGES ?= %
@@ -38,7 +40,7 @@ docker-image-%: $(DOCKER_FILES_DIR)/%.docker echo WARNING: EXECUTABLE is not set, debootstrap may fail. 2>&1 ; \ fi $(call quiet-command,\ - $(SRC_PATH)/tests/docker/docker.py build qemu:$* $< \ + $(DOCKER_SCRIPT) build qemu:$* $< \ $(if $V,,--quiet) $(if $(NOCACHE),--no-cache) \ $(if $(NOUSER),,--add-current-user) \ $(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES))\
@@ -133,11 +135,11 @@ docker-run: docker-qemu-src fi $(if $(EXECUTABLE), \ $(call quiet-command, \ - $(SRC_PATH)/tests/docker/docker.py update \ + $(DOCKER_SCRIPT) update \ $(IMAGE) $(EXECUTABLE), \ " COPYING $(EXECUTABLE) to $(IMAGE)")) $(call quiet-command, \ - $(SRC_PATH)/tests/docker/docker.py run \ + $(DOCKER_SCRIPT) run \ $(if $(NOUSER),,-u $(shell id -u)) \ --security-opt seccomp=unconfined \ $(if $V,,--rm) \
@@ -167,4 +169,4 @@ docker-run-%: @$(MAKE) docker-run TEST=$(CMD) IMAGE=qemu:$(IMAGE) docker-clean: - $(call quiet-command, $(SRC_PATH)/tests/docker/docker.py clean) + $(call quiet-command, $(DOCKER_SCRIPT) clean)
Signature · 1 line
2.17.1
PATCH 5/8 docker: docker.py use "ps" to probe usage
In reply to Alex BennéeThe "images" command is a fairly heavyweight command to run as it involves searching the whole docker file-system inventory. On a machine with a lot of images this makes start-up fairly expensive.
--- tests/docker/docker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tests/docker/docker.py +1 -1
@@ -49,7 +49,7 @@ def _guess_docker_command(): commands = [["docker"], ["sudo", "-n", "docker"]] for cmd in commands: try: - if subprocess.call(cmd + ["images"], + if subprocess.call(cmd + ["ps"], stdout=DEVNULL, stderr=DEVNULL) == 0: return cmd except OSError:
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH v1 5/8] docker: docker.py use "ps" to probe usage
Quoted 3 lines · Alex Bennée
The "images" command is a fairly heavyweight command to run as it involves searching the whole docker file-system inventory. On a machine with a lot of images this makes start-up fairly expensive.
"version" would be even quicker.
Quoted 2 lines
Signed-off-by Alex Bennée <[email protected]>
Quoted 18 lines
--- tests/docker/docker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tests/docker/docker.py +1
-1@@ -49,7 +49,7 @@ def _guess_docker_command(): commands = [["docker"], ["sudo", "-n", "docker"]] for cmd in commands: try: - if subprocess.call(cmd + ["images"], + if subprocess.call(cmd + ["ps"], stdout=DEVNULL, stderr=DEVNULL) == 0: return cmd except OSError:
PATCH 6/8 docker: docker.py don't conflate checksums for extra_files
In reply to Alex BennéeThis just gets confusing especially as the helper function doesn't even take into account any extra files (or the executable). Currently the actual check just ignores them and also passes the result through _dockerfile_preprocess so we fix that too.
--- tests/docker/docker.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
tests/docker/docker.py +5 -3
@@ -201,8 +201,10 @@ class Docker(object): tmp_df.write("\n") tmp_df.write("LABEL com.qemu.dockerfile-checksum=%s" % - _text_checksum("\n".join([dockerfile] + - extra_files_cksum))) + _text_checksum(_dockerfile_preprocess(dockerfile))) + for f, c in extra_files_cksum: + tmp_df.write("LABEL com.qemu.%s-checksum=%s" % (f, c)) + tmp_df.flush() self._do_check(["build", "-t", tag, "-f", tmp_df.name] + argv + \
@@ -317,7 +319,7 @@ class BuildCommand(SubCommand): _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)] + cksum += [(filename, _file_checksum(filename))] argv += ["--build-arg=" + k.lower() + "=" + v for k, v in os.environ.iteritems()
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH v1 6/8] docker: docker.py don't conflate checksums for extra_files
Quoted 4 lines · Alex Bennée
This just gets confusing especially as the helper function doesn't even take into account any extra files (or the executable). Currently the actual check just ignores them and also passes the result through
_dockerfile_preprocessso we fix that too.
Looks correct.
Quoted 3 lines
Signed-off-by Alex Bennée <[email protected]>
Quoted 31 lines
--- tests/docker/docker.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)tests/docker/docker.py +5
-3@@ -201,8 +201,10 @@ class Docker(object): tmp_df.write("\n") tmp_df.write("LABEL com.qemu.dockerfile-checksum=%s" % - _text_checksum("\n".join([dockerfile] + - extra_files_cksum))) + _text_checksum(_dockerfile_preprocess(dockerfile))) + for f, c in extra_files_cksum: + tmp_df.write("LABEL com.qemu.%s-checksum=%s" % (f, c)) + tmp_df.flush() self._do_check(["build", "-t", tag, "-f", tmp_df.name] + argv + \@@ -317,7 +319,7 @@ class BuildCommand(SubCommand): _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)] + cksum += [(filename, _file_checksum(filename))] argv += ["--build-arg=" + k.lower() + "=" + v for k, v in os.environ.iteritems()
PATCH 7/8 docker: docker.py add check sub-command
In reply to Alex BennéeThis command allows you to check if we need to re-build a docker image. If the image isn't in the repository or the checksums don't match then we return false and some text (for processing in makefiles).
---
- fix prints to be "modern"
- PEP8 fixes
- split inspect into function
--- tests/docker/docker.py | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-)
tests/docker/docker.py +37 -1
@@ -179,8 +179,14 @@ class Docker(object): stderr=subprocess.STDOUT, **kwargs) + def inspect_tag(self, tag): + try: + return self._output(["inspect", tag]) + except subprocess.CalledProcessError: + return None + def get_image_dockerfile_checksum(self, tag): - resp = self._output(["inspect", tag]) + resp = self.inspect_tag(tag) labels = json.loads(resp)[0]["Config"].get("Labels", {}) return labels.get("com.qemu.dockerfile-checksum", "")
@@ -442,6 +448,36 @@ class CcCommand(SubCommand): return Docker().command("run", cmd, args.quiet) +class CheckCommand(SubCommand): + """Check if we need to re-build a docker image out of a dockerfile. + Arguments: <tag> <dockerfile>""" + name = "check" + + def args(self, parser): + parser.add_argument("tag", + help="Image Tag") + parser.add_argument("dockerfile", + help="Dockerfile name") + + def run(self, args, argv): + dockerfile = open(args.dockerfile, "rb").read() + tag = args.tag + + dkr = Docker() + info = dkr.inspect_tag(tag) + if info is None: + print("Image does not exist") + return 1 + + if dkr.image_matches_dockerfile(tag, dockerfile): + if not args.quiet: + print("Image is up to date") + return 0 + else: + print("Image needs updating") + return 1 + + def main(): parser = argparse.ArgumentParser(description="A Docker helper", usage="%s <subcommand> ..." % os.path.basename(sys.argv[0]))
Signature · 1 line
2.17.1
PATCH 8/8 docker: docker.py adding age check command
In reply to Alex BennéeThis is useful for querying if an image is too old.
---
v7
- use cached info
--- tests/docker/docker.py | 46 +++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 10 deletions(-)
tests/docker/docker.py +36 -10
@@ -29,6 +29,7 @@ from tarfile import TarFile, TarInfo from StringIO import StringIO from shutil import copy, rmtree from pwd import getpwuid +from datetime import datetime,timedelta FILTERED_ENV_NAMES = ['ftp_proxy', 'http_proxy', 'https_proxy']
@@ -185,6 +186,9 @@ class Docker(object): except subprocess.CalledProcessError: return None + def get_image_creation_time(self, info): + return json.loads(info)[0]["Created"] + def get_image_dockerfile_checksum(self, tag): resp = self.inspect_tag(tag) labels = json.loads(resp)[0]["Config"].get("Labels", {})
@@ -456,11 +460,14 @@ class CheckCommand(SubCommand): def args(self, parser): parser.add_argument("tag", help="Image Tag") - parser.add_argument("dockerfile", - help="Dockerfile name") + parser.add_argument("dockerfile", default=None, + help="Dockerfile name", nargs='?') + parser.add_argument("--checktype", choices=["checksum", "age"], + default="checksum", help="check type") + parser.add_argument("--olderthan", default=60, type=int, + help="number of minutes") def run(self, args, argv): - dockerfile = open(args.dockerfile, "rb").read() tag = args.tag dkr = Docker()
@@ -469,13 +476,32 @@ class CheckCommand(SubCommand): print("Image does not exist") return 1 - if dkr.image_matches_dockerfile(tag, dockerfile): - if not args.quiet: - print("Image is up to date") - return 0 - else: - print("Image needs updating") - return 1 + if args.checktype == "checksum": + if not args.dockerfile: + print("Need a dockerfile for tag:%s" % (tag)) + return 1 + + dockerfile = open(args.dockerfile, "rb").read() + + if dkr.image_matches_dockerfile(tag, dockerfile): + if not args.quiet: + print("Image is up to date") + return 0 + else: + print("Image needs updating") + return 1 + elif args.checktype == "age": + timestr = dkr.get_image_creation_time(info).split(".")[0] + created = datetime.strptime(timestr, "%Y-%m-%dT%H:%M:%S") + past = datetime.now() - timedelta(minutes=args.olderthan) + if created < past: + print ("Image created @ %s more than %d minutes old" % + (timestr, args.olderthan)) + return 1 + else: + if not args.quiet: + print ("Image less than %d minutes old" % (args.olderthan)) + return 0 def main():
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH v1 8/8] docker: docker.py adding age check command
Quoted 16 lines · Alex Bennée
This is useful for querying if an image is too old.
Signed-off-by Alex Bennée <[email protected]>---
v7
- use cached info--- tests/docker/docker.py | 46 +++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 10 deletions(-)tests/docker/docker.py +0
-0
tests/docker/docker.py from tarfile import TarFile, TarInfo · line 32
Quoted 4 lines
tests/docker/docker.py +1
-0@@ -29,6 +29,7 @@ from tarfile import TarFile, TarInfo from StringIO import StringIO from shutil import copy, rmtree from pwd import getpwuid +from datetime import datetime,timedelta
datetime,<space>timedelta
Quoted 72 lines
tests/docker/docker.py +35
-10FILTERED_ENV_NAMES = ['ftp_proxy', 'http_proxy', 'https_proxy']@@ -185,6 +186,9 @@ class Docker(object): except subprocess.CalledProcessError: return None + def get_image_creation_time(self, info): + return json.loads(info)[0]["Created"] + def get_image_dockerfile_checksum(self, tag): resp = self.inspect_tag(tag) labels = json.loads(resp)[0]["Config"].get("Labels", {})@@ -456,11 +460,14 @@ class CheckCommand(SubCommand): def args(self, parser): parser.add_argument("tag", help="Image Tag") - parser.add_argument("dockerfile", - help="Dockerfile name") + parser.add_argument("dockerfile", default=None, + help="Dockerfile name", nargs='?') + parser.add_argument("--checktype", choices=["checksum", "age"], + default="checksum", help="check type") + parser.add_argument("--olderthan", default=60, type=int, + help="number of minutes") def run(self, args, argv): - dockerfile = open(args.dockerfile, "rb").read() tag = args.tag dkr = Docker()@@ -469,13 +476,32 @@ class CheckCommand(SubCommand): print("Image does not exist") return 1 - if dkr.image_matches_dockerfile(tag, dockerfile): - if not args.quiet: - print("Image is up to date") - return 0 - else: - print("Image needs updating") - return 1 + if args.checktype == "checksum": + if not args.dockerfile: + print("Need a dockerfile for tag:%s" % (tag)) + return 1 + + dockerfile = open(args.dockerfile, "rb").read() + + if dkr.image_matches_dockerfile(tag, dockerfile): + if not args.quiet: + print("Image is up to date") + return 0 + else: + print("Image needs updating") + return 1 + elif args.checktype == "age": + timestr = dkr.get_image_creation_time(info).split(".")[0] + created = datetime.strptime(timestr, "%Y-%m-%dT%H:%M:%S") + past = datetime.now() - timedelta(minutes=args.olderthan) + if created < past: + print ("Image created @ %s more than %d minutes old" % + (timestr, args.olderthan)) + return 1 + else: + if not args.quiet: + print ("Image less than %d minutes old" % (args.olderthan)) + return 0 def main():