From 7c30a35701e6923e0197dee2caaacf66e94da490 Mon Sep 17 00:00:00 2001 From: Frederic Lepied Date: Jan 28 2018 20:42:06 +0000 Subject: Install extra package in docker inventory Use TEST_SUBJECTS to specify the extra packages. ex: export TEST_SUBJECTS="docker:docker.io/library/fedora:27 /home/fred/RPM/RPMS/x86_64/systemd-236-0.1.20171207151953.e97b7b5.fc27.x86_64.rpm /home/fred/RPM/RPMS/x86_64/systemd-libs-236-0.1.20171207151953.e97b7b5.fc27.x86_64.rpm /home/fred/RPM/RPMS/x86_64/systemd-pam-236-0.1.20171207151953.e97b7b5.fc27.x86_64.rpm" Fixes: #106 --- diff --git a/inventory/standard-inventory-docker b/inventory/standard-inventory-docker index b8d625d..98bd197 100755 --- a/inventory/standard-inventory-docker +++ b/inventory/standard-inventory-docker @@ -39,16 +39,20 @@ def main(argv): def list(subjects, docker_extra_args): hosts = [ ] variables = { } + rpms = [] + for subject in subjects: + if subject.endswith(".rpm"): + rpms.append(subject) for subject in subjects: if subject.startswith("docker:"): image = subject[7:] - name, vars = host(image, docker_extra_args) + name, vars = host(image, docker_extra_args, rpms) if vars: hosts.append(name) variables[name] = vars return { "localhost": { "hosts": hosts, "vars": { } }, "subjects": { "hosts": hosts, "vars": { } }, "_meta": { "hostvars": variables } } -def host(image, docker_extra_args): +def host(image, docker_extra_args, rpms=None): null = open(os.devnull, 'w') try: @@ -79,10 +83,7 @@ def host(image, docker_extra_args): cmd = [ "/usr/sbin/service", "docker", "start" ] - try: - subprocess.check_call(cmd, stdout=sys.stderr.fileno()) - except subprocess.CalledProcessError as ex: - raise RuntimeError("Could not start docker service") + run_cmd(cmd, "Could not start docker service") # And launch the actual container cmd = [ @@ -90,10 +91,7 @@ def host(image, docker_extra_args): ] + extra_arg_list + [ "--entrypoint=/bin/sh", image, "-c", "sleep 1000000" ] - try: - subprocess.check_call(cmd, stdout=sys.stderr.fileno()) - except subprocess.CalledProcessError as ex: - raise RuntimeError("Could not start container image: {0}".format(image)) + run_cmd(cmd, "Could not start container image: {0}".format(image)) # Read out the container environment variable for x in range(1, 90): @@ -106,15 +104,26 @@ def host(image, docker_extra_args): with open(cidfile, "r") as f: name = f.read().strip() + if len(rpms) > 0: + sys.stderr.write("Installing %d extra rpms in container\n" % len(rpms)) + + # Copy the necessary packages in the container + for rpm in rpms: + copy = ["/usr/bin/docker", "cp", rpm, '%s:/var/tmp/' % name] + run_cmd(copy, "Could not copy extra packages in launched container") + + # Install the necessary packages in the container + install = ["/usr/bin/docker", "exec", "--user=root", name, + "/usr/bin/yum", "-y", "install"] + ['/var/tmp/%s' % os.path.basename(rpm) for rpm in rpms] + run_cmd(install, "Could not install extra packages in launched container") + # Now install the necessary stuff in the container :S + sys.stderr.write("Installing python runtime in container\n") install = [ "/usr/bin/docker", "exec", "--user=root", name, "/usr/bin/yum", "-y", "install", "python2", "python2-dnf", "libselinux-python" ] - try: - subprocess.check_call(install, stdout=sys.stderr.fileno()) - except subprocess.CalledProcessError as ex: - raise RuntimeError("Could not install Ansible dependencies in launched container") + run_cmd(install, "Could not install Ansible dependencies in launched container") # Directory to place artifacts artifacts = os.environ.get("TEST_ARTIFACTS", os.path.join(os.getcwd(), "artifacts")) @@ -181,5 +190,13 @@ def host(image, docker_extra_args): shutil.rmtree(directory) sys.exit(0) + +def run_cmd(cmd, error_msg): + try: + subprocess.check_call(cmd, stdout=sys.stderr.fileno()) + except subprocess.CalledProcessError as ex: + raise RuntimeError("%s: %s" % (error_msg, str(ex))) + + if __name__ == '__main__': sys.exit(main(sys.argv))