]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Move CentOS family to DistributionInstaller
authorJoerg Behrmann <behrmann@physik.fu-berlin.de>
Thu, 24 Nov 2022 13:54:35 +0000 (14:54 +0100)
committerJoerg Behrmann <behrmann@physik.fu-berlin.de>
Thu, 24 Nov 2022 14:58:00 +0000 (15:58 +0100)
mkosi/__init__.py
mkosi/distributions/alma.py [new file with mode: 0644]
mkosi/distributions/alma_epel.py [new file with mode: 0644]
mkosi/distributions/centos.py [new file with mode: 0644]
mkosi/distributions/centos_epel.py [new file with mode: 0644]
mkosi/distributions/rocky.py [new file with mode: 0644]
mkosi/distributions/rocky_epel.py [new file with mode: 0644]

index 150c615dd6bdf197d5fb11166ab6a98fc55c83c7..981824244be8b2e27c6acc144c8cff9552d7d9f6 100644 (file)
@@ -1290,10 +1290,6 @@ def configure_hostname(state: MkosiState, cached: bool) -> None:
 def mount_cache(state: MkosiState) -> Iterator[None]:
     if state.installer is not None:
         cache_paths = state.installer.cache_path()
-    elif is_centos_variant(state.config.distribution):
-        # We mount both the YUM and the DNF cache in this case, as YUM might
-        # just be redirected to DNF even if we invoke the former
-        cache_paths = ["var/cache/yum", "var/cache/dnf"]
     else:
         cache_paths = []
 
@@ -1705,131 +1701,6 @@ def setup_dnf(state: MkosiState, repos: Sequence[Repo] = ()) -> None:
     )
 
 
-
-
-def centos_variant_gpg_locations(distribution: Distribution, epel_release: int) -> Tuple[Path, str]:
-    if distribution in (Distribution.centos, Distribution.centos_epel):
-        return (
-            Path("/etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial"),
-            "https://www.centos.org/keys/RPM-GPG-KEY-CentOS-Official"
-        )
-    elif distribution in (Distribution.alma, Distribution.alma_epel):
-        return (
-            Path("/etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux"),
-            "https://repo.almalinux.org/almalinux/RPM-GPG-KEY-AlmaLinux"
-        )
-    elif distribution in (Distribution.rocky, Distribution.rocky_epel):
-        if epel_release >= 9:
-            keyname = f"Rocky-{epel_release}"
-        else:
-            keyname = "rockyofficial"
-
-        return (
-             Path(f"/etc/pki/rpm-gpg/RPM-GPG-KEY-{keyname}"),
-             f"https://download.rockylinux.org/pub/rocky/RPM-GPG-KEY-{keyname}"
-        )
-    else:
-        die(f"{distribution} is not a CentOS variant")
-
-
-def epel_gpg_locations(epel_release: int) -> Tuple[Path, str]:
-    return (
-        Path(f"/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-{epel_release}"),
-        f"https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-{epel_release}",
-    )
-
-
-def centos_variant_mirror_directory(distribution: Distribution) -> str:
-    if distribution in (Distribution.centos, Distribution.centos_epel):
-        return "centos"
-    elif distribution in (Distribution.alma, Distribution.alma_epel):
-        return "almalinux"
-    elif distribution in (Distribution.rocky, Distribution.rocky_epel):
-        return "rocky"
-    else:
-        die(f"{distribution} is not a CentOS variant")
-
-
-def centos_variant_mirror_repo_url(config: MkosiConfig, repo: str) -> str:
-    if config.distribution in (Distribution.centos, Distribution.centos_epel):
-        return f"http://mirrorlist.centos.org/?release={config.release}&arch=$basearch&repo={repo}"
-    elif config.distribution in (Distribution.alma, Distribution.alma_epel):
-        return f"https://mirrors.almalinux.org/mirrorlist/{config.release}/{repo.lower()}"
-    elif config.distribution in (Distribution.rocky, Distribution.rocky_epel):
-        return f"https://mirrors.rockylinux.org/mirrorlist?arch=$basearch&repo={repo}-{config.release}"
-    else:
-        die(f"{config.distribution} is not a CentOS variant")
-
-
-def centos_variant_repos(config: MkosiConfig, epel_release: int) -> List[Repo]:
-    # Repos for CentOS Linux 8, CentOS Stream 8 and CentOS variants
-
-    directory = centos_variant_mirror_directory(config.distribution)
-    gpgpath, gpgurl = centos_variant_gpg_locations(config.distribution, epel_release)
-    epel_gpgpath, epel_gpgurl = epel_gpg_locations(epel_release)
-
-    if config.local_mirror:
-        appstream_url = f"baseurl={config.local_mirror}"
-        baseos_url = extras_url = powertools_url = epel_url = None
-    elif config.mirror:
-        appstream_url = f"baseurl={config.mirror}/{directory}/{config.release}/AppStream/$basearch/os"
-        baseos_url = f"baseurl={config.mirror}/{directory}/{config.release}/BaseOS/$basearch/os"
-        extras_url = f"baseurl={config.mirror}/{directory}/{config.release}/extras/$basearch/os"
-        powertools_url = f"baseurl={config.mirror}/{directory}/{config.release}/PowerTools/$basearch/os"
-        epel_url = f"baseurl={config.mirror}/epel/{epel_release}/Everything/$basearch"
-    else:
-        appstream_url = f"mirrorlist={centos_variant_mirror_repo_url(config, 'AppStream')}"
-        baseos_url = f"mirrorlist={centos_variant_mirror_repo_url(config, 'BaseOS')}"
-        extras_url = f"mirrorlist={centos_variant_mirror_repo_url(config, 'extras')}"
-        powertools_url = f"mirrorlist={centos_variant_mirror_repo_url(config, 'PowerTools')}"
-        epel_url = f"mirrorlist=https://mirrors.fedoraproject.org/mirrorlist?repo=epel-{epel_release}&arch=$basearch"
-
-    repos = [Repo("AppStream", appstream_url, gpgpath, gpgurl)]
-    if baseos_url is not None:
-        repos += [Repo("BaseOS", baseos_url, gpgpath, gpgurl)]
-    if extras_url is not None:
-        repos += [Repo("extras", extras_url, gpgpath, gpgurl)]
-    if powertools_url is not None:
-        repos += [Repo("PowerTools", powertools_url, gpgpath, gpgurl)]
-    if epel_url is not None and is_epel_variant(config.distribution):
-        repos += [Repo("epel", epel_url, epel_gpgpath, epel_gpgurl)]
-
-    return repos
-
-
-def centos_stream_repos(config: MkosiConfig, epel_release: int) -> List[Repo]:
-    # Repos for CentOS Stream 9 and later
-
-    gpgpath, gpgurl = centos_variant_gpg_locations(config.distribution, epel_release)
-    epel_gpgpath, epel_gpgurl = epel_gpg_locations(epel_release)
-
-    release = f"{epel_release}-stream"
-
-    if config.local_mirror:
-        appstream_url = f"baseurl={config.local_mirror}"
-        baseos_url = crb_url = epel_url = None
-    elif config.mirror:
-        appstream_url = f"baseurl={config.mirror}/centos-stream/{release}/AppStream/$basearch/os"
-        baseos_url = f"baseurl={config.mirror}/centos-stream/{release}/BaseOS/$basearch/os"
-        crb_url = f"baseurl={config.mirror}/centos-stream/{release}/CRB/$basearch/os"
-        epel_url = f"baseurl={config.mirror}/epel/{epel_release}/Everything/$basearch"
-    else:
-        appstream_url = f"metalink=https://mirrors.centos.org/metalink?repo=centos-appstream-{release}&arch=$basearch"
-        baseos_url = f"metalink=https://mirrors.centos.org/metalink?repo=centos-baseos-{release}&arch=$basearch"
-        crb_url = f"metalink=https://mirrors.centos.org/metalink?repo=centos-crb-{release}&arch=$basearch"
-        epel_url = f"mirrorlist=https://mirrors.fedoraproject.org/mirrorlist?repo=epel-{epel_release}&arch=$basearch"
-
-    repos = [Repo("AppStream", appstream_url, gpgpath, gpgurl)]
-    if baseos_url is not None:
-        repos += [Repo("BaseOS", baseos_url, gpgpath, gpgurl)]
-    if crb_url is not None:
-        repos += [Repo("CRB", crb_url, gpgpath, gpgurl)]
-    if epel_url is not None and is_epel_variant(config.distribution):
-        repos += [Repo("epel", epel_url, epel_gpgpath, epel_gpgurl)]
-
-    return repos
-
-
 def parse_epel_release(release: str) -> int:
     fields = release.split(".")
     if fields[0].endswith("-stream"):
@@ -1840,52 +1711,6 @@ def parse_epel_release(release: str) -> int:
     return int(epel_release)
 
 
-@complete_step("Installing CentOS…")
-def install_centos_variant(state: MkosiState) -> None:
-    epel_release = parse_epel_release(state.config.release)
-
-    if epel_release <= 7:
-        die("CentOS 7 or earlier variants are not supported")
-    elif epel_release <= 8 or not "-stream" in state.config.release:
-        repos = centos_variant_repos(state.config, epel_release)
-    else:
-        repos = centos_stream_repos(state.config, epel_release)
-
-    setup_dnf(state, repos)
-
-    if "-stream" in state.config.release:
-        state.workspace.joinpath("vars/stream").write_text(state.config.release)
-
-    packages = {*state.config.packages}
-    add_packages(state.config, packages, "systemd", "dnf")
-    if not state.do_run_build_script and state.config.bootable:
-        add_packages(state.config, packages, "kernel", "dracut")
-        add_packages(state.config, packages, "systemd-udev", conditional="systemd")
-
-    if state.do_run_build_script:
-        packages.update(state.config.build_packages)
-
-    if state.do_run_build_script:
-        packages.update(state.config.build_packages)
-
-    if not state.do_run_build_script and is_epel_variant(state.config.distribution):
-        if state.config.netdev:
-            add_packages(state.config, packages, "systemd-networkd", conditional="systemd")
-        if epel_release >= 9:
-            add_packages(state.config, packages, "systemd-boot", conditional="systemd")
-
-    install_packages_dnf(state, packages)
-
-    # Centos Stream 8 and below can't write to the sqlite db backend used by
-    # default in newer RPM releases so let's rebuild the DB to use the old bdb
-    # backend instead. Because newer RPM releases have dropped support for the
-    # bdb backend completely, we check if rpm is installed and use
-    # run_workspace_command() to rebuild the rpm db.
-    if epel_release <= 8 and state.root.joinpath("usr/bin/rpm").exists():
-        cmdline = ["rpm", "--rebuilddb", "--define", "_db_backend bdb"]
-        run_workspace_command(state, cmdline)
-
-
 def install_distribution(state: MkosiState, cached: bool) -> None:
     if cached:
         return
@@ -1894,8 +1719,6 @@ def install_distribution(state: MkosiState, cached: bool) -> None:
 
     if state.installer is not None:
         install = state.installer.install
-    elif is_centos_variant(state.config.distribution):
-        install = install_centos_variant
     else:
         die("No Installer")
 
@@ -1919,8 +1742,6 @@ def remove_packages(state: MkosiState) -> None:
 
     if state.installer is not None:
         remove = lambda p: state.installer.remove_packages(state, p) # type: ignore
-    elif (state.config.distribution.package_type == PackageType.rpm):
-        remove = lambda p: invoke_dnf(state, 'remove', p)
     else:
         die(f"Removing packages is not supported for {state.config.distribution}")
 
diff --git a/mkosi/distributions/alma.py b/mkosi/distributions/alma.py
new file mode 100644 (file)
index 0000000..6431da8
--- /dev/null
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: LGPL-2.1+
+
+from mkosi.distributions.centos import CentosInstaller
+
+
+class AlmaInstaller(CentosInstaller):
+    pass
diff --git a/mkosi/distributions/alma_epel.py b/mkosi/distributions/alma_epel.py
new file mode 100644 (file)
index 0000000..4e7de89
--- /dev/null
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: LGPL-2.1+
+
+from mkosi.distributions.alma import AlmaInstaller
+
+
+class AlmaEpelInstaller(AlmaInstaller):
+    pass
diff --git a/mkosi/distributions/centos.py b/mkosi/distributions/centos.py
new file mode 100644 (file)
index 0000000..c3dac0b
--- /dev/null
@@ -0,0 +1,210 @@
+# SPDX-License-Identifier: LGPL-2.1+
+
+from pathlib import Path
+from typing import List, Tuple
+
+from mkosi.backend import (
+    Distribution,
+    MkosiConfig,
+    MkosiState,
+    add_packages,
+    complete_step,
+    die,
+    is_epel_variant,
+    run_workspace_command,
+)
+from mkosi.distributions import DistributionInstaller
+from mkosi.distributions.fedora import Repo, install_packages_dnf, invoke_dnf, setup_dnf
+
+
+class CentosInstaller(DistributionInstaller):
+    @classmethod
+    def cache_path(cls) -> List[str]:
+        return ["var/cache/yum", "var/cache/dnf"]
+
+    @classmethod
+    def install(cls, state: "MkosiState") -> None:
+        return install_centos_variant(state)
+
+    @classmethod
+    def remove_packages(cls, state: MkosiState, remove: List[str]) -> None:
+        invoke_dnf(state, 'remove', remove)
+
+
+def centos_variant_gpg_locations(distribution: Distribution, epel_release: int) -> Tuple[Path, str]:
+    if distribution in (Distribution.centos, Distribution.centos_epel):
+        return (
+            Path("/etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial"),
+            "https://www.centos.org/keys/RPM-GPG-KEY-CentOS-Official"
+        )
+    elif distribution in (Distribution.alma, Distribution.alma_epel):
+        return (
+            Path("/etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux"),
+            "https://repo.almalinux.org/almalinux/RPM-GPG-KEY-AlmaLinux"
+        )
+    elif distribution in (Distribution.rocky, Distribution.rocky_epel):
+        if epel_release >= 9:
+            keyname = f"Rocky-{epel_release}"
+        else:
+            keyname = "rockyofficial"
+
+        return (
+             Path(f"/etc/pki/rpm-gpg/RPM-GPG-KEY-{keyname}"),
+             f"https://download.rockylinux.org/pub/rocky/RPM-GPG-KEY-{keyname}"
+        )
+    else:
+        die(f"{distribution} is not a CentOS variant")
+
+
+def epel_gpg_locations(epel_release: int) -> Tuple[Path, str]:
+    return (
+        Path(f"/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-{epel_release}"),
+        f"https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-{epel_release}",
+    )
+
+
+def centos_variant_mirror_directory(distribution: Distribution) -> str:
+    if distribution in (Distribution.centos, Distribution.centos_epel):
+        return "centos"
+    elif distribution in (Distribution.alma, Distribution.alma_epel):
+        return "almalinux"
+    elif distribution in (Distribution.rocky, Distribution.rocky_epel):
+        return "rocky"
+    else:
+        die(f"{distribution} is not a CentOS variant")
+
+
+def centos_variant_mirror_repo_url(config: MkosiConfig, repo: str) -> str:
+    if config.distribution in (Distribution.centos, Distribution.centos_epel):
+        return f"http://mirrorlist.centos.org/?release={config.release}&arch=$basearch&repo={repo}"
+    elif config.distribution in (Distribution.alma, Distribution.alma_epel):
+        return f"https://mirrors.almalinux.org/mirrorlist/{config.release}/{repo.lower()}"
+    elif config.distribution in (Distribution.rocky, Distribution.rocky_epel):
+        return f"https://mirrors.rockylinux.org/mirrorlist?arch=$basearch&repo={repo}-{config.release}"
+    else:
+        die(f"{config.distribution} is not a CentOS variant")
+
+
+def centos_variant_repos(config: MkosiConfig, epel_release: int) -> List[Repo]:
+    # Repos for CentOS Linux 8, CentOS Stream 8 and CentOS variants
+
+    directory = centos_variant_mirror_directory(config.distribution)
+    gpgpath, gpgurl = centos_variant_gpg_locations(config.distribution, epel_release)
+    epel_gpgpath, epel_gpgurl = epel_gpg_locations(epel_release)
+
+    if config.local_mirror:
+        appstream_url = f"baseurl={config.local_mirror}"
+        baseos_url = extras_url = powertools_url = epel_url = None
+    elif config.mirror:
+        appstream_url = f"baseurl={config.mirror}/{directory}/{config.release}/AppStream/$basearch/os"
+        baseos_url = f"baseurl={config.mirror}/{directory}/{config.release}/BaseOS/$basearch/os"
+        extras_url = f"baseurl={config.mirror}/{directory}/{config.release}/extras/$basearch/os"
+        powertools_url = f"baseurl={config.mirror}/{directory}/{config.release}/PowerTools/$basearch/os"
+        epel_url = f"baseurl={config.mirror}/epel/{epel_release}/Everything/$basearch"
+    else:
+        appstream_url = f"mirrorlist={centos_variant_mirror_repo_url(config, 'AppStream')}"
+        baseos_url = f"mirrorlist={centos_variant_mirror_repo_url(config, 'BaseOS')}"
+        extras_url = f"mirrorlist={centos_variant_mirror_repo_url(config, 'extras')}"
+        powertools_url = f"mirrorlist={centos_variant_mirror_repo_url(config, 'PowerTools')}"
+        epel_url = f"mirrorlist=https://mirrors.fedoraproject.org/mirrorlist?repo=epel-{epel_release}&arch=$basearch"
+
+    repos = [Repo("AppStream", appstream_url, gpgpath, gpgurl)]
+    if baseos_url is not None:
+        repos += [Repo("BaseOS", baseos_url, gpgpath, gpgurl)]
+    if extras_url is not None:
+        repos += [Repo("extras", extras_url, gpgpath, gpgurl)]
+    if powertools_url is not None:
+        repos += [Repo("PowerTools", powertools_url, gpgpath, gpgurl)]
+    if epel_url is not None and is_epel_variant(config.distribution):
+        repos += [Repo("epel", epel_url, epel_gpgpath, epel_gpgurl)]
+
+    return repos
+
+
+def centos_stream_repos(config: MkosiConfig, epel_release: int) -> List[Repo]:
+    # Repos for CentOS Stream 9 and later
+
+    gpgpath, gpgurl = centos_variant_gpg_locations(config.distribution, epel_release)
+    epel_gpgpath, epel_gpgurl = epel_gpg_locations(epel_release)
+
+    release = f"{epel_release}-stream"
+
+    if config.local_mirror:
+        appstream_url = f"baseurl={config.local_mirror}"
+        baseos_url = crb_url = epel_url = None
+    elif config.mirror:
+        appstream_url = f"baseurl={config.mirror}/centos-stream/{release}/AppStream/$basearch/os"
+        baseos_url = f"baseurl={config.mirror}/centos-stream/{release}/BaseOS/$basearch/os"
+        crb_url = f"baseurl={config.mirror}/centos-stream/{release}/CRB/$basearch/os"
+        epel_url = f"baseurl={config.mirror}/epel/{epel_release}/Everything/$basearch"
+    else:
+        appstream_url = f"metalink=https://mirrors.centos.org/metalink?repo=centos-appstream-{release}&arch=$basearch"
+        baseos_url = f"metalink=https://mirrors.centos.org/metalink?repo=centos-baseos-{release}&arch=$basearch"
+        crb_url = f"metalink=https://mirrors.centos.org/metalink?repo=centos-crb-{release}&arch=$basearch"
+        epel_url = f"mirrorlist=https://mirrors.fedoraproject.org/mirrorlist?repo=epel-{epel_release}&arch=$basearch"
+
+    repos = [Repo("AppStream", appstream_url, gpgpath, gpgurl)]
+    if baseos_url is not None:
+        repos += [Repo("BaseOS", baseos_url, gpgpath, gpgurl)]
+    if crb_url is not None:
+        repos += [Repo("CRB", crb_url, gpgpath, gpgurl)]
+    if epel_url is not None and is_epel_variant(config.distribution):
+        repos += [Repo("epel", epel_url, epel_gpgpath, epel_gpgurl)]
+
+    return repos
+
+
+def parse_epel_release(release: str) -> int:
+    fields = release.split(".")
+    if fields[0].endswith("-stream"):
+        epel_release = fields[0].split("-")[0]
+    else:
+        epel_release = fields[0]
+
+    return int(epel_release)
+
+
+@complete_step("Installing CentOS…")
+def install_centos_variant(state: MkosiState) -> None:
+    epel_release = parse_epel_release(state.config.release)
+
+    if epel_release <= 7:
+        die("CentOS 7 or earlier variants are not supported")
+    elif epel_release <= 8 or not "-stream" in state.config.release:
+        repos = centos_variant_repos(state.config, epel_release)
+    else:
+        repos = centos_stream_repos(state.config, epel_release)
+
+    setup_dnf(state, repos)
+
+    if "-stream" in state.config.release:
+        state.workspace.joinpath("vars/stream").write_text(state.config.release)
+
+    packages = {*state.config.packages}
+    add_packages(state.config, packages, "systemd", "dnf")
+    if not state.do_run_build_script and state.config.bootable:
+        add_packages(state.config, packages, "kernel", "dracut")
+        add_packages(state.config, packages, "systemd-udev", conditional="systemd")
+
+    if state.do_run_build_script:
+        packages.update(state.config.build_packages)
+
+    if state.do_run_build_script:
+        packages.update(state.config.build_packages)
+
+    if not state.do_run_build_script and is_epel_variant(state.config.distribution):
+        if state.config.netdev:
+            add_packages(state.config, packages, "systemd-networkd", conditional="systemd")
+        if epel_release >= 9:
+            add_packages(state.config, packages, "systemd-boot", conditional="systemd")
+
+    install_packages_dnf(state, packages)
+
+    # Centos Stream 8 and below can't write to the sqlite db backend used by
+    # default in newer RPM releases so let's rebuild the DB to use the old bdb
+    # backend instead. Because newer RPM releases have dropped support for the
+    # bdb backend completely, we check if rpm is installed and use
+    # run_workspace_command() to rebuild the rpm db.
+    if epel_release <= 8 and state.root.joinpath("usr/bin/rpm").exists():
+        cmdline = ["rpm", "--rebuilddb", "--define", "_db_backend bdb"]
+        run_workspace_command(state, cmdline)
diff --git a/mkosi/distributions/centos_epel.py b/mkosi/distributions/centos_epel.py
new file mode 100644 (file)
index 0000000..a6ba48d
--- /dev/null
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: LGPL-2.1+
+
+from mkosi.distributions.centos import CentosInstaller
+
+
+class CentosEpelInstaller(CentosInstaller):
+    pass
diff --git a/mkosi/distributions/rocky.py b/mkosi/distributions/rocky.py
new file mode 100644 (file)
index 0000000..d2cea8e
--- /dev/null
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: LGPL-2.1+
+
+from mkosi.distributions.centos import CentosInstaller
+
+
+class RockyInstaller(CentosInstaller):
+    pass
diff --git a/mkosi/distributions/rocky_epel.py b/mkosi/distributions/rocky_epel.py
new file mode 100644 (file)
index 0000000..5542738
--- /dev/null
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: LGPL-2.1+
+
+from mkosi.distributions.rocky import RockyInstaller
+
+
+class RockyEpelInstaller(RockyInstaller):
+    pass