summaryrefslogtreecommitdiffstats
path: root/tests/docker/docker.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/docker/docker.py')
-rwxr-xr-xtests/docker/docker.py159
1 files changed, 150 insertions, 9 deletions
diff --git a/tests/docker/docker.py b/tests/docker/docker.py
index 0151362d17..222a1053fe 100755
--- a/tests/docker/docker.py
+++ b/tests/docker/docker.py
@@ -20,7 +20,10 @@ import atexit
import uuid
import argparse
import tempfile
-from shutil import copy
+import re
+from tarfile import TarFile, TarInfo
+from StringIO import StringIO
+from shutil import copy, rmtree
def _text_checksum(text):
"""Calculate a digest string unique to the text content"""
@@ -38,6 +41,54 @@ def _guess_docker_command():
raise Exception("Cannot find working docker command. Tried:\n%s" % \
commands_txt)
+def _copy_with_mkdir(src, root_dir, sub_path):
+ """Copy src into root_dir, creating sub_path as needed."""
+ dest_dir = os.path.normpath("%s/%s" % (root_dir, sub_path))
+ try:
+ os.makedirs(dest_dir)
+ except OSError:
+ # we can safely ignore already created directories
+ pass
+
+ dest_file = "%s/%s" % (dest_dir, os.path.basename(src))
+ copy(src, dest_file)
+
+
+def _get_so_libs(executable):
+ """Return a list of libraries associated with an executable.
+
+ The paths may be symbolic links which would need to be resolved to
+ ensure theright data is copied."""
+
+ libs = []
+ ldd_re = re.compile(r"(/.*/)(\S*)")
+ try:
+ ldd_output = subprocess.check_output(["ldd", executable])
+ for line in ldd_output.split("\n"):
+ search = ldd_re.search(line)
+ if search and len(search.groups()) == 2:
+ so_path = search.groups()[0]
+ so_lib = search.groups()[1]
+ libs.append("%s/%s" % (so_path, so_lib))
+ except subprocess.CalledProcessError:
+ print "%s had no associated libraries (static build?)" % (executable)
+
+ return libs
+
+def _copy_binary_with_libs(src, dest_dir):
+ """Copy a binary executable and all its dependant libraries.
+
+ This does rely on the host file-system being fairly multi-arch
+ aware so the file don't clash with the guests layout."""
+
+ _copy_with_mkdir(src, dest_dir, "/usr/bin")
+
+ libs = _get_so_libs(src)
+ if libs:
+ for l in libs:
+ so_path = os.path.dirname(l)
+ _copy_with_mkdir(l , dest_dir, so_path)
+
class Docker(object):
""" Running Docker commands """
def __init__(self):
@@ -45,9 +96,11 @@ class Docker(object):
self._instances = []
atexit.register(self._kill_instances)
- def _do(self, cmd, quiet=True, **kwargs):
+ def _do(self, cmd, quiet=True, infile=None, **kwargs):
if quiet:
kwargs["stdout"] = subprocess.PIPE
+ if infile:
+ kwargs["stdin"] = infile
return subprocess.call(self._command + cmd, **kwargs)
def _do_kill_instances(self, only_known, only_active=True):
@@ -87,22 +140,27 @@ class Docker(object):
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):
+ def build_image(self, tag, docker_dir, dockerfile, quiet=True, argv=None):
if argv == None:
argv = []
- tmp_dir = tempfile.mkdtemp(prefix="docker_build")
- tmp_df = tempfile.NamedTemporaryFile(dir=tmp_dir, suffix=".docker")
+ tmp_df = tempfile.NamedTemporaryFile(dir=docker_dir, suffix=".docker")
tmp_df.write(dockerfile)
tmp_df.write("\n")
tmp_df.write("LABEL com.qemu.dockerfile-checksum=%s" %
_text_checksum(dockerfile))
tmp_df.flush()
+
self._do(["build", "-t", tag, "-f", tmp_df.name] + argv + \
- [tmp_dir],
+ [docker_dir],
quiet=quiet)
+ def update_image(self, tag, tarball, quiet=True):
+ "Update a tagged image using "
+
+ self._do(["build", "-t", tag, "-"], quiet=quiet, infile=tarball)
+
def image_matches_dockerfile(self, tag, dockerfile):
try:
checksum = self.get_image_dockerfile_checksum(tag)
@@ -121,6 +179,9 @@ class Docker(object):
self._instances.remove(label)
return ret
+ def command(self, cmd, argv, quiet):
+ return self._do([cmd] + argv, quiet=quiet)
+
class SubCommand(object):
"""A SubCommand template base class"""
name = None # Subcommand name
@@ -151,6 +212,10 @@ class BuildCommand(SubCommand):
""" Build docker image out of a dockerfile. Arguments: <tag> <dockerfile>"""
name = "build"
def args(self, parser):
+ parser.add_argument("--include-executable", "-e",
+ help="""Specify a binary that will be copied to the
+ container together with all its dependent
+ libraries""")
parser.add_argument("tag",
help="Image Tag")
parser.add_argument("dockerfile",
@@ -164,10 +229,80 @@ class BuildCommand(SubCommand):
if dkr.image_matches_dockerfile(tag, dockerfile):
if not args.quiet:
print "Image is up to date."
- return 0
+ else:
+ # Create a docker context directory for the build
+ docker_dir = tempfile.mkdtemp(prefix="docker_build")
+
+ # Is there a .pre file to run in the build context?
+ docker_pre = os.path.splitext(args.dockerfile)[0]+".pre"
+ if os.path.exists(docker_pre):
+ rc = subprocess.call(os.path.realpath(docker_pre),
+ cwd=docker_dir)
+ if rc == 3:
+ print "Skip"
+ return 0
+ elif rc != 0:
+ print "%s exited with code %d" % (docker_pre, rc)
+ return 1
+
+ # Do we include a extra binary?
+ if args.include_executable:
+ _copy_binary_with_libs(args.include_executable,
+ docker_dir)
+
+ dkr.build_image(tag, docker_dir, dockerfile,
+ quiet=args.quiet, argv=argv)
+
+ rmtree(docker_dir)
+
+ return 0
+
+class UpdateCommand(SubCommand):
+ """ Update a docker image with new executables. Arguments: <tag> <executable>"""
+ name = "update"
+ def args(self, parser):
+ parser.add_argument("tag",
+ help="Image Tag")
+ parser.add_argument("executable",
+ help="Executable to copy")
+
+ def run(self, args, argv):
+ # Create a temporary tarball with our whole build context and
+ # dockerfile for the update
+ tmp = tempfile.NamedTemporaryFile(suffix="dckr.tar.gz")
+ tmp_tar = TarFile(fileobj=tmp, mode='w')
+
+ # Add the executable to the tarball
+ bn = os.path.basename(args.executable)
+ ff = "/usr/bin/%s" % bn
+ tmp_tar.add(args.executable, arcname=ff)
+
+ # Add any associated libraries
+ libs = _get_so_libs(args.executable)
+ if libs:
+ for l in libs:
+ tmp_tar.add(os.path.realpath(l), arcname=l)
+
+ # Create a Docker buildfile
+ df = StringIO()
+ df.write("FROM %s\n" % args.tag)
+ df.write("ADD . /\n")
+ df.seek(0)
+
+ df_tar = TarInfo(name="Dockerfile")
+ df_tar.size = len(df.buf)
+ tmp_tar.addfile(df_tar, fileobj=df)
+
+ tmp_tar.close()
+
+ # reset the file pointers
+ tmp.flush()
+ tmp.seek(0)
+
+ # Run the build with our tarball context
+ dkr = Docker()
+ dkr.update_image(args.tag, tmp, quiet=args.quiet)
- dkr.build_image(tag, dockerfile, args.dockerfile,
- quiet=args.quiet, argv=argv)
return 0
class CleanCommand(SubCommand):
@@ -177,6 +312,12 @@ class CleanCommand(SubCommand):
Docker().clean()
return 0
+class ImagesCommand(SubCommand):
+ """Run "docker images" command"""
+ name = "images"
+ def run(self, args, argv):
+ return Docker().command("images", argv, args.quiet)
+
def main():
parser = argparse.ArgumentParser(description="A Docker helper",
usage="%s <subcommand> ..." % os.path.basename(sys.argv[0]))