[libvirt PATCH v2 29/35] ci: helper: Rework _lcitool_run method logic

Erik Skultety posted 35 patches 1 year, 3 months ago
[libvirt PATCH v2 29/35] ci: helper: Rework _lcitool_run method logic
Posted by Erik Skultety 1 year, 3 months ago
This method wasn't even utilized before this patch. This patch adds all
the necessary logic to successfully execute a container workload via
lcitool (which will later allow us to ditch ci/Makefile). Because
container executions via lcitool creates the following inside the
container:

    $ ls
    script datadir

where 'datadir' is the workload directory (in this case a local git
repo clone) and 'script' is the code that runs whatever the workload is
over 'datadir'.

In order to satisfy the ^above, our helper generates a trivial
temporary 'script' that will source ci/build.sh and run whatever was
specified as --job essentially to simulate the exact steps a GitLab
pipeline job would go through.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Erik Skultety <eskultet@redhat.com>:
---
 ci/helper | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 2 deletions(-)

diff --git a/ci/helper b/ci/helper
index 392702ae41..fce370f995 100755
--- a/ci/helper
+++ b/ci/helper
@@ -11,6 +11,9 @@ import subprocess
 import sys
 import textwrap
 
+from pathlib import Path
+from tempfile import TemporaryDirectory
+
 import util
 
 
@@ -201,8 +204,56 @@ class Application:
         return repo.clone(dest, local=True)
 
     def _lcitool_run(self, args):
-        output = subprocess.check_output([self._args.lcitool] + args)
-        return output.decode("utf-8")
+        positional_args = ["container"]
+        opts = ["--user", self._args.login]
+        tmpdir = TemporaryDirectory(prefix="scratch",
+                                    dir=Path(self.repo.working_dir, "ci"))
+
+        repo_dest_path = Path(tmpdir.name, "libvirt.git").as_posix()
+        repo_clone = self._prepare_repo_copy(self.repo, repo_dest_path)
+        opts.extend(["--workload-dir", repo_clone.working_dir])
+
+        if self._args.job == "shell":
+            positional_args.append("shell")
+        else:
+            job2func = {
+                "test": "run_test",
+                "build": "run_build",
+                "codestyle": "run_codestyle",
+                "potfile": "run_potfile",
+                "rpmbuild": "run_rpmbuild",
+                "website": "run_website_build",
+            }
+
+            if self._args.engine != "auto":
+                positional_args.extend(["--engine", self._args.engine])
+
+            with open(Path(tmpdir.name, "script"), "w") as f:
+                script_path = f.name
+                contents = textwrap.dedent(f"""\
+                #!/bin/sh
+
+                cd datadir
+                . ci/jobs.sh
+
+                {job2func[self._args.job]}
+                """)
+
+                f.write(contents)
+
+            positional_args.append("run")
+            opts.extend(["--script", script_path])
+
+        opts.append(f"{self._args.image_prefix}{self._args.target}:{self._args.image_tag}")
+        proc = None
+        try:
+            proc = subprocess.run([self._args.lcitool] + positional_args + opts)
+        except KeyboardInterrupt:
+            sys.exit(1)
+        finally:
+            # this will take care of the generated script file above as well
+            tmpdir.cleanup()
+        return proc.returncode
 
     def _check_stale_images(self):
         namespace = self._args.namespace
-- 
2.41.0