Skip to content

Commit

Permalink
squid: Update squid_dnf_mirrors script to use "libdnf5".
Browse files Browse the repository at this point in the history
  • Loading branch information
JGoutin committed Nov 15, 2024
1 parent 266fb74 commit f491b55
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions roles/squid/files/squid_dnf_mirrors.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,35 +15,46 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

__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 []: # TODO: Find how to list "repo" objects
mirrors = repo.get_mirrors()

if len(mirrors) <= 2:
# Skip if only one URL since no need to create StoreID to optimize hit ratio
continue
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit f491b55

Please sign in to comment.