1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
# (c) 2018, Ansible Project
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)
from __future__ import annotations
import ansible.module_utils.compat.typing as t
from abc import ABCMeta, abstractmethod
from ansible.module_utils.six import with_metaclass
from ansible.module_utils.basic import missing_required_lib
from ansible.module_utils.common.process import get_bin_path
from ansible.module_utils.common.respawn import has_respawned, probe_interpreters_for_module, respawn_module
from ansible.module_utils.common._utils import get_all_subclasses
def get_all_pkg_managers():
return {obj.__name__.lower(): obj for obj in get_all_subclasses(PkgMgr) if obj not in (CLIMgr, LibMgr, RespawningLibMgr)}
class PkgMgr(with_metaclass(ABCMeta, object)): # type: ignore[misc]
@abstractmethod
def is_available(self, handle_exceptions):
# This method is supposed to return True/False if the package manager is currently installed/usable
# It can also 'prep' the required systems in the process of detecting availability
# If handle_exceptions is false it should raise exceptions related to manager discovery instead of handling them.
pass
@abstractmethod
def list_installed(self):
# This method should return a list of installed packages, each list item will be passed to get_package_details
pass
@abstractmethod
def get_package_details(self, package):
# This takes a 'package' item and returns a dictionary with the package information, name and version are minimal requirements
pass
def get_packages(self):
# Take all of the above and return a dictionary of lists of dictionaries (package = list of installed versions)
installed_packages = {}
for package in self.list_installed():
package_details = self.get_package_details(package)
if 'source' not in package_details:
package_details['source'] = self.__class__.__name__.lower()
name = package_details['name']
if name not in installed_packages:
installed_packages[name] = [package_details]
else:
installed_packages[name].append(package_details)
return installed_packages
class LibMgr(PkgMgr):
LIB = None # type: str | None
def __init__(self):
self._lib = None
super(LibMgr, self).__init__()
def is_available(self, handle_exceptions=True):
found = False
try:
self._lib = __import__(self.LIB)
found = True
except ImportError:
if not handle_exceptions:
raise Exception(missing_required_lib(self.LIB))
return found
class RespawningLibMgr(LibMgr):
CLI_BINARIES = [] # type: t.List[str]
INTERPRETERS = ['/usr/bin/python3']
def is_available(self, handle_exceptions=True):
if super(RespawningLibMgr, self).is_available():
return True
for binary in self.CLI_BINARIES:
try:
bin_path = get_bin_path(binary)
except ValueError:
# Not an interesting exception to raise, just a speculative probe
continue
else:
# It looks like this package manager is installed
if not has_respawned():
# See if respawning will help
interpreter_path = probe_interpreters_for_module(self.INTERPRETERS, self.LIB)
if interpreter_path:
respawn_module(interpreter_path)
# The module will exit when the respawned copy completes
if not handle_exceptions:
raise Exception(f'Found executable at {bin_path}. {missing_required_lib(self.LIB)}')
if not handle_exceptions:
raise Exception(missing_required_lib(self.LIB))
return False
class CLIMgr(PkgMgr):
CLI = None # type: str | None
def __init__(self):
self._cli = None
super(CLIMgr, self).__init__()
def is_available(self, handle_exceptions=True):
found = False
try:
self._cli = get_bin_path(self.CLI)
found = True
except ValueError:
if not handle_exceptions:
raise
return found
|