diff --git a/mock/py/mockbuild/config.py b/mock/py/mockbuild/config.py index 533d9f3ce..e786506ef 100644 --- a/mock/py/mockbuild/config.py +++ b/mock/py/mockbuild/config.py @@ -31,7 +31,7 @@ 'ccache', 'selinux', 'package_state', 'chroot_scan', 'lvm_root', 'compress_logs', 'sign', 'pm_request', 'hw_info', 'procenv', 'showrc', 'rpkg_preprocessor', - 'rpmautospec', 'buildroot_lock'] + 'rpmautospec', 'buildroot_lock', 'oci_as_buildroot'] def nspawn_supported(): """Detect some situations where the systemd-nspawn chroot code won't work""" @@ -234,6 +234,8 @@ def setup_default_config_opts(): 'process-distgit', ] }, + 'oci_as_buildroot_enable': True, + 'oci_as_buildroot_opts': {}, } config_opts['environment'] = { diff --git a/mock/py/mockbuild/plugins/oci_as_buildroot.py b/mock/py/mockbuild/plugins/oci_as_buildroot.py new file mode 100644 index 000000000..9d19da294 --- /dev/null +++ b/mock/py/mockbuild/plugins/oci_as_buildroot.py @@ -0,0 +1,51 @@ +""" +Generate OCI from prepared build chroot. +Use given OCI image as build chroot (TODO). +""" + +import os +import mockbuild.util +from mockbuild.trace_decorator import getLog + +requires_api_version = "1.1" + + +def init(plugins, conf, buildroot): + """ The obligatory plugin entry point """ + OCIAsBuildroot(plugins, conf, buildroot) + + +class OCIAsBuildroot: + """ + OCIAsBuildroot plugin (class). + """ + def __init__(self, plugins, conf, buildroot): + self.buildroot = buildroot + self.state = buildroot.state + self.conf = conf + plugins.add_hook("postdeps", self.produce_buildroot_image) + + def do(self, cmd): + """ Execute command on host (as root) """ + getLog().info("Executing %s", ' '.join(cmd)) + mockbuild.util.do(cmd) + + def _produce_image_as_root(self): + name = f"mock-container-{self.buildroot.config['root']}" + # TODO: chain build produces multiple buildroots + tarball = os.path.join(self.buildroot.resultdir, "buildroot-oci.tar") + chroot = self.buildroot.make_chroot_path() + self.do(["buildah", "from", "--name", name, "scratch"]) + self.do(["buildah", "add", name, chroot, "/"]) + self.do(["buildah", "commit", "--format", "oci", name, + "oci-archive:" + tarball]) + self.do(["buildah", "rm", name]) + + def produce_buildroot_image(self): + """ Generate OCI image from buildroot using Buildah """ + try: + self.state.start("producing buildroot as OCI image") + with self.buildroot.uid_manager.elevated_privileges(): + self._produce_image_as_root() + finally: + self.state.finish("producing buildroot as OCI image")