| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2014, Kim Nørgaard |
| 4 |
# Written by Kim Nørgaard <[email protected]> |
| 5 |
# Based on pkgng module written by bleader <[email protected]> |
| 6 |
# that was based on pkgin module written by Shaun Zinck <shaun.zinck at gmail.com> |
| 7 |
# that was based on pacman module written by Afterburn <https://github.com/afterburn> |
| 8 |
# that was based on apt module written by Matthew Williams <[email protected]> |
| 9 |
# |
| 10 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 11 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 12 |
|
| 13 |
from __future__ import annotations |
| 14 |
|
| 15 |
DOCUMENTATION = r""" |
| 16 |
module: slackpkg |
| 17 |
short_description: Package manager for Slackware >= 12.2 |
| 18 |
description: |
| 19 |
- Manage binary packages for Slackware using C(slackpkg) which is available in versions after 12.2. |
| 20 |
extends_documentation_fragment: |
| 21 |
- community.general._attributes |
| 22 |
attributes: |
| 23 |
check_mode: |
| 24 |
support: full |
| 25 |
diff_mode: |
| 26 |
support: none |
| 27 |
options: |
| 28 |
name: |
| 29 |
description: |
| 30 |
- Name of package to install/remove. |
| 31 |
required: true |
| 32 |
type: list |
| 33 |
elements: str |
| 34 |
aliases: [pkg] |
| 35 |
|
| 36 |
state: |
| 37 |
description: |
| 38 |
- State of the package, you can use V(installed) as an alias for V(present) and V(removed) as one for V(absent). |
| 39 |
choices: ['present', 'absent', 'latest', 'installed', 'removed'] |
| 40 |
default: present |
| 41 |
type: str |
| 42 |
|
| 43 |
update_cache: |
| 44 |
description: |
| 45 |
- Update the package database first. |
| 46 |
default: false |
| 47 |
type: bool |
| 48 |
|
| 49 |
author: Kim Nørgaard (@KimNorgaard) |
| 50 |
requirements: ["Slackware >= 12.2"] |
| 51 |
""" |
| 52 |
|
| 53 |
EXAMPLES = r""" |
| 54 |
- name: Install package foo |
| 55 |
community.general.slackpkg: |
| 56 |
name: foo |
| 57 |
state: present |
| 58 |
|
| 59 |
- name: Remove packages foo and bar |
| 60 |
community.general.slackpkg: |
| 61 |
name: foo,bar |
| 62 |
state: absent |
| 63 |
|
| 64 |
- name: Make sure that it is the most updated package |
| 65 |
community.general.slackpkg: |
| 66 |
name: foo |
| 67 |
state: latest |
| 68 |
""" |
| 69 |
|
| 70 |
import os |
| 71 |
import platform |
| 72 |
import re |
| 73 |
|
| 74 |
from ansible.module_utils.basic import AnsibleModule |
| 75 |
|
| 76 |
|
| 77 |
def query_package(name): |
| 78 |
machine = platform.machine() |
| 79 |
# Exception for kernel-headers package on x86_64 |
| 80 |
if name == "kernel-headers" and machine == "x86_64": |
| 81 |
machine = "x86" |
| 82 |
pattern = re.compile(f"^{re.escape(name)}-[^-]+-({re.escape(machine)}|noarch|fw)-[^-]+$") |
| 83 |
packages = [f for f in os.listdir("/var/log/packages") if pattern.match(f)] |
| 84 |
|
| 85 |
return bool(packages) |
| 86 |
|
| 87 |
|
| 88 |
def remove_packages(module, slackpkg_path, packages): |
| 89 |
remove_c = 0 |
| 90 |
# Using a for loop in case of error, we can report the package that failed |
| 91 |
for package in packages: |
| 92 |
# Query the package first, to see if we even need to remove |
| 93 |
if not query_package(package): |
| 94 |
continue |
| 95 |
|
| 96 |
if not module.check_mode: |
| 97 |
rc, out, err = module.run_command([slackpkg_path, "-default_answer=y", "-batch=on", "remove", package]) |
| 98 |
|
| 99 |
if not module.check_mode and query_package(package): |
| 100 |
module.fail_json(msg=f"failed to remove {package}: {out}") |
| 101 |
|
| 102 |
remove_c += 1 |
| 103 |
|
| 104 |
if remove_c > 0: |
| 105 |
module.exit_json(changed=True, msg=f"removed {remove_c} package(s)") |
| 106 |
|
| 107 |
module.exit_json(changed=False, msg="package(s) already absent") |
| 108 |
|
| 109 |
|
| 110 |
def install_packages(module, slackpkg_path, packages): |
| 111 |
install_c = 0 |
| 112 |
|
| 113 |
for package in packages: |
| 114 |
if query_package(package): |
| 115 |
continue |
| 116 |
|
| 117 |
if not module.check_mode: |
| 118 |
rc, out, err = module.run_command([slackpkg_path, "-default_answer=y", "-batch=on", "install", package]) |
| 119 |
|
| 120 |
if not module.check_mode and not query_package(package): |
| 121 |
module.fail_json(msg=f"failed to install {package}: {out}", stderr=err) |
| 122 |
|
| 123 |
install_c += 1 |
| 124 |
|
| 125 |
if install_c > 0: |
| 126 |
module.exit_json(changed=True, msg=f"present {install_c} package(s)") |
| 127 |
|
| 128 |
module.exit_json(changed=False, msg="package(s) already present") |
| 129 |
|
| 130 |
|
| 131 |
def upgrade_packages(module, slackpkg_path, packages): |
| 132 |
install_c = 0 |
| 133 |
|
| 134 |
for package in packages: |
| 135 |
if not module.check_mode: |
| 136 |
rc, out, err = module.run_command([slackpkg_path, "-default_answer=y", "-batch=on", "upgrade", package]) |
| 137 |
|
| 138 |
if not module.check_mode and not query_package(package): |
| 139 |
module.fail_json(msg=f"failed to install {package}: {out}", stderr=err) |
| 140 |
|
| 141 |
install_c += 1 |
| 142 |
|
| 143 |
if install_c > 0: |
| 144 |
module.exit_json(changed=True, msg=f"present {install_c} package(s)") |
| 145 |
|
| 146 |
module.exit_json(changed=False, msg="package(s) already present") |
| 147 |
|
| 148 |
|
| 149 |
def update_cache(module, slackpkg_path): |
| 150 |
rc, out, err = module.run_command([slackpkg_path, "-batch=on", "update"]) |
| 151 |
if rc != 0: |
| 152 |
module.fail_json(msg="Could not update package cache") |
| 153 |
|
| 154 |
|
| 155 |
def main(): |
| 156 |
module = AnsibleModule( |
| 157 |
argument_spec=dict( |
| 158 |
state=dict(default="present", choices=["installed", "removed", "absent", "present", "latest"]), |
| 159 |
name=dict(aliases=["pkg"], required=True, type="list", elements="str"), |
| 160 |
update_cache=dict(default=False, type="bool"), |
| 161 |
), |
| 162 |
supports_check_mode=True, |
| 163 |
) |
| 164 |
|
| 165 |
slackpkg_path = module.get_bin_path("slackpkg", True) |
| 166 |
|
| 167 |
p = module.params |
| 168 |
|
| 169 |
pkgs = p["name"] |
| 170 |
|
| 171 |
if p["update_cache"]: |
| 172 |
update_cache(module, slackpkg_path) |
| 173 |
|
| 174 |
if p["state"] == "latest": |
| 175 |
upgrade_packages(module, slackpkg_path, pkgs) |
| 176 |
|
| 177 |
elif p["state"] in ["present", "installed"]: |
| 178 |
install_packages(module, slackpkg_path, pkgs) |
| 179 |
|
| 180 |
elif p["state"] in ["removed", "absent"]: |
| 181 |
remove_packages(module, slackpkg_path, pkgs) |
| 182 |
|
| 183 |
|
| 184 |
if __name__ == "__main__": |
| 185 |
main() |