| 1 |
# Collect facts related to the system package manager |
| 2 |
|
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import os |
| 8 |
import subprocess |
| 9 |
import typing as t |
| 10 |
|
| 11 |
from ansible.module_utils.facts.collector import BaseFactCollector |
| 12 |
|
| 13 |
# A list of dicts. If there is a platform with more than one |
| 14 |
# package manager, put the preferred one last. If there is an |
| 15 |
# ansible module, use that as the value for the 'name' key. |
| 16 |
PKG_MGRS = [{'path': '/usr/bin/rpm-ostree', 'name': 'atomic_container'}, |
| 17 |
|
| 18 |
# NOTE the `path` key for dnf/dnf5 is effectively discarded when matched for Red Hat OS family, |
| 19 |
# special logic to infer the default `pkg_mgr` is used in `PkgMgrFactCollector._check_rh_versions()` |
| 20 |
# leaving them here so a list of package modules can be constructed by iterating over `name` keys |
| 21 |
{'path': '/usr/bin/yum', 'name': 'dnf'}, |
| 22 |
{'path': '/usr/bin/dnf-3', 'name': 'dnf'}, |
| 23 |
{'path': '/usr/bin/dnf5', 'name': 'dnf5'}, |
| 24 |
|
| 25 |
{'path': '/usr/bin/apt-get', 'name': 'apt'}, |
| 26 |
{'path': '/usr/bin/zypper', 'name': 'zypper'}, |
| 27 |
{'path': '/usr/sbin/urpmi', 'name': 'urpmi'}, |
| 28 |
{'path': '/usr/bin/pacman', 'name': 'pacman'}, |
| 29 |
{'path': '/bin/opkg', 'name': 'opkg'}, |
| 30 |
{'path': '/usr/pkg/bin/pkgin', 'name': 'pkgin'}, |
| 31 |
{'path': '/opt/local/bin/pkgin', 'name': 'pkgin'}, |
| 32 |
{'path': '/opt/tools/bin/pkgin', 'name': 'pkgin'}, |
| 33 |
{'path': '/opt/local/bin/port', 'name': 'macports'}, |
| 34 |
{'path': '/usr/local/bin/brew', 'name': 'homebrew'}, |
| 35 |
{'path': '/opt/homebrew/bin/brew', 'name': 'homebrew'}, |
| 36 |
{'path': '/sbin/apk', 'name': 'apk'}, |
| 37 |
{'path': '/usr/sbin/pkg', 'name': 'pkgng'}, |
| 38 |
{'path': '/usr/sbin/swlist', 'name': 'swdepot'}, |
| 39 |
{'path': '/usr/bin/emerge', 'name': 'portage'}, |
| 40 |
{'path': '/usr/sbin/pkgadd', 'name': 'svr4pkg'}, |
| 41 |
{'path': '/usr/bin/pkg', 'name': 'pkg5'}, |
| 42 |
{'path': '/usr/bin/xbps-install', 'name': 'xbps'}, |
| 43 |
{'path': '/usr/local/sbin/pkg', 'name': 'pkgng'}, |
| 44 |
{'path': '/usr/bin/swupd', 'name': 'swupd'}, |
| 45 |
{'path': '/usr/sbin/sorcery', 'name': 'sorcery'}, |
| 46 |
{'path': '/usr/bin/installp', 'name': 'installp'}, |
| 47 |
] |
| 48 |
|
| 49 |
|
| 50 |
class OpenBSDPkgMgrFactCollector(BaseFactCollector): |
| 51 |
name = 'pkg_mgr' |
| 52 |
_fact_ids = set() # type: t.Set[str] |
| 53 |
_platform = 'OpenBSD' |
| 54 |
|
| 55 |
def collect(self, module=None, collected_facts=None): |
| 56 |
return {'pkg_mgr': 'openbsd_pkg'} |
| 57 |
|
| 58 |
|
| 59 |
# the fact ends up being 'pkg_mgr' so stick with that naming/spelling |
| 60 |
class PkgMgrFactCollector(BaseFactCollector): |
| 61 |
name = 'pkg_mgr' |
| 62 |
_fact_ids = set() # type: t.Set[str] |
| 63 |
_platform = 'Generic' |
| 64 |
required_facts = set(['distribution']) |
| 65 |
|
| 66 |
def __init__(self, *args, **kwargs): |
| 67 |
super(PkgMgrFactCollector, self).__init__(*args, **kwargs) |
| 68 |
self._default_unknown_pkg_mgr = 'unknown' |
| 69 |
|
| 70 |
def _check_rh_versions(self): |
| 71 |
if os.path.exists('/run/ostree-booted'): |
| 72 |
return "atomic_container" |
| 73 |
|
| 74 |
# Since /usr/bin/dnf and /usr/bin/microdnf can point to different versions of dnf in different distributions |
| 75 |
# the only way to infer the default package manager is to look at the binary they are pointing to. |
| 76 |
# /usr/bin/microdnf is likely used only in fedora minimal container so /usr/bin/dnf takes precedence |
| 77 |
for bin_path in ('/usr/bin/dnf', '/usr/bin/microdnf'): |
| 78 |
if os.path.exists(bin_path): |
| 79 |
return 'dnf5' if os.path.realpath(bin_path) == '/usr/bin/dnf5' else 'dnf' |
| 80 |
|
| 81 |
return self._default_unknown_pkg_mgr |
| 82 |
|
| 83 |
def _check_apt_flavor(self, pkg_mgr_name): |
| 84 |
# Check if '/usr/bin/apt' is APT-RPM or an ordinary (dpkg-based) APT. |
| 85 |
# There's rpm package on Debian, so checking if /usr/bin/rpm exists |
| 86 |
# is not enough. Instead ask RPM if /usr/bin/apt-get belongs to some |
| 87 |
# RPM package. |
| 88 |
rpm_query = '/usr/bin/rpm -q --whatprovides /usr/bin/apt-get'.split() |
| 89 |
if os.path.exists('/usr/bin/rpm'): |
| 90 |
with open(os.devnull, 'w') as null: |
| 91 |
try: |
| 92 |
subprocess.check_call(rpm_query, stdout=null, stderr=null) |
| 93 |
pkg_mgr_name = 'apt_rpm' |
| 94 |
except subprocess.CalledProcessError: |
| 95 |
# No apt-get in RPM database. Looks like Debian/Ubuntu |
| 96 |
# with rpm package installed |
| 97 |
pkg_mgr_name = 'apt' |
| 98 |
return pkg_mgr_name |
| 99 |
|
| 100 |
def pkg_mgrs(self, collected_facts): |
| 101 |
# Filter out the /usr/bin/pkg because on Altlinux it is actually the |
| 102 |
# perl-Package (not Solaris package manager). |
| 103 |
# Since the pkg5 takes precedence over apt, this workaround |
| 104 |
# is required to select the suitable package manager on Altlinux. |
| 105 |
if collected_facts['ansible_os_family'] == 'Altlinux': |
| 106 |
return filter(lambda pkg: pkg['path'] != '/usr/bin/pkg', PKG_MGRS) |
| 107 |
else: |
| 108 |
return PKG_MGRS |
| 109 |
|
| 110 |
def collect(self, module=None, collected_facts=None): |
| 111 |
collected_facts = collected_facts or {} |
| 112 |
|
| 113 |
pkg_mgr_name = self._default_unknown_pkg_mgr |
| 114 |
for pkg in self.pkg_mgrs(collected_facts): |
| 115 |
if os.path.exists(pkg['path']): |
| 116 |
pkg_mgr_name = pkg['name'] |
| 117 |
|
| 118 |
# Handle distro family defaults when more than one package manager is |
| 119 |
# installed or available to the distro, the ansible_fact entry should be |
| 120 |
# the default package manager officially supported by the distro. |
| 121 |
if collected_facts['ansible_os_family'] == "RedHat": |
| 122 |
pkg_mgr_name = self._check_rh_versions() |
| 123 |
elif collected_facts['ansible_os_family'] == 'Debian' and pkg_mgr_name != 'apt': |
| 124 |
# It's possible to install dnf, zypper, rpm, etc inside of |
| 125 |
# Debian. Doing so does not mean the system wants to use them. |
| 126 |
pkg_mgr_name = 'apt' |
| 127 |
elif collected_facts['ansible_os_family'] == 'Altlinux': |
| 128 |
if pkg_mgr_name == 'apt': |
| 129 |
pkg_mgr_name = 'apt_rpm' |
| 130 |
|
| 131 |
# Check if /usr/bin/apt-get is ordinary (dpkg-based) APT or APT-RPM |
| 132 |
if pkg_mgr_name == 'apt': |
| 133 |
pkg_mgr_name = self._check_apt_flavor(pkg_mgr_name) |
| 134 |
|
| 135 |
return {'pkg_mgr': pkg_mgr_name} |