| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2013, Alexander Winkler <mail () winkler-alexander.de> |
| 4 |
# based on svr4pkg by |
| 5 |
# Boyd Adamson <boyd () boydadamson.com> (2012) |
| 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: pkgutil |
| 14 |
short_description: OpenCSW package management on Solaris |
| 15 |
description: |
| 16 |
- This module installs, updates and removes packages from the OpenCSW project for Solaris. |
| 17 |
- Unlike the M(community.general.svr4pkg) module, it resolves and downloads dependencies. |
| 18 |
- See U(https://www.opencsw.org/) for more information about the project. |
| 19 |
author: |
| 20 |
- Alexander Winkler (@dermute) |
| 21 |
- David Ponessa (@scathatheworm) |
| 22 |
extends_documentation_fragment: |
| 23 |
- community.general._attributes |
| 24 |
attributes: |
| 25 |
check_mode: |
| 26 |
support: full |
| 27 |
details: |
| 28 |
- In order to check the availability of packages, the catalog cache under C(/var/opt/csw/pkgutil) may be refreshed even |
| 29 |
in check mode. |
| 30 |
diff_mode: |
| 31 |
support: none |
| 32 |
options: |
| 33 |
name: |
| 34 |
description: |
| 35 |
- The name of the package. |
| 36 |
- When using O(state=latest), this can be V('*'), which updates all installed packages managed by pkgutil. |
| 37 |
type: list |
| 38 |
required: true |
| 39 |
elements: str |
| 40 |
aliases: [pkg] |
| 41 |
site: |
| 42 |
description: |
| 43 |
- The repository path to install the package from. |
| 44 |
- Its global definition is in C(/etc/opt/csw/pkgutil.conf). |
| 45 |
type: str |
| 46 |
state: |
| 47 |
description: |
| 48 |
- Whether to install (V(present)/V(installed)), or remove (V(absent)/V(removed)) packages. |
| 49 |
- The upgrade (V(latest)) operation updates/installs the packages to the latest version available. |
| 50 |
type: str |
| 51 |
required: true |
| 52 |
choices: [absent, installed, latest, present, removed] |
| 53 |
update_catalog: |
| 54 |
description: |
| 55 |
- If you always want to refresh your catalog from the mirror, even when it is not stale, set this to V(true). |
| 56 |
type: bool |
| 57 |
default: false |
| 58 |
force: |
| 59 |
description: |
| 60 |
- To allow the update process to downgrade packages to match what is present in the repository, set this to V(true). |
| 61 |
- This is useful for rolling back to stable from testing, or similar operations. |
| 62 |
type: bool |
| 63 |
default: false |
| 64 |
version_added: 1.2.0 |
| 65 |
""" |
| 66 |
|
| 67 |
EXAMPLES = r""" |
| 68 |
- name: Install a package |
| 69 |
community.general.pkgutil: |
| 70 |
name: CSWcommon |
| 71 |
state: present |
| 72 |
|
| 73 |
- name: Install a package from a specific repository |
| 74 |
community.general.pkgutil: |
| 75 |
name: CSWnrpe |
| 76 |
site: ftp://myinternal.repo/opencsw/kiel |
| 77 |
state: latest |
| 78 |
|
| 79 |
- name: Remove a package |
| 80 |
community.general.pkgutil: |
| 81 |
name: CSWtop |
| 82 |
state: absent |
| 83 |
|
| 84 |
- name: Install several packages |
| 85 |
community.general.pkgutil: |
| 86 |
name: |
| 87 |
- CSWsudo |
| 88 |
- CSWtop |
| 89 |
state: present |
| 90 |
|
| 91 |
- name: Update all packages |
| 92 |
community.general.pkgutil: |
| 93 |
name: '*' |
| 94 |
state: latest |
| 95 |
|
| 96 |
- name: Update all packages and force versions to match latest in catalog |
| 97 |
community.general.pkgutil: |
| 98 |
name: '*' |
| 99 |
state: latest |
| 100 |
force: true |
| 101 |
""" |
| 102 |
|
| 103 |
RETURN = r""" # """ |
| 104 |
|
| 105 |
from ansible.module_utils.basic import AnsibleModule |
| 106 |
|
| 107 |
|
| 108 |
def packages_not_installed(module, names): |
| 109 |
"""Check if each package is installed and return list of the ones absent""" |
| 110 |
pkgs = [] |
| 111 |
for pkg in names: |
| 112 |
rc, out, err = run_command(module, ["pkginfo", "-q", pkg]) |
| 113 |
if rc != 0: |
| 114 |
pkgs.append(pkg) |
| 115 |
return pkgs |
| 116 |
|
| 117 |
|
| 118 |
def packages_installed(module, names): |
| 119 |
"""Check if each package is installed and return list of the ones present""" |
| 120 |
pkgs = [] |
| 121 |
for pkg in names: |
| 122 |
if not pkg.startswith("CSW"): |
| 123 |
continue |
| 124 |
rc, out, err = run_command(module, ["pkginfo", "-q", pkg]) |
| 125 |
if rc == 0: |
| 126 |
pkgs.append(pkg) |
| 127 |
return pkgs |
| 128 |
|
| 129 |
|
| 130 |
def packages_not_latest(module, names, site, update_catalog): |
| 131 |
"""Check status of each package and return list of the ones with an upgrade available""" |
| 132 |
cmd = ["pkgutil"] |
| 133 |
if update_catalog: |
| 134 |
cmd.append("-U") |
| 135 |
cmd.append("-c") |
| 136 |
if site is not None: |
| 137 |
cmd.extend(["-t", site]) |
| 138 |
if names != ["*"]: |
| 139 |
cmd.extend(names) |
| 140 |
rc, out, err = run_command(module, cmd) |
| 141 |
|
| 142 |
# Find packages in the catalog which are not up to date |
| 143 |
packages = [] |
| 144 |
for line in out.split("\n")[1:-1]: |
| 145 |
if "catalog" not in line and "SAME" not in line: |
| 146 |
packages.append(line.split(" ")[0]) |
| 147 |
|
| 148 |
# Remove duplicates |
| 149 |
return list(set(packages)) |
| 150 |
|
| 151 |
|
| 152 |
def run_command(module, cmd, **kwargs): |
| 153 |
progname = cmd[0] |
| 154 |
cmd[0] = module.get_bin_path(progname, True, ["/opt/csw/bin"]) |
| 155 |
return module.run_command(cmd, **kwargs) |
| 156 |
|
| 157 |
|
| 158 |
def package_install(module, state, pkgs, site, update_catalog, force): |
| 159 |
cmd = ["pkgutil"] |
| 160 |
if module.check_mode: |
| 161 |
cmd.append("-n") |
| 162 |
cmd.append("-iy") |
| 163 |
if update_catalog: |
| 164 |
cmd.append("-U") |
| 165 |
if site is not None: |
| 166 |
cmd.extend(["-t", site]) |
| 167 |
if force: |
| 168 |
cmd.append("-f") |
| 169 |
cmd.extend(pkgs) |
| 170 |
return run_command(module, cmd) |
| 171 |
|
| 172 |
|
| 173 |
def package_upgrade(module, pkgs, site, update_catalog, force): |
| 174 |
cmd = ["pkgutil"] |
| 175 |
if module.check_mode: |
| 176 |
cmd.append("-n") |
| 177 |
cmd.append("-uy") |
| 178 |
if update_catalog: |
| 179 |
cmd.append("-U") |
| 180 |
if site is not None: |
| 181 |
cmd.extend(["-t", site]) |
| 182 |
if force: |
| 183 |
cmd.append("-f") |
| 184 |
cmd += pkgs |
| 185 |
return run_command(module, cmd) |
| 186 |
|
| 187 |
|
| 188 |
def package_uninstall(module, pkgs): |
| 189 |
cmd = ["pkgutil"] |
| 190 |
if module.check_mode: |
| 191 |
cmd.append("-n") |
| 192 |
cmd.append("-ry") |
| 193 |
cmd.extend(pkgs) |
| 194 |
return run_command(module, cmd) |
| 195 |
|
| 196 |
|
| 197 |
def main(): |
| 198 |
module = AnsibleModule( |
| 199 |
argument_spec=dict( |
| 200 |
name=dict(type="list", elements="str", required=True, aliases=["pkg"]), |
| 201 |
state=dict(type="str", required=True, choices=["absent", "installed", "latest", "present", "removed"]), |
| 202 |
site=dict(type="str"), |
| 203 |
update_catalog=dict(type="bool", default=False), |
| 204 |
force=dict(type="bool", default=False), |
| 205 |
), |
| 206 |
supports_check_mode=True, |
| 207 |
) |
| 208 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 209 |
name = module.params["name"] |
| 210 |
state = module.params["state"] |
| 211 |
site = module.params["site"] |
| 212 |
update_catalog = module.params["update_catalog"] |
| 213 |
force = module.params["force"] |
| 214 |
|
| 215 |
rc = None |
| 216 |
out = "" |
| 217 |
err = "" |
| 218 |
result = dict( |
| 219 |
name=name, |
| 220 |
state=state, |
| 221 |
) |
| 222 |
|
| 223 |
if state in ["installed", "present"]: |
| 224 |
# Fail with an explicit error when trying to "install" '*' |
| 225 |
if name == ["*"]: |
| 226 |
module.fail_json(msg="Can not use 'state: present' with name: '*'") |
| 227 |
|
| 228 |
# Build list of packages that are actually not installed from the ones requested |
| 229 |
pkgs = packages_not_installed(module, name) |
| 230 |
|
| 231 |
# If the package list is empty then all packages are already present |
| 232 |
if pkgs == []: |
| 233 |
module.exit_json(changed=False) |
| 234 |
|
| 235 |
(rc, out, err) = package_install(module, state, pkgs, site, update_catalog, force) |
| 236 |
if rc != 0: |
| 237 |
module.fail_json(msg=(err or out)) |
| 238 |
|
| 239 |
elif state in ["latest"]: |
| 240 |
# When using latest for * |
| 241 |
if name == ["*"]: |
| 242 |
# Check for packages that are actually outdated |
| 243 |
pkgs = packages_not_latest(module, name, site, update_catalog) |
| 244 |
|
| 245 |
# If the package list comes up empty, everything is already up to date |
| 246 |
if pkgs == []: |
| 247 |
module.exit_json(changed=False) |
| 248 |
|
| 249 |
# If there are packages to update, just empty the list and run the command without it |
| 250 |
# pkgutil logic is to update all when run without packages names |
| 251 |
pkgs = [] |
| 252 |
(rc, out, err) = package_upgrade(module, pkgs, site, update_catalog, force) |
| 253 |
if rc != 0: |
| 254 |
module.fail_json(msg=(err or out)) |
| 255 |
else: |
| 256 |
# Build list of packages that are either outdated or not installed |
| 257 |
pkgs = packages_not_installed(module, name) |
| 258 |
pkgs += packages_not_latest(module, name, site, update_catalog) |
| 259 |
|
| 260 |
# If the package list is empty that means all packages are installed and up to date |
| 261 |
if pkgs == []: |
| 262 |
module.exit_json(changed=False) |
| 263 |
|
| 264 |
(rc, out, err) = package_upgrade(module, pkgs, site, update_catalog, force) |
| 265 |
if rc != 0: |
| 266 |
module.fail_json(msg=(err or out)) |
| 267 |
|
| 268 |
elif state in ["absent", "removed"]: |
| 269 |
# Build list of packages requested for removal that are actually present |
| 270 |
pkgs = packages_installed(module, name) |
| 271 |
|
| 272 |
# If the list is empty, no packages need to be removed |
| 273 |
if pkgs == []: |
| 274 |
module.exit_json(changed=False) |
| 275 |
|
| 276 |
(rc, out, err) = package_uninstall(module, pkgs) |
| 277 |
if rc != 0: |
| 278 |
module.fail_json(msg=(err or out)) |
| 279 |
|
| 280 |
if rc is None: |
| 281 |
# pkgutil was not executed because the package was already present/absent/up to date |
| 282 |
result["changed"] = False |
| 283 |
elif rc == 0: |
| 284 |
result["changed"] = True |
| 285 |
else: |
| 286 |
result["changed"] = False |
| 287 |
result["failed"] = True |
| 288 |
|
| 289 |
if out: |
| 290 |
result["stdout"] = out |
| 291 |
if err: |
| 292 |
result["stderr"] = err |
| 293 |
|
| 294 |
module.exit_json(**result) |
| 295 |
|
| 296 |
|
| 297 |
if __name__ == "__main__": |
| 298 |
main() |