| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2013 Shaun Zinck <shaun.zinck at gmail.com> |
| 4 |
# Copyright (c) 2015 Lawrence Leonard Gilbert <[email protected]> |
| 5 |
# Copyright (c) 2016 Jasper Lievisse Adriaanse <j at jasper.la> |
| 6 |
# |
| 7 |
# Written by Shaun Zinck |
| 8 |
# Based on pacman module written by Afterburn <http://github.com/afterburn> |
| 9 |
# that was based on apt module written by Matthew Williams <[email protected]> |
| 10 |
# |
| 11 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 12 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 13 |
|
| 14 |
from __future__ import annotations |
| 15 |
|
| 16 |
DOCUMENTATION = r""" |
| 17 |
module: pkgin |
| 18 |
short_description: Package manager for SmartOS, NetBSD, et al |
| 19 |
description: |
| 20 |
- 'The standard package manager for SmartOS, but also usable on NetBSD or any OS that uses C(pkgsrc). (Home: U(http://pkgin.net/)).' |
| 21 |
author: |
| 22 |
- "Larry Gilbert (@L2G)" |
| 23 |
- "Shaun Zinck (@szinck)" |
| 24 |
- "Jasper Lievisse Adriaanse (@jasperla)" |
| 25 |
notes: |
| 26 |
- 'Known bug with pkgin < 0.8.0: if a package is removed and another package depends on it, the other package is silently |
| 27 |
removed as well.' |
| 28 |
extends_documentation_fragment: |
| 29 |
- community.general._attributes |
| 30 |
attributes: |
| 31 |
check_mode: |
| 32 |
support: full |
| 33 |
diff_mode: |
| 34 |
support: none |
| 35 |
options: |
| 36 |
name: |
| 37 |
description: |
| 38 |
- Name of package to install/remove; |
| 39 |
- Multiple names may be given, separated by commas. |
| 40 |
aliases: [pkg] |
| 41 |
type: list |
| 42 |
elements: str |
| 43 |
state: |
| 44 |
description: |
| 45 |
- Intended state of the package. |
| 46 |
choices: ['present', 'absent'] |
| 47 |
default: present |
| 48 |
type: str |
| 49 |
update_cache: |
| 50 |
description: |
| 51 |
- Update repository database. Can be run with other steps or on its own. |
| 52 |
type: bool |
| 53 |
default: false |
| 54 |
upgrade: |
| 55 |
description: |
| 56 |
- Upgrade main packages to their newer versions. |
| 57 |
type: bool |
| 58 |
default: false |
| 59 |
full_upgrade: |
| 60 |
description: |
| 61 |
- Upgrade all packages to their newer versions. |
| 62 |
type: bool |
| 63 |
default: false |
| 64 |
clean: |
| 65 |
description: |
| 66 |
- Clean packages cache. |
| 67 |
type: bool |
| 68 |
default: false |
| 69 |
force: |
| 70 |
description: |
| 71 |
- Force package reinstall. |
| 72 |
type: bool |
| 73 |
default: false |
| 74 |
""" |
| 75 |
|
| 76 |
EXAMPLES = r""" |
| 77 |
- name: Install package foo |
| 78 |
community.general.pkgin: |
| 79 |
name: foo |
| 80 |
state: present |
| 81 |
|
| 82 |
- name: Install specific version of foo package |
| 83 |
community.general.pkgin: |
| 84 |
name: foo-2.0.1 |
| 85 |
state: present |
| 86 |
|
| 87 |
- name: Update cache and install foo package |
| 88 |
community.general.pkgin: |
| 89 |
name: foo |
| 90 |
update_cache: true |
| 91 |
|
| 92 |
- name: Remove package foo |
| 93 |
community.general.pkgin: |
| 94 |
name: foo |
| 95 |
state: absent |
| 96 |
|
| 97 |
- name: Remove packages foo and bar |
| 98 |
community.general.pkgin: |
| 99 |
name: foo,bar |
| 100 |
state: absent |
| 101 |
|
| 102 |
- name: Update repositories as a separate step |
| 103 |
community.general.pkgin: |
| 104 |
update_cache: true |
| 105 |
|
| 106 |
- name: Upgrade main packages (equivalent to pkgin upgrade) |
| 107 |
community.general.pkgin: |
| 108 |
upgrade: true |
| 109 |
|
| 110 |
- name: Upgrade all packages (equivalent to pkgin full-upgrade) |
| 111 |
community.general.pkgin: |
| 112 |
full_upgrade: true |
| 113 |
|
| 114 |
- name: Force-upgrade all packages (equivalent to pkgin -F full-upgrade) |
| 115 |
community.general.pkgin: |
| 116 |
full_upgrade: true |
| 117 |
force: true |
| 118 |
|
| 119 |
- name: Clean packages cache (equivalent to pkgin clean) |
| 120 |
community.general.pkgin: |
| 121 |
clean: true |
| 122 |
""" |
| 123 |
|
| 124 |
|
| 125 |
import re |
| 126 |
|
| 127 |
from ansible.module_utils.basic import AnsibleModule |
| 128 |
|
| 129 |
|
| 130 |
class PackageState: |
| 131 |
PRESENT = 1 |
| 132 |
NOT_INSTALLED = 2 |
| 133 |
OUTDATED = 4 |
| 134 |
NOT_FOUND = 8 |
| 135 |
|
| 136 |
|
| 137 |
def query_package(module, name): |
| 138 |
"""Search for the package by name and return state of the package.""" |
| 139 |
|
| 140 |
# test whether '-p' (parsable) flag is supported. |
| 141 |
rc, out, err = module.run_command([PKGIN_PATH, "-p", "-v"]) |
| 142 |
|
| 143 |
if rc == 0: |
| 144 |
pflag = ["-p"] |
| 145 |
splitchar = ";" |
| 146 |
else: |
| 147 |
pflag = [] |
| 148 |
splitchar = " " |
| 149 |
|
| 150 |
# Use "pkgin search" to find the package. The regular expression will |
| 151 |
# only match on the complete name. |
| 152 |
rc, out, err = module.run_command([PKGIN_PATH] + pflag + ["search", f"^{name}$"]) |
| 153 |
|
| 154 |
# rc will not be 0 unless the search was a success |
| 155 |
if rc == 0: |
| 156 |
# Search results may contain more than one line (e.g., 'emacs'), so iterate |
| 157 |
# through each line to see if we have a match. |
| 158 |
packages = out.split("\n") |
| 159 |
|
| 160 |
for package in packages: |
| 161 |
# Break up line at spaces. The first part will be the package with its |
| 162 |
# version (e.g. 'gcc47-libs-4.7.2nb4'), and the second will be the state |
| 163 |
# of the package: |
| 164 |
# '' - not installed |
| 165 |
# '<' - installed but out of date |
| 166 |
# '=' - installed and up to date |
| 167 |
# '>' - installed but newer than the repository version |
| 168 |
|
| 169 |
if ( |
| 170 |
package |
| 171 |
in ("reading local summary...", "processing local summary...", "downloading pkg_summary.xz done.") |
| 172 |
) or (package.startswith("processing remote summary (")): |
| 173 |
continue |
| 174 |
|
| 175 |
pkgname_with_version, raw_state = package.split(splitchar)[0:2] |
| 176 |
|
| 177 |
# Search for package, stripping version |
| 178 |
# (results in sth like 'gcc47-libs' or 'emacs24-nox11') |
| 179 |
pkg_search_obj = re.search(r"^(.*?)\-[0-9][0-9.]*(nb[0-9]+)*", pkgname_with_version, re.M) |
| 180 |
|
| 181 |
# Do not proceed unless we have a match |
| 182 |
if not pkg_search_obj: |
| 183 |
continue |
| 184 |
|
| 185 |
# Grab matched string |
| 186 |
pkgname_without_version = pkg_search_obj.group(1) |
| 187 |
|
| 188 |
if name not in (pkgname_with_version, pkgname_without_version): |
| 189 |
continue |
| 190 |
|
| 191 |
# The package was found; now return its state |
| 192 |
if raw_state == "<": |
| 193 |
return PackageState.OUTDATED |
| 194 |
elif raw_state == "=" or raw_state == ">": |
| 195 |
return PackageState.PRESENT |
| 196 |
else: |
| 197 |
# Package found but not installed |
| 198 |
return PackageState.NOT_INSTALLED |
| 199 |
# no fall-through |
| 200 |
|
| 201 |
# No packages were matched |
| 202 |
return PackageState.NOT_FOUND |
| 203 |
|
| 204 |
# Search failed |
| 205 |
return PackageState.NOT_FOUND |
| 206 |
|
| 207 |
|
| 208 |
def format_action_message(module, action, count): |
| 209 |
vars = {"actioned": action, "count": count} |
| 210 |
|
| 211 |
if module.check_mode: |
| 212 |
message = f"would have {vars['actioned']} {vars['count']} package" |
| 213 |
else: |
| 214 |
message = f"{vars['actioned']} {vars['count']} package" |
| 215 |
|
| 216 |
if count == 1: |
| 217 |
return message |
| 218 |
else: |
| 219 |
return f"{message}s" |
| 220 |
|
| 221 |
|
| 222 |
def format_pkgin_command(module, command, package=None): |
| 223 |
# Not all commands take a package argument, so cover this up by passing |
| 224 |
# an empty string. Some commands (e.g. 'update') will ignore extra |
| 225 |
# arguments, however this behaviour cannot be relied on for others. |
| 226 |
if package is None: |
| 227 |
packages = [] |
| 228 |
else: |
| 229 |
packages = [package] |
| 230 |
|
| 231 |
if module.params["force"]: |
| 232 |
force = ["-F"] |
| 233 |
else: |
| 234 |
force = [] |
| 235 |
|
| 236 |
if module.check_mode: |
| 237 |
return [PKGIN_PATH, "-n", command] + packages |
| 238 |
else: |
| 239 |
return [PKGIN_PATH, "-y"] + force + [command] + packages |
| 240 |
|
| 241 |
|
| 242 |
def remove_packages(module, packages): |
| 243 |
remove_c = 0 |
| 244 |
|
| 245 |
# Using a for loop in case of error, we can report the package that failed |
| 246 |
for package in packages: |
| 247 |
# Query the package first, to see if we even need to remove |
| 248 |
if query_package(module, package) in [PackageState.NOT_INSTALLED, PackageState.NOT_FOUND]: |
| 249 |
continue |
| 250 |
|
| 251 |
rc, out, err = module.run_command(format_pkgin_command(module, "remove", package)) |
| 252 |
|
| 253 |
if not module.check_mode and query_package(module, package) in [PackageState.PRESENT, PackageState.OUTDATED]: |
| 254 |
module.fail_json(msg=f"failed to remove {package}: {out}", stdout=out, stderr=err) |
| 255 |
|
| 256 |
remove_c += 1 |
| 257 |
|
| 258 |
if remove_c > 0: |
| 259 |
module.exit_json(changed=True, msg=format_action_message(module, "removed", remove_c)) |
| 260 |
|
| 261 |
module.exit_json(changed=False, msg="package(s) already absent") |
| 262 |
|
| 263 |
|
| 264 |
def install_packages(module, packages): |
| 265 |
install_c = 0 |
| 266 |
|
| 267 |
for package in packages: |
| 268 |
query_result = query_package(module, package) |
| 269 |
if query_result in [PackageState.PRESENT, PackageState.OUTDATED]: |
| 270 |
continue |
| 271 |
elif query_result is PackageState.NOT_FOUND: |
| 272 |
module.fail_json(msg=f"failed to find package {package} for installation") |
| 273 |
|
| 274 |
rc, out, err = module.run_command(format_pkgin_command(module, "install", package)) |
| 275 |
|
| 276 |
if not module.check_mode and query_package(module, package) not in [ |
| 277 |
PackageState.PRESENT, |
| 278 |
PackageState.OUTDATED, |
| 279 |
]: |
| 280 |
module.fail_json(msg=f"failed to install {package}: {out}", stdout=out, stderr=err) |
| 281 |
|
| 282 |
install_c += 1 |
| 283 |
|
| 284 |
if install_c > 0: |
| 285 |
module.exit_json( |
| 286 |
changed=True, msg=format_action_message(module, "installed", install_c), stdout=out, stderr=err |
| 287 |
) |
| 288 |
|
| 289 |
module.exit_json(changed=False, msg="package(s) already present") |
| 290 |
|
| 291 |
|
| 292 |
def update_package_db(module): |
| 293 |
rc, out, err = module.run_command(format_pkgin_command(module, "update")) |
| 294 |
|
| 295 |
if rc == 0: |
| 296 |
if re.search("database for.*is up-to-date\n$", out): |
| 297 |
return False, "database is up-to-date" |
| 298 |
else: |
| 299 |
return True, "updated repository database" |
| 300 |
else: |
| 301 |
module.fail_json(msg="could not update package db", stdout=out, stderr=err) |
| 302 |
|
| 303 |
|
| 304 |
def do_upgrade_packages(module, full=False): |
| 305 |
if full: |
| 306 |
cmd = "full-upgrade" |
| 307 |
else: |
| 308 |
cmd = "upgrade" |
| 309 |
|
| 310 |
rc, out, err = module.run_command(format_pkgin_command(module, cmd)) |
| 311 |
|
| 312 |
if rc == 0: |
| 313 |
if re.search("^(.*\n|)nothing to do.\n$", out): |
| 314 |
module.exit_json(changed=False, msg="nothing left to upgrade") |
| 315 |
else: |
| 316 |
module.fail_json(msg=f"could not {cmd} packages", stdout=out, stderr=err) |
| 317 |
|
| 318 |
|
| 319 |
def upgrade_packages(module): |
| 320 |
do_upgrade_packages(module) |
| 321 |
|
| 322 |
|
| 323 |
def full_upgrade_packages(module): |
| 324 |
do_upgrade_packages(module, True) |
| 325 |
|
| 326 |
|
| 327 |
def clean_cache(module): |
| 328 |
rc, out, err = module.run_command(format_pkgin_command(module, "clean")) |
| 329 |
|
| 330 |
if rc == 0: |
| 331 |
# There's no indication if 'clean' actually removed anything, |
| 332 |
# so assume it did. |
| 333 |
module.exit_json(changed=True, msg="cleaned caches") |
| 334 |
else: |
| 335 |
module.fail_json(msg="could not clean package cache", stdout=out, stderr=err) |
| 336 |
|
| 337 |
|
| 338 |
def main(): |
| 339 |
module = AnsibleModule( |
| 340 |
argument_spec=dict( |
| 341 |
state=dict(default="present", choices=["present", "absent"]), |
| 342 |
name=dict(aliases=["pkg"], type="list", elements="str"), |
| 343 |
update_cache=dict(default=False, type="bool"), |
| 344 |
upgrade=dict(default=False, type="bool"), |
| 345 |
full_upgrade=dict(default=False, type="bool"), |
| 346 |
clean=dict(default=False, type="bool"), |
| 347 |
force=dict(default=False, type="bool"), |
| 348 |
), |
| 349 |
required_one_of=[["name", "update_cache", "upgrade", "full_upgrade", "clean"]], |
| 350 |
supports_check_mode=True, |
| 351 |
) |
| 352 |
|
| 353 |
global PKGIN_PATH |
| 354 |
PKGIN_PATH = module.get_bin_path("pkgin", True, ["/opt/local/bin"]) |
| 355 |
|
| 356 |
module.run_command_environ_update = dict(LANGUAGE="C", LC_ALL="C") |
| 357 |
|
| 358 |
p = module.params |
| 359 |
|
| 360 |
if p["update_cache"]: |
| 361 |
c, msg = update_package_db(module) |
| 362 |
if not (p["name"] or p["upgrade"] or p["full_upgrade"]): |
| 363 |
module.exit_json(changed=c, msg=msg) |
| 364 |
|
| 365 |
if p["upgrade"]: |
| 366 |
upgrade_packages(module) |
| 367 |
if not p["name"]: |
| 368 |
module.exit_json(changed=True, msg="upgraded packages") |
| 369 |
|
| 370 |
if p["full_upgrade"]: |
| 371 |
full_upgrade_packages(module) |
| 372 |
if not p["name"]: |
| 373 |
module.exit_json(changed=True, msg="upgraded all packages") |
| 374 |
|
| 375 |
if p["clean"]: |
| 376 |
clean_cache(module) |
| 377 |
if not p["name"]: |
| 378 |
module.exit_json(changed=True, msg="cleaned caches") |
| 379 |
|
| 380 |
pkgs = p["name"] |
| 381 |
|
| 382 |
if p["state"] == "present": |
| 383 |
install_packages(module, pkgs) |
| 384 |
|
| 385 |
elif p["state"] == "absent": |
| 386 |
remove_packages(module, pkgs) |
| 387 |
|
| 388 |
|
| 389 |
if __name__ == "__main__": |
| 390 |
main() |