Skip to content

Commit

Permalink
oci-as-buildroot
Browse files Browse the repository at this point in the history
  • Loading branch information
praiskup committed Oct 29, 2024
1 parent 15c2b9b commit b32169c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mock/py/mockbuild/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -234,6 +234,8 @@ def setup_default_config_opts():
'process-distgit',
]
},
'oci_as_buildroot_enable': True,
'oci_as_buildroot_opts': {},
}

config_opts['environment'] = {
Expand Down
51 changes: 51 additions & 0 deletions mock/py/mockbuild/plugins/oci_as_buildroot.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit b32169c

Please sign in to comment.