2626
2727import os
2828
29+ from leapp .libraries .stdlib import CalledProcessError , run
30+
2931
3032RHN_SYSTEMID = '/etc/sysconfig/rhn/systemid'
3133SPACEWALK_DNF_CONF = '/etc/dnf/plugins/spacewalk.conf'
3234SPACEWALK_YUM_CONF = '/etc/yum/pluginconf.d/spacewalk.conf'
3335
36+ # Packages that ship the spacewalk-protocol DNF/YUM plugin. Any one of
37+ # them being installed is sufficient evidence that CLN may serve
38+ # packages here; if none of them are present the plugin cannot run, no
39+ # matter what config files happen to be lying around (see
40+ # _spacewalk_plugin_installed below).
41+ _SPACEWALK_PLUGIN_PKGS = (
42+ 'dnf-plugin-spacewalk' ,
43+ 'python3-dnf-plugin-spacewalk' ,
44+ 'yum-rhn-plugin' ,
45+ )
46+
3447
3548def _plugin_explicitly_disabled (conf_path ):
3649 try :
@@ -47,23 +60,58 @@ def _plugin_explicitly_disabled(conf_path):
4760 return False
4861
4962
63+ def _spacewalk_plugin_installed ():
64+ """True iff at least one spacewalk-protocol plugin package is installed.
65+
66+ Done via `rpm -q --quiet <pkg>` per package: rpm returns 0 only when
67+ *that* package is installed. We OR across the candidate names and
68+ return on the first hit. Errors invoking rpm itself (broken database,
69+ PATH issues) are treated as "not installed" - a false negative here
70+ only causes CLN-related actors to stand down, which is the safe side
71+ of the call.
72+ """
73+ for pkg in _SPACEWALK_PLUGIN_PKGS :
74+ try :
75+ run (['rpm' , '-q' , '--quiet' , pkg ])
76+ return True
77+ except CalledProcessError :
78+ continue
79+ except (OSError , IOError ):
80+ return False
81+ return False
82+
83+
5084def is_cln_package_channel_active ():
5185 """Return True when CLN is the active package channel for this system.
5286
53- A True result means the spacewalk DNF/YUM plugin is installed, not
54- explicitly disabled, and the system has CLN registration state for
55- the plugin to authenticate with. A False result means the system is
56- either deregistered or has been moved to the no-auth (SWNG) scheme,
57- so CLN-targeting actions (channel switch, mirror pinning, version
58- overrides) are not meaningful and should be skipped.
87+ Requires all of:
88+
89+ * `/etc/sysconfig/rhn/systemid` present (CLN registration state),
90+ * at least one spacewalk-protocol plugin package installed,
91+ * a spacewalk plugin config file present, and
92+ * none of the present plugin config files explicitly setting `enabled = 0`.
93+
94+ A False result means the system is either deregistered, has no
95+ spacewalk plugin installed, or has been moved to the no-auth (SWNG)
96+ scheme, so CLN-targeting actions (channel switch, mirror pinning,
97+ version overrides) are not meaningful and should be skipped.
5998
6099 This is a deliberately heuristic check - it asks "is CLN going to
61100 serve packages here", not "is the system registered with CLN" (the
62101 two were the same thing pre-no-auth and have since diverged).
102+
103+ The plugin-package check guards against stale-config edge cases: when
104+ rhn-client-tools 3.0+ Obsoletes dnf-plugin-spacewalk, a leftover
105+ /etc/dnf/plugins/spacewalk.conf (saved without the .rpmsave suffix,
106+ or manually preserved) would otherwise make the helper claim CLN is
107+ active when no plugin can actually run.
63108 """
64109 if not os .path .exists (RHN_SYSTEMID ):
65110 return False
66111
112+ if not _spacewalk_plugin_installed ():
113+ return False
114+
67115 configs = [p for p in (SPACEWALK_DNF_CONF , SPACEWALK_YUM_CONF ) if os .path .exists (p )]
68116 if not configs :
69117 return False
0 commit comments