1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Xbps various changes (#95)

* fix(modules/xbps): do not upgrade in check mode

Ensure that while in check mode, this module does not upgrade all
packages on the system.

* chore(modules/xbps): refactor update_cache

Pull out the update_cache logic from the main function.

* feat(modules/xbps): upgrade xbps package itself

Perform an upgrade of the xbps package itself, when necessary.
Otherwise, users' playbooks will fail to install or upgrade packages
when the xbps version is out of date.
See https://github.com/void-linux/xbps/issues/229 for more information.
This commit is contained in:
Cameron Nemo 2020-04-10 11:15:02 -07:00 committed by GitHub
parent 63b6b8a9d3
commit 45b171a064
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -50,21 +50,41 @@ options:
- Whether or not to upgrade whole system - Whether or not to upgrade whole system
type: bool type: bool
default: 'no' default: 'no'
upgrade_xbps:
description:
- Whether or not to upgrade the xbps package when necessary.
Before installing new packages,
xbps requires the user to update the xbps package itself.
Thus when this option is set to C(no),
upgrades and installations will fail when xbps is not up to date.
type: bool
default: 'yes'
''' '''
EXAMPLES = ''' EXAMPLES = '''
# Install package foo - name: Install package foo (automatically updating the xbps package if needed)
- xbps: name=foo state=present xbps: name=foo state=present
# Upgrade package foo
- xbps: name=foo state=latest update_cache=yes - name: Upgrade package foo
# Remove packages foo and bar xbps: name=foo state=latest update_cache=yes
- xbps: name=foo,bar state=absent
# Recursively remove package foo - name: Remove packages foo and bar
- xbps: name=foo state=absent recurse=yes xbps: name=foo,bar state=absent
# Update package cache
- xbps: update_cache=yes - name: Recursively remove package foo
# Upgrade packages xbps: name=foo state=absent recurse=yes
- xbps: upgrade=yes
- name: Update package cache
xbps: update_cache=yes
- name: Upgrade packages
xbps: upgrade=yes
- name: Install a package, failing if the xbps package is out of date
xbps:
name: foo
state: present
upgrade_xbps: no
''' '''
RETURN = ''' RETURN = '''
@ -124,6 +144,13 @@ def update_package_db(module, xbps_path):
return False return False
def upgrade_xbps(module, xbps_path, exit_on_success=False):
cmdupgradexbps = "%s -uy xbps" % (xbps_path['install'])
rc, stdout, stderr = module.run_command(cmdupgradexbps, check_rc=False)
if rc != 0:
module.fail_json(msg='Could not upgrade xbps itself')
def upgrade(module, xbps_path): def upgrade(module, xbps_path):
"""Returns true is full upgrade succeeds""" """Returns true is full upgrade succeeds"""
cmdupgrade = "%s -uy" % (xbps_path['install']) cmdupgrade = "%s -uy" % (xbps_path['install'])
@ -133,10 +160,17 @@ def upgrade(module, xbps_path):
if rc == 0: if rc == 0:
if(len(stdout.splitlines()) == 0): if(len(stdout.splitlines()) == 0):
module.exit_json(changed=False, msg='Nothing to upgrade') module.exit_json(changed=False, msg='Nothing to upgrade')
elif module.check_mode:
module.exit_json(changed=True, msg='Would have performed upgrade')
else: else:
rc, stdout, stderr = module.run_command(cmdupgrade, check_rc=False) rc, stdout, stderr = module.run_command(cmdupgrade, check_rc=False)
if rc == 0: if rc == 0:
module.exit_json(changed=True, msg='System upgraded') module.exit_json(changed=True, msg='System upgraded')
elif rc == 16 and module.params['upgrade_xbps']:
upgrade_xbps(module, xbps_path)
# avoid loops by not trying self-upgrade again
module.params['upgrade_xbps'] = False
upgrade(module, xbps_path)
else: else:
module.fail_json(msg="Could not upgrade") module.fail_json(msg="Could not upgrade")
else: else:
@ -188,16 +222,18 @@ def install_packages(module, xbps_path, state, packages):
cmd = "%s -y %s" % (xbps_path['install'], " ".join(toInstall)) cmd = "%s -y %s" % (xbps_path['install'], " ".join(toInstall))
rc, stdout, stderr = module.run_command(cmd, check_rc=False) rc, stdout, stderr = module.run_command(cmd, check_rc=False)
if rc != 0 and not (state == 'latest' and rc == 17): if rc == 16 and module.params['upgrade_xbps']:
upgrade_xbps(module, xbps_path)
# avoid loops by not trying self-update again
module.params['upgrade_xbps'] = False
install_packages(module, xbps_path, state, packages)
elif rc != 0 and not (state == 'latest' and rc == 17):
module.fail_json(msg="failed to install %s" % (package)) module.fail_json(msg="failed to install %s" % (package))
module.exit_json(changed=True, msg="installed %s package(s)" module.exit_json(changed=True, msg="installed %s package(s)"
% (len(toInstall)), % (len(toInstall)),
packages=toInstall) packages=toInstall)
module.exit_json(changed=False, msg="package(s) already installed",
packages=[])
def check_packages(module, xbps_path, packages, state): def check_packages(module, xbps_path, packages, state):
"""Returns change status of command""" """Returns change status of command"""
@ -219,6 +255,22 @@ def check_packages(module, xbps_path, packages, state):
packages=[]) packages=[])
def update_cache(module, xbps_path, upgrade_planned):
"""Update package cache"""
if module.check_mode:
if upgrade_planned:
return
module.exit_json(
changed=True, msg='Would have updated the package cache'
)
changed = update_package_db(module, xbps_path)
if not upgrade_planned:
module.exit_json(changed=changed, msg=(
'Updated the package master lists' if changed
else 'Package list already up to date'
))
def main(): def main():
"""Returns, calling appropriate command""" """Returns, calling appropriate command"""
@ -232,7 +284,8 @@ def main():
force=dict(default=False, type='bool'), force=dict(default=False, type='bool'),
upgrade=dict(default=False, type='bool'), upgrade=dict(default=False, type='bool'),
update_cache=dict(default=True, aliases=['update-cache'], update_cache=dict(default=True, aliases=['update-cache'],
type='bool') type='bool'),
upgrade_xbps=dict(default=True, type='bool')
), ),
required_one_of=[['name', 'update_cache', 'upgrade']], required_one_of=[['name', 'update_cache', 'upgrade']],
supports_check_mode=True) supports_check_mode=True)
@ -254,20 +307,8 @@ def main():
elif p['state'] in ['absent', 'removed']: elif p['state'] in ['absent', 'removed']:
p['state'] = 'absent' p['state'] = 'absent'
if p["update_cache"] and not module.check_mode: if p['update_cache']:
changed = update_package_db(module, xbps_path) update_cache(module, xbps_path, (p['name'] or p['upgrade']))
if p['name'] is None and not p['upgrade']:
if changed:
module.exit_json(changed=True,
msg='Updated the package master lists')
else:
module.exit_json(changed=False,
msg='Package list already up to date')
if (p['update_cache'] and module.check_mode and not
(p['name'] or p['upgrade'])):
module.exit_json(changed=True,
msg='Would have updated the package cache')
if p['upgrade']: if p['upgrade']:
upgrade(module, xbps_path) upgrade(module, xbps_path)