mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-06-15 04:17:41 +00:00
[PR #12206/ff7f90fb backport][stable-13] Fix dnf_config_manager incompatibility with DNF5 (#12266)
* Fix dnf_config_manager incompatibility with DNF5 (#12206) * Extract fixtures from dnf_config_manager tests * Implement support for DNF5 * Format code (cherry picked from commitff7f90fb4f) * Exclude dnf_config_manager fixtures from trailing space check. (cherry picked from commitebb813680e) --------- Co-authored-by: Maksym Hazevych <mhazevych@mailbox.org> Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
5bcc247768
commit
d0bdd8a357
21 changed files with 1305 additions and 396 deletions
|
|
@ -121,16 +121,30 @@ changed_repos:
|
|||
|
||||
import os
|
||||
import re
|
||||
import typing as t
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
|
||||
DNF_BIN = "/usr/bin/dnf"
|
||||
REPO_ID_RE = re.compile(r"^Repo-id\s*:\s*(\S+)$")
|
||||
REPO_STATUS_RE = re.compile(r"^Repo-status\s*:\s*(disabled|enabled)$")
|
||||
REPO_ID_RE = re.compile(r"^Repo[-\s]id\s*:\s*(\S+)$", re.IGNORECASE)
|
||||
REPO_STATUS_RE = re.compile(r"^(?:Repo-)?status\s*:\s*(disabled|enabled)$", re.IGNORECASE)
|
||||
|
||||
|
||||
def get_repo_states(module):
|
||||
rc, out, err = module.run_command([DNF_BIN, "repolist", "--all", "--verbose"], check_rc=True)
|
||||
def get_dnf_version(module) -> t.Literal[4, 5]:
|
||||
rc, out, err = module.run_command([DNF_BIN, "--version"], check_rc=True)
|
||||
line, separator, rest = out.partition("\n")
|
||||
|
||||
return 5 if re.match(r"^dnf5\s*", line) else 4
|
||||
|
||||
|
||||
def get_repo_states(module, dnf_v):
|
||||
command = [DNF_BIN]
|
||||
if dnf_v == 4:
|
||||
command.extend(["repolist", "--all", "--verbose"])
|
||||
else:
|
||||
command.extend(["repo", "info", "--all"])
|
||||
|
||||
rc, out, err = module.run_command(command, check_rc=True)
|
||||
|
||||
repos = dict()
|
||||
last_repo = ""
|
||||
|
|
@ -150,8 +164,13 @@ def get_repo_states(module):
|
|||
return repos
|
||||
|
||||
|
||||
def set_repo_states(module, repo_ids, state):
|
||||
module.run_command([DNF_BIN, "config-manager", "--assumeyes", f"--set-{state}"] + repo_ids, check_rc=True)
|
||||
def set_repo_states(module, dnf_v, repo_ids, state):
|
||||
if dnf_v == 4:
|
||||
module.run_command([DNF_BIN, "config-manager", "--assumeyes", f"--set-{state}"] + repo_ids, check_rc=True)
|
||||
else:
|
||||
state = "1" if state == "enabled" else "0"
|
||||
opts = map(lambda v: f"{v}.enabled={state}", repo_ids)
|
||||
module.run_command([DNF_BIN, "config-manager", "setopt"] + list(opts), check_rc=True)
|
||||
|
||||
|
||||
def pack_repo_states_for_return(states):
|
||||
|
|
@ -184,7 +203,9 @@ def main():
|
|||
if not os.path.exists(DNF_BIN):
|
||||
module.fail_json(msg=f"{DNF_BIN} was not found")
|
||||
|
||||
repo_states = get_repo_states(module)
|
||||
dnf_v = get_dnf_version(module)
|
||||
|
||||
repo_states = get_repo_states(module, dnf_v)
|
||||
result["repo_states_pre"] = pack_repo_states_for_return(repo_states)
|
||||
|
||||
desired_repo_state = module.params["state"]
|
||||
|
|
@ -203,9 +224,9 @@ def main():
|
|||
module.exit_json(**result)
|
||||
|
||||
if len(to_change) > 0:
|
||||
set_repo_states(module, to_change, desired_repo_state)
|
||||
set_repo_states(module, dnf_v, to_change, desired_repo_state)
|
||||
|
||||
repo_states_post = get_repo_states(module)
|
||||
repo_states_post = get_repo_states(module, dnf_v)
|
||||
result["repo_states_post"] = pack_repo_states_for_return(repo_states_post)
|
||||
|
||||
for repo_id in to_change:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue