| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2012, Afterburn <https://github.com/afterburn> |
| 4 |
# Copyright (c) 2013, Aaron Bull Schaefer <[email protected]> |
| 5 |
# Copyright (c) 2015, Jonathan Lestrelin <[email protected]> |
| 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: pear |
| 14 |
short_description: Manage pear/pecl packages |
| 15 |
description: |
| 16 |
- Manage PHP packages with the pear package manager. |
| 17 |
author: |
| 18 |
- Jonathan Lestrelin (@jle64) <[email protected]> |
| 19 |
extends_documentation_fragment: |
| 20 |
- community.general._attributes |
| 21 |
attributes: |
| 22 |
check_mode: |
| 23 |
support: full |
| 24 |
diff_mode: |
| 25 |
support: none |
| 26 |
options: |
| 27 |
name: |
| 28 |
type: str |
| 29 |
description: |
| 30 |
- Name of the package to install, upgrade, or remove. |
| 31 |
required: true |
| 32 |
aliases: [pkg] |
| 33 |
state: |
| 34 |
type: str |
| 35 |
description: |
| 36 |
- Desired state of the package. |
| 37 |
default: "present" |
| 38 |
choices: ["present", "installed", "latest", "absent", "removed"] |
| 39 |
executable: |
| 40 |
type: path |
| 41 |
description: |
| 42 |
- Path to the pear executable. |
| 43 |
prompts: |
| 44 |
description: |
| 45 |
- List of regular expressions that can be used to detect prompts during pear package installation to answer the expected |
| 46 |
question. |
| 47 |
- Prompts are processed in the same order as the packages list. |
| 48 |
- You can optionally specify an answer to any question in the list. |
| 49 |
- If no answer is provided, the list item must contain only the regular expression. |
| 50 |
- "To specify an answer, the item must be a dictionary with the regular expression as key and the answer as value C(my_regular_expression: |
| 51 |
'an_answer')." |
| 52 |
- You can provide a list containing items with or without answer. |
| 53 |
- A prompt list can be shorter or longer than the packages list but it issues a warning. |
| 54 |
- If you want to specify that a package does not need prompts in the middle of a list, V(null). |
| 55 |
type: list |
| 56 |
elements: raw |
| 57 |
version_added: 0.2.0 |
| 58 |
""" |
| 59 |
|
| 60 |
EXAMPLES = r""" |
| 61 |
- name: Install pear package |
| 62 |
community.general.pear: |
| 63 |
name: Net_URL2 |
| 64 |
state: present |
| 65 |
|
| 66 |
- name: Install pecl package |
| 67 |
community.general.pear: |
| 68 |
name: pecl/json_post |
| 69 |
state: present |
| 70 |
|
| 71 |
- name: Install pecl package with expected prompt |
| 72 |
community.general.pear: |
| 73 |
name: pecl/apcu |
| 74 |
state: present |
| 75 |
prompts: |
| 76 |
- (.*)Enable internal debugging in APCu \[no\] |
| 77 |
|
| 78 |
- name: Install pecl package with expected prompt and an answer |
| 79 |
community.general.pear: |
| 80 |
name: pecl/apcu |
| 81 |
state: present |
| 82 |
prompts: |
| 83 |
- (.*)Enable internal debugging in APCu \[no\]: "yes" |
| 84 |
|
| 85 |
- name: Install multiple pear/pecl packages at once with prompts. Prompts will be processed on the same order as the packages |
| 86 |
order. If there is more prompts than packages, packages without prompts will be installed without any prompt expected. |
| 87 |
If there is more packages than prompts, additional prompts will be ignored. |
| 88 |
community.general.pear: |
| 89 |
name: pecl/gnupg, pecl/apcu |
| 90 |
state: present |
| 91 |
prompts: |
| 92 |
- I am a test prompt because gnupg doesnt asks anything |
| 93 |
- (.*)Enable internal debugging in APCu \[no\]: "yes" |
| 94 |
|
| 95 |
- name: Install multiple pear/pecl packages at once skipping the first prompt. Prompts will be processed on the same order |
| 96 |
as the packages order. If there is more prompts than packages, packages without prompts will be installed without any |
| 97 |
prompt expected. If there is more packages than prompts, additional prompts will be ignored. |
| 98 |
community.general.pear: |
| 99 |
name: pecl/gnupg, pecl/apcu |
| 100 |
state: present |
| 101 |
prompts: |
| 102 |
- null |
| 103 |
- (.*)Enable internal debugging in APCu \[no\]: "yes" |
| 104 |
|
| 105 |
- name: Upgrade package |
| 106 |
community.general.pear: |
| 107 |
name: Net_URL2 |
| 108 |
state: latest |
| 109 |
|
| 110 |
- name: Remove packages |
| 111 |
community.general.pear: |
| 112 |
name: Net_URL2,pecl/json_post |
| 113 |
state: absent |
| 114 |
""" |
| 115 |
|
| 116 |
import os |
| 117 |
|
| 118 |
from ansible.module_utils.basic import AnsibleModule |
| 119 |
from ansible.module_utils.common.text.converters import to_text |
| 120 |
|
| 121 |
|
| 122 |
def get_local_version(pear_output): |
| 123 |
"""Take pear remoteinfo output and get the installed version""" |
| 124 |
lines = pear_output.split("\n") |
| 125 |
for line in lines: |
| 126 |
if "Installed " in line: |
| 127 |
installed = line.rsplit(None, 1)[-1].strip() |
| 128 |
if installed == "-": |
| 129 |
continue |
| 130 |
return installed |
| 131 |
return None |
| 132 |
|
| 133 |
|
| 134 |
def _get_pear_path(module): |
| 135 |
if module.params["executable"] and os.path.isfile(module.params["executable"]): |
| 136 |
result = module.params["executable"] |
| 137 |
else: |
| 138 |
result = module.get_bin_path("pear", True, [module.params["executable"]]) |
| 139 |
return result |
| 140 |
|
| 141 |
|
| 142 |
def get_repository_version(pear_output): |
| 143 |
"""Take pear remote-info output and get the latest version""" |
| 144 |
lines = pear_output.split("\n") |
| 145 |
for line in lines: |
| 146 |
if "Latest " in line: |
| 147 |
return line.rsplit(None, 1)[-1].strip() |
| 148 |
return None |
| 149 |
|
| 150 |
|
| 151 |
def query_package(module, name): |
| 152 |
"""Query the package status in both the local system and the repository. |
| 153 |
Returns a boolean to indicate if the package is installed, |
| 154 |
and a second boolean to indicate if the package is up-to-date.""" |
| 155 |
lcmd = [_get_pear_path(module), "info", name] |
| 156 |
lrc, lstdout, lstderr = module.run_command(lcmd, check_rc=False) |
| 157 |
if lrc != 0: |
| 158 |
# package is not installed locally |
| 159 |
return False, False |
| 160 |
|
| 161 |
rcmd = [_get_pear_path(module), "remote-info", name] |
| 162 |
rrc, rstdout, rstderr = module.run_command(rcmd, check_rc=False) |
| 163 |
|
| 164 |
# get the version installed locally (if any) |
| 165 |
lversion = get_local_version(rstdout) |
| 166 |
|
| 167 |
# get the version in the repository |
| 168 |
rversion = get_repository_version(rstdout) |
| 169 |
|
| 170 |
if rrc == 0: |
| 171 |
# Return True to indicate that the package is installed locally, |
| 172 |
# and the result of the version number comparison |
| 173 |
# to determine if the package is up-to-date. |
| 174 |
return True, (lversion == rversion) |
| 175 |
|
| 176 |
return False, False |
| 177 |
|
| 178 |
|
| 179 |
def remove_packages(module, packages): |
| 180 |
remove_c = 0 |
| 181 |
# Using a for loop in case of error, we can report the package that failed |
| 182 |
for package in packages: |
| 183 |
# Query the package first, to see if we even need to remove |
| 184 |
installed, updated = query_package(module, package) |
| 185 |
if not installed: |
| 186 |
continue |
| 187 |
|
| 188 |
cmd = [_get_pear_path(module), "uninstall", package] |
| 189 |
rc, stdout, stderr = module.run_command(cmd, check_rc=False) |
| 190 |
|
| 191 |
if rc != 0: |
| 192 |
module.fail_json(msg=f"failed to remove {package}: {to_text(stdout + stderr)}") |
| 193 |
|
| 194 |
remove_c += 1 |
| 195 |
|
| 196 |
if remove_c > 0: |
| 197 |
module.exit_json(changed=True, msg=f"removed {remove_c} package(s)") |
| 198 |
|
| 199 |
module.exit_json(changed=False, msg="package(s) already absent") |
| 200 |
|
| 201 |
|
| 202 |
def install_packages(module, state, packages, prompts): |
| 203 |
install_c = 0 |
| 204 |
has_prompt = bool(prompts) |
| 205 |
default_stdin = "\n" |
| 206 |
|
| 207 |
if has_prompt: |
| 208 |
nb_prompts = len(prompts) |
| 209 |
nb_packages = len(packages) |
| 210 |
|
| 211 |
if nb_prompts > 0 and (nb_prompts != nb_packages): |
| 212 |
if nb_prompts > nb_packages: |
| 213 |
diff = nb_prompts - nb_packages |
| 214 |
msg = f"{to_text(nb_packages)} packages to install but {to_text(nb_prompts)} prompts to expect. {to_text(diff)} prompts will be ignored" |
| 215 |
else: |
| 216 |
diff = nb_packages - nb_prompts |
| 217 |
msg = ( |
| 218 |
f"{to_text(nb_packages)} packages to install but only {to_text(nb_prompts)} prompts to expect. " |
| 219 |
f"{to_text(diff)} packages won't be expected to have a prompt" |
| 220 |
) |
| 221 |
module.warn(msg) |
| 222 |
|
| 223 |
# Preparing prompts answer according to item type |
| 224 |
tmp_prompts = [] |
| 225 |
for _item in prompts: |
| 226 |
# If the current item is a dict then we expect its key to be the prompt regex and its value to be the answer |
| 227 |
# We also expect here that the dict only has ONE key and the first key will be taken |
| 228 |
if isinstance(_item, dict): |
| 229 |
key = list(_item.keys())[0] |
| 230 |
answer = f"{_item[key]}\n" |
| 231 |
|
| 232 |
tmp_prompts.append((key, answer)) |
| 233 |
elif not _item: |
| 234 |
tmp_prompts.append((None, default_stdin)) |
| 235 |
else: |
| 236 |
tmp_prompts.append((_item, default_stdin)) |
| 237 |
prompts = tmp_prompts |
| 238 |
for i, package in enumerate(packages): |
| 239 |
# if the package is installed and state == present |
| 240 |
# or state == latest and is up-to-date then skip |
| 241 |
installed, updated = query_package(module, package) |
| 242 |
if installed and (state == "present" or (state == "latest" and updated)): |
| 243 |
continue |
| 244 |
|
| 245 |
if state == "present": |
| 246 |
command = "install" |
| 247 |
|
| 248 |
if state == "latest": |
| 249 |
command = "upgrade" |
| 250 |
|
| 251 |
if has_prompt and i < len(prompts): |
| 252 |
prompt_regex = prompts[i][0] |
| 253 |
data = prompts[i][1] |
| 254 |
else: |
| 255 |
prompt_regex = None |
| 256 |
data = default_stdin |
| 257 |
|
| 258 |
cmd = [_get_pear_path(module), command, package] |
| 259 |
rc, stdout, stderr = module.run_command( |
| 260 |
cmd, check_rc=False, prompt_regex=prompt_regex, data=data, binary_data=True |
| 261 |
) |
| 262 |
if rc != 0: |
| 263 |
module.fail_json(msg=f"failed to install {package}: {to_text(stdout + stderr)}") |
| 264 |
|
| 265 |
install_c += 1 |
| 266 |
|
| 267 |
if install_c > 0: |
| 268 |
module.exit_json(changed=True, msg=f"installed {install_c} package(s)") |
| 269 |
|
| 270 |
module.exit_json(changed=False, msg="package(s) already installed") |
| 271 |
|
| 272 |
|
| 273 |
def check_packages(module, packages, state): |
| 274 |
would_be_changed = [] |
| 275 |
for package in packages: |
| 276 |
installed, updated = query_package(module, package) |
| 277 |
if ( |
| 278 |
(state in ["present", "latest"] and not installed) |
| 279 |
or (state == "absent" and installed) |
| 280 |
or (state == "latest" and not updated) |
| 281 |
): |
| 282 |
would_be_changed.append(package) |
| 283 |
if would_be_changed: |
| 284 |
if state == "absent": |
| 285 |
state = "removed" |
| 286 |
module.exit_json(changed=True, msg=f"{len(would_be_changed)} package(s) would be {state}") |
| 287 |
else: |
| 288 |
module.exit_json(change=False, msg=f"package(s) already {state}") |
| 289 |
|
| 290 |
|
| 291 |
def main(): |
| 292 |
module = AnsibleModule( |
| 293 |
argument_spec=dict( |
| 294 |
name=dict(aliases=["pkg"], required=True), |
| 295 |
state=dict(default="present", choices=["present", "installed", "latest", "absent", "removed"]), |
| 296 |
executable=dict(type="path"), |
| 297 |
prompts=dict(type="list", elements="raw"), |
| 298 |
), |
| 299 |
supports_check_mode=True, |
| 300 |
) |
| 301 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 302 |
|
| 303 |
p = module.params |
| 304 |
|
| 305 |
# normalize the state parameter |
| 306 |
if p["state"] in ["present", "installed"]: |
| 307 |
p["state"] = "present" |
| 308 |
elif p["state"] in ["absent", "removed"]: |
| 309 |
p["state"] = "absent" |
| 310 |
|
| 311 |
if p["name"]: |
| 312 |
pkgs = p["name"].split(",") |
| 313 |
|
| 314 |
if module.check_mode: |
| 315 |
check_packages(module, pkgs, p["state"]) |
| 316 |
|
| 317 |
if p["state"] in ["present", "latest"]: |
| 318 |
install_packages(module, p["state"], pkgs, p["prompts"]) |
| 319 |
elif p["state"] == "absent": |
| 320 |
remove_packages(module, pkgs) |
| 321 |
|
| 322 |
|
| 323 |
if __name__ == "__main__": |
| 324 |
main() |