2019-08-29 13:08:27 +02:00
|
|
|
#!/usr/bin/env python3
|
2016-06-01 06:25:14 +02:00
|
|
|
#
|
|
|
|
# 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
|
2018-06-19 00:51:30 +02:00
|
|
|
import argparse
|
2019-07-12 14:46:13 +02:00
|
|
|
import enum
|
2016-06-01 06:25:14 +02:00
|
|
|
import tempfile
|
2016-07-19 15:20:37 +02:00
|
|
|
import re
|
2016-09-21 05:49:27 +02:00
|
|
|
import signal
|
2023-03-06 13:27:43 +01:00
|
|
|
import getpass
|
2016-07-19 15:20:40 +02:00
|
|
|
from tarfile import TarFile, TarInfo
|
2020-07-24 08:45:02 +02:00
|
|
|
from io import StringIO, BytesIO
|
2016-07-19 15:20:36 +02:00
|
|
|
from shutil import copy, rmtree
|
2019-01-23 18:13:55 +01:00
|
|
|
from datetime import datetime, timedelta
|
2016-06-01 06:25:14 +02:00
|
|
|
|
2016-09-06 22:05:44 +02:00
|
|
|
|
2017-03-06 21:55:20 +01:00
|
|
|
FILTERED_ENV_NAMES = ['ftp_proxy', 'http_proxy', 'https_proxy']
|
|
|
|
|
|
|
|
|
2016-09-06 22:05:44 +02:00
|
|
|
DEVNULL = open(os.devnull, 'wb')
|
|
|
|
|
2019-07-12 14:46:13 +02:00
|
|
|
class EngineEnum(enum.IntEnum):
|
|
|
|
AUTO = 1
|
|
|
|
DOCKER = 2
|
|
|
|
PODMAN = 3
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name.lower()
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return str(self)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def argparse(s):
|
|
|
|
try:
|
|
|
|
return EngineEnum[s.upper()]
|
|
|
|
except KeyError:
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
USE_ENGINE = EngineEnum.AUTO
|
2016-09-06 22:05:44 +02:00
|
|
|
|
2020-04-22 18:19:43 +02:00
|
|
|
def _bytes_checksum(bytes):
|
|
|
|
"""Calculate a digest string unique to the text content"""
|
|
|
|
return hashlib.sha1(bytes).hexdigest()
|
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
def _text_checksum(text):
|
|
|
|
"""Calculate a digest string unique to the text content"""
|
2020-04-22 18:19:43 +02:00
|
|
|
return _bytes_checksum(text.encode('utf-8'))
|
2016-06-01 06:25:14 +02:00
|
|
|
|
2019-08-29 13:08:27 +02:00
|
|
|
def _read_dockerfile(path):
|
|
|
|
return open(path, 'rt', encoding='utf-8').read()
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2017-06-02 20:56:09 +02:00
|
|
|
def _file_checksum(filename):
|
2020-04-22 18:19:43 +02:00
|
|
|
return _bytes_checksum(open(filename, 'rb').read())
|
2017-06-02 20:56:09 +02:00
|
|
|
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2019-07-12 14:46:13 +02:00
|
|
|
def _guess_engine_command():
|
|
|
|
""" Guess a working engine command or raise exception if not found"""
|
|
|
|
commands = []
|
|
|
|
|
|
|
|
if USE_ENGINE in [EngineEnum.AUTO, EngineEnum.PODMAN]:
|
|
|
|
commands += [["podman"]]
|
|
|
|
if USE_ENGINE in [EngineEnum.AUTO, EngineEnum.DOCKER]:
|
|
|
|
commands += [["docker"], ["sudo", "-n", "docker"]]
|
2016-06-01 06:25:14 +02:00
|
|
|
for cmd in commands:
|
docker: Handle exceptions when looking for docker command
When trying to run docker tests on a host without the docker
command, we get the following Python backtrace:
$ make docker-test-quick@centos6 V=1
.../qemu/tests/docker/docker.py build qemu:centos6 .../qemu/tests/docker/dockerfiles/centos6.docker
Traceback (most recent call last):
File ".../qemu/tests/docker/docker.py", line 339, in <module>
sys.exit(main())
File ".../qemu/tests/docker/docker.py", line 336, in main
return args.cmdobj.run(args, argv)
File ".../qemu/tests/docker/docker.py", line 231, in run
dkr = Docker()
File ".../qemu/tests/docker/docker.py", line 98, in __init__
self._command = _guess_docker_command()
File ".../qemu/tests/docker/docker.py", line 41, in _guess_docker_command
stdout=DEVNULL, stderr=DEVNULL) == 0:
File "/usr/lib64/python2.7/subprocess.py", line 523, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
.../qemu/tests/docker/Makefile.include:47: recipe for target 'docker-image-centos6' failed
make: *** [docker-image-centos6] Error 1
Change _guess_docker_command() to handle OSError exceptions
raised by subprocess.call(), so we will keep looking for other
commands and print a better error message.
New output will be:
$ make docker-test-quick@centos6 V=1
.../qemu/tests/docker/docker.py build qemu:centos6 .../qemu/tests/docker/dockerfiles/centos6.docker
Traceback (most recent call last):
File ".../qemu/tests/docker/docker.py", line 343, in <module>
sys.exit(main())
File ".../qemu/tests/docker/docker.py", line 340, in main
return args.cmdobj.run(args, argv)
File ".../qemu/tests/docker/docker.py", line 235, in run
dkr = Docker()
File ".../qemu/tests/docker/docker.py", line 102, in __init__
self._command = _guess_docker_command()
File ".../qemu/tests/docker/docker.py", line 49, in _guess_docker_command
commands_txt)
Exception: Cannot find working docker command. Tried:
docker
sudo -n docker
.../qemu/tests/docker/Makefile.include:47: recipe for target 'docker-image-centos6' failed
make: *** [docker-image-centos6] Error 1
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
Message-Id: <1474369559-16903-1-git-send-email-ehabkost@redhat.com>
[exceptions.OSError -> OSError and drop the import. - Fam]
Signed-off-by: Fam Zheng <famz@redhat.com>
2016-09-20 13:05:59 +02:00
|
|
|
try:
|
2018-06-18 12:14:40 +02:00
|
|
|
# docker version will return the client details in stdout
|
|
|
|
# but still report a status of 1 if it can't contact the daemon
|
|
|
|
if subprocess.call(cmd + ["version"],
|
docker: Handle exceptions when looking for docker command
When trying to run docker tests on a host without the docker
command, we get the following Python backtrace:
$ make docker-test-quick@centos6 V=1
.../qemu/tests/docker/docker.py build qemu:centos6 .../qemu/tests/docker/dockerfiles/centos6.docker
Traceback (most recent call last):
File ".../qemu/tests/docker/docker.py", line 339, in <module>
sys.exit(main())
File ".../qemu/tests/docker/docker.py", line 336, in main
return args.cmdobj.run(args, argv)
File ".../qemu/tests/docker/docker.py", line 231, in run
dkr = Docker()
File ".../qemu/tests/docker/docker.py", line 98, in __init__
self._command = _guess_docker_command()
File ".../qemu/tests/docker/docker.py", line 41, in _guess_docker_command
stdout=DEVNULL, stderr=DEVNULL) == 0:
File "/usr/lib64/python2.7/subprocess.py", line 523, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
.../qemu/tests/docker/Makefile.include:47: recipe for target 'docker-image-centos6' failed
make: *** [docker-image-centos6] Error 1
Change _guess_docker_command() to handle OSError exceptions
raised by subprocess.call(), so we will keep looking for other
commands and print a better error message.
New output will be:
$ make docker-test-quick@centos6 V=1
.../qemu/tests/docker/docker.py build qemu:centos6 .../qemu/tests/docker/dockerfiles/centos6.docker
Traceback (most recent call last):
File ".../qemu/tests/docker/docker.py", line 343, in <module>
sys.exit(main())
File ".../qemu/tests/docker/docker.py", line 340, in main
return args.cmdobj.run(args, argv)
File ".../qemu/tests/docker/docker.py", line 235, in run
dkr = Docker()
File ".../qemu/tests/docker/docker.py", line 102, in __init__
self._command = _guess_docker_command()
File ".../qemu/tests/docker/docker.py", line 49, in _guess_docker_command
commands_txt)
Exception: Cannot find working docker command. Tried:
docker
sudo -n docker
.../qemu/tests/docker/Makefile.include:47: recipe for target 'docker-image-centos6' failed
make: *** [docker-image-centos6] Error 1
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
Message-Id: <1474369559-16903-1-git-send-email-ehabkost@redhat.com>
[exceptions.OSError -> OSError and drop the import. - Fam]
Signed-off-by: Fam Zheng <famz@redhat.com>
2016-09-20 13:05:59 +02:00
|
|
|
stdout=DEVNULL, stderr=DEVNULL) == 0:
|
|
|
|
return cmd
|
|
|
|
except OSError:
|
|
|
|
pass
|
2016-06-01 06:25:14 +02:00
|
|
|
commands_txt = "\n".join([" " + " ".join(x) for x in commands])
|
2019-07-12 14:46:13 +02:00
|
|
|
raise Exception("Cannot find working engine command. Tried:\n%s" %
|
2016-06-01 06:25:14 +02:00
|
|
|
commands_txt)
|
|
|
|
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2021-02-02 14:39:49 +01:00
|
|
|
def _copy_with_mkdir(src, root_dir, sub_path='.', name=None):
|
2016-07-19 15:20:37 +02:00
|
|
|
"""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
|
|
|
|
|
2021-02-02 14:39:49 +01:00
|
|
|
dest_file = "%s/%s" % (dest_dir, name if name else os.path.basename(src))
|
2021-02-02 14:39:48 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
copy(src, dest_file)
|
|
|
|
except FileNotFoundError:
|
|
|
|
print("Couldn't copy %s to %s" % (src, dest_file))
|
|
|
|
pass
|
2016-07-19 15:20:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2020-02-03 10:09:17 +01:00
|
|
|
ensure the right data is copied."""
|
2016-07-19 15:20:37 +02:00
|
|
|
|
|
|
|
libs = []
|
2020-02-03 10:09:17 +01:00
|
|
|
ldd_re = re.compile(r"(?:\S+ => )?(\S*) \(:?0x[0-9a-f]+\)")
|
2016-07-19 15:20:37 +02:00
|
|
|
try:
|
2019-09-09 12:36:20 +02:00
|
|
|
ldd_output = subprocess.check_output(["ldd", executable]).decode('utf-8')
|
2016-07-19 15:20:37 +02:00
|
|
|
for line in ldd_output.split("\n"):
|
|
|
|
search = ldd_re.search(line)
|
2020-02-03 10:09:17 +01:00
|
|
|
if search:
|
|
|
|
try:
|
tests/docker: Fix _get_so_libs() for docker-binfmt-image
Fix a variable rename mistake from commit 5e33f7fead5:
Traceback (most recent call last):
File "./tests/docker/docker.py", line 710, in <module>
sys.exit(main())
File "./tests/docker/docker.py", line 706, in main
return args.cmdobj.run(args, argv)
File "./tests/docker/docker.py", line 489, in run
_copy_binary_with_libs(args.include_executable,
File "./tests/docker/docker.py", line 149, in _copy_binary_with_libs
libs = _get_so_libs(src)
File "./tests/docker/docker.py", line 123, in _get_so_libs
libs.append(s.group(1))
NameError: name 's' is not defined
Fixes: 5e33f7fead5 ("tests/docker: better handle symlinked libs")
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Message-Id: <20210119050149.516910-1-f4bug@amsat.org>
Message-Id: <20210202134001.25738-2-alex.bennee@linaro.org>
2021-02-02 14:39:46 +01:00
|
|
|
libs.append(search.group(1))
|
2020-02-03 10:09:17 +01:00
|
|
|
except IndexError:
|
|
|
|
pass
|
2016-07-19 15:20:37 +02:00
|
|
|
except subprocess.CalledProcessError:
|
2018-06-08 14:29:43 +02:00
|
|
|
print("%s had no associated libraries (static build?)" % (executable))
|
2016-07-19 15:20:37 +02:00
|
|
|
|
|
|
|
return libs
|
|
|
|
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2019-01-23 18:07:08 +01:00
|
|
|
def _copy_binary_with_libs(src, bin_dest, dest_dir):
|
|
|
|
"""Maybe copy a binary and all its dependent libraries.
|
|
|
|
|
|
|
|
If bin_dest isn't set we only copy the support libraries because
|
|
|
|
we don't need qemu in the docker path to run (due to persistent
|
|
|
|
mapping). Indeed users may get confused if we aren't running what
|
|
|
|
is in the image.
|
2016-07-19 15:20:37 +02:00
|
|
|
|
|
|
|
This does rely on the host file-system being fairly multi-arch
|
2019-01-23 18:07:08 +01:00
|
|
|
aware so the file don't clash with the guests layout.
|
|
|
|
"""
|
2016-07-19 15:20:37 +02:00
|
|
|
|
2019-01-23 18:07:08 +01:00
|
|
|
if bin_dest:
|
|
|
|
_copy_with_mkdir(src, dest_dir, os.path.dirname(bin_dest))
|
|
|
|
else:
|
|
|
|
print("only copying support libraries for %s" % (src))
|
2016-07-19 15:20:37 +02:00
|
|
|
|
|
|
|
libs = _get_so_libs(src)
|
|
|
|
if libs:
|
|
|
|
for l in libs:
|
|
|
|
so_path = os.path.dirname(l)
|
2021-02-02 14:39:49 +01:00
|
|
|
name = os.path.basename(l)
|
2020-02-03 10:09:17 +01:00
|
|
|
real_l = os.path.realpath(l)
|
2021-02-02 14:39:49 +01:00
|
|
|
_copy_with_mkdir(real_l, dest_dir, so_path, name)
|
2016-07-19 15:20:37 +02:00
|
|
|
|
2018-07-17 18:11:26 +02:00
|
|
|
|
|
|
|
def _check_binfmt_misc(executable):
|
|
|
|
"""Check binfmt_misc has entry for executable in the right place.
|
|
|
|
|
|
|
|
The details of setting up binfmt_misc are outside the scope of
|
|
|
|
this script but we should at least fail early with a useful
|
2019-01-23 18:07:08 +01:00
|
|
|
message if it won't work.
|
|
|
|
|
|
|
|
Returns the configured binfmt path and a valid flag. For
|
|
|
|
persistent configurations we will still want to copy and dependent
|
|
|
|
libraries.
|
|
|
|
"""
|
2018-07-17 18:11:26 +02:00
|
|
|
|
|
|
|
binary = os.path.basename(executable)
|
|
|
|
binfmt_entry = "/proc/sys/fs/binfmt_misc/%s" % (binary)
|
|
|
|
|
|
|
|
if not os.path.exists(binfmt_entry):
|
|
|
|
print ("No binfmt_misc entry for %s" % (binary))
|
2019-01-23 18:07:08 +01:00
|
|
|
return None, False
|
2018-07-17 18:11:26 +02:00
|
|
|
|
|
|
|
with open(binfmt_entry) as x: entry = x.read()
|
|
|
|
|
2019-01-15 15:37:51 +01:00
|
|
|
if re.search("flags:.*F.*\n", entry):
|
2019-01-23 18:13:55 +01:00
|
|
|
print("binfmt_misc for %s uses persistent(F) mapping to host binary" %
|
2019-01-15 15:37:51 +01:00
|
|
|
(binary))
|
2019-01-23 18:07:08 +01:00
|
|
|
return None, True
|
2019-01-15 15:37:51 +01:00
|
|
|
|
2023-10-16 08:22:56 +02:00
|
|
|
m = re.search(r"interpreter (\S+)\n", entry)
|
2019-01-15 15:28:39 +01:00
|
|
|
interp = m.group(1)
|
|
|
|
if interp and interp != executable:
|
|
|
|
print("binfmt_misc for %s does not point to %s, using %s" %
|
|
|
|
(binary, executable, interp))
|
2018-07-17 18:11:26 +02:00
|
|
|
|
2019-01-23 18:07:08 +01:00
|
|
|
return interp, True
|
|
|
|
|
2018-07-17 18:11:26 +02:00
|
|
|
|
docker: Improved image checksum
When a base image locally defined by QEMU, such as in the debian images,
is updated, the dockerfile checksum mechanism in docker.py still skips
updating the derived image, because it only looks at the literal content
of the dockerfile, without considering changes to the base image.
For example we have a recent fix e58c1f9b35e81 that fixed
debian-win64-cross by updating its base image, debian8-mxe, but due to
above "feature" of docker.py the image in question is automatically NOT
rebuilt unless you add NOCACHE=1. It is noticed on Shippable:
https://app.shippable.com/github/qemu/qemu/runs/541/2/console
because after the fix is merged, the error still occurs, and the log
shows the container image is, as explained above, not updated.
This is because at the time docker.py was written, there wasn't any
dependencies between QEMU's docker images.
Now improve this to preprocess any "FROM qemu:*" directives in the
dockerfiles while doing checksum, and inline the base image's dockerfile
content, recursively. This ensures any changes on the depended _QEMU_
images are taken into account.
This means for external images that we expect to retrieve from docker
registries, we still do it as before. It is not perfect, because
registry images can get updated too. Technically we could substitute the
image name with its hex ID as obtained with $(docker images $IMAGE
--format="{{.Id}}"), but --format is not supported by RHEL 7, so leave
it for now.
Reported-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Signed-off-by: Fam Zheng <famz@redhat.com>
Message-Id: <20171103131229.4737-1-famz@redhat.com>
Tested-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Fam Zheng <famz@redhat.com>
2017-11-03 14:12:29 +01:00
|
|
|
def _read_qemu_dockerfile(img_name):
|
2018-06-29 18:46:49 +02:00
|
|
|
# special case for Debian linux-user images
|
|
|
|
if img_name.startswith("debian") and img_name.endswith("user"):
|
|
|
|
img_name = "debian-bootstrap"
|
|
|
|
|
docker: Improved image checksum
When a base image locally defined by QEMU, such as in the debian images,
is updated, the dockerfile checksum mechanism in docker.py still skips
updating the derived image, because it only looks at the literal content
of the dockerfile, without considering changes to the base image.
For example we have a recent fix e58c1f9b35e81 that fixed
debian-win64-cross by updating its base image, debian8-mxe, but due to
above "feature" of docker.py the image in question is automatically NOT
rebuilt unless you add NOCACHE=1. It is noticed on Shippable:
https://app.shippable.com/github/qemu/qemu/runs/541/2/console
because after the fix is merged, the error still occurs, and the log
shows the container image is, as explained above, not updated.
This is because at the time docker.py was written, there wasn't any
dependencies between QEMU's docker images.
Now improve this to preprocess any "FROM qemu:*" directives in the
dockerfiles while doing checksum, and inline the base image's dockerfile
content, recursively. This ensures any changes on the depended _QEMU_
images are taken into account.
This means for external images that we expect to retrieve from docker
registries, we still do it as before. It is not perfect, because
registry images can get updated too. Technically we could substitute the
image name with its hex ID as obtained with $(docker images $IMAGE
--format="{{.Id}}"), but --format is not supported by RHEL 7, so leave
it for now.
Reported-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Signed-off-by: Fam Zheng <famz@redhat.com>
Message-Id: <20171103131229.4737-1-famz@redhat.com>
Tested-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Fam Zheng <famz@redhat.com>
2017-11-03 14:12:29 +01:00
|
|
|
df = os.path.join(os.path.dirname(__file__), "dockerfiles",
|
|
|
|
img_name + ".docker")
|
2019-08-29 13:08:27 +02:00
|
|
|
return _read_dockerfile(df)
|
docker: Improved image checksum
When a base image locally defined by QEMU, such as in the debian images,
is updated, the dockerfile checksum mechanism in docker.py still skips
updating the derived image, because it only looks at the literal content
of the dockerfile, without considering changes to the base image.
For example we have a recent fix e58c1f9b35e81 that fixed
debian-win64-cross by updating its base image, debian8-mxe, but due to
above "feature" of docker.py the image in question is automatically NOT
rebuilt unless you add NOCACHE=1. It is noticed on Shippable:
https://app.shippable.com/github/qemu/qemu/runs/541/2/console
because after the fix is merged, the error still occurs, and the log
shows the container image is, as explained above, not updated.
This is because at the time docker.py was written, there wasn't any
dependencies between QEMU's docker images.
Now improve this to preprocess any "FROM qemu:*" directives in the
dockerfiles while doing checksum, and inline the base image's dockerfile
content, recursively. This ensures any changes on the depended _QEMU_
images are taken into account.
This means for external images that we expect to retrieve from docker
registries, we still do it as before. It is not perfect, because
registry images can get updated too. Technically we could substitute the
image name with its hex ID as obtained with $(docker images $IMAGE
--format="{{.Id}}"), but --format is not supported by RHEL 7, so leave
it for now.
Reported-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Signed-off-by: Fam Zheng <famz@redhat.com>
Message-Id: <20171103131229.4737-1-famz@redhat.com>
Tested-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Fam Zheng <famz@redhat.com>
2017-11-03 14:12:29 +01:00
|
|
|
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2022-09-14 17:59:49 +02:00
|
|
|
def _dockerfile_verify_flat(df):
|
|
|
|
"Verify we do not include other qemu/ layers"
|
docker: Improved image checksum
When a base image locally defined by QEMU, such as in the debian images,
is updated, the dockerfile checksum mechanism in docker.py still skips
updating the derived image, because it only looks at the literal content
of the dockerfile, without considering changes to the base image.
For example we have a recent fix e58c1f9b35e81 that fixed
debian-win64-cross by updating its base image, debian8-mxe, but due to
above "feature" of docker.py the image in question is automatically NOT
rebuilt unless you add NOCACHE=1. It is noticed on Shippable:
https://app.shippable.com/github/qemu/qemu/runs/541/2/console
because after the fix is merged, the error still occurs, and the log
shows the container image is, as explained above, not updated.
This is because at the time docker.py was written, there wasn't any
dependencies between QEMU's docker images.
Now improve this to preprocess any "FROM qemu:*" directives in the
dockerfiles while doing checksum, and inline the base image's dockerfile
content, recursively. This ensures any changes on the depended _QEMU_
images are taken into account.
This means for external images that we expect to retrieve from docker
registries, we still do it as before. It is not perfect, because
registry images can get updated too. Technically we could substitute the
image name with its hex ID as obtained with $(docker images $IMAGE
--format="{{.Id}}"), but --format is not supported by RHEL 7, so leave
it for now.
Reported-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Signed-off-by: Fam Zheng <famz@redhat.com>
Message-Id: <20171103131229.4737-1-famz@redhat.com>
Tested-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Fam Zheng <famz@redhat.com>
2017-11-03 14:12:29 +01:00
|
|
|
for l in df.splitlines():
|
|
|
|
if len(l.strip()) == 0 or l.startswith("#"):
|
|
|
|
continue
|
2020-07-01 15:56:29 +02:00
|
|
|
from_pref = "FROM qemu/"
|
docker: Improved image checksum
When a base image locally defined by QEMU, such as in the debian images,
is updated, the dockerfile checksum mechanism in docker.py still skips
updating the derived image, because it only looks at the literal content
of the dockerfile, without considering changes to the base image.
For example we have a recent fix e58c1f9b35e81 that fixed
debian-win64-cross by updating its base image, debian8-mxe, but due to
above "feature" of docker.py the image in question is automatically NOT
rebuilt unless you add NOCACHE=1. It is noticed on Shippable:
https://app.shippable.com/github/qemu/qemu/runs/541/2/console
because after the fix is merged, the error still occurs, and the log
shows the container image is, as explained above, not updated.
This is because at the time docker.py was written, there wasn't any
dependencies between QEMU's docker images.
Now improve this to preprocess any "FROM qemu:*" directives in the
dockerfiles while doing checksum, and inline the base image's dockerfile
content, recursively. This ensures any changes on the depended _QEMU_
images are taken into account.
This means for external images that we expect to retrieve from docker
registries, we still do it as before. It is not perfect, because
registry images can get updated too. Technically we could substitute the
image name with its hex ID as obtained with $(docker images $IMAGE
--format="{{.Id}}"), but --format is not supported by RHEL 7, so leave
it for now.
Reported-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Signed-off-by: Fam Zheng <famz@redhat.com>
Message-Id: <20171103131229.4737-1-famz@redhat.com>
Tested-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Fam Zheng <famz@redhat.com>
2017-11-03 14:12:29 +01:00
|
|
|
if l.startswith(from_pref):
|
2022-09-14 17:59:49 +02:00
|
|
|
print("We no longer support multiple QEMU layers.")
|
|
|
|
print("Dockerfiles should be flat, ideally created by lcitool")
|
|
|
|
return False
|
|
|
|
return True
|
docker: Improved image checksum
When a base image locally defined by QEMU, such as in the debian images,
is updated, the dockerfile checksum mechanism in docker.py still skips
updating the derived image, because it only looks at the literal content
of the dockerfile, without considering changes to the base image.
For example we have a recent fix e58c1f9b35e81 that fixed
debian-win64-cross by updating its base image, debian8-mxe, but due to
above "feature" of docker.py the image in question is automatically NOT
rebuilt unless you add NOCACHE=1. It is noticed on Shippable:
https://app.shippable.com/github/qemu/qemu/runs/541/2/console
because after the fix is merged, the error still occurs, and the log
shows the container image is, as explained above, not updated.
This is because at the time docker.py was written, there wasn't any
dependencies between QEMU's docker images.
Now improve this to preprocess any "FROM qemu:*" directives in the
dockerfiles while doing checksum, and inline the base image's dockerfile
content, recursively. This ensures any changes on the depended _QEMU_
images are taken into account.
This means for external images that we expect to retrieve from docker
registries, we still do it as before. It is not perfect, because
registry images can get updated too. Technically we could substitute the
image name with its hex ID as obtained with $(docker images $IMAGE
--format="{{.Id}}"), but --format is not supported by RHEL 7, so leave
it for now.
Reported-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Signed-off-by: Fam Zheng <famz@redhat.com>
Message-Id: <20171103131229.4737-1-famz@redhat.com>
Tested-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Fam Zheng <famz@redhat.com>
2017-11-03 14:12:29 +01:00
|
|
|
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
class Docker(object):
|
|
|
|
""" Running Docker commands """
|
|
|
|
def __init__(self):
|
2019-07-12 14:46:13 +02:00
|
|
|
self._command = _guess_engine_command()
|
2020-07-01 15:56:36 +02:00
|
|
|
|
2021-07-09 16:29:35 +02:00
|
|
|
if ("docker" in self._command and
|
|
|
|
"TRAVIS" not in os.environ and
|
|
|
|
"GITLAB_CI" not in os.environ):
|
2020-07-01 15:56:36 +02:00
|
|
|
os.environ["DOCKER_BUILDKIT"] = "1"
|
|
|
|
self._buildkit = True
|
|
|
|
else:
|
|
|
|
self._buildkit = False
|
|
|
|
|
2019-09-19 18:51:27 +02:00
|
|
|
self._instance = None
|
2016-06-01 06:25:14 +02:00
|
|
|
atexit.register(self._kill_instances)
|
2016-09-21 05:49:27 +02:00
|
|
|
signal.signal(signal.SIGTERM, self._kill_instances)
|
|
|
|
signal.signal(signal.SIGHUP, self._kill_instances)
|
2016-06-01 06:25:14 +02:00
|
|
|
|
2017-07-12 09:55:27 +02:00
|
|
|
def _do(self, cmd, quiet=True, **kwargs):
|
2016-06-01 06:25:14 +02:00
|
|
|
if quiet:
|
2016-09-06 22:05:44 +02:00
|
|
|
kwargs["stdout"] = DEVNULL
|
2016-06-01 06:25:14 +02:00
|
|
|
return subprocess.call(self._command + cmd, **kwargs)
|
|
|
|
|
2017-07-12 09:55:28 +02:00
|
|
|
def _do_check(self, cmd, quiet=True, **kwargs):
|
|
|
|
if quiet:
|
|
|
|
kwargs["stdout"] = DEVNULL
|
|
|
|
return subprocess.check_call(self._command + cmd, **kwargs)
|
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
def _do_kill_instances(self, only_known, only_active=True):
|
|
|
|
cmd = ["ps", "-q"]
|
|
|
|
if not only_active:
|
|
|
|
cmd.append("-a")
|
2019-09-19 18:51:27 +02:00
|
|
|
|
|
|
|
filter = "--filter=label=com.qemu.instance.uuid"
|
|
|
|
if only_known:
|
|
|
|
if self._instance:
|
|
|
|
filter += "=%s" % (self._instance)
|
|
|
|
else:
|
|
|
|
# no point trying to kill, we finished
|
|
|
|
return
|
|
|
|
|
|
|
|
print("filter=%s" % (filter))
|
|
|
|
cmd.append(filter)
|
2016-06-01 06:25:14 +02:00
|
|
|
for i in self._output(cmd).split():
|
2019-09-19 18:51:27 +02:00
|
|
|
self._do(["rm", "-f", i])
|
2016-06-01 06:25:14 +02:00
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
self._do_kill_instances(False, False)
|
|
|
|
return 0
|
|
|
|
|
2016-09-21 05:49:27 +02:00
|
|
|
def _kill_instances(self, *args, **kwargs):
|
2016-06-01 06:25:14 +02:00
|
|
|
return self._do_kill_instances(True)
|
|
|
|
|
|
|
|
def _output(self, cmd, **kwargs):
|
2020-05-14 05:52:30 +02:00
|
|
|
try:
|
2019-09-04 19:46:36 +02:00
|
|
|
return subprocess.check_output(self._command + cmd,
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
encoding='utf-8',
|
|
|
|
**kwargs)
|
2020-05-14 05:52:30 +02:00
|
|
|
except TypeError:
|
|
|
|
# 'encoding' argument was added in 3.6+
|
2019-09-04 19:46:36 +02:00
|
|
|
return subprocess.check_output(self._command + cmd,
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
**kwargs).decode('utf-8')
|
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
|
2018-06-08 17:20:48 +02:00
|
|
|
def inspect_tag(self, tag):
|
|
|
|
try:
|
|
|
|
return self._output(["inspect", tag])
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
return None
|
|
|
|
|
2018-06-12 22:28:45 +02:00
|
|
|
def get_image_creation_time(self, info):
|
|
|
|
return json.loads(info)[0]["Created"]
|
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
def get_image_dockerfile_checksum(self, tag):
|
2018-06-08 17:20:48 +02:00
|
|
|
resp = self.inspect_tag(tag)
|
2016-06-01 06:25:14 +02:00
|
|
|
labels = json.loads(resp)[0]["Config"].get("Labels", {})
|
|
|
|
return labels.get("com.qemu.dockerfile-checksum", "")
|
|
|
|
|
2017-02-20 11:51:36 +01:00
|
|
|
def build_image(self, tag, docker_dir, dockerfile,
|
2020-07-01 15:56:36 +02:00
|
|
|
quiet=True, user=False, argv=None, registry=None,
|
|
|
|
extra_files_cksum=[]):
|
2019-01-23 18:13:55 +01:00
|
|
|
if argv is None:
|
2016-06-01 06:25:14 +02:00
|
|
|
argv = []
|
|
|
|
|
2022-09-14 17:59:49 +02:00
|
|
|
if not _dockerfile_verify_flat(dockerfile):
|
|
|
|
return -1
|
2020-07-01 15:56:36 +02:00
|
|
|
|
2022-09-14 17:59:49 +02:00
|
|
|
checksum = _text_checksum(dockerfile)
|
2020-07-01 15:56:36 +02:00
|
|
|
|
2019-08-29 13:08:27 +02:00
|
|
|
tmp_df = tempfile.NamedTemporaryFile(mode="w+t",
|
|
|
|
encoding='utf-8',
|
|
|
|
dir=docker_dir, suffix=".docker")
|
2016-06-01 06:25:14 +02:00
|
|
|
tmp_df.write(dockerfile)
|
|
|
|
|
2017-02-20 11:51:36 +01:00
|
|
|
if user:
|
|
|
|
uid = os.getuid()
|
2023-03-06 13:27:43 +01:00
|
|
|
uname = getpass.getuser()
|
2017-02-20 11:51:36 +01:00
|
|
|
tmp_df.write("\n")
|
|
|
|
tmp_df.write("RUN id %s 2>/dev/null || useradd -u %d -U %s" %
|
|
|
|
(uname, uid, uname))
|
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
tmp_df.write("\n")
|
2021-01-14 17:57:22 +01:00
|
|
|
tmp_df.write("LABEL com.qemu.dockerfile-checksum=%s\n" % (checksum))
|
2018-06-08 16:20:25 +02:00
|
|
|
for f, c in extra_files_cksum:
|
2021-01-14 17:57:22 +01:00
|
|
|
tmp_df.write("LABEL com.qemu.%s-checksum=%s\n" % (f, c))
|
2018-06-08 16:20:25 +02:00
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
tmp_df.flush()
|
2016-07-19 15:20:36 +02:00
|
|
|
|
2020-07-01 15:56:36 +02:00
|
|
|
build_args = ["build", "-t", tag, "-f", tmp_df.name]
|
|
|
|
if self._buildkit:
|
|
|
|
build_args += ["--build-arg", "BUILDKIT_INLINE_CACHE=1"]
|
|
|
|
|
|
|
|
if registry is not None:
|
2020-07-13 22:04:07 +02:00
|
|
|
pull_args = ["pull", "%s/%s" % (registry, tag)]
|
|
|
|
self._do(pull_args, quiet=quiet)
|
2020-07-01 15:56:36 +02:00
|
|
|
cache = "%s/%s" % (registry, tag)
|
|
|
|
build_args += ["--cache-from", cache]
|
|
|
|
build_args += argv
|
|
|
|
build_args += [docker_dir]
|
|
|
|
|
|
|
|
self._do_check(build_args,
|
2017-07-12 09:55:28 +02:00
|
|
|
quiet=quiet)
|
2016-06-01 06:25:14 +02:00
|
|
|
|
2016-07-19 15:20:40 +02:00
|
|
|
def update_image(self, tag, tarball, quiet=True):
|
|
|
|
"Update a tagged image using "
|
|
|
|
|
2017-07-12 09:55:28 +02:00
|
|
|
self._do_check(["build", "-t", tag, "-"], quiet=quiet, stdin=tarball)
|
2016-07-19 15:20:40 +02:00
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
def image_matches_dockerfile(self, tag, dockerfile):
|
|
|
|
try:
|
|
|
|
checksum = self.get_image_dockerfile_checksum(tag)
|
|
|
|
except Exception:
|
|
|
|
return False
|
2022-09-14 17:59:49 +02:00
|
|
|
return checksum == _text_checksum(dockerfile)
|
2016-06-01 06:25:14 +02:00
|
|
|
|
2019-09-04 11:07:17 +02:00
|
|
|
def run(self, cmd, keep, quiet, as_user=False):
|
2019-09-19 18:51:27 +02:00
|
|
|
label = uuid.uuid4().hex
|
2016-06-01 06:25:14 +02:00
|
|
|
if not keep:
|
2019-09-19 18:51:27 +02:00
|
|
|
self._instance = label
|
2019-09-04 11:07:17 +02:00
|
|
|
|
|
|
|
if as_user:
|
|
|
|
uid = os.getuid()
|
|
|
|
cmd = [ "-u", str(uid) ] + cmd
|
|
|
|
# podman requires a bit more fiddling
|
|
|
|
if self._command[0] == "podman":
|
2019-09-13 21:38:21 +02:00
|
|
|
cmd.insert(0, '--userns=keep-id')
|
2019-09-04 11:07:17 +02:00
|
|
|
|
2020-09-17 12:44:41 +02:00
|
|
|
ret = self._do_check(["run", "--rm", "--label",
|
2017-07-12 09:55:28 +02:00
|
|
|
"com.qemu.instance.uuid=" + label] + cmd,
|
|
|
|
quiet=quiet)
|
2016-06-01 06:25:14 +02:00
|
|
|
if not keep:
|
2019-09-19 18:51:27 +02:00
|
|
|
self._instance = None
|
2016-06-01 06:25:14 +02:00
|
|
|
return ret
|
|
|
|
|
2016-07-19 15:20:43 +02:00
|
|
|
def command(self, cmd, argv, quiet):
|
|
|
|
return self._do([cmd] + argv, quiet=quiet)
|
|
|
|
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
class SubCommand(object):
|
|
|
|
"""A SubCommand template base class"""
|
2019-01-23 18:13:55 +01:00
|
|
|
name = None # Subcommand name
|
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
def shared_args(self, parser):
|
|
|
|
parser.add_argument("--quiet", action="store_true",
|
2018-07-13 07:47:55 +02:00
|
|
|
help="Run quietly unless an error occurred")
|
2016-06-01 06:25:14 +02:00
|
|
|
|
|
|
|
def args(self, parser):
|
|
|
|
"""Setup argument parser"""
|
|
|
|
pass
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
def run(self, args, argv):
|
|
|
|
"""Run command.
|
|
|
|
args: parsed argument by argument parser.
|
|
|
|
argv: remaining arguments from sys.argv.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
class RunCommand(SubCommand):
|
|
|
|
"""Invoke docker run and take care of cleaning up"""
|
|
|
|
name = "run"
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
def args(self, parser):
|
|
|
|
parser.add_argument("--keep", action="store_true",
|
|
|
|
help="Don't remove image when command completes")
|
2019-07-12 12:50:52 +02:00
|
|
|
parser.add_argument("--run-as-current-user", action="store_true",
|
|
|
|
help="Run container using the current user's uid")
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
def run(self, args, argv):
|
2019-09-04 11:07:17 +02:00
|
|
|
return Docker().run(argv, args.keep, quiet=args.quiet,
|
|
|
|
as_user=args.run_as_current_user)
|
2016-06-01 06:25:14 +02:00
|
|
|
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
class BuildCommand(SubCommand):
|
2019-01-23 18:13:55 +01:00
|
|
|
""" Build docker image out of a dockerfile. Arg: <tag> <dockerfile>"""
|
2016-06-01 06:25:14 +02:00
|
|
|
name = "build"
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
def args(self, parser):
|
2016-07-19 15:20:37 +02:00
|
|
|
parser.add_argument("--include-executable", "-e",
|
|
|
|
help="""Specify a binary that will be copied to the
|
|
|
|
container together with all its dependent
|
|
|
|
libraries""")
|
2021-02-02 14:39:51 +01:00
|
|
|
parser.add_argument("--skip-binfmt",
|
|
|
|
action="store_true",
|
|
|
|
help="""Skip binfmt entry check (used for testing)""")
|
2020-04-22 16:17:08 +02:00
|
|
|
parser.add_argument("--extra-files", nargs='*',
|
2017-06-02 20:56:08 +02:00
|
|
|
help="""Specify files that will be copied in the
|
|
|
|
Docker image, fulfilling the ADD directive from the
|
|
|
|
Dockerfile""")
|
2017-02-20 11:51:36 +01:00
|
|
|
parser.add_argument("--add-current-user", "-u", dest="user",
|
|
|
|
action="store_true",
|
|
|
|
help="Add the current user to image's passwd")
|
2020-07-01 15:56:36 +02:00
|
|
|
parser.add_argument("--registry", "-r",
|
|
|
|
help="cache from docker registry")
|
2020-04-22 16:17:08 +02:00
|
|
|
parser.add_argument("-t", dest="tag",
|
2016-06-01 06:25:14 +02:00
|
|
|
help="Image Tag")
|
2020-04-22 16:17:08 +02:00
|
|
|
parser.add_argument("-f", dest="dockerfile",
|
2016-06-01 06:25:14 +02:00
|
|
|
help="Dockerfile name")
|
|
|
|
|
|
|
|
def run(self, args, argv):
|
2019-08-29 13:08:27 +02:00
|
|
|
dockerfile = _read_dockerfile(args.dockerfile)
|
2016-06-01 06:25:14 +02:00
|
|
|
tag = args.tag
|
|
|
|
|
|
|
|
dkr = Docker()
|
2017-07-25 15:34:23 +02:00
|
|
|
if "--no-cache" not in argv and \
|
|
|
|
dkr.image_matches_dockerfile(tag, dockerfile):
|
2016-06-01 06:25:14 +02:00
|
|
|
if not args.quiet:
|
2018-06-08 14:29:43 +02:00
|
|
|
print("Image is up to date.")
|
2016-07-19 15:20:36 +02:00
|
|
|
else:
|
|
|
|
# Create a docker context directory for the build
|
|
|
|
docker_dir = tempfile.mkdtemp(prefix="docker_build")
|
|
|
|
|
2018-07-17 18:11:26 +02:00
|
|
|
# Validate binfmt_misc will work
|
2021-02-02 14:39:51 +01:00
|
|
|
if args.skip_binfmt:
|
|
|
|
qpath = args.include_executable
|
|
|
|
elif args.include_executable:
|
2019-01-23 18:07:08 +01:00
|
|
|
qpath, enabled = _check_binfmt_misc(args.include_executable)
|
|
|
|
if not enabled:
|
2018-07-17 18:11:26 +02:00
|
|
|
return 1
|
|
|
|
|
2016-07-19 15:20:38 +02:00
|
|
|
# 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):
|
2016-09-06 22:05:51 +02:00
|
|
|
stdout = DEVNULL if args.quiet else None
|
2016-07-19 15:20:38 +02:00
|
|
|
rc = subprocess.call(os.path.realpath(docker_pre),
|
2016-09-06 22:05:51 +02:00
|
|
|
cwd=docker_dir, stdout=stdout)
|
2016-07-19 15:20:38 +02:00
|
|
|
if rc == 3:
|
2018-06-08 14:29:43 +02:00
|
|
|
print("Skip")
|
2016-07-19 15:20:38 +02:00
|
|
|
return 0
|
|
|
|
elif rc != 0:
|
2018-06-08 14:29:43 +02:00
|
|
|
print("%s exited with code %d" % (docker_pre, rc))
|
2016-07-19 15:20:38 +02:00
|
|
|
return 1
|
|
|
|
|
2017-06-02 20:56:08 +02:00
|
|
|
# Copy any extra files into the Docker context. These can be
|
|
|
|
# included by the use of the ADD directive in the Dockerfile.
|
2017-06-02 20:56:09 +02:00
|
|
|
cksum = []
|
2016-07-19 15:20:37 +02:00
|
|
|
if args.include_executable:
|
2017-06-02 20:56:09 +02:00
|
|
|
# FIXME: there is no checksum of this executable and the linked
|
|
|
|
# libraries, once the image built any change of this executable
|
|
|
|
# or any library won't trigger another build.
|
2019-01-23 18:07:08 +01:00
|
|
|
_copy_binary_with_libs(args.include_executable,
|
|
|
|
qpath, docker_dir)
|
|
|
|
|
2017-06-02 20:56:08 +02:00
|
|
|
for filename in args.extra_files or []:
|
|
|
|
_copy_with_mkdir(filename, docker_dir)
|
2018-06-08 16:20:25 +02:00
|
|
|
cksum += [(filename, _file_checksum(filename))]
|
2016-07-19 15:20:37 +02:00
|
|
|
|
2017-03-06 21:55:20 +01:00
|
|
|
argv += ["--build-arg=" + k.lower() + "=" + v
|
2019-08-29 13:08:27 +02:00
|
|
|
for k, v in os.environ.items()
|
2019-01-23 18:13:55 +01:00
|
|
|
if k.lower() in FILTERED_ENV_NAMES]
|
2016-07-19 15:20:36 +02:00
|
|
|
dkr.build_image(tag, docker_dir, dockerfile,
|
2020-07-01 15:56:36 +02:00
|
|
|
quiet=args.quiet, user=args.user,
|
|
|
|
argv=argv, registry=args.registry,
|
2017-06-02 20:56:09 +02:00
|
|
|
extra_files_cksum=cksum)
|
2016-07-19 15:20:36 +02:00
|
|
|
|
|
|
|
rmtree(docker_dir)
|
2016-06-01 06:25:14 +02:00
|
|
|
|
|
|
|
return 0
|
|
|
|
|
2021-05-12 12:20:25 +02:00
|
|
|
class FetchCommand(SubCommand):
|
|
|
|
""" Fetch a docker image from the registry. Args: <tag> <registry>"""
|
|
|
|
name = "fetch"
|
|
|
|
|
|
|
|
def args(self, parser):
|
|
|
|
parser.add_argument("tag",
|
|
|
|
help="Local tag for image")
|
|
|
|
parser.add_argument("registry",
|
|
|
|
help="Docker registry")
|
|
|
|
|
|
|
|
def run(self, args, argv):
|
|
|
|
dkr = Docker()
|
|
|
|
dkr.command(cmd="pull", quiet=args.quiet,
|
|
|
|
argv=["%s/%s" % (args.registry, args.tag)])
|
|
|
|
dkr.command(cmd="tag", quiet=args.quiet,
|
|
|
|
argv=["%s/%s" % (args.registry, args.tag), args.tag])
|
|
|
|
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2016-07-19 15:20:40 +02:00
|
|
|
class UpdateCommand(SubCommand):
|
2021-05-12 12:20:24 +02:00
|
|
|
""" Update a docker image. Args: <tag> <actions>"""
|
2016-07-19 15:20:40 +02:00
|
|
|
name = "update"
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2016-07-19 15:20:40 +02:00
|
|
|
def args(self, parser):
|
|
|
|
parser.add_argument("tag",
|
|
|
|
help="Image Tag")
|
2021-05-12 12:20:23 +02:00
|
|
|
parser.add_argument("--executable",
|
2016-07-19 15:20:40 +02:00
|
|
|
help="Executable to copy")
|
2021-05-12 12:20:24 +02:00
|
|
|
parser.add_argument("--add-current-user", "-u", dest="user",
|
|
|
|
action="store_true",
|
|
|
|
help="Add the current user to image's passwd")
|
2016-07-19 15:20:40 +02:00
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
# Create a Docker buildfile
|
|
|
|
df = StringIO()
|
2020-07-24 08:45:02 +02:00
|
|
|
df.write(u"FROM %s\n" % args.tag)
|
2021-05-12 12:20:23 +02:00
|
|
|
|
|
|
|
if args.executable:
|
|
|
|
# Add the executable to the tarball, using the current
|
|
|
|
# configured binfmt_misc path. If we don't get a path then we
|
|
|
|
# only need the support libraries copied
|
|
|
|
ff, enabled = _check_binfmt_misc(args.executable)
|
|
|
|
|
|
|
|
if not enabled:
|
|
|
|
print("binfmt_misc not enabled, update disabled")
|
|
|
|
return 1
|
|
|
|
|
|
|
|
if ff:
|
|
|
|
tmp_tar.add(args.executable, arcname=ff)
|
|
|
|
|
|
|
|
# Add any associated libraries
|
|
|
|
libs = _get_so_libs(args.executable)
|
|
|
|
if libs:
|
|
|
|
for l in libs:
|
|
|
|
so_path = os.path.dirname(l)
|
|
|
|
name = os.path.basename(l)
|
|
|
|
real_l = os.path.realpath(l)
|
|
|
|
try:
|
|
|
|
tmp_tar.add(real_l, arcname="%s/%s" % (so_path, name))
|
|
|
|
except FileNotFoundError:
|
|
|
|
print("Couldn't add %s/%s to archive" % (so_path, name))
|
|
|
|
pass
|
|
|
|
|
|
|
|
df.write(u"ADD . /\n")
|
2020-07-24 08:45:02 +02:00
|
|
|
|
2021-05-12 12:20:24 +02:00
|
|
|
if args.user:
|
|
|
|
uid = os.getuid()
|
2023-03-06 13:27:43 +01:00
|
|
|
uname = getpass.getuser()
|
2021-05-12 12:20:24 +02:00
|
|
|
df.write("\n")
|
|
|
|
df.write("RUN id %s 2>/dev/null || useradd -u %d -U %s" %
|
|
|
|
(uname, uid, uname))
|
|
|
|
|
2020-07-24 08:45:02 +02:00
|
|
|
df_bytes = BytesIO(bytes(df.getvalue(), "UTF-8"))
|
2016-07-19 15:20:40 +02:00
|
|
|
|
|
|
|
df_tar = TarInfo(name="Dockerfile")
|
2020-07-24 08:45:02 +02:00
|
|
|
df_tar.size = df_bytes.getbuffer().nbytes
|
|
|
|
tmp_tar.addfile(df_tar, fileobj=df_bytes)
|
2016-07-19 15:20:40 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
class CleanCommand(SubCommand):
|
|
|
|
"""Clean up docker instances"""
|
|
|
|
name = "clean"
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
def run(self, args, argv):
|
|
|
|
Docker().clean()
|
|
|
|
return 0
|
|
|
|
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2016-07-19 15:20:43 +02:00
|
|
|
class ImagesCommand(SubCommand):
|
|
|
|
"""Run "docker images" command"""
|
|
|
|
name = "images"
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2016-07-19 15:20:43 +02:00
|
|
|
def run(self, args, argv):
|
|
|
|
return Docker().command("images", argv, args.quiet)
|
|
|
|
|
2018-05-10 11:45:55 +02:00
|
|
|
|
|
|
|
class ProbeCommand(SubCommand):
|
|
|
|
"""Probe if we can run docker automatically"""
|
|
|
|
name = "probe"
|
|
|
|
|
|
|
|
def run(self, args, argv):
|
|
|
|
try:
|
|
|
|
docker = Docker()
|
|
|
|
if docker._command[0] == "docker":
|
2019-09-03 11:33:39 +02:00
|
|
|
print("docker")
|
2018-05-10 11:45:55 +02:00
|
|
|
elif docker._command[0] == "sudo":
|
2019-09-03 11:33:39 +02:00
|
|
|
print("sudo docker")
|
2019-07-12 14:46:13 +02:00
|
|
|
elif docker._command[0] == "podman":
|
|
|
|
print("podman")
|
2018-05-10 11:45:55 +02:00
|
|
|
except Exception:
|
2018-06-08 14:29:43 +02:00
|
|
|
print("no")
|
2018-05-10 11:45:55 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2018-04-12 17:49:11 +02:00
|
|
|
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")
|
2018-04-12 18:18:12 +02:00
|
|
|
parser.add_argument("--cc", default="cc",
|
|
|
|
help="The compiler executable to call")
|
2018-04-12 17:49:11 +02:00
|
|
|
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()
|
2020-09-17 12:44:41 +02:00
|
|
|
cmd = ["-w", cwd,
|
2018-04-12 17:49:11 +02:00
|
|
|
"-v", "%s:%s:rw" % (cwd, cwd)]
|
|
|
|
if args.paths:
|
|
|
|
for p in args.paths:
|
|
|
|
cmd += ["-v", "%s:%s:ro,z" % (p, p)]
|
2018-04-12 18:18:12 +02:00
|
|
|
cmd += [args.image, args.cc]
|
2018-04-12 17:49:11 +02:00
|
|
|
cmd += argv
|
2019-09-04 11:07:17 +02:00
|
|
|
return Docker().run(cmd, False, quiet=args.quiet,
|
|
|
|
as_user=True)
|
2018-04-12 17:49:11 +02:00
|
|
|
|
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
def main():
|
2019-07-12 14:46:13 +02:00
|
|
|
global USE_ENGINE
|
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
parser = argparse.ArgumentParser(description="A Docker helper",
|
2019-01-23 18:13:55 +01:00
|
|
|
usage="%s <subcommand> ..." %
|
|
|
|
os.path.basename(sys.argv[0]))
|
2019-07-12 14:46:13 +02:00
|
|
|
parser.add_argument("--engine", type=EngineEnum.argparse, choices=list(EngineEnum),
|
|
|
|
help="specify which container engine to use")
|
2016-06-01 06:25:14 +02:00
|
|
|
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()
|
2019-09-03 11:33:39 +02:00
|
|
|
if args.engine:
|
|
|
|
USE_ENGINE = args.engine
|
2016-06-01 06:25:14 +02:00
|
|
|
return args.cmdobj.run(args, argv)
|
|
|
|
|
2019-01-23 18:13:55 +01:00
|
|
|
|
2016-06-01 06:25:14 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main())
|