mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 21:00:22 -07:00
scan_packages: made adding package managers easier (#49079)
* made adding package managers easier added portage support * moar pkg mgrs and moar info - added 'pkg' pkg manager (freebsd) - added pip - more apt info * updated clgo * Updates from feedback Co-Authored-By: bcoca <bcoca@users.noreply.github.com> * incorporated more feedback and added docstrings * moar from feedback - made manager list dynamic and names based on class - better not found msg - made abstract metaclass again - test is now init exception - module to global - better dedupe comments * more targetted errors/warnings * added strategy, reordered to conserve priority * rpm > apt * move break to top * fix trate * piping it * lines and meta * refactored common functions - moved pip into it's own module - cleaned up base clases - ensure 'lower' match in package_facts * missing license * avoid facts * update clog * addressed feedback * fix clog * cleanup * upd * removed pip as that was removed * renamed cpan * added a single line since 2 lines are needed to be readabnle instead of just 1 line, it is a huge problem otherwise * fix internal ref * not intended in this round * updated as per fb
This commit is contained in:
parent
3fe2013b3f
commit
b94bfed1a6
5 changed files with 393 additions and 75 deletions
|
@ -19,11 +19,24 @@ description:
|
|||
options:
|
||||
manager:
|
||||
description:
|
||||
- The package manager used by the system so we can query the package information
|
||||
default: auto
|
||||
choices: ["auto", "rpm", "apt"]
|
||||
- The package manager used by the system so we can query the package information.
|
||||
- Since 2.8 this is a list and can support multiple package managers per system.
|
||||
- The 'portage' and 'pkg' options were added in version 2.8.
|
||||
default: ['auto']
|
||||
choices: ['auto', 'rpm', 'apt', 'portage', 'pkg']
|
||||
required: False
|
||||
type: list
|
||||
strategy:
|
||||
description:
|
||||
- This option controls how the module queres the package managers on the system.
|
||||
C(first) means it will return only informatino for the first supported package manager available.
|
||||
C(all) will return information for all supported and available package managers on the system.
|
||||
choices: ['first', 'all']
|
||||
default: 'first'
|
||||
version_added: "2.8"
|
||||
version_added: "2.5"
|
||||
requirements:
|
||||
- For 'portage' support it requires the `qlist` utility, which is part of 'app-portage/portage-utils'.
|
||||
author:
|
||||
- Matthew Jones (@matburt)
|
||||
- Brian Coca (@bcoca)
|
||||
|
@ -140,94 +153,162 @@ ansible_facts:
|
|||
}
|
||||
'''
|
||||
|
||||
import sys
|
||||
|
||||
from ansible.module_utils._text import to_native, to_text
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.module_utils.facts.packages import LibMgr, CLIMgr, get_all_pkg_managers
|
||||
|
||||
|
||||
def rpm_package_list():
|
||||
class RPM(LibMgr):
|
||||
|
||||
try:
|
||||
import rpm
|
||||
except ImportError:
|
||||
module.fail_json(msg='Unable to use the rpm python bindings, please ensure they are installed under the python the module runs under')
|
||||
LIB = 'rpm'
|
||||
|
||||
trans_set = rpm.TransactionSet()
|
||||
installed_packages = {}
|
||||
for package in trans_set.dbMatch():
|
||||
package_details = dict(name=package[rpm.RPMTAG_NAME],
|
||||
version=package[rpm.RPMTAG_VERSION],
|
||||
release=package[rpm.RPMTAG_RELEASE],
|
||||
epoch=package[rpm.RPMTAG_EPOCH],
|
||||
arch=package[rpm.RPMTAG_ARCH],
|
||||
source='rpm')
|
||||
if package_details['name'] not in installed_packages:
|
||||
installed_packages[package_details['name']] = [package_details]
|
||||
else:
|
||||
installed_packages[package_details['name']].append(package_details)
|
||||
return installed_packages
|
||||
def list_installed(self):
|
||||
return self._lib.TransactionSet().dbMatch()
|
||||
|
||||
def get_package_details(self, package):
|
||||
return dict(name=package[self._lib.RPMTAG_NAME],
|
||||
version=package[self._lib.RPMTAG_VERSION],
|
||||
release=package[self._lib.RPMTAG_RELEASE],
|
||||
epoch=package[self._lib.RPMTAG_EPOCH],
|
||||
arch=package[self._lib.RPMTAG_ARCH],)
|
||||
|
||||
|
||||
def apt_package_list():
|
||||
class APT(LibMgr):
|
||||
|
||||
try:
|
||||
import apt
|
||||
except ImportError:
|
||||
module.fail_json(msg='Unable to use the apt python bindings, please ensure they are installed under the python the module runs under')
|
||||
LIB = 'apt'
|
||||
|
||||
apt_cache = apt.Cache()
|
||||
installed_packages = {}
|
||||
apt_installed_packages = [pk for pk in apt_cache.keys() if apt_cache[pk].is_installed]
|
||||
for package in apt_installed_packages:
|
||||
ac_pkg = apt_cache[package].installed
|
||||
package_details = dict(name=package, version=ac_pkg.version, arch=ac_pkg.architecture, source='apt')
|
||||
if package_details['name'] not in installed_packages:
|
||||
installed_packages[package_details['name']] = [package_details]
|
||||
else:
|
||||
installed_packages[package_details['name']].append(package_details)
|
||||
return installed_packages
|
||||
@property
|
||||
def pkg_cache(self):
|
||||
if self._cache:
|
||||
return self._cache
|
||||
|
||||
self._cache = self._lib.Cache()
|
||||
return self._cache
|
||||
|
||||
def list_installed(self):
|
||||
return [pk for pk in self.pkg_cache.keys() if self.pkg_cache[pk].is_installed]
|
||||
|
||||
def get_package_details(self, package):
|
||||
ac_pkg = self.pkg_cache[package].installed
|
||||
return dict(name=package, version=ac_pkg.version, arch=ac_pkg.architecture, category=ac_pkg.section, origin=ac_pkg.origins[0].origin)
|
||||
|
||||
|
||||
# FIXME: add more listing methods
|
||||
def main():
|
||||
global module
|
||||
module = AnsibleModule(argument_spec=dict(manager=dict()), supports_check_mode=True)
|
||||
manager = module.params['manager']
|
||||
packages = {}
|
||||
results = {}
|
||||
class PKG(CLIMgr):
|
||||
|
||||
if manager is None or manager == 'auto':
|
||||
CLI = 'pkg'
|
||||
atoms = ['name', 'version', 'origin', 'installed', 'automatic', 'arch', 'category', 'prefix', 'vital']
|
||||
|
||||
# detect!
|
||||
for manager_lib in ('rpm', 'apt'):
|
||||
def list_installed(self):
|
||||
rc, out, err = module.run_command([self._cli, 'query', "%%%s" % '\t%'.join(['n', 'v', 'R', 't', 'a', 'q', 'o', 'p', 'V'])])
|
||||
if rc != 0 or err:
|
||||
raise Exception("Unable to list packages rc=%s : %s" % (rc, err))
|
||||
return out.splitlines()
|
||||
|
||||
def get_package_details(self, package):
|
||||
|
||||
pkg = dict(zip(self.atoms, package.split('\t')))
|
||||
|
||||
if 'arch' in pkg:
|
||||
try:
|
||||
dummy = __import__(manager_lib)
|
||||
manager = manager_lib
|
||||
break
|
||||
except ImportError:
|
||||
pkg['arch'] = pkg['arch'].split(':')[2]
|
||||
except IndexError:
|
||||
pass
|
||||
|
||||
# FIXME: add more detection methods
|
||||
try:
|
||||
if manager == "rpm":
|
||||
packages = rpm_package_list()
|
||||
elif manager == "apt":
|
||||
packages = apt_package_list()
|
||||
else:
|
||||
if manager:
|
||||
results['msg'] = 'Unsupported package manager: %s' % manager
|
||||
results['skipped'] = True
|
||||
else:
|
||||
module.fail_json(msg='Could not detect supported package manager')
|
||||
except Exception as e:
|
||||
from traceback import format_tb
|
||||
module.fail_json(msg='Failed to retrieve packages: %s' % to_text(e), exception=format_tb(sys.exc_info()[2]))
|
||||
if 'automatic' in pkg:
|
||||
pkg['automatic'] = bool(pkg['automatic'])
|
||||
|
||||
results['ansible_facts'] = {}
|
||||
# Set the facts, this will override the facts in ansible_facts that might
|
||||
# exist from previous runs when using operating system level or distribution
|
||||
# package managers
|
||||
if 'category' in pkg:
|
||||
pkg['category'] = pkg['category'].split('/', 1)[0]
|
||||
|
||||
if 'version' in pkg:
|
||||
if ',' in pkg['version']:
|
||||
pkg['version'], pkg['port_epoch'] = pkg['version'].split(',', 1)
|
||||
else:
|
||||
pkg['port_epoch'] = 0
|
||||
|
||||
if '_' in pkg['version']:
|
||||
pkg['version'], pkg['revision'] = pkg['version'].split('_', 1)
|
||||
else:
|
||||
pkg['revision'] = '0'
|
||||
|
||||
if 'vital' in pkg:
|
||||
pkg['vital'] = bool(pkg['vital'])
|
||||
|
||||
return pkg
|
||||
|
||||
|
||||
class PORTAGE(CLIMgr):
|
||||
|
||||
CLI = 'qlist'
|
||||
atoms = ['category', 'name', 'version', 'ebuild_revision', 'slots', 'prefixes', 'sufixes']
|
||||
|
||||
def list_installed(self):
|
||||
rc, out, err = module.run_command(' '.join([self._cli, '-Iv', '|', 'xargs', '-n', '1024', 'qatom']), use_unsafe_shell=True)
|
||||
if rc != 0:
|
||||
raise RuntimeError("Unable to list packages rc=%s : %s" % (rc, to_native(err)))
|
||||
return out.splitlines()
|
||||
|
||||
def get_package_details(self, package):
|
||||
return dict(zip(self.atoms, package.split()))
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
# get supported pkg managers
|
||||
PKG_MANAGERS = get_all_pkg_managers()
|
||||
PKG_MANAGER_NAMES = [x.lower() for x in PKG_MANAGERS.keys()]
|
||||
|
||||
# start work
|
||||
global module
|
||||
module = AnsibleModule(argument_spec=dict(manager={'type': 'list', 'default': ['auto']},
|
||||
strategy={'choices': ['first', 'all'], 'default': 'first'}),
|
||||
supports_check_mode=True)
|
||||
packages = {}
|
||||
results = {'ansible_facts': {}}
|
||||
managers = [x.lower() for x in module.params['manager']]
|
||||
strategy = module.params['strategy']
|
||||
|
||||
if 'auto' in managers:
|
||||
# keep order from user, we do dedupe below
|
||||
managers.extend(PKG_MANAGER_NAMES)
|
||||
managers.remove('auto')
|
||||
|
||||
unsupported = set(managers).difference(PKG_MANAGER_NAMES)
|
||||
if unsupported:
|
||||
module.fail_json(msg='Unsupported package managers requested: %s' % (', '.join(unsupported)))
|
||||
|
||||
found = 0
|
||||
seen = set()
|
||||
for pkgmgr in managers:
|
||||
|
||||
if found and strategy == 'first':
|
||||
break
|
||||
|
||||
# dedupe as per above
|
||||
if pkgmgr in seen:
|
||||
continue
|
||||
seen.add(pkgmgr)
|
||||
try:
|
||||
try:
|
||||
# manager throws exception on init (calls self.test) if not usable.
|
||||
manager = PKG_MANAGERS[pkgmgr]()
|
||||
if manager.is_available():
|
||||
found += 1
|
||||
packages.update(manager.get_packages())
|
||||
except Exception as e:
|
||||
if pkgmgr in module.params['manager']:
|
||||
module.warn('Requested package manager %s was not usable by this module: %s' % (pkgmgr, to_text(e)))
|
||||
continue
|
||||
|
||||
except Exception as e:
|
||||
if pkgmgr in module.params['manager']:
|
||||
module.warn('Failed to retrieve packages with %s: %s' % (pkgmgr, to_text(e)))
|
||||
|
||||
if found == 0:
|
||||
module.fail_json(msg='Could not detect a supported package manager from the following list: %s' % managers)
|
||||
|
||||
# Set the facts, this will override the facts in ansible_facts that might exist from previous runs
|
||||
# when using operating system level or distribution package managers
|
||||
results['ansible_facts']['packages'] = packages
|
||||
|
||||
module.exit_json(**results)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue