]>
git.proxmox.com Git - mirror_qemu.git/blob - tests/docker/docker.py
3 # Docker controlling module
5 # Copyright (c) 2016 Red Hat Inc.
8 # Fam Zheng <famz@redhat.com>
10 # This work is licensed under the terms of the GNU GPL, version 2
11 # or (at your option) any later version. See the COPYING file in
12 # the top-level directory.
14 from __future__
import print_function
17 sys
.path
.append(os
.path
.join(os
.path
.dirname(__file__
),
18 '..', '..', 'scripts'))
28 from tarfile
import TarFile
, TarInfo
30 from StringIO
import StringIO
32 from io
import StringIO
33 from shutil
import copy
, rmtree
34 from pwd
import getpwuid
35 from datetime
import datetime
,timedelta
38 FILTERED_ENV_NAMES
= ['ftp_proxy', 'http_proxy', 'https_proxy']
41 DEVNULL
= open(os
.devnull
, 'wb')
44 def _text_checksum(text
):
45 """Calculate a digest string unique to the text content"""
46 return hashlib
.sha1(text
).hexdigest()
48 def _file_checksum(filename
):
49 return _text_checksum(open(filename
, 'rb').read())
51 def _guess_docker_command():
52 """ Guess a working docker command or raise exception if not found"""
53 commands
= [["docker"], ["sudo", "-n", "docker"]]
56 # docker version will return the client details in stdout
57 # but still report a status of 1 if it can't contact the daemon
58 if subprocess
.call(cmd
+ ["version"],
59 stdout
=DEVNULL
, stderr
=DEVNULL
) == 0:
63 commands_txt
= "\n".join([" " + " ".join(x
) for x
in commands
])
64 raise Exception("Cannot find working docker command. Tried:\n%s" % \
67 def _copy_with_mkdir(src
, root_dir
, sub_path
='.'):
68 """Copy src into root_dir, creating sub_path as needed."""
69 dest_dir
= os
.path
.normpath("%s/%s" % (root_dir
, sub_path
))
73 # we can safely ignore already created directories
76 dest_file
= "%s/%s" % (dest_dir
, os
.path
.basename(src
))
80 def _get_so_libs(executable
):
81 """Return a list of libraries associated with an executable.
83 The paths may be symbolic links which would need to be resolved to
84 ensure theright data is copied."""
87 ldd_re
= re
.compile(r
"(/.*/)(\S*)")
89 ldd_output
= subprocess
.check_output(["ldd", executable
])
90 for line
in ldd_output
.split("\n"):
91 search
= ldd_re
.search(line
)
92 if search
and len(search
.groups()) == 2:
93 so_path
= search
.groups()[0]
94 so_lib
= search
.groups()[1]
95 libs
.append("%s/%s" % (so_path
, so_lib
))
96 except subprocess
.CalledProcessError
:
97 print("%s had no associated libraries (static build?)" % (executable
))
101 def _copy_binary_with_libs(src
, dest_dir
):
102 """Copy a binary executable and all its dependant libraries.
104 This does rely on the host file-system being fairly multi-arch
105 aware so the file don't clash with the guests layout."""
107 _copy_with_mkdir(src
, dest_dir
, "/usr/bin")
109 libs
= _get_so_libs(src
)
112 so_path
= os
.path
.dirname(l
)
113 _copy_with_mkdir(l
, dest_dir
, so_path
)
115 def _read_qemu_dockerfile(img_name
):
116 # special case for Debian linux-user images
117 if img_name
.startswith("debian") and img_name
.endswith("user"):
118 img_name
= "debian-bootstrap"
120 df
= os
.path
.join(os
.path
.dirname(__file__
), "dockerfiles",
121 img_name
+ ".docker")
122 return open(df
, "r").read()
124 def _dockerfile_preprocess(df
):
126 for l
in df
.splitlines():
127 if len(l
.strip()) == 0 or l
.startswith("#"):
129 from_pref
= "FROM qemu:"
130 if l
.startswith(from_pref
):
131 # TODO: Alternatively we could replace this line with "FROM $ID"
132 # where $ID is the image's hex id obtained with
133 # $ docker images $IMAGE --format="{{.Id}}"
134 # but unfortunately that's not supported by RHEL 7.
135 inlining
= _read_qemu_dockerfile(l
[len(from_pref
):])
136 out
+= _dockerfile_preprocess(inlining
)
141 class Docker(object):
142 """ Running Docker commands """
144 self
._command
= _guess_docker_command()
146 atexit
.register(self
._kill
_instances
)
147 signal
.signal(signal
.SIGTERM
, self
._kill
_instances
)
148 signal
.signal(signal
.SIGHUP
, self
._kill
_instances
)
150 def _do(self
, cmd
, quiet
=True, **kwargs
):
152 kwargs
["stdout"] = DEVNULL
153 return subprocess
.call(self
._command
+ cmd
, **kwargs
)
155 def _do_check(self
, cmd
, quiet
=True, **kwargs
):
157 kwargs
["stdout"] = DEVNULL
158 return subprocess
.check_call(self
._command
+ cmd
, **kwargs
)
160 def _do_kill_instances(self
, only_known
, only_active
=True):
164 for i
in self
._output
(cmd
).split():
165 resp
= self
._output
(["inspect", i
])
166 labels
= json
.loads(resp
)[0]["Config"]["Labels"]
167 active
= json
.loads(resp
)[0]["State"]["Running"]
170 instance_uuid
= labels
.get("com.qemu.instance.uuid", None)
171 if not instance_uuid
:
173 if only_known
and instance_uuid
not in self
._instances
:
175 print("Terminating", i
)
177 self
._do
(["kill", i
])
181 self
._do
_kill
_instances
(False, False)
184 def _kill_instances(self
, *args
, **kwargs
):
185 return self
._do
_kill
_instances
(True)
187 def _output(self
, cmd
, **kwargs
):
188 return subprocess
.check_output(self
._command
+ cmd
,
189 stderr
=subprocess
.STDOUT
,
192 def inspect_tag(self
, tag
):
194 return self
._output
(["inspect", tag
])
195 except subprocess
.CalledProcessError
:
198 def get_image_creation_time(self
, info
):
199 return json
.loads(info
)[0]["Created"]
201 def get_image_dockerfile_checksum(self
, tag
):
202 resp
= self
.inspect_tag(tag
)
203 labels
= json
.loads(resp
)[0]["Config"].get("Labels", {})
204 return labels
.get("com.qemu.dockerfile-checksum", "")
206 def build_image(self
, tag
, docker_dir
, dockerfile
,
207 quiet
=True, user
=False, argv
=None, extra_files_cksum
=[]):
211 tmp_df
= tempfile
.NamedTemporaryFile(dir=docker_dir
, suffix
=".docker")
212 tmp_df
.write(dockerfile
)
216 uname
= getpwuid(uid
).pw_name
218 tmp_df
.write("RUN id %s 2>/dev/null || useradd -u %d -U %s" %
222 tmp_df
.write("LABEL com.qemu.dockerfile-checksum=%s" %
223 _text_checksum(_dockerfile_preprocess(dockerfile
)))
224 for f
, c
in extra_files_cksum
:
225 tmp_df
.write("LABEL com.qemu.%s-checksum=%s" % (f
, c
))
229 self
._do
_check
(["build", "-t", tag
, "-f", tmp_df
.name
] + argv
+ \
233 def update_image(self
, tag
, tarball
, quiet
=True):
234 "Update a tagged image using "
236 self
._do
_check
(["build", "-t", tag
, "-"], quiet
=quiet
, stdin
=tarball
)
238 def image_matches_dockerfile(self
, tag
, dockerfile
):
240 checksum
= self
.get_image_dockerfile_checksum(tag
)
243 return checksum
== _text_checksum(_dockerfile_preprocess(dockerfile
))
245 def run(self
, cmd
, keep
, quiet
):
246 label
= uuid
.uuid1().hex
248 self
._instances
.append(label
)
249 ret
= self
._do
_check
(["run", "--label",
250 "com.qemu.instance.uuid=" + label
] + cmd
,
253 self
._instances
.remove(label
)
256 def command(self
, cmd
, argv
, quiet
):
257 return self
._do
([cmd
] + argv
, quiet
=quiet
)
259 class SubCommand(object):
260 """A SubCommand template base class"""
261 name
= None # Subcommand name
262 def shared_args(self
, parser
):
263 parser
.add_argument("--quiet", action
="store_true",
264 help="Run quietly unless an error occured")
266 def args(self
, parser
):
267 """Setup argument parser"""
269 def run(self
, args
, argv
):
271 args: parsed argument by argument parser.
272 argv: remaining arguments from sys.argv.
276 class RunCommand(SubCommand
):
277 """Invoke docker run and take care of cleaning up"""
279 def args(self
, parser
):
280 parser
.add_argument("--keep", action
="store_true",
281 help="Don't remove image when command completes")
282 def run(self
, args
, argv
):
283 return Docker().run(argv
, args
.keep
, quiet
=args
.quiet
)
285 class BuildCommand(SubCommand
):
286 """ Build docker image out of a dockerfile. Arguments: <tag> <dockerfile>"""
288 def args(self
, parser
):
289 parser
.add_argument("--include-executable", "-e",
290 help="""Specify a binary that will be copied to the
291 container together with all its dependent
293 parser
.add_argument("--extra-files", "-f", nargs
='*',
294 help="""Specify files that will be copied in the
295 Docker image, fulfilling the ADD directive from the
297 parser
.add_argument("--add-current-user", "-u", dest
="user",
299 help="Add the current user to image's passwd")
300 parser
.add_argument("tag",
302 parser
.add_argument("dockerfile",
303 help="Dockerfile name")
305 def run(self
, args
, argv
):
306 dockerfile
= open(args
.dockerfile
, "rb").read()
310 if "--no-cache" not in argv
and \
311 dkr
.image_matches_dockerfile(tag
, dockerfile
):
313 print("Image is up to date.")
315 # Create a docker context directory for the build
316 docker_dir
= tempfile
.mkdtemp(prefix
="docker_build")
318 # Is there a .pre file to run in the build context?
319 docker_pre
= os
.path
.splitext(args
.dockerfile
)[0]+".pre"
320 if os
.path
.exists(docker_pre
):
321 stdout
= DEVNULL
if args
.quiet
else None
322 rc
= subprocess
.call(os
.path
.realpath(docker_pre
),
323 cwd
=docker_dir
, stdout
=stdout
)
328 print("%s exited with code %d" % (docker_pre
, rc
))
331 # Copy any extra files into the Docker context. These can be
332 # included by the use of the ADD directive in the Dockerfile.
334 if args
.include_executable
:
335 # FIXME: there is no checksum of this executable and the linked
336 # libraries, once the image built any change of this executable
337 # or any library won't trigger another build.
338 _copy_binary_with_libs(args
.include_executable
, docker_dir
)
339 for filename
in args
.extra_files
or []:
340 _copy_with_mkdir(filename
, docker_dir
)
341 cksum
+= [(filename
, _file_checksum(filename
))]
343 argv
+= ["--build-arg=" + k
.lower() + "=" + v
344 for k
, v
in os
.environ
.iteritems()
345 if k
.lower() in FILTERED_ENV_NAMES
]
346 dkr
.build_image(tag
, docker_dir
, dockerfile
,
347 quiet
=args
.quiet
, user
=args
.user
, argv
=argv
,
348 extra_files_cksum
=cksum
)
354 class UpdateCommand(SubCommand
):
355 """ Update a docker image with new executables. Arguments: <tag> <executable>"""
357 def args(self
, parser
):
358 parser
.add_argument("tag",
360 parser
.add_argument("executable",
361 help="Executable to copy")
363 def run(self
, args
, argv
):
364 # Create a temporary tarball with our whole build context and
365 # dockerfile for the update
366 tmp
= tempfile
.NamedTemporaryFile(suffix
="dckr.tar.gz")
367 tmp_tar
= TarFile(fileobj
=tmp
, mode
='w')
369 # Add the executable to the tarball
370 bn
= os
.path
.basename(args
.executable
)
371 ff
= "/usr/bin/%s" % bn
372 tmp_tar
.add(args
.executable
, arcname
=ff
)
374 # Add any associated libraries
375 libs
= _get_so_libs(args
.executable
)
378 tmp_tar
.add(os
.path
.realpath(l
), arcname
=l
)
380 # Create a Docker buildfile
382 df
.write("FROM %s\n" % args
.tag
)
383 df
.write("ADD . /\n")
386 df_tar
= TarInfo(name
="Dockerfile")
387 df_tar
.size
= len(df
.buf
)
388 tmp_tar
.addfile(df_tar
, fileobj
=df
)
392 # reset the file pointers
396 # Run the build with our tarball context
398 dkr
.update_image(args
.tag
, tmp
, quiet
=args
.quiet
)
402 class CleanCommand(SubCommand
):
403 """Clean up docker instances"""
405 def run(self
, args
, argv
):
409 class ImagesCommand(SubCommand
):
410 """Run "docker images" command"""
412 def run(self
, args
, argv
):
413 return Docker().command("images", argv
, args
.quiet
)
416 class ProbeCommand(SubCommand
):
417 """Probe if we can run docker automatically"""
420 def run(self
, args
, argv
):
423 if docker
._command
[0] == "docker":
425 elif docker
._command
[0] == "sudo":
433 class CcCommand(SubCommand
):
434 """Compile sources with cc in images"""
437 def args(self
, parser
):
438 parser
.add_argument("--image", "-i", required
=True,
439 help="The docker image in which to run cc")
440 parser
.add_argument("--cc", default
="cc",
441 help="The compiler executable to call")
442 parser
.add_argument("--user",
443 help="The user-id to run under")
444 parser
.add_argument("--source-path", "-s", nargs
="*", dest
="paths",
445 help="""Extra paths to (ro) mount into container for
448 def run(self
, args
, argv
):
449 if argv
and argv
[0] == "--":
452 cmd
= ["--rm", "-w", cwd
,
453 "-v", "%s:%s:rw" % (cwd
, cwd
)]
456 cmd
+= ["-v", "%s:%s:ro,z" % (p
, p
)]
458 cmd
+= ["-u", args
.user
]
459 cmd
+= [args
.image
, args
.cc
]
461 return Docker().command("run", cmd
, args
.quiet
)
464 class CheckCommand(SubCommand
):
465 """Check if we need to re-build a docker image out of a dockerfile.
466 Arguments: <tag> <dockerfile>"""
469 def args(self
, parser
):
470 parser
.add_argument("tag",
472 parser
.add_argument("dockerfile", default
=None,
473 help="Dockerfile name", nargs
='?')
474 parser
.add_argument("--checktype", choices
=["checksum", "age"],
475 default
="checksum", help="check type")
476 parser
.add_argument("--olderthan", default
=60, type=int,
477 help="number of minutes")
479 def run(self
, args
, argv
):
483 info
= dkr
.inspect_tag(tag
)
485 print("Image does not exist")
488 if args
.checktype
== "checksum":
489 if not args
.dockerfile
:
490 print("Need a dockerfile for tag:%s" % (tag
))
493 dockerfile
= open(args
.dockerfile
, "rb").read()
495 if dkr
.image_matches_dockerfile(tag
, dockerfile
):
497 print("Image is up to date")
500 print("Image needs updating")
502 elif args
.checktype
== "age":
503 timestr
= dkr
.get_image_creation_time(info
).split(".")[0]
504 created
= datetime
.strptime(timestr
, "%Y-%m-%dT%H:%M:%S")
505 past
= datetime
.now() - timedelta(minutes
=args
.olderthan
)
507 print ("Image created @ %s more than %d minutes old" %
508 (timestr
, args
.olderthan
))
512 print ("Image less than %d minutes old" % (args
.olderthan
))
517 parser
= argparse
.ArgumentParser(description
="A Docker helper",
518 usage
="%s <subcommand> ..." % os
.path
.basename(sys
.argv
[0]))
519 subparsers
= parser
.add_subparsers(title
="subcommands", help=None)
520 for cls
in SubCommand
.__subclasses
__():
522 subp
= subparsers
.add_parser(cmd
.name
, help=cmd
.__doc
__)
523 cmd
.shared_args(subp
)
525 subp
.set_defaults(cmdobj
=cmd
)
526 args
, argv
= parser
.parse_known_args()
527 return args
.cmdobj
.run(args
, argv
)
529 if __name__
== "__main__":