| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2015, Kevin Brebanov <https://github.com/kbrebanov> |
| 4 |
# Based on pacman (Afterburn <https://github.com/afterburn>, Aaron Bull Schaefer <[email protected]>) |
| 5 |
# and apt (Matthew Williams <[email protected]>) modules. |
| 6 |
# |
| 7 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: apk |
| 14 |
short_description: Manages apk packages |
| 15 |
description: |
| 16 |
- Manages C(apk) packages for Alpine Linux. |
| 17 |
author: "Kevin Brebanov (@kbrebanov)" |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.general._attributes |
| 20 |
attributes: |
| 21 |
check_mode: |
| 22 |
support: full |
| 23 |
diff_mode: |
| 24 |
support: none |
| 25 |
options: |
| 26 |
available: |
| 27 |
description: |
| 28 |
- During upgrade, reset versioned world dependencies and change logic to prefer replacing or downgrading packages (instead |
| 29 |
of holding them) if the currently installed package is no longer available from any repository. |
| 30 |
type: bool |
| 31 |
default: false |
| 32 |
name: |
| 33 |
description: |
| 34 |
- A package name, like V(foo), or multiple packages, like V(foo,bar). |
| 35 |
- Do not include additional whitespace when specifying multiple packages as a string. Prefer YAML lists over comma-separating |
| 36 |
multiple package names. |
| 37 |
type: list |
| 38 |
elements: str |
| 39 |
no_cache: |
| 40 |
description: |
| 41 |
- Do not use any local cache path. |
| 42 |
type: bool |
| 43 |
default: false |
| 44 |
version_added: 1.0.0 |
| 45 |
repository: |
| 46 |
description: |
| 47 |
- A package repository or multiple repositories. Unlike with the underlying apk command, this list overrides the system |
| 48 |
repositories rather than supplement them. |
| 49 |
type: list |
| 50 |
elements: str |
| 51 |
state: |
| 52 |
description: |
| 53 |
- Indicates the desired package(s) state. |
| 54 |
- V(present) ensures the package(s) is/are present. V(installed) can be used as an alias. |
| 55 |
- V(absent) ensures the package(s) is/are absent. V(removed) can be used as an alias. |
| 56 |
- V(latest) ensures the package(s) is/are present and the latest version(s). |
| 57 |
default: present |
| 58 |
choices: ["present", "absent", "latest", "installed", "removed"] |
| 59 |
type: str |
| 60 |
update_cache: |
| 61 |
description: |
| 62 |
- Update repository indexes. Can be run with other steps or on its own. |
| 63 |
type: bool |
| 64 |
default: false |
| 65 |
upgrade: |
| 66 |
description: |
| 67 |
- Upgrade all installed packages to their latest version. |
| 68 |
type: bool |
| 69 |
default: false |
| 70 |
world: |
| 71 |
description: |
| 72 |
- Use a custom world file when checking for explicitly installed packages. The file is used only when a value is provided |
| 73 |
for O(name), and O(state) is set to V(present) or V(latest). |
| 74 |
type: str |
| 75 |
default: /etc/apk/world |
| 76 |
version_added: 5.4.0 |
| 77 |
notes: |
| 78 |
- O(name) and O(upgrade) are mutually exclusive. |
| 79 |
- When used with a C(loop:) each package is processed individually, it is much more efficient to pass the list directly |
| 80 |
to the O(name) option. |
| 81 |
- This module expects C(apk) to run in non-interactive mode. On Chimera Linux, C(apk) runs interactively by default, |
| 82 |
which will cause this module to hang. Interactive mode can be configured in C(/etc/apk/config). Even though not |
| 83 |
officially supported, other systems using C(apk) should be able to use this module, as long as C(apk) is running |
| 84 |
in non-interactive mode by default. |
| 85 |
""" |
| 86 |
|
| 87 |
EXAMPLES = r""" |
| 88 |
- name: Update repositories and install foo package |
| 89 |
community.general.apk: |
| 90 |
name: foo |
| 91 |
update_cache: true |
| 92 |
|
| 93 |
- name: Update repositories and install foo and bar packages |
| 94 |
community.general.apk: |
| 95 |
name: foo,bar |
| 96 |
update_cache: true |
| 97 |
|
| 98 |
- name: Remove foo package |
| 99 |
community.general.apk: |
| 100 |
name: foo |
| 101 |
state: absent |
| 102 |
|
| 103 |
- name: Remove foo and bar packages |
| 104 |
community.general.apk: |
| 105 |
name: foo,bar |
| 106 |
state: absent |
| 107 |
|
| 108 |
- name: Install the package foo |
| 109 |
community.general.apk: |
| 110 |
name: foo |
| 111 |
state: present |
| 112 |
|
| 113 |
- name: Install the packages foo and bar |
| 114 |
community.general.apk: |
| 115 |
name: foo,bar |
| 116 |
state: present |
| 117 |
|
| 118 |
- name: Update repositories and update package foo to latest version |
| 119 |
community.general.apk: |
| 120 |
name: foo |
| 121 |
state: latest |
| 122 |
update_cache: true |
| 123 |
|
| 124 |
- name: Update repositories and update packages foo and bar to latest versions |
| 125 |
community.general.apk: |
| 126 |
name: foo,bar |
| 127 |
state: latest |
| 128 |
update_cache: true |
| 129 |
|
| 130 |
- name: Update all installed packages to the latest versions |
| 131 |
community.general.apk: |
| 132 |
upgrade: true |
| 133 |
|
| 134 |
- name: Upgrade / replace / downgrade / uninstall all installed packages to the latest versions available |
| 135 |
community.general.apk: |
| 136 |
available: true |
| 137 |
upgrade: true |
| 138 |
|
| 139 |
- name: Update repositories as a separate step |
| 140 |
community.general.apk: |
| 141 |
update_cache: true |
| 142 |
|
| 143 |
- name: Install package from a specific repository |
| 144 |
community.general.apk: |
| 145 |
name: foo |
| 146 |
state: latest |
| 147 |
update_cache: true |
| 148 |
repository: http://dl-3.alpinelinux.org/alpine/edge/main |
| 149 |
|
| 150 |
- name: Install package without using cache |
| 151 |
community.general.apk: |
| 152 |
name: foo |
| 153 |
state: latest |
| 154 |
no_cache: true |
| 155 |
|
| 156 |
- name: Install package checking a custom world |
| 157 |
community.general.apk: |
| 158 |
name: foo |
| 159 |
state: latest |
| 160 |
world: /etc/apk/world.custom |
| 161 |
""" |
| 162 |
|
| 163 |
RETURN = r""" |
| 164 |
packages: |
| 165 |
description: A list of packages that have been changed. |
| 166 |
returned: when packages have changed |
| 167 |
type: list |
| 168 |
sample: ["package", "other-package"] |
| 169 |
""" |
| 170 |
|
| 171 |
import re |
| 172 |
|
| 173 |
# Import module snippets. |
| 174 |
from ansible.module_utils.basic import AnsibleModule |
| 175 |
|
| 176 |
|
| 177 |
def parse_for_packages(stdout): |
| 178 |
packages = [] |
| 179 |
data = stdout.split("\n") |
| 180 |
regex = re.compile(r"^\(\s*\d+/\d+\)\s+\S+\s+(\S+)") |
| 181 |
for l in data: |
| 182 |
p = regex.search(l) |
| 183 |
if p: |
| 184 |
packages.append(p.group(1)) |
| 185 |
return packages |
| 186 |
|
| 187 |
|
| 188 |
def update_package_db(module, exit): |
| 189 |
cmd = APK_PATH + ["update"] |
| 190 |
rc, stdout, stderr = module.run_command(cmd, check_rc=False) |
| 191 |
if rc != 0: |
| 192 |
module.fail_json(msg="could not update package db", stdout=stdout, stderr=stderr) |
| 193 |
elif exit: |
| 194 |
module.exit_json(changed=True, msg="updated repository indexes", stdout=stdout, stderr=stderr) |
| 195 |
else: |
| 196 |
return True |
| 197 |
|
| 198 |
|
| 199 |
def query_toplevel(module, name, world): |
| 200 |
# world contains a list of top-level packages separated by ' ' or \n |
| 201 |
# packages may contain repository (@) or version (=<>~) separator characters or start with negation ! |
| 202 |
regex = re.compile(rf"^{re.escape(name)}([@=<>~].+)?$") |
| 203 |
with open(world) as f: |
| 204 |
content = f.read().split() |
| 205 |
for p in content: |
| 206 |
if regex.search(p): |
| 207 |
return True |
| 208 |
return False |
| 209 |
|
| 210 |
|
| 211 |
def query_package(module, name): |
| 212 |
cmd = APK_PATH + ["-v", "info", "--installed", name] |
| 213 |
rc, stdout, stderr = module.run_command(cmd, check_rc=False) |
| 214 |
return rc == 0 |
| 215 |
|
| 216 |
|
| 217 |
def query_latest(module, name): |
| 218 |
cmd = APK_PATH + ["version", name] |
| 219 |
rc, stdout, stderr = module.run_command(cmd, check_rc=False) |
| 220 |
search_pattern = rf"({re.escape(name)})-[\d\.\w]+-[\d\w]+\s+(.)\s+[\d\.\w]+-[\d\w]+\s+" |
| 221 |
match = re.search(search_pattern, stdout) |
| 222 |
return not (match and match.group(2) == "<") |
| 223 |
|
| 224 |
|
| 225 |
def query_virtual(module, name): |
| 226 |
cmd = APK_PATH + ["-v", "info", "--description", name] |
| 227 |
rc, stdout, stderr = module.run_command(cmd, check_rc=False) |
| 228 |
search_pattern = rf"^{re.escape(name)}: virtual meta package" |
| 229 |
return bool(re.search(search_pattern, stdout)) |
| 230 |
|
| 231 |
|
| 232 |
def get_dependencies(module, name): |
| 233 |
cmd = APK_PATH + ["-v", "info", "--depends", name] |
| 234 |
rc, stdout, stderr = module.run_command(cmd, check_rc=False) |
| 235 |
dependencies = stdout.split() |
| 236 |
if len(dependencies) > 1: |
| 237 |
return dependencies[1:] |
| 238 |
else: |
| 239 |
return [] |
| 240 |
|
| 241 |
|
| 242 |
def upgrade_packages(module, available): |
| 243 |
if module.check_mode: |
| 244 |
cmd = APK_PATH + ["upgrade", "--simulate"] |
| 245 |
else: |
| 246 |
cmd = APK_PATH + ["upgrade"] |
| 247 |
if available: |
| 248 |
cmd.append("--available") |
| 249 |
rc, stdout, stderr = module.run_command(cmd, check_rc=False) |
| 250 |
packagelist = parse_for_packages(stdout) |
| 251 |
if rc != 0: |
| 252 |
module.fail_json(msg="failed to upgrade packages", stdout=stdout, stderr=stderr, packages=packagelist) |
| 253 |
if re.search(r"^OK", stdout): |
| 254 |
module.exit_json( |
| 255 |
changed=False, msg="packages already upgraded", stdout=stdout, stderr=stderr, packages=packagelist |
| 256 |
) |
| 257 |
module.exit_json(changed=True, msg="upgraded packages", stdout=stdout, stderr=stderr, packages=packagelist) |
| 258 |
|
| 259 |
|
| 260 |
def install_packages(module, names, state, world): |
| 261 |
upgrade = False |
| 262 |
to_install = [] |
| 263 |
to_upgrade = [] |
| 264 |
for name in names: |
| 265 |
# Check if virtual package |
| 266 |
if query_virtual(module, name): |
| 267 |
# Get virtual package dependencies |
| 268 |
dependencies = get_dependencies(module, name) |
| 269 |
for dependency in dependencies: |
| 270 |
if state == "latest" and not query_latest(module, dependency): |
| 271 |
to_upgrade.append(dependency) |
| 272 |
else: |
| 273 |
if not query_toplevel(module, name, world): |
| 274 |
to_install.append(name) |
| 275 |
elif state == "latest" and not query_latest(module, name): |
| 276 |
to_upgrade.append(name) |
| 277 |
if to_upgrade: |
| 278 |
upgrade = True |
| 279 |
if not to_install and not upgrade: |
| 280 |
module.exit_json(changed=False, msg="package(s) already installed") |
| 281 |
packages = to_install + to_upgrade |
| 282 |
if upgrade: |
| 283 |
if module.check_mode: |
| 284 |
cmd = APK_PATH + ["add", "--upgrade", "--simulate"] + packages |
| 285 |
else: |
| 286 |
cmd = APK_PATH + ["add", "--upgrade"] + packages |
| 287 |
else: |
| 288 |
if module.check_mode: |
| 289 |
cmd = APK_PATH + ["add", "--simulate"] + packages |
| 290 |
else: |
| 291 |
cmd = APK_PATH + ["add"] + packages |
| 292 |
rc, stdout, stderr = module.run_command(cmd, check_rc=False) |
| 293 |
packagelist = parse_for_packages(stdout) |
| 294 |
if rc != 0: |
| 295 |
module.fail_json(msg=f"failed to install {packages}", stdout=stdout, stderr=stderr, packages=packagelist) |
| 296 |
module.exit_json( |
| 297 |
changed=True, msg=f"installed {packages} package(s)", stdout=stdout, stderr=stderr, packages=packagelist |
| 298 |
) |
| 299 |
|
| 300 |
|
| 301 |
def remove_packages(module, names): |
| 302 |
installed = [] |
| 303 |
for name in names: |
| 304 |
if query_package(module, name): |
| 305 |
installed.append(name) |
| 306 |
if not installed: |
| 307 |
module.exit_json(changed=False, msg="package(s) already removed") |
| 308 |
names = installed |
| 309 |
if module.check_mode: |
| 310 |
cmd = APK_PATH + ["del", "--purge", "--simulate"] + names |
| 311 |
else: |
| 312 |
cmd = APK_PATH + ["del", "--purge"] + names |
| 313 |
rc, stdout, stderr = module.run_command(cmd, check_rc=False) |
| 314 |
packagelist = parse_for_packages(stdout) |
| 315 |
# Check to see if packages are still present because of dependencies |
| 316 |
for name in installed: |
| 317 |
if query_package(module, name): |
| 318 |
rc = 1 |
| 319 |
break |
| 320 |
if rc != 0: |
| 321 |
module.fail_json(msg=f"failed to remove {names} package(s)", stdout=stdout, stderr=stderr, packages=packagelist) |
| 322 |
module.exit_json( |
| 323 |
changed=True, msg=f"removed {names} package(s)", stdout=stdout, stderr=stderr, packages=packagelist |
| 324 |
) |
| 325 |
|
| 326 |
|
| 327 |
# ========================================== |
| 328 |
# Main control flow. |
| 329 |
|
| 330 |
|
| 331 |
def main(): |
| 332 |
module = AnsibleModule( |
| 333 |
argument_spec=dict( |
| 334 |
state=dict(default="present", choices=["present", "installed", "absent", "removed", "latest"]), |
| 335 |
name=dict(type="list", elements="str"), |
| 336 |
no_cache=dict(default=False, type="bool"), |
| 337 |
repository=dict(type="list", elements="str"), |
| 338 |
update_cache=dict(default=False, type="bool"), |
| 339 |
upgrade=dict(default=False, type="bool"), |
| 340 |
available=dict(default=False, type="bool"), |
| 341 |
world=dict(default="/etc/apk/world", type="str"), |
| 342 |
), |
| 343 |
required_one_of=[["name", "update_cache", "upgrade"]], |
| 344 |
mutually_exclusive=[["name", "upgrade"]], |
| 345 |
supports_check_mode=True, |
| 346 |
) |
| 347 |
|
| 348 |
# Set LANG env since we parse stdout |
| 349 |
module.run_command_environ_update = dict(LANGUAGE="C", LC_ALL="C") |
| 350 |
|
| 351 |
global APK_PATH |
| 352 |
APK_PATH = [module.get_bin_path("apk", required=True)] |
| 353 |
|
| 354 |
p = module.params |
| 355 |
|
| 356 |
if p["name"] and any(not name.strip() for name in p["name"]): |
| 357 |
module.fail_json(msg="Package name(s) cannot be empty or whitespace-only") |
| 358 |
|
| 359 |
if p["no_cache"]: |
| 360 |
APK_PATH.append("--no-cache") |
| 361 |
|
| 362 |
# add repositories to the APK_PATH |
| 363 |
if p["repository"]: |
| 364 |
for r in p["repository"]: |
| 365 |
APK_PATH.extend(["--repository", r, "--repositories-file", "/dev/null"]) |
| 366 |
|
| 367 |
# normalize the state parameter |
| 368 |
if p["state"] in ["present", "installed"]: |
| 369 |
p["state"] = "present" |
| 370 |
if p["state"] in ["absent", "removed"]: |
| 371 |
p["state"] = "absent" |
| 372 |
|
| 373 |
if p["update_cache"]: |
| 374 |
update_package_db(module, not p["name"] and not p["upgrade"]) |
| 375 |
|
| 376 |
if p["upgrade"]: |
| 377 |
upgrade_packages(module, p["available"]) |
| 378 |
|
| 379 |
if p["state"] in ["present", "latest"]: |
| 380 |
install_packages(module, p["name"], p["state"], p["world"]) |
| 381 |
elif p["state"] == "absent": |
| 382 |
remove_packages(module, p["name"]) |
| 383 |
|
| 384 |
|
| 385 |
if __name__ == "__main__": |
| 386 |
main() |