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

pkgng: Add support for upgrading all installed packages (#569)

* pkgng: Add support for upgrading all installed packages

Adds support for ``name: "*", state: latest`` to upgrade all installed
packages, similar to other package providers.

Co-authored-by: Felix Fontein <felix@fontein.de>

* pkgng: Improve wording for warning in example, fix formatting

* pkgng.py: Fix capitalization

Co-authored-by: Felix Fontein <felix@fontein.de>

Co-authored-by: Davíð Steinn Geirsson <david@isnic.is>
Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Davíð Steinn Geirsson 2020-07-07 21:04:35 +00:00 committed by GitHub
parent c1b5b51366
commit d0b07885f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 6 deletions

View file

@ -0,0 +1,6 @@
---
minor_changes:
- "pkgng - added support for upgrading all packages using ``name: *, state: latest``, similar to other package providers (https://github.com/ansible-collections/community.general/pull/569)."
breaking_changes:
- "pkgng - passing ``name: *`` with ``state: latest`` or ``state: present`` will no longer install every package from the configured package repositories. Instead, ``name: *, state: latest`` will upgrade all already-installed packages, and ``name: *, state: present`` is a noop. (https://github.com/ansible-collections/community.general/pull/569)."
- "pkgng - passing ``name: *`` with ``state: absent`` will no longer remove every installed package from the system. It is now a noop. (https://github.com/ansible-collections/community.general/pull/569)."

View file

@ -23,6 +23,13 @@ options:
name: name:
description: description:
- Name or list of names of packages to install/remove. - Name or list of names of packages to install/remove.
- "With I(name=*), I(state: latest) will operate, but I(state: present) and I(state: absent) will be noops."
- >
Warning: In Ansible 2.9 and earlier this module had a misfeature
where I(name=*) with I(state: latest) or I(state: present) would
install every package from every package repository, filling up
the machines disk. Avoid using them unless you are certain that
your role will only be used with newer versions.
required: true required: true
aliases: [pkg] aliases: [pkg]
type: list type: list
@ -111,6 +118,11 @@ EXAMPLES = '''
pkgng: pkgng:
name: baz name: baz
state: latest state: latest
- name: Upgrade all installed packages (see warning for the name option first!)
pkgng:
name: "*"
state: latest
''' '''
@ -161,6 +173,24 @@ def pkgng_older_than(module, pkgng_path, compare_version):
return not new_pkgng return not new_pkgng
def upgrade_packages(module, pkgng_path, dir_arg):
# Run a 'pkg upgrade', updating all packages.
upgraded_c = 0
cmd = "%s %s upgrade -y" % (pkgng_path, dir_arg)
if module.check_mode:
cmd += " -n"
rc, out, err = module.run_command(cmd)
match = re.search('^Number of packages to be upgraded: ([0-9]+)', out, re.MULTILINE)
if match:
upgraded_c = int(match.group(1))
if upgraded_c > 0:
return (True, "updated %s package(s)" % upgraded_c, out, err)
return (False, "no packages need upgrades", out, err)
def remove_packages(module, pkgng_path, packages, dir_arg): def remove_packages(module, pkgng_path, packages, dir_arg):
remove_c = 0 remove_c = 0
stdout = "" stdout = ""
@ -391,18 +421,28 @@ def main():
if p["jail"] != "": if p["jail"] != "":
dir_arg = '--jail %s' % (p["jail"]) dir_arg = '--jail %s' % (p["jail"])
if p["state"] in ("present", "latest"): if pkgs == ['*'] and p["state"] == 'latest':
_changed, _msg, _stdout, _stderr = install_packages(module, pkgng_path, pkgs, p["cached"], p["pkgsite"], dir_arg, p["state"]) # Operate on all installed packages. Only state: latest makes sense here.
_changed, _msg, _stdout, _stderr = upgrade_packages(module, pkgng_path, dir_arg)
changed = changed or _changed changed = changed or _changed
stdout += _stdout stdout += _stdout
stderr += _stderr stderr += _stderr
msgs.append(_msg) msgs.append(_msg)
elif p["state"] == "absent": # Operate on named packages
_changed, _msg, _stdout, _stderr = remove_packages(module, pkgng_path, pkgs, dir_arg) named_packages = [pkg for pkg in pkgs if pkg != '*']
if p["state"] in ("present", "latest") and named_packages:
_changed, _msg, _out, _err = install_packages(module, pkgng_path, named_packages, p["cached"], p["pkgsite"], dir_arg, p["state"])
stdout += _out
stderr += _err
changed = changed or _changed
msgs.append(_msg)
elif p["state"] == "absent" and named_packages:
_changed, _msg, _out, _err = remove_packages(module, pkgng_path, named_packages, dir_arg)
stdout += _out
stderr += _err
changed = changed or _changed changed = changed or _changed
stdout += _stdout
stderr += _stderr
msgs.append(_msg) msgs.append(_msg)
if p["autoremove"]: if p["autoremove"]: