]> git.ipfire.org Git - pakfire.git/commitdiff
builder: Automatically create/restore snapshots
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 24 Mar 2021 10:25:07 +0000 (10:25 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 24 Mar 2021 10:25:07 +0000 (10:25 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/builder.py
src/pakfire/constants.py

index 0d7fe0f44ab8a463be6d8b0f694a342371988e71..e90988c8d2630914d58e2a7ef22d74155b774291 100644 (file)
@@ -49,6 +49,18 @@ BUILD_LOG_HEADER = """
 
 """
 
+BUILD_PACKAGES = [
+       "@Build",
+       "ccache",
+       "pakfire-build",
+       "/bin/bash",
+
+       # Handy shell tools
+       "elinks",
+       "less",
+       "vim",
+]
+
 BUILD_SCRIPT = """#!/bin/bash --login
 
 set -e
@@ -168,6 +180,9 @@ class BuilderContext(object):
                # Get a reference to the logger
                self.log = self.builder.log
 
+               # Setup the environment
+               self._setup()
+
        @property
        def environ(self):
                # Build a minimal environment for executing, but try to inherit TERM and LANG
@@ -192,6 +207,24 @@ class BuilderContext(object):
 
                return env
 
+       def _setup(self):
+               """
+                       Sets up the environment by installing some basic packages
+               """
+               # XXX this needs to be unique for each distribution
+               snapshot_path = "/tmp/pakfire-snapshot.tar.zst"
+
+               # Restore the snapshot if available
+               try:
+                       self.pakfire.restore_snapshot(snapshot_path)
+               except FileNotFoundError:
+                       pass
+
+               # Install any updates and essential packages
+               # If there have been updates, or on a fresh install, re-create the snapshot
+               if self._install(BUILD_PACKAGES):
+                       self.pakfire.create_snapshot(snapshot_path)
+
        def _install(self, packages):
                self.log.debug(_("Installing packages in build environment:"))
                for package in packages:
@@ -202,6 +235,9 @@ class BuilderContext(object):
                        # Install all required packages
                        transaction = p.install(packages)
 
+                       # Return how many packages were installed/changed
+                       ret = len(transaction)
+
                        # Dump transaction to log
                        t = transaction.dump()
                        self.log.info(t)
@@ -212,16 +248,9 @@ class BuilderContext(object):
                        # Run the transaction
                        transaction.run()
 
-       def build(self, path, shell=True):
-               # Install build environment
-               packages = [
-                       "@Build",
-               ]
-
-               # If we have ccache enabled, we need to install it, too
-               if self.builder.settings.get("enable_ccache"):
-                       packages.append("ccache")
+                       return ret
 
+       def build(self, path, shell=True):
                # Open the package archive
                archive = _pakfire.Archive(self.pakfire, path)
 
@@ -230,10 +259,10 @@ class BuilderContext(object):
 
                requires = archive.get("dependencies", "requires")
                if requires:
-                       packages += requires.splitlines()
+                       packages = requires.splitlines()
 
-               # Setup the environment including any build dependencies
-               self._install(packages)
+                       # Setup the environment including any build dependencies
+                       self._install(packages)
 
                # Extract the source archive (if we have one)
                if archive:
@@ -284,24 +313,19 @@ class BuilderContext(object):
                        archive = _pakfire.Archive(self.pakfire, path)
                        archives.append(archive)
 
-               # Collect packages to install
-               packages = []
-
-               # Install our standard shell packages
-               packages += SHELL_PACKAGES
-
                # Install any packages the user requested
-               if install:
-                       packages += install
+               if not install:
+                       install = []
 
                # Install all build requirements for archives
                for archive in archives:
                        requires = archive.get("dependencies.requires")
                        if requires:
-                               packages += requires.splitlines()
+                               install += requires.splitlines()
 
                # Install all required packages
-               self._install(packages)
+               if install:
+                       self._install(install)
 
                # Extract archives
                for archive in archives:
index ae75c74d75606c47294c4b7a5a39305265d9fe3d..368ce55cc4b953c8ed1d01b937c3795d05ca69b7 100644 (file)
@@ -67,8 +67,6 @@ DATABASE_FORMATS_SUPPORTED = [0, 1, 2, 3, 4, 5, 6, 7]
 PACKAGE_FILENAME_FMT = "%(name)s-%(version)s-%(release)s.%(arch)s.%(ext)s"
 
 # A script that is called, when a user is dropped to a chroot shell.
-SHELL_SCRIPT = "/usr/lib/pakfire/chroot-shell"
-SHELL_PACKAGES = ["elinks", "less", "vim", SHELL_SCRIPT,]
 BUILD_ROOT = "/var/lib/pakfire/build"
 
 SOURCE_CACHE_DIR = os.path.join(CACHE_DIR, "sources")