Skip to content

Commit

Permalink
apt_rpm: add clean, dist_upgrade and update_kernel
Browse files Browse the repository at this point in the history
* clean performs apt-get clean.

* dist_upgrade performs apt-get dist-upgrade.

* update_kernel performs update-kernel to upgrade kernel packages.

* update_cache, clean, dist_upgrade and update_kernel can be used with and without specified packages.
  • Loading branch information
sibskull committed Feb 16, 2023
1 parent c4b1836 commit a5772d6
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 12 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/5867-apt_rpm-add-clean-and-upgrade.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- apt_rpm - adds ``clean``, ``dist_upgrade`` and ``update_kernel`` parameters for clear caches, complete upgrade system and upgrade kernel packages (https://github.com/ansible-collections/community.general/pull/5867).
105 changes: 93 additions & 12 deletions plugins/modules/apt_rpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
options:
package:
description:
- list of packages to install, upgrade or remove.
required: true
- List of packages to install, upgrade, or remove.
aliases: [ name, pkg ]
type: list
elements: str
Expand All @@ -33,9 +32,30 @@
type: str
update_cache:
description:
- update the package database first C(apt-get update).
- Run the equivalent of C(apt-get update) before the operation. Can be run as part of the package installation or as a separate step.
- Default is not to update the cache.
type: bool
default: false
clean:
description:
- Run the equivalent of C(apt-get clean) to clear out the local repository of retrieved package files. It removes everything but
the lock file from C(/var/cache/apt/archives/) and C(/var/cache/apt/archives/partial/).
- Can be run as part of the package installation (clean runs before install) or as a separate step.
type: bool
default: false
version_added: 6.3.0
dist_upgrade:
description:
- If true performs an C(apt-get dist-upgrade) to upgrade system.
type: bool
default: false
version_added: 6.3.0
update_kernel:
description:
- If true performs an C(update-kernel) to upgrade kernel packages.
type: bool
default: false
version_added: 6.3.0
author:
- Evgenii Terechkov (@evgkrsk)
'''
Expand Down Expand Up @@ -69,6 +89,16 @@
name: bar
state: present
update_cache: true
- name: Run the equivalent of "apt-get clean" as a separate step
community.general.apt_rpm:
clean: true
- name: Perform cache update and complete system upgrade (includes kernel)
community.general.apt_rpm:
update_cache: true
dist_upgrade: true
update_kernel: true
'''

import os
Expand All @@ -77,6 +107,8 @@

APT_PATH = "/usr/bin/apt-get"
RPM_PATH = "/usr/bin/rpm"
APT_GET_ZERO = "\n0 upgraded, 0 newly installed"
UPDATE_KERNEL_ZERO = "\nTry to install new kernel "


def query_package(module, name):
Expand All @@ -97,10 +129,33 @@ def query_package_provides(module, name):


def update_package_db(module):
rc, out, err = module.run_command("%s update" % APT_PATH)
rc, out, err = module.run_command(["md5sum", "/var/cache/apt/pkgcache.bin"])
md5sum_before = out.split()[0]
rc, update_out, err = module.run_command([APT_PATH, "update"], check_rc=True, environ_update={"LANG": "C"})
rc, out, err = module.run_command(["md5sum", "/var/cache/apt/pkgcache.bin"])
md5sum_after = out.split()[0]
return (md5sum_before != md5sum_after, update_out)


def dir_size(module, path):
rc, out, err = module.run_command(["du", "-sb", path], check_rc=True)
return out.split()[0]


def clean(module):
t = dir_size(module, "/var/cache/apt/archives")
rc, out, err = module.run_command([APT_PATH, "clean"], check_rc=True)
return (t != dir_size(module, "/var/cache/apt/archives"), out)


def dist_upgrade(module):
rc, out, err = module.run_command([APT_PATH, "-y", "dist-upgrade"], check_rc=True, environ_update={"LANG": "C"})
return (APT_GET_ZERO not in out, out)

if rc != 0:
module.fail_json(msg="could not update package db: %s" % err)

def update_kernel(module):
rc, out, err = module.run_command(["/usr/sbin/update-kernel", "-y"], check_rc=True, environ_update={"LANG": "C"})
return (UPDATE_KERNEL_ZERO not in out, out)


def remove_packages(module, packages):
Expand Down Expand Up @@ -155,25 +210,51 @@ def main():
argument_spec=dict(
state=dict(type='str', default='present', choices=['absent', 'installed', 'present', 'removed']),
update_cache=dict(type='bool', default=False),
package=dict(type='list', elements='str', required=True, aliases=['name', 'pkg']),
clean=dict(type='bool', default=False),
dist_upgrade=dict(type='bool', default=False),
update_kernel=dict(type='bool', default=False),
package=dict(type='list', elements='str', aliases=['name', 'pkg']),
),
)

if not os.path.exists(APT_PATH) or not os.path.exists(RPM_PATH):
module.fail_json(msg="cannot find /usr/bin/apt-get and/or /usr/bin/rpm")

p = module.params
modified = False
output = ""

if p['update_cache']:
update_package_db(module)
(m, out) = update_package_db(module)
modified = modified or m
output += out

if p['clean']:
(m, out) = clean(module)
modified = modified or m

if p['dist_upgrade']:
(m, out) = dist_upgrade(module)
modified = modified or m
output += out

if p['update_kernel']:
(m, out) = update_kernel(module)
modified = modified or m
output += out

# If package is missing return exit_json
packages = p['package']

if p['state'] in ['installed', 'present']:
install_packages(module, packages)
if not isinstance(packages, list):
module.exit_json(changed=modified, msg=output)

else:
if p['state'] in ['installed', 'present']:
install_packages(module, packages)

elif p['state'] in ['absent', 'removed']:
remove_packages(module, packages)
elif p['state'] in ['absent', 'removed']:
remove_packages(module, packages)


if __name__ == '__main__':
Expand Down

0 comments on commit a5772d6

Please sign in to comment.