#109 Install extra packages in docker inventory
Opened 6 years ago by flepied. Modified 6 years ago
flepied/standard-test-roles docker-extra-rpms  into  master

Install extra package in docker inventory
Frederic Lepied • 6 years ago  
@@ -39,16 +39,20 @@ 

  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 @@ 

      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 @@ 

      ] + 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 @@ 

      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]

Fedora vs. CentOS/RHEL

+         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 @@ 

      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))

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

why not adding a task to install those packages in your playbook?

I would propose to stick to https://fedoraproject.org/wiki/Changes/InvokingTests

There is a definition:

Test Subject: The items that are to be tested.
Examples: RPMs, OCI image, ISO, QCow2, Module repository ...

In this case Test Subject is: Docker image.
New RPMs are not test subject.

Let's keep clear and easy to understand workflow.

I would go for: clear and transparent and independent step: preparing TEST_SUBJECT.

Do not squash two task into one. This will bring more confusion.

Metadata