diff --git a/src/cosalib/buildah.py b/src/cosalib/buildah.py new file mode 100644 index 0000000000..2746e58162 --- /dev/null +++ b/src/cosalib/buildah.py @@ -0,0 +1,16 @@ +import os + +# https://access.redhat.com/documentation/en-us/openshift_container_platform/4.1/html/builds/custom-builds-buildah +NESTED_BUILD_ARGS = ['--storage-driver', 'vfs'] + + +def buildah_base_args(containers_storage=None): + buildah_base_argv = ['buildah'] + if containers_storage is not None: + buildah_base_argv.append(f"--root={containers_storage}") + if os.environ.get('container') is not None: + print("Using nested container mode due to container environment variable") + buildah_base_argv.extend(NESTED_BUILD_ARGS) + else: + print("Skipping nested container mode") + return buildah_base_argv diff --git a/src/cosalib/kubevirt.py b/src/cosalib/kubevirt.py index f7650a2dbf..8771fb5500 100644 --- a/src/cosalib/kubevirt.py +++ b/src/cosalib/kubevirt.py @@ -3,7 +3,11 @@ import logging as log from cosalib.cmdlib import ( - run_verbose + run_verbose, +) + +from cosalib.buildah import ( + buildah_base_args ) from cosalib.qemuvariants import QemuVariantImage @@ -26,12 +30,13 @@ def write_oci(self, image_name): """ Take the qcow2 base image and convert it to an oci-archive. """ + buildah_base_argv = buildah_base_args() final_img = os.path.join(os.path.abspath(self.build_dir), self.image_name) - buildah_img = run_verbose(["buildah", "from", "scratch"], stdout=subprocess.PIPE).stdout.decode("utf-8").strip() - run_verbose(["buildah", "add", "--chmod", "0555", buildah_img, image_name, "/disk/coreos.img"]) - digest = run_verbose(["buildah", "commit", buildah_img], stdout=subprocess.PIPE).stdout.decode("utf-8").strip() - run_verbose(["buildah", "push", "--format", "oci", digest, f"oci-archive:{final_img}"]) + buildah_img = run_verbose_collect_stdout(buildah_base_argv + ["from", "scratch"]) + run_verbose(buildah_base_argv + ["add", "--chmod", "0555", buildah_img, image_name, "/disk/coreos.img"]) + digest = run_verbose_collect_stdout(buildah_base_argv + ["commit", buildah_img]) + run_verbose(buildah_base_argv + ["push", "--format", "oci", digest, f"oci-archive:{final_img}"]) def kubevirt_run_ore(build, args): @@ -83,3 +88,7 @@ def get_kubevirt_variant(variant, parser, kwargs={}): arch=parser.arch, compress=parser.compress, **kwargs) + + +def run_verbose_collect_stdout(args): + return run_verbose(args, stdout=subprocess.PIPE).stdout.decode("utf-8").strip() diff --git a/src/oscontainer.py b/src/oscontainer.py index 733352bb71..136648c87c 100755 --- a/src/oscontainer.py +++ b/src/oscontainer.py @@ -20,12 +20,12 @@ import shutil import subprocess from cosalib import cmdlib +from cosalib.buildah import ( + buildah_base_args +) OSCONTAINER_COMMIT_LABEL = 'com.coreos.ostree-commit' -# https://access.redhat.com/documentation/en-us/openshift_container_platform/4.1/html/builds/custom-builds-buildah -NESTED_BUILD_ARGS = ['--storage-driver', 'vfs'] - def run_get_json(args): return json.loads(subprocess.check_output(args)) @@ -113,14 +113,7 @@ def oscontainer_build(containers_storage, tmpdir, src, ref, image_name_and_tag, else: ostree_version = None - buildah_base_argv = ['buildah'] - if containers_storage is not None: - buildah_base_argv.append(f"--root={containers_storage}") - if os.environ.get('container') is not None: - print("Using nested container mode due to container environment variable") - buildah_base_argv.extend(NESTED_BUILD_ARGS) - else: - print("Skipping nested container mode") + buildah_base_argv = buildah_base_args(containers_storage) # In general, we just stick with the default tmpdir set up. But if a # workdir is provided, then we want to be sure that all the heavy I/O work