]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
centos: Remove distribution specific checks 1691/head
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 21 Jul 2023 09:16:51 +0000 (11:16 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 21 Jul 2023 09:41:57 +0000 (11:41 +0200)
This also reworks the mirror specification for centos and related
distros to duplicate less code. gpgurls of the Repo() struct is also
made into a tuple instead of a list.

mkosi/distributions/alma.py
mkosi/distributions/centos.py
mkosi/distributions/fedora.py
mkosi/distributions/mageia.py
mkosi/distributions/openmandriva.py
mkosi/distributions/opensuse.py
mkosi/distributions/rocky.py

index 9febabe1173fc76da11128dc7197df1107ba5cff..dd66169ac05b3487c02d7ede1c5d81f3d2d3edbf 100644 (file)
@@ -1,21 +1,22 @@
 # SPDX-License-Identifier: LGPL-2.1+
 
+from mkosi.config import MkosiConfig
 from mkosi.distributions.centos import CentosInstaller
+from mkosi.distributions.fedora import Repo
 
 
 class AlmaInstaller(CentosInstaller):
     @staticmethod
-    def _gpgurl(release: int) -> str:
-        return "https://repo.almalinux.org/almalinux/RPM-GPG-KEY-AlmaLinux-$releasever"
-
-    @staticmethod
-    def _extras_gpgurl(release: int) -> str:
-        return "https://repo.almalinux.org/almalinux/RPM-GPG-KEY-AlmaLinux-$releasever"
+    def gpgurls() -> tuple[str, ...]:
+        return ("https://repo.almalinux.org/almalinux/RPM-GPG-KEY-AlmaLinux-$releasever",)
 
     @classmethod
-    def _mirror_directory(cls) -> str:
-        return "almalinux"
+    def repository_url(cls, config: MkosiConfig, repo: str) -> str:
+        if config.mirror:
+            return f"baseurl={config.mirror}/almalinux/$releasever/{repo}/$basearch/os"
+        else:
+            return f"mirrorlist=https://mirrors.almalinux.org/mirrorlist/$releasever/{repo.lower()}"
 
     @classmethod
-    def _mirror_repo_url(cls, repo: str) -> str:
-        return f"https://mirrors.almalinux.org/mirrorlist/$releasever/{repo.lower()}"
+    def sig_repositories(cls, config: MkosiConfig) -> list[Repo]:
+        return []
index ba95d05a19483bacd009b95cd91490ab798b783f..0c6533e3d96748cd70f16195e99581cc8474d181 100644 (file)
@@ -12,7 +12,6 @@ from mkosi.distributions.fedora import Repo, invoke_dnf, setup_dnf
 from mkosi.log import complete_step, die
 from mkosi.remove import unlink_try_hard
 from mkosi.state import MkosiState
-from mkosi.util import Distribution
 
 
 def move_rpm_db(root: Path) -> None:
@@ -92,19 +91,10 @@ class CentosInstaller(DistributionInstaller):
 
         if release <= 7:
             die("CentOS 7 or earlier variants are not supported")
-        elif release == 8 or state.config.distribution != Distribution.centos:
-            repos = cls._variant_repos(state.config, release)
-        else:
-            repos = cls._stream_repos(state.config, release)
-
-        setup_dnf(state, repos)
-
-        if state.config.distribution == Distribution.centos:
-            env = dict(DNF_VAR_stream=f"{state.config.release}-stream")
-        else:
-            env = {}
 
-        invoke_dnf(state, "install", packages, env, apivfs=apivfs)
+        setup_dnf(state, cls.repositories(state.config, release))
+        invoke_dnf(state, "install", packages, apivfs=apivfs,
+                   env=dict(DNF_VAR_stream=f"{state.config.release}-stream"))
 
     @classmethod
     def remove_packages(cls, state: MkosiState, packages: Sequence[str]) -> None:
@@ -125,24 +115,83 @@ class CentosInstaller(DistributionInstaller):
         return a
 
     @staticmethod
-    def _gpgurl(release: int) -> str:
-        return "https://www.centos.org/keys/RPM-GPG-KEY-CentOS-Official"
+    def gpgurls() -> tuple[str, ...]:
+        return (
+            "https://www.centos.org/keys/RPM-GPG-KEY-CentOS-Official",
+            "https://www.centos.org/keys/RPM-GPG-KEY-CentOS-SIG-Extras",
+        )
 
-    @staticmethod
-    def _extras_gpgurl(release: int) -> str:
-        return "https://www.centos.org/keys/RPM-GPG-KEY-CentOS-SIG-Extras"
+    @classmethod
+    def repository_url(cls, config: MkosiConfig, repo: str) -> str:
+        if config.mirror:
+            if int(config.release) <= 8:
+                return f"baseurl={config.mirror}/centos/$stream/{repo}/$basearch/os"
+            else:
+                if repo == "extras":
+                    return f"baseurl={config.mirror}/SIGS/$stream/{repo}/$basearch/os"
+
+                return f"baseurl={config.mirror}/$stream/{repo}/$basearch/os"
+        else:
+            if int(config.release) <= 8:
+                return f"mirrorlist=http://mirrorlist.centos.org/?release=$stream&arch=$basearch&repo={repo}"
+            else:
+                if repo == "extras":
+                    repo = "extras-sig-extras-common"
+
+                return f"metalink=https://mirrors.centos.org/metalink?arch=$basearch&repo=centos-{repo.lower()}-$stream"
 
     @classmethod
-    def _mirror_directory(cls) -> str:
-        return "centos"
+    def repositories(cls, config: MkosiConfig, release: int) -> list[Repo]:
+        if config.local_mirror:
+            appstream_url = f"baseurl={config.local_mirror}"
+            baseos_url = extras_url = powertools_url = crb_url = None
+        else:
+            appstream_url = cls.repository_url(config, "AppStream")
+            baseos_url = cls.repository_url(config, "BaseOS")
+            extras_url = cls.repository_url(config, "extras")
+            if release >= 9:
+                crb_url = cls.repository_url(config, "CRB")
+                powertools_url = None
+            else:
+                crb_url = None
+                powertools_url = cls.repository_url(config, "PowerTools")
+
+        repos = []
+        for name, url in (("appstream",  appstream_url),
+                               ("baseos",     baseos_url),
+                               ("extras",     extras_url),
+                               ("crb",        crb_url),
+                               ("powertools", powertools_url)):
+            if url:
+                repos += [Repo(name, url, cls.gpgurls())]
+
+        return repos + cls.epel_repositories(config) + cls.sig_repositories(config)
 
     @classmethod
-    def _mirror_repo_url(cls, repo: str) -> str:
-        return f"http://mirrorlist.centos.org/?release=$stream&arch=$basearch&repo={repo}"
+    def epel_repositories(cls, config: MkosiConfig) -> list[Repo]:
+        epel_gpgurl = "https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-$releasever"
+
+        if config.local_mirror:
+            return []
+
+        if config.mirror:
+            epel_url = f"baseurl={config.mirror}/epel/$releasever/Everything/$basearch"
+            epel_next_url = f"baseurl={config.mirror}/epel/next/$releasever/Everything/$basearch"
+            epel_testing_url = f"baseurl={config.mirror}/epel/testing/$releasever/Everything/$basearch"
+        else:
+            epel_url = "metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-$releasever&arch=$basearch"
+            epel_next_url = "metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-next-$releasever&arch=$basearch"
+            epel_testing_url = "metalink=https://mirrors.fedoraproject.org/metalink?repo=testing-epel$releasever&arch=$basearch"
+
+        return [
+            Repo("epel", epel_url, (epel_gpgurl,), enabled=False),
+            Repo("epel-next", epel_next_url, (epel_next_url,), enabled=False),
+            Repo("epel-testing", epel_testing_url, (epel_gpgurl,), enabled=False),
+        ]
 
     @classmethod
-    def _sig_repos(cls, config: MkosiConfig, release: int) -> list[Repo]:
-        if config.local_mirror or config.distribution != Distribution.centos:
+    def sig_repositories(cls, config: MkosiConfig) -> list[Repo]:
+        if config.local_mirror:
             return []
 
         sigs = (
@@ -158,124 +207,27 @@ class CentosInstaller(DistributionInstaller):
         for sig, components, gpgurl in sigs:
             for c in components:
                 if config.mirror:
-                    if release <= 8:
+                    if int(config.release) <= 8:
                         url = f"baseurl={config.mirror}/centos/$stream/{sig}/$basearch/{c}"
                     else:
                         url = f"baseurl={config.mirror}/SIGs/$stream/{sig}/$basearch/{c}"
                 else:
-                    if release <= 8:
-                        url = f"mirrorlist=http://mirrorlist.centos.org/?release=$stream&arch=$basearch&repo={sig}-{c}"
-                    else:
-                        url = f"metalink=https://mirrors.centos.org/metalink?repo=centos-{sig}-sig-{c}-$stream&arch=$basearch"
+                    repo = f"{sig}-{c}" if int(config.release) <= 8 else f"{sig}-sig-{c}"
+                    url = cls.repository_url(config, repo)
 
                 repos += [
                     Repo(
                         id=f"{sig}-{c}",
                         url=url,
-                        gpgurls=[gpgurl],
+                        gpgurls=(gpgurl,),
                         enabled=False
                     ),
                     Repo(
                         id=f"{sig}-{c}-testing",
                         url=f"baseurl=https://buildlogs.centos.org/centos/$stream/{sig}/$basearch/{c}",
-                        gpgurls=[gpgurl],
+                        gpgurls=(gpgurl,),
                         enabled=False,
                     ),
                 ]
 
         return repos
-
-    @classmethod
-    def _epel_repos(cls, config: MkosiConfig) -> list[Repo]:
-        epel_gpgurl = "https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-$releasever"
-
-        if config.local_mirror:
-            return []
-
-        if config.mirror:
-            epel_url = f"baseurl={config.mirror}/epel/$releasever/Everything/$basearch"
-            epel_next_url = f"baseurl={config.mirror}/epel/next/$releasever/Everything/$basearch"
-            epel_testing_url = f"baseurl={config.mirror}/epel/testing/$releasever/Everything/$basearch"
-        else:
-            epel_url = "metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-$releasever&arch=$basearch"
-            epel_next_url = "metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-next-$releasever&arch=$basearch"
-            epel_testing_url = "metalink=https://mirrors.fedoraproject.org/metalink?repo=testing-epel$releasever&arch=$basearch"
-
-        return [
-            Repo("epel", epel_url, [epel_gpgurl], enabled=False),
-            Repo("epel-next", epel_next_url, [epel_next_url], enabled=False),
-            Repo("epel-testing", epel_testing_url, [epel_gpgurl], enabled=False),
-        ]
-
-    @classmethod
-    def _variant_repos(cls, config: MkosiConfig, release: int) -> list[Repo]:
-        # Repos for CentOS Linux 8, CentOS Stream 8 and CentOS variants
-
-        directory = cls._mirror_directory()
-        gpgurl = cls._gpgurl(release)
-
-        if config.local_mirror:
-            appstream_url = f"baseurl={config.local_mirror}"
-            baseos_url = extras_url = powertools_url = crb_url = None
-        elif config.mirror:
-            appstream_url = f"baseurl={config.mirror}/{directory}/$stream/AppStream/$basearch/os"
-            baseos_url = f"baseurl={config.mirror}/{directory}/$stream/BaseOS/$basearch/os"
-            extras_url = f"baseurl={config.mirror}/{directory}/$stream/extras/$basearch/os"
-            if release >= 9:
-                crb_url = f"baseurl={config.mirror}/{directory}/$stream/CRB/$basearch/os"
-                powertools_url = None
-            else:
-                crb_url = None
-                powertools_url = f"baseurl={config.mirror}/{directory}/$stream/PowerTools/$basearch/os"
-        else:
-            appstream_url = f"mirrorlist={cls._mirror_repo_url('AppStream')}"
-            baseos_url = f"mirrorlist={cls._mirror_repo_url('BaseOS')}"
-            extras_url = f"mirrorlist={cls._mirror_repo_url('extras')}"
-            if release >= 9:
-                crb_url = f"mirrorlist={cls._mirror_repo_url('CRB')}"
-                powertools_url = None
-            else:
-                crb_url = None
-                powertools_url = f"mirrorlist={cls._mirror_repo_url('PowerTools')}"
-
-        repos = []
-        for name, url in (("appstream",  appstream_url),
-                          ("baseos",     baseos_url),
-                          ("extras",     extras_url),
-                          ("crb",        crb_url),
-                          ("powertools", powertools_url)):
-            if url:
-                repos += [Repo(name, url, [gpgurl])]
-
-        return repos + cls._epel_repos(config) + cls._sig_repos(config, release)
-
-    @classmethod
-    def _stream_repos(cls, config: MkosiConfig, release: int) -> list[Repo]:
-        # Repos for CentOS Stream 9 and later
-
-        gpgurl = cls._gpgurl(release)
-        extras_gpgurl = cls._extras_gpgurl(release)
-
-        if config.local_mirror:
-            appstream_url = f"baseurl={config.local_mirror}"
-            baseos_url = extras_url = crb_url = None
-        elif config.mirror:
-            appstream_url = f"baseurl={config.mirror}/centos-stream/$stream/AppStream/$basearch/os"
-            baseos_url = f"baseurl={config.mirror}/centos-stream/$stream/BaseOS/$basearch/os"
-            extras_url = f"baseurl={config.mirror}/centos-stream/SIGS/$stream/extras/$basearch/extras-common"
-            crb_url = f"baseurl={config.mirror}/centos-stream/$stream/CRB/$basearch/os"
-        else:
-            appstream_url = "metalink=https://mirrors.centos.org/metalink?repo=centos-appstream-$stream&arch=$basearch&protocol=https,http"
-            baseos_url = "metalink=https://mirrors.centos.org/metalink?repo=centos-baseos-$stream&arch=$basearch&protocol=https,http"
-            extras_url = "metalink=https://mirrors.centos.org/metalink?repo=centos-extras-sig-extras-common-$stream&arch=$basearch&protocol=https,http"
-            crb_url = "metalink=https://mirrors.centos.org/metalink?repo=centos-crb-$stream&arch=$basearch&protocol=https,http"
-
-        repos = []
-        for name, url, gpgurl in (("appstream", appstream_url, gpgurl),
-                                  ("baseos",    baseos_url,    gpgurl),
-                                  ("extras",    extras_url,    extras_gpgurl),
-                                  ("crb",       crb_url,       gpgurl)):
-            if url:
-                repos += [Repo(name, url, [gpgurl])]
-
-        return repos + cls._epel_repos(config) + cls._sig_repos(config, release)
index 39c49d534fb7d5bb70ed3bee096379610f0d92f4..5509affd99d5b6b480acc0f871c9f62b28869656 100644 (file)
@@ -64,7 +64,7 @@ class FedoraInstaller(DistributionInstaller):
                           ("extras",    extras_url),
                           ("crb",       crb_url)):
             if url:
-                repos += [Repo(name, url, [gpgurl])]
+                repos += [Repo(name, url, (gpgurl,))]
 
         setup_dnf(state, repos)
         invoke_dnf(state, "install", packages, apivfs=apivfs,
@@ -106,7 +106,7 @@ def fedora_release_at_least(release: str, threshold: str) -> bool:
 class Repo(NamedTuple):
     id: str
     url: str
-    gpgurls: list[str]
+    gpgurls: tuple[str, ...]
     enabled: bool = True
 
 
index 54ca55d8c62454a8257e54b537ca6c04a9b75e37..edd4be9f0785ab2a9897357938080248c67aa358 100644 (file)
@@ -43,9 +43,9 @@ class MageiaInstaller(DistributionInstaller):
 
         gpgurl = f"https://mirrors.kernel.org/mageia/distrib/{release}/{arch}/media/core/release/media_info/pubkey"
 
-        repos = [Repo(f"mageia-{release}", release_url, [gpgurl])]
+        repos = [Repo(f"mageia-{release}", release_url, (gpgurl,))]
         if updates_url is not None:
-            repos += [Repo(f"mageia-{release}-updates", updates_url, [gpgurl])]
+            repos += [Repo(f"mageia-{release}-updates", updates_url, (gpgurl,))]
 
         setup_dnf(state, repos)
         invoke_dnf(state, "install", packages, apivfs=apivfs)
index a0bfbe02d6604189e2a8167854cbe16c3f3e0422..2331bcaa8e89145964ad8afec6ac938bd78499b0 100644 (file)
@@ -44,9 +44,9 @@ class OpenmandrivaInstaller(DistributionInstaller):
 
         gpgurl = "https://raw.githubusercontent.com/OpenMandrivaAssociation/openmandriva-repos/master/RPM-GPG-KEY-OpenMandriva"
 
-        repos = [Repo("openmandriva", release_url, [gpgurl])]
+        repos = [Repo("openmandriva", release_url, (gpgurl,))]
         if updates_url is not None:
-            repos += [Repo("updates", updates_url, [gpgurl])]
+            repos += [Repo("updates", updates_url, (gpgurl,))]
 
         setup_dnf(state, repos)
         invoke_dnf(state, "install", packages, apivfs=apivfs)
index afca52dc849113230c570f52a5a22715313d9c95..fe4de2493712d1c1dd01cf08cae9bf700e74879a 100644 (file)
@@ -49,11 +49,11 @@ class OpensuseInstaller(DistributionInstaller):
         # If we need to use a local mirror, create a temporary repository definition
         # that doesn't get in the image, as it is valid only at image build time.
         if state.config.local_mirror:
-            repos = [Repo("local-mirror", f"baseurl={state.config.local_mirror}", [])]
+            repos = [Repo("local-mirror", f"baseurl={state.config.local_mirror}", ())]
         else:
-            repos = [Repo("repo-oss", f"baseurl={release_url}", fetch_gpgurls(release_url) if not zypper else [])]
+            repos = [Repo("repo-oss", f"baseurl={release_url}", fetch_gpgurls(release_url) if not zypper else ())]
             if updates_url is not None:
-                repos += [Repo("repo-update", f"baseurl={updates_url}", fetch_gpgurls(updates_url) if not zypper else [])]
+                repos += [Repo("repo-update", f"baseurl={updates_url}", fetch_gpgurls(updates_url) if not zypper else ())]
 
         if zypper:
             setup_zypper(state, repos)
@@ -146,7 +146,7 @@ def invoke_zypper(
     fixup_rpmdb_location(state.root)
 
 
-def fetch_gpgurls(repourl: str) -> list[str]:
+def fetch_gpgurls(repourl: str) -> tuple[str, ...]:
     gpgurls = [f"{repourl}/repodata/repomd.xml.key"]
 
     with urllib.request.urlopen(f"{repourl}/repodata/repomd.xml") as f:
@@ -162,4 +162,4 @@ def fetch_gpgurls(repourl: str) -> list[str]:
                 gpgkey = child.text.partition("?")[0]
                 gpgurls += [f"{repourl}{gpgkey}"]
 
-    return gpgurls
+    return tuple(gpgurls)
index d480493d3eba020366349f5b9cfb729206c43457..52cc5504782df40fd18802bd96816da1416bc9c3 100644 (file)
@@ -1,23 +1,22 @@
 # SPDX-License-Identifier: LGPL-2.1+
 
+from mkosi.config import MkosiConfig
 from mkosi.distributions.centos import CentosInstaller
+from mkosi.distributions.fedora import Repo
 
 
 class RockyInstaller(CentosInstaller):
     @staticmethod
-    def _gpgurl(release: int) -> str:
-        keyname = "Rocky-$releasever" if release >= 9 else "rockyofficial"
-        return f"https://download.rockylinux.org/pub/rocky/RPM-GPG-KEY-{keyname}"
-
-    @staticmethod
-    def _extras_gpgurl(release: int) -> str:
-        keyname = "Rocky-$releasever" if release >= 9 else "rockyofficial"
-        return f"https://download.rockylinux.org/pub/rocky/RPM-GPG-KEY-{keyname}"
+    def gpgurls() -> tuple[str, ...]:
+        return ("https://download.rockylinux.org/pub/rocky/RPM-GPG-KEY-Rocky-$releasever",)
 
     @classmethod
-    def _mirror_directory(cls) -> str:
-        return "rocky"
+    def repository_url(cls, config: MkosiConfig, repo: str) -> str:
+        if config.mirror:
+            return f"baseurl={config.mirror}/rocky/$releasever/{repo}/$basearch/os"
+        else:
+            return f"mirrorlist=https://mirrors.rockylinux.org/mirrorlist?arch=$basearch&repo={repo}-$releasever"
 
     @classmethod
-    def _mirror_repo_url(cls, repo: str) -> str:
-        return f"https://mirrors.rockylinux.org/mirrorlist?arch=$basearch&repo={repo}-$releasever"
+    def sig_repositories(cls, config: MkosiConfig) -> list[Repo]:
+        return []