Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[master][FEATURE REQUEST] Added support for dnf5 for Fedora #64675

Merged
merged 11 commits into from Jul 20, 2023
1 change: 1 addition & 0 deletions changelog/64532.added.md
@@ -0,0 +1 @@
Added support for dnf5 and its new command syntax
34 changes: 22 additions & 12 deletions salt/modules/yumpkg.py
Expand Up @@ -14,6 +14,8 @@

.. versionadded:: 3003
Support for ``tdnf`` on Photon OS.
.. versionadded:: 3007.0
Support for ``dnf5``` on Fedora 39
dmurphy18 marked this conversation as resolved.
Show resolved Hide resolved
"""


Expand Down Expand Up @@ -122,12 +124,12 @@ def _get_hold(line, pattern=__HOLD_PATTERN, full=True):
dnf ==> vim-enhanced-2:7.4.827-1.fc22.*
"""
if full:
if _yum() == "dnf":
if _yum() in ("dnf", "dnf5"):
lock_re = rf"({pattern}-\S+)"
else:
lock_re = rf"(\d+:{pattern}-\S+)"
else:
if _yum() == "dnf":
if _yum() in ("dnf", "dnf5"):
lock_re = rf"({pattern}-\S+)"
else:
lock_re = rf"\d+:({pattern}-\S+)"
Expand All @@ -145,7 +147,7 @@ def _get_hold(line, pattern=__HOLD_PATTERN, full=True):

def _yum():
"""
Determine package manager name (yum or dnf),
Determine package manager name (yum or dnf[5]),
depending on the executable existence in $PATH.
"""

Expand All @@ -168,7 +170,10 @@ def _check(file):
contextkey = "yum_bin"
if contextkey not in context:
for dir in os.environ.get("PATH", os.defpath).split(os.pathsep):
if _check(os.path.join(dir, "dnf")):
if _check(os.path.join(dir, "dnf5")):
context[contextkey] = "dnf5"
break
elif _check(os.path.join(dir, "dnf")):
context[contextkey] = "dnf"
break
elif _check(os.path.join(dir, "tdnf")):
Expand Down Expand Up @@ -245,7 +250,8 @@ def _versionlock_pkg(grains=None):
"""
if grains is None:
grains = __grains__
if _yum() == "dnf":

if _yum() in ("dnf", "dnf5"):
if grains["os"].lower() == "fedora":
return (
"python3-dnf-plugin-versionlock"
Expand All @@ -272,10 +278,11 @@ def _check_versionlock():

def _get_options(**kwargs):
"""
Returns a list of options to be used in the yum/dnf command, based on the
Returns a list of options to be used in the yum/dnf[5] command, based on the
kwargs passed.
"""
# Get repo options from the kwargs
# dnf5 aliases dnf options, so no need to change
fromrepo = kwargs.pop("fromrepo", "")
repo = kwargs.pop("repo", "")
disablerepo = kwargs.pop("disablerepo", "")
Expand Down Expand Up @@ -1053,7 +1060,7 @@ def list_upgrades(refresh=True, **kwargs):

cmd = ["--quiet"]
cmd.extend(options)
cmd.extend(["list", "upgrades" if _yum() == "dnf" else "updates"])
cmd.extend(["list", "upgrades" if _yum() in ("dnf", "dnf5") else "updates"])
out = _call_yum(cmd, ignore_retcode=True)
if out["retcode"] != 0 and "Error:" in out:
return {}
Expand Down Expand Up @@ -1708,7 +1715,8 @@ def _add_common_args(cmd):
if skip_verify:
cmd.append("--nogpgcheck")
if downloadonly:
cmd.append("--downloadonly")
if _yum() != "dnf5":
cmd.append("--downloadonly")

try:
holds = list_holds(full=False)
Expand Down Expand Up @@ -1769,6 +1777,8 @@ def _temporarily_unhold(pkgs, targets):
cmd.extend(["--best", "--allowerasing"])
_add_common_args(cmd)
cmd.append("install" if pkg_type != "advisory" else "update")
if _yum() == "dnf5":
dmurphy18 marked this conversation as resolved.
Show resolved Hide resolved
cmd.extend(["--best", "--allowerasing"])
cmd.extend(targets)
out = _call_yum(cmd, ignore_retcode=False, redirect_stderr=True)
if out["retcode"] != 0:
Expand Down Expand Up @@ -2002,7 +2012,7 @@ def upgrade(

salt '*' pkg.upgrade security=True exclude='kernel*'
"""
if _yum() == "dnf" and not obsoletes:
if _yum() in ("dnf", "dnf5") and not obsoletes:
# for dnf we can just disable obsoletes
_setopt = [
opt
Expand Down Expand Up @@ -2040,7 +2050,7 @@ def upgrade(
cmd.append("upgrade" if not minimal else "upgrade-minimal")
else:
# do not force the removal of obsolete packages
if _yum() == "dnf":
if _yum() in ("dnf", "dnf5"):
cmd.append("upgrade" if not minimal else "upgrade-minimal")
else:
# for yum we have to use update instead of upgrade
Expand Down Expand Up @@ -2396,7 +2406,7 @@ def unhold(name=None, pkgs=None, sources=None, **kwargs): # pylint: disable=W06

ret[target] = {"name": target, "changes": {}, "result": False, "comment": ""}

if _yum() == "dnf":
if _yum() in ("dnf", "dnf5"):
search_locks = [x for x in current_locks if x == target]
else:
# To accommodate yum versionlock's lack of support for removing
Expand Down Expand Up @@ -3032,7 +3042,7 @@ def mod_repo(repo, basedir=None, **kwargs):
if use_copr:
# Is copr plugin installed?
copr_plugin_name = ""
if _yum() == "dnf":
if _yum() in ("dnf", "dnf5"):
copr_plugin_name = "dnf-plugins-core"
else:
copr_plugin_name = "yum-plugin-copr"
Expand Down
14 changes: 11 additions & 3 deletions tests/pytests/unit/modules/test_yumpkg.py
Expand Up @@ -72,7 +72,7 @@ def list_repos_var():


@pytest.fixture(
ids=["yum", "dnf"],
ids=["yum", "dnf", "dnf5"],
params=[
{
"context": {"yum_bin": "yum"},
Expand All @@ -84,6 +84,11 @@ def list_repos_var():
"grains": {"os": "Fedora", "osrelease": 27},
"cmd": ["dnf", "-y", "--best", "--allowerasing"],
},
{
"context": {"yum_bin": "dnf5"},
"grains": {"os": "Fedora", "osrelease": 39},
"cmd": ["dnf5", "-y"],
},
],
)
def yum_and_dnf(request):
Expand Down Expand Up @@ -692,7 +697,7 @@ def test_list_repo_pkgs_with_options(list_repos_var):
except AssertionError:
continue
else:
pytest.fail("repo '{}' not checked".format(repo))
pytest.fail(f"repo '{repo}' not checked")


def test_list_upgrades_dnf():
Expand Down Expand Up @@ -2085,7 +2090,10 @@ def test_59705_version_as_accidental_float_should_become_text(
new, full_pkg_string, yum_and_dnf
):
name = "fnord"
expected_cmd = yum_and_dnf + ["install", full_pkg_string]
expected_cmd = yum_and_dnf + ["install"]
if expected_cmd[0] == "dnf5":
expected_cmd += ["--best", "--allowerasing"]
expected_cmd += [full_pkg_string]
cmd_mock = MagicMock(
return_value={"pid": 12345, "retcode": 0, "stdout": "", "stderr": ""}
)
Expand Down