From 0c03e834cdb873c49debe74e149a83f5fad0ad76 Mon Sep 17 00:00:00 2001 From: jgoutin Date: Fri, 15 Nov 2024 18:42:57 +0100 Subject: [PATCH] squid: Update squid_dnf_mirrors script to use "libdnf5". --- roles/squid/files/squid_dnf_mirrors.py | 47 +++++++++++++++----------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/roles/squid/files/squid_dnf_mirrors.py b/roles/squid/files/squid_dnf_mirrors.py index 862e124..9728896 100644 --- a/roles/squid/files/squid_dnf_mirrors.py +++ b/roles/squid/files/squid_dnf_mirrors.py @@ -1,6 +1,6 @@ #! /usr/bin/env python3 """Squid DNF repositories mirrors updater""" -# Copyright (C) 2021 J.Goutin +# Copyright (C) 2024 J.Goutin # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -15,35 +15,46 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -__version__ = "1.0.0" -__copyright__ = "Copyright 2021 J.Goutin" +__version__ = "2.0.0" +__copyright__ = "Copyright 2024 J.Goutin" -import dnf +import libdnf5 import re +from os import getenv _HTTP = re.compile(r"^https?://") _is_http = _HTTP.match _http_sub = _HTTP.sub -def update_dnf_mirrors(store_ids, releasever): +def update_dnf_mirrors(store_ids: dict[str, str], releasever: str | int | None = None, arch: str | None = None) -> None: """ Updates DNF repositories mirrors list. Args: - store_ids (dict): IDs as key, mirrors URLs as values. - releasever (str or int): OS release version. + store_ids: IDs as key, mirrors URLs as values. + releasever: OS release version. + arch: CPU architecture. """ # Update repository information and get mirrors - conf = dnf.conf.Conf() - conf.releasever = str(releasever) + dnf = libdnf5.base.Base() + dnf_config = dnf.get_config() + dnf_config.plugins = False + dnf.load_config() + dnf_vars = dnf.get_vars().get() + if arch: + dnf_vars.set("arch", arch) + if releasever: + dnf_vars.set("releasever", releasever) + dnf.setup() + + dnf_repo_sack = dnf.get_repo_sack() + dnf_repo_sack.create_repos_from_reposdir() + repos = {} - for repo in dnf.conf.read.RepoReader(conf, {}): - try: - repo.load() - except dnf.exceptions.RepoError: - pass - mirrors = repo._repo.getMirrors() + for repo in dnf_repo_sack: + mirrors = repo.get_mirrors() + if len(mirrors) <= 2: # Skip if only one URL since no need to create StoreID to optimize hit ratio continue @@ -85,12 +96,10 @@ def update_dnf_mirrors(store_ids, releasever): if __name__ == "__main__": # Get current OS version repository mirrors _store_ids = dict() - update_dnf_mirrors( - _store_ids, dnf.rpm.detect_releasever(dnf.conf.Conf().installroot) - ) + update_dnf_mirrors(_store_ids) # Write the Squid StoreID file - with open("/etc/squid/dnf_mirrors", "wt") as _store_id_file: + with open(getenv("STOREID_FILE_PATH", "/etc/squid/dnf_mirrors"), "wt") as _store_id_file: for _store_id, _urls in _store_ids.items(): line = f"^%s(.*)\thttp://{_store_id}.squid.internal/$1\n" for _url in sorted(_urls):