All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: Fam Zheng <famz@redhat.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH v5 01/14] tests: Add utilities for docker testing
Date: Thu, 26 May 2016 11:16:25 +0100	[thread overview]
Message-ID: <87pos9cefa.fsf@linaro.org> (raw)
In-Reply-To: <1463986466-764-2-git-send-email-famz@redhat.com>


Fam Zheng <famz@redhat.com> writes:

> docker.py is added with a number of useful subcommands to manager docker
> images and instances for QEMU docker testing. Subcommands are:
>
> run: A wrapper of "docker run" (or "sudo -n docker run" if necessary),
> which takes care of killing and removing the running container at
> SIGINT.
>
> clean: Tear down all the containers including inactive ones that are
> started by docker_run.
>
> build: Compare an image from given dockerfile and rebuild it if they're
> different.
>
> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
> Signed-off-by: Fam Zheng <famz@redhat.com>
> ---
>  tests/docker/docker.py | 191 +++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 191 insertions(+)
>  create mode 100755 tests/docker/docker.py
>
> diff --git a/tests/docker/docker.py b/tests/docker/docker.py
> new file mode 100755
> index 0000000..fe73de7
> --- /dev/null
> +++ b/tests/docker/docker.py
> @@ -0,0 +1,191 @@
> +#!/usr/bin/env python2
> +#
> +# Docker controlling module
> +#
> +# Copyright (c) 2016 Red Hat Inc.
> +#
> +# Authors:
> +#  Fam Zheng <famz@redhat.com>
> +#
> +# This work is licensed under the terms of the GNU GPL, version 2
> +# or (at your option) any later version. See the COPYING file in
> +# the top-level directory.
> +
> +import os
> +import sys
> +import subprocess
> +import json
> +import hashlib
> +import atexit
> +import uuid
> +import argparse
> +import tempfile
> +
> +def _text_checksum(text):
> +    """Calculate a digest string unique to the text content"""
> +    return hashlib.sha1(text).hexdigest()
> +
> +def _guess_docker_command():
> +    """ Guess a working docker command or raise exception if not found"""
> +    commands = [["docker"], ["sudo", "-n", "docker"]]
> +    for cmd in commands:
> +        if subprocess.call(cmd + ["images"],
> +                           stdout=subprocess.PIPE,
> +                           stderr=subprocess.PIPE) == 0:
> +            return cmd
> +    commands_txt = "\n".join(["  " + " ".join(x) for x in commands])
> +    raise Exception("Cannot find working docker command. Tried:\n%s" % \
> +                    commands_txt)
> +
> +class Docker(object):
> +    """ Running Docker commands """
> +    def __init__(self):
> +        self._command = _guess_docker_command()
> +        self._instances = []
> +        atexit.register(self._kill_instances)
> +
> +    def _do(self, cmd, quiet=True, **kwargs):
> +        if quiet:
> +            kwargs["stdout"] = subprocess.PIPE
> +        return subprocess.call(self._command + cmd, **kwargs)
> +
> +    def _do_kill_instances(self, only_known, only_active=True):
> +        cmd = ["ps", "-q"]
> +        if not only_active:
> +            cmd.append("-a")
> +        for i in self._output(cmd).split():
> +            resp = self._output(["inspect", i])
> +            labels = json.loads(resp)[0]["Config"]["Labels"]
> +            active = json.loads(resp)[0]["State"]["Running"]
> +            if not labels:
> +                continue
> +            instance_uuid = labels.get("com.qemu.instance.uuid", None)
> +            if not instance_uuid:
> +                continue
> +            if only_known and instance_uuid not in self._instances:
> +                continue
> +            print "Terminating", i
> +            if active:
> +                self._do(["kill", i])
> +            self._do(["rm", i])
> +
> +    def clean(self):
> +        self._do_kill_instances(False, False)
> +        return 0
> +
> +    def _kill_instances(self):
> +        return self._do_kill_instances(True)
> +
> +    def _output(self, cmd, **kwargs):
> +        return subprocess.check_output(self._command + cmd,
> +                                       stderr=subprocess.STDOUT,
> +                                       **kwargs)
> +
> +    def get_image_dockerfile_checksum(self, tag):
> +        resp = self._output(["inspect", tag])
> +        labels = json.loads(resp)[0]["Config"].get("Labels", {})
> +        return labels.get("com.qemu.dockerfile-checksum", "")
> +
> +    def build_image(self, tag, dockerfile, df_path, quiet=True, argv=None):
> +        if argv == None:
> +            argv = []
> +        tmp = dockerfile + "\n" + \
> +              "LABEL com.qemu.dockerfile-checksum=%s" % \
> +              _text_checksum(dockerfile)
> +        dirname = os.path.dirname(df_path)
> +        tmp_df = tempfile.NamedTemporaryFile(dir=dirname)
> +        tmp_df.write(tmp)
> +        tmp_df.flush()
> +        self._do(["build", "-t", tag, "-f", tmp_df.name] + argv + \
> +                 [dirname],
> +                 quiet=quiet)

I've been doing some hacking to build qemu-$arch enabled docker images
and I think we should use a clean temp dir for the build. Along the
lines of:

    def build_image(self, tag, dockerfile, quiet=True, qemu=None, argv=None):
        if argv == None:
            argv = []
        tmp = dockerfile + "\n" + \
              "LABEL com.qemu.dockerfile-checksum=%s" % \
              _text_checksum(dockerfile)

        tmp_dir = tempfile.mkdtemp(prefix="docker_build")
        tmp_df = tempfile.NamedTemporaryFile(dir=tmp_dir, suffix=".docker")

        print "dirname=%s tmp_df=%s" % (tmp_dir, tmp_df.name)

        tmp_df.write(tmp)
        tmp_df.flush()
        self._do(["build", "-t", tag, "-f", tmp_df.name] + argv + \
                 [tmp_dir],
                 quiet=quiet)

Having a clean empty dir means I can use this to dump stuff in
(debootstrap output, qemu binaries) without having to worry about
polluting the source tree. Also it is one less parameter to pass to
build_image ;-)

Otherwise I think the series is looking good.

> +
> +    def image_matches_dockerfile(self, tag, dockerfile):
> +        try:
> +            checksum = self.get_image_dockerfile_checksum(tag)
> +        except Exception:
> +            return False
> +        return checksum == _text_checksum(dockerfile)
> +
> +    def run(self, cmd, keep, quiet):
> +        label = uuid.uuid1().hex
> +        if not keep:
> +            self._instances.append(label)
> +        ret = self._do(["run", "--label",
> +                        "com.qemu.instance.uuid=" + label] + cmd,
> +                       quiet=quiet)
> +        if not keep:
> +            self._instances.remove(label)
> +        return ret
> +
> +class SubCommand(object):
> +    """A SubCommand template base class"""
> +    name = None # Subcommand name
> +    def shared_args(self, parser):
> +        parser.add_argument("--quiet", action="store_true",
> +                            help="Run quietly unless an error occured")
> +
> +    def args(self, parser):
> +        """Setup argument parser"""
> +        pass
> +    def run(self, args, argv):
> +        """Run command.
> +        args: parsed argument by argument parser.
> +        argv: remaining arguments from sys.argv.
> +        """
> +        pass
> +
> +class RunCommand(SubCommand):
> +    """Invoke docker run and take care of cleaning up"""
> +    name = "run"
> +    def args(self, parser):
> +        parser.add_argument("--keep", action="store_true",
> +                            help="Don't remove image when command completes")
> +    def run(self, args, argv):
> +        return Docker().run(argv, args.keep, quiet=args.quiet)
> +
> +class BuildCommand(SubCommand):
> +    """ Build docker image out of a dockerfile. Arguments: <tag> <dockerfile>"""
> +    name = "build"
> +    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()
> +        if dkr.image_matches_dockerfile(tag, dockerfile):
> +            if not args.quiet:
> +                print "Image is up to date."
> +            return 0
> +
> +        dkr.build_image(tag, dockerfile, args.dockerfile,
> +                        quiet=args.quiet, argv=argv)
> +        return 0
> +
> +class CleanCommand(SubCommand):
> +    """Clean up docker instances"""
> +    name = "clean"
> +    def run(self, args, argv):
> +        Docker().clean()
> +        return 0
> +
> +def main():
> +    parser = argparse.ArgumentParser(description="A Docker helper",
> +            usage="%s <subcommand> ..." % os.path.basename(sys.argv[0]))
> +    subparsers = parser.add_subparsers(title="subcommands", help=None)
> +    for cls in SubCommand.__subclasses__():
> +        cmd = cls()
> +        subp = subparsers.add_parser(cmd.name, help=cmd.__doc__)
> +        cmd.shared_args(subp)
> +        cmd.args(subp)
> +        subp.set_defaults(cmdobj=cmd)
> +    args, argv = parser.parse_known_args()
> +    return args.cmdobj.run(args, argv)
> +
> +if __name__ == "__main__":
> +    sys.exit(main())


--
Alex Bennée

  reply	other threads:[~2016-05-26 10:15 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-23  6:54 [Qemu-devel] [PATCH v5 00/14] tests: Introducing docker tests Fam Zheng
2016-05-23  6:54 ` [Qemu-devel] [PATCH v5 01/14] tests: Add utilities for docker testing Fam Zheng
2016-05-26 10:16   ` Alex Bennée [this message]
2016-05-23  6:54 ` [Qemu-devel] [PATCH v5 02/14] rules.mak: Add "COMMA" constant Fam Zheng
2016-05-23  6:54 ` [Qemu-devel] [PATCH v5 03/14] Makefile: Rules for docker testing Fam Zheng
2016-05-23  6:54 ` [Qemu-devel] [PATCH v5 04/14] docker: Add images Fam Zheng
2016-05-23  6:54 ` [Qemu-devel] [PATCH v5 05/14] docker: Add test runner Fam Zheng
2016-05-23  6:54 ` [Qemu-devel] [PATCH v5 06/14] docker: Add common.rc Fam Zheng
2016-05-23  6:54 ` [Qemu-devel] [PATCH v5 07/14] docker: Add quick test Fam Zheng
2016-05-23  6:54 ` [Qemu-devel] [PATCH v5 08/14] docker: Add full test Fam Zheng
2016-05-26 16:15   ` Alex Bennée
2016-05-23  6:54 ` [Qemu-devel] [PATCH v5 09/14] docker: Add clang test Fam Zheng
2016-05-26 16:20   ` Alex Bennée
2016-05-26 16:23   ` Alex Bennée
2016-05-23  6:54 ` [Qemu-devel] [PATCH v5 10/14] docker: Add mingw test Fam Zheng
2016-05-23  6:54 ` [Qemu-devel] [PATCH v5 11/14] docker: Add travis tool Fam Zheng
2016-05-23  6:54 ` [Qemu-devel] [PATCH v5 12/14] docs: Add text for tests/docker in build-system.txt Fam Zheng
2016-05-23  6:54 ` [Qemu-devel] [PATCH v5 13/14] docker: Add EXTRA_CONFIGURE_OPTS Fam Zheng
2016-05-23  6:54 ` [Qemu-devel] [PATCH v5 14/14] MAINTAINERS: Add tests/docker Fam Zheng
2016-05-26 16:26 ` [Qemu-devel] [PATCH v5 00/14] tests: Introducing docker tests Alex Bennée
2016-05-27  0:44   ` Fam Zheng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87pos9cefa.fsf@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=famz@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.