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