From 6ed62ee473affa69c09e3a1f5f38359956985669 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Fri, 21 Jul 2023 11:16:51 +0200 Subject: [PATCH] centos: Remove distribution specific checks 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 | 21 +-- mkosi/distributions/centos.py | 204 +++++++++++----------------- mkosi/distributions/fedora.py | 4 +- mkosi/distributions/mageia.py | 4 +- mkosi/distributions/openmandriva.py | 4 +- mkosi/distributions/opensuse.py | 10 +- mkosi/distributions/rocky.py | 23 ++-- 7 files changed, 111 insertions(+), 159 deletions(-) diff --git a/mkosi/distributions/alma.py b/mkosi/distributions/alma.py index 9febabe11..dd66169ac 100644 --- a/mkosi/distributions/alma.py +++ b/mkosi/distributions/alma.py @@ -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 [] diff --git a/mkosi/distributions/centos.py b/mkosi/distributions/centos.py index ba95d05a1..0c6533e3d 100644 --- a/mkosi/distributions/centos.py +++ b/mkosi/distributions/centos.py @@ -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) diff --git a/mkosi/distributions/fedora.py b/mkosi/distributions/fedora.py index 39c49d534..5509affd9 100644 --- a/mkosi/distributions/fedora.py +++ b/mkosi/distributions/fedora.py @@ -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 diff --git a/mkosi/distributions/mageia.py b/mkosi/distributions/mageia.py index 54ca55d8c..edd4be9f0 100644 --- a/mkosi/distributions/mageia.py +++ b/mkosi/distributions/mageia.py @@ -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) diff --git a/mkosi/distributions/openmandriva.py b/mkosi/distributions/openmandriva.py index a0bfbe02d..2331bcaa8 100644 --- a/mkosi/distributions/openmandriva.py +++ b/mkosi/distributions/openmandriva.py @@ -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) diff --git a/mkosi/distributions/opensuse.py b/mkosi/distributions/opensuse.py index afca52dc8..fe4de2493 100644 --- a/mkosi/distributions/opensuse.py +++ b/mkosi/distributions/opensuse.py @@ -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) diff --git a/mkosi/distributions/rocky.py b/mkosi/distributions/rocky.py index d480493d3..52cc55047 100644 --- a/mkosi/distributions/rocky.py +++ b/mkosi/distributions/rocky.py @@ -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 [] -- 2.47.2