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.
25 from tarfile
import TarFile
, TarInfo
26 from StringIO
import StringIO
27 from shutil
import copy
, rmtree
28 from pwd
import getpwuid
31 DEVNULL
= open(os
.devnull
, 'wb')
34 def _text_checksum(text
):
35 """Calculate a digest string unique to the text content"""
36 return hashlib
.sha1(text
).hexdigest()
38 def _guess_docker_command():
39 """ Guess a working docker command or raise exception if not found"""
40 commands
= [["docker"], ["sudo", "-n", "docker"]]
43 if subprocess
.call(cmd
+ ["images"],
44 stdout
=DEVNULL
, stderr
=DEVNULL
) == 0:
48 commands_txt
= "\n".join([" " + " ".join(x
) for x
in commands
])
49 raise Exception("Cannot find working docker command. Tried:\n%s" % \
52 def _copy_with_mkdir(src
, root_dir
, sub_path
):
53 """Copy src into root_dir, creating sub_path as needed."""
54 dest_dir
= os
.path
.normpath("%s/%s" % (root_dir
, sub_path
))
58 # we can safely ignore already created directories
61 dest_file
= "%s/%s" % (dest_dir
, os
.path
.basename(src
))
65 def _get_so_libs(executable
):
66 """Return a list of libraries associated with an executable.
68 The paths may be symbolic links which would need to be resolved to
69 ensure theright data is copied."""
72 ldd_re
= re
.compile(r
"(/.*/)(\S*)")
74 ldd_output
= subprocess
.check_output(["ldd", executable
])
75 for line
in ldd_output
.split("\n"):
76 search
= ldd_re
.search(line
)
77 if search
and len(search
.groups()) == 2:
78 so_path
= search
.groups()[0]
79 so_lib
= search
.groups()[1]
80 libs
.append("%s/%s" % (so_path
, so_lib
))
81 except subprocess
.CalledProcessError
:
82 print "%s had no associated libraries (static build?)" % (executable
)
86 def _copy_binary_with_libs(src
, dest_dir
):
87 """Copy a binary executable and all its dependant libraries.
89 This does rely on the host file-system being fairly multi-arch
90 aware so the file don't clash with the guests layout."""
92 _copy_with_mkdir(src
, dest_dir
, "/usr/bin")
94 libs
= _get_so_libs(src
)
97 so_path
= os
.path
.dirname(l
)
98 _copy_with_mkdir(l
, dest_dir
, so_path
)
100 class Docker(object):
101 """ Running Docker commands """
103 self
._command
= _guess_docker_command()
105 atexit
.register(self
._kill
_instances
)
106 signal
.signal(signal
.SIGTERM
, self
._kill
_instances
)
107 signal
.signal(signal
.SIGHUP
, self
._kill
_instances
)
109 def _do(self
, cmd
, quiet
=True, infile
=None, **kwargs
):
111 kwargs
["stdout"] = DEVNULL
113 kwargs
["stdin"] = infile
114 return subprocess
.call(self
._command
+ cmd
, **kwargs
)
116 def _do_kill_instances(self
, only_known
, only_active
=True):
120 for i
in self
._output
(cmd
).split():
121 resp
= self
._output
(["inspect", i
])
122 labels
= json
.loads(resp
)[0]["Config"]["Labels"]
123 active
= json
.loads(resp
)[0]["State"]["Running"]
126 instance_uuid
= labels
.get("com.qemu.instance.uuid", None)
127 if not instance_uuid
:
129 if only_known
and instance_uuid
not in self
._instances
:
131 print "Terminating", i
133 self
._do
(["kill", i
])
137 self
._do
_kill
_instances
(False, False)
140 def _kill_instances(self
, *args
, **kwargs
):
141 return self
._do
_kill
_instances
(True)
143 def _output(self
, cmd
, **kwargs
):
144 return subprocess
.check_output(self
._command
+ cmd
,
145 stderr
=subprocess
.STDOUT
,
148 def get_image_dockerfile_checksum(self
, tag
):
149 resp
= self
._output
(["inspect", tag
])
150 labels
= json
.loads(resp
)[0]["Config"].get("Labels", {})
151 return labels
.get("com.qemu.dockerfile-checksum", "")
153 def build_image(self
, tag
, docker_dir
, dockerfile
,
154 quiet
=True, user
=False, argv
=None):
158 tmp_df
= tempfile
.NamedTemporaryFile(dir=docker_dir
, suffix
=".docker")
159 tmp_df
.write(dockerfile
)
163 uname
= getpwuid(uid
).pw_name
165 tmp_df
.write("RUN id %s 2>/dev/null || useradd -u %d -U %s" %
169 tmp_df
.write("LABEL com.qemu.dockerfile-checksum=%s" %
170 _text_checksum(dockerfile
))
173 self
._do
(["build", "-t", tag
, "-f", tmp_df
.name
] + argv
+ \
177 def update_image(self
, tag
, tarball
, quiet
=True):
178 "Update a tagged image using "
180 self
._do
(["build", "-t", tag
, "-"], quiet
=quiet
, infile
=tarball
)
182 def image_matches_dockerfile(self
, tag
, dockerfile
):
184 checksum
= self
.get_image_dockerfile_checksum(tag
)
187 return checksum
== _text_checksum(dockerfile
)
189 def run(self
, cmd
, keep
, quiet
):
190 label
= uuid
.uuid1().hex
192 self
._instances
.append(label
)
193 ret
= self
._do
(["run", "--label",
194 "com.qemu.instance.uuid=" + label
] + cmd
,
197 self
._instances
.remove(label
)
200 def command(self
, cmd
, argv
, quiet
):
201 return self
._do
([cmd
] + argv
, quiet
=quiet
)
203 class SubCommand(object):
204 """A SubCommand template base class"""
205 name
= None # Subcommand name
206 def shared_args(self
, parser
):
207 parser
.add_argument("--quiet", action
="store_true",
208 help="Run quietly unless an error occured")
210 def args(self
, parser
):
211 """Setup argument parser"""
213 def run(self
, args
, argv
):
215 args: parsed argument by argument parser.
216 argv: remaining arguments from sys.argv.
220 class RunCommand(SubCommand
):
221 """Invoke docker run and take care of cleaning up"""
223 def args(self
, parser
):
224 parser
.add_argument("--keep", action
="store_true",
225 help="Don't remove image when command completes")
226 def run(self
, args
, argv
):
227 return Docker().run(argv
, args
.keep
, quiet
=args
.quiet
)
229 class BuildCommand(SubCommand
):
230 """ Build docker image out of a dockerfile. Arguments: <tag> <dockerfile>"""
232 def args(self
, parser
):
233 parser
.add_argument("--include-executable", "-e",
234 help="""Specify a binary that will be copied to the
235 container together with all its dependent
237 parser
.add_argument("--add-current-user", "-u", dest
="user",
239 help="Add the current user to image's passwd")
240 parser
.add_argument("tag",
242 parser
.add_argument("dockerfile",
243 help="Dockerfile name")
245 def run(self
, args
, argv
):
246 dockerfile
= open(args
.dockerfile
, "rb").read()
250 if dkr
.image_matches_dockerfile(tag
, dockerfile
):
252 print "Image is up to date."
254 # Create a docker context directory for the build
255 docker_dir
= tempfile
.mkdtemp(prefix
="docker_build")
257 # Is there a .pre file to run in the build context?
258 docker_pre
= os
.path
.splitext(args
.dockerfile
)[0]+".pre"
259 if os
.path
.exists(docker_pre
):
260 stdout
= DEVNULL
if args
.quiet
else None
261 rc
= subprocess
.call(os
.path
.realpath(docker_pre
),
262 cwd
=docker_dir
, stdout
=stdout
)
267 print "%s exited with code %d" % (docker_pre
, rc
)
270 # Do we include a extra binary?
271 if args
.include_executable
:
272 _copy_binary_with_libs(args
.include_executable
,
275 dkr
.build_image(tag
, docker_dir
, dockerfile
,
276 quiet
=args
.quiet
, user
=args
.user
, argv
=argv
)
282 class UpdateCommand(SubCommand
):
283 """ Update a docker image with new executables. Arguments: <tag> <executable>"""
285 def args(self
, parser
):
286 parser
.add_argument("tag",
288 parser
.add_argument("executable",
289 help="Executable to copy")
291 def run(self
, args
, argv
):
292 # Create a temporary tarball with our whole build context and
293 # dockerfile for the update
294 tmp
= tempfile
.NamedTemporaryFile(suffix
="dckr.tar.gz")
295 tmp_tar
= TarFile(fileobj
=tmp
, mode
='w')
297 # Add the executable to the tarball
298 bn
= os
.path
.basename(args
.executable
)
299 ff
= "/usr/bin/%s" % bn
300 tmp_tar
.add(args
.executable
, arcname
=ff
)
302 # Add any associated libraries
303 libs
= _get_so_libs(args
.executable
)
306 tmp_tar
.add(os
.path
.realpath(l
), arcname
=l
)
308 # Create a Docker buildfile
310 df
.write("FROM %s\n" % args
.tag
)
311 df
.write("ADD . /\n")
314 df_tar
= TarInfo(name
="Dockerfile")
315 df_tar
.size
= len(df
.buf
)
316 tmp_tar
.addfile(df_tar
, fileobj
=df
)
320 # reset the file pointers
324 # Run the build with our tarball context
326 dkr
.update_image(args
.tag
, tmp
, quiet
=args
.quiet
)
330 class CleanCommand(SubCommand
):
331 """Clean up docker instances"""
333 def run(self
, args
, argv
):
337 class ImagesCommand(SubCommand
):
338 """Run "docker images" command"""
340 def run(self
, args
, argv
):
341 return Docker().command("images", argv
, args
.quiet
)
344 parser
= argparse
.ArgumentParser(description
="A Docker helper",
345 usage
="%s <subcommand> ..." % os
.path
.basename(sys
.argv
[0]))
346 subparsers
= parser
.add_subparsers(title
="subcommands", help=None)
347 for cls
in SubCommand
.__subclasses
__():
349 subp
= subparsers
.add_parser(cmd
.name
, help=cmd
.__doc
__)
350 cmd
.shared_args(subp
)
352 subp
.set_defaults(cmdobj
=cmd
)
353 args
, argv
= parser
.parse_known_args()
354 return args
.cmdobj
.run(args
, argv
)
356 if __name__
== "__main__":