Skip to content

Commit

Permalink
dnf5: implement enable_plugin and disable_plugin options (#83105)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrizek committed May 9, 2024
1 parent 036d14f commit 3c280e6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/dnf5-enable-disable-plugins.yml
@@ -0,0 +1,2 @@
minor_changes:
- dnf5 - implement ``enable_plugin`` and ``disable_plugin`` options
43 changes: 34 additions & 9 deletions lib/ansible/modules/dnf5.py
Expand Up @@ -127,17 +127,19 @@
type: bool
enable_plugin:
description:
- This is currently a no-op as dnf5 itself does not implement this feature.
- I(Plugin) name to enable for the install/update operation.
The enabled plugin will not persist beyond the transaction.
- O(disable_plugin) takes precedence in case a plugin is listed in both O(enable_plugin) and O(disable_plugin).
- Requires python3-libdnf5 5.2.0.0+.
type: list
elements: str
default: []
disable_plugin:
description:
- This is currently a no-op as dnf5 itself does not implement this feature.
- I(Plugin) name to disable for the install/update operation.
The disabled plugins will not persist beyond the transaction.
- O(disable_plugin) takes precedence in case a plugin is listed in both O(enable_plugin) and O(disable_plugin).
- Requires python3-libdnf5 5.2.0.0+.
type: list
default: []
elements: str
Expand Down Expand Up @@ -434,6 +436,21 @@ def __init__(self, module):

self.pkg_mgr_name = "dnf5"

def fail_on_non_existing_plugins(self, base):
# https://github.com/rpm-software-management/dnf5/issues/1460
plugin_names = [p.get_name() for p in base.get_plugins_info()]
msg = []
if enable_unmatched := set(self.enable_plugin).difference(plugin_names):
msg.append(
f"No matches were found for the following plugin name patterns while enabling libdnf5 plugins: {', '.join(enable_unmatched)}."
)
if disable_unmatched := set(self.disable_plugin).difference(plugin_names):
msg.append(
f"No matches were found for the following plugin name patterns while disabling libdnf5 plugins: {', '.join(disable_unmatched)}."
)
if msg:
self.module.fail_json(msg=" ".join(msg))

def _ensure_dnf(self):
locale = get_best_parsable_locale(self.module)
os.environ["LC_ALL"] = os.environ["LC_MESSAGES"] = locale
Expand Down Expand Up @@ -482,13 +499,6 @@ def run(self):
rc=1,
)

if self.enable_plugin or self.disable_plugin:
self.module.fail_json(
msg="enable_plugin and disable_plugin options are not yet implemented in DNF5",
failures=[],
rc=1,
)

base = libdnf5.base.Base()
conf = base.get_config()

Expand Down Expand Up @@ -531,8 +541,23 @@ def run(self):
if self.download_dir:
conf.destdir = self.download_dir

if self.enable_plugin:
try:
base.enable_disable_plugins(self.enable_plugin, True)
except AttributeError:
self.module.fail_json(msg="'enable_plugin' requires python3-libdnf5 5.2.0.0+")

if self.disable_plugin:
try:
base.enable_disable_plugins(self.disable_plugin, False)
except AttributeError:
self.module.fail_json(msg="'disable_plugin' requires python3-libdnf5 5.2.0.0+")

base.setup()

# https://github.com/rpm-software-management/dnf5/issues/1460
self.fail_on_non_existing_plugins(base)

log_router = base.get_logger()
global_logger = libdnf5.logger.GlobalLogger()
global_logger.set(log_router.get(), libdnf5.logger.Logger.Level_DEBUG)
Expand Down
24 changes: 24 additions & 0 deletions test/integration/targets/dnf/tasks/dnf.yml
Expand Up @@ -873,3 +873,27 @@
- dnf:
name: langpacks-ja
state: absent

- name: test passing non existing plugins, should fail
block:
- dnf:
name: sos
enable_plugin: nonexisting
ignore_errors: true
register: enable_plugin_result

- dnf:
name: sos
disable_plugin: nonexisting
ignore_errors: true
register: disable_plugin_result

- assert:
that:
- enable_plugin_result is failed
- disable_plugin_result is failed
- '"No matches were found for the following plugin name patterns while enabling libdnf5 plugins: " in enable_plugin_result.msg'
- '"No matches were found for the following plugin name patterns while disabling libdnf5 plugins: " in disable_plugin_result.msg'
- '"nonexisting" in enable_plugin_result.msg'
- '"nonexisting" in disable_plugin_result.msg'
when: dnf5|default(false)

0 comments on commit 3c280e6

Please sign in to comment.