| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2013, Johan Wiren <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: gem |
| 11 |
short_description: Manage Ruby gems |
| 12 |
description: |
| 13 |
- Manage installation and uninstallation of Ruby gems. |
| 14 |
extends_documentation_fragment: |
| 15 |
- community.general._attributes |
| 16 |
attributes: |
| 17 |
check_mode: |
| 18 |
support: full |
| 19 |
diff_mode: |
| 20 |
support: none |
| 21 |
options: |
| 22 |
name: |
| 23 |
type: str |
| 24 |
description: |
| 25 |
- The name of the gem to be managed. |
| 26 |
required: true |
| 27 |
state: |
| 28 |
type: str |
| 29 |
description: |
| 30 |
- The desired state of the gem. V(latest) ensures that the latest version is installed. |
| 31 |
choices: [present, absent, latest] |
| 32 |
default: present |
| 33 |
gem_source: |
| 34 |
type: path |
| 35 |
description: |
| 36 |
- The path to a local gem used as installation source. |
| 37 |
include_dependencies: |
| 38 |
description: |
| 39 |
- Whether to include dependencies or not. |
| 40 |
type: bool |
| 41 |
default: true |
| 42 |
repository: |
| 43 |
type: str |
| 44 |
description: |
| 45 |
- The repository from which the gem is installed. |
| 46 |
aliases: [source] |
| 47 |
user_install: |
| 48 |
description: |
| 49 |
- Install gem in user's local gems cache or for all users. |
| 50 |
type: bool |
| 51 |
default: true |
| 52 |
executable: |
| 53 |
type: path |
| 54 |
description: |
| 55 |
- Override the path to the gem executable. |
| 56 |
install_dir: |
| 57 |
type: path |
| 58 |
description: |
| 59 |
- Install the gems into a specific directory. These gems are independent from the global installed ones. Specifying |
| 60 |
this requires user_install to be false. |
| 61 |
bindir: |
| 62 |
type: path |
| 63 |
description: |
| 64 |
- Install executables into a specific directory. |
| 65 |
version_added: 3.3.0 |
| 66 |
norc: |
| 67 |
type: bool |
| 68 |
default: true |
| 69 |
description: |
| 70 |
- Avoid loading any C(.gemrc) file. Ignored for RubyGems prior to 2.5.2. |
| 71 |
- The default changed from V(false) to V(true) in community.general 6.0.0. |
| 72 |
version_added: 3.3.0 |
| 73 |
env_shebang: |
| 74 |
description: |
| 75 |
- Rewrite the shebang line on installed scripts to use /usr/bin/env. |
| 76 |
default: false |
| 77 |
type: bool |
| 78 |
version: |
| 79 |
type: str |
| 80 |
description: |
| 81 |
- Version of the gem to be installed/removed. |
| 82 |
pre_release: |
| 83 |
description: |
| 84 |
- Allow installation of pre-release versions of the gem. |
| 85 |
default: false |
| 86 |
type: bool |
| 87 |
include_doc: |
| 88 |
description: |
| 89 |
- Install with or without docs. |
| 90 |
default: false |
| 91 |
type: bool |
| 92 |
build_flags: |
| 93 |
type: str |
| 94 |
description: |
| 95 |
- Allow adding build flags for gem compilation. |
| 96 |
force: |
| 97 |
description: |
| 98 |
- Force gem to (un-)install, bypassing dependency checks. |
| 99 |
default: false |
| 100 |
type: bool |
| 101 |
override_platform_install_dir: |
| 102 |
description: |
| 103 |
- Resolve the user gem installation directory via C(gem environment) and pass it explicitly |
| 104 |
as C(--install-dir) to both C(gem install) and C(gem uninstall), instead of using C(--user-install). |
| 105 |
- This is needed on distributions (such as Fedora) where a platform-specific C(operating_system.rb) |
| 106 |
injects C(--install-dir) as a default for all gem commands, which conflicts with C(--user-install) |
| 107 |
and causes C(gem uninstall) to search the wrong directory. |
| 108 |
- Cannot be combined with O(user_install=false) or O(install_dir). |
| 109 |
default: false |
| 110 |
type: bool |
| 111 |
version_added: 13.0.0 |
| 112 |
author: |
| 113 |
- "Ansible Core Team" |
| 114 |
- "Johan Wiren (@johanwiren)" |
| 115 |
""" |
| 116 |
|
| 117 |
EXAMPLES = r""" |
| 118 |
- name: Install version 1.0 of vagrant |
| 119 |
community.general.gem: |
| 120 |
name: vagrant |
| 121 |
version: 1.0 |
| 122 |
state: present |
| 123 |
|
| 124 |
- name: Install latest available version of rake |
| 125 |
community.general.gem: |
| 126 |
name: rake |
| 127 |
state: latest |
| 128 |
|
| 129 |
- name: Install rake version 1.0 from a local gem on disk |
| 130 |
community.general.gem: |
| 131 |
name: rake |
| 132 |
gem_source: /path/to/gems/rake-1.0.gem |
| 133 |
state: present |
| 134 |
""" |
| 135 |
|
| 136 |
import os |
| 137 |
import re |
| 138 |
|
| 139 |
from ansible.module_utils.basic import AnsibleModule |
| 140 |
|
| 141 |
from ansible_collections.community.general.plugins.module_utils import _cmd_runner_fmt as fmt |
| 142 |
from ansible_collections.community.general.plugins.module_utils._cmd_runner import CmdRunner |
| 143 |
|
| 144 |
RE_VERSION = re.compile(r"^(\d+)\.(\d+)\.(\d+)") |
| 145 |
RE_INSTALLED = re.compile(r"\S+\s+\((?:default: )?(.+)\)") |
| 146 |
|
| 147 |
|
| 148 |
def get_rubygems_path(module: AnsibleModule) -> list[str]: |
| 149 |
if module.params["executable"]: |
| 150 |
return module.params["executable"].split() |
| 151 |
return [module.get_bin_path("gem", True)] |
| 152 |
|
| 153 |
|
| 154 |
def get_user_install_dir(module: AnsibleModule) -> str | None: |
| 155 |
cmd = get_rubygems_path(module) |
| 156 |
rc, out, err = module.run_command(cmd + ["environment"], check_rc=True) |
| 157 |
for line in out.splitlines(): |
| 158 |
match = re.search(r"USER INSTALLATION DIRECTORY:\s*(.+)", line) |
| 159 |
if match: |
| 160 |
return match.group(1).strip() |
| 161 |
return None |
| 162 |
|
| 163 |
|
| 164 |
def get_rubygems_version(module: AnsibleModule) -> tuple[int, ...] | None: |
| 165 |
cmd = get_rubygems_path(module) + ["--version"] |
| 166 |
rc, out, err = module.run_command(cmd, check_rc=True) |
| 167 |
match = RE_VERSION.match(out) |
| 168 |
if not match: |
| 169 |
return None |
| 170 |
return tuple(int(x) for x in match.groups()) |
| 171 |
|
| 172 |
|
| 173 |
def make_runner(module: AnsibleModule, ver: tuple[int, ...] | None) -> CmdRunner: |
| 174 |
command = get_rubygems_path(module) |
| 175 |
|
| 176 |
environ_update = {} |
| 177 |
if module.params["install_dir"]: |
| 178 |
environ_update["GEM_HOME"] = module.params["install_dir"] |
| 179 |
|
| 180 |
if ver and ver < (2, 0, 0): |
| 181 |
include_dependencies_fmt = fmt.as_bool("--include-dependencies", "--ignore-dependencies") |
| 182 |
include_doc_fmt = fmt.as_bool_not(["--no-rdoc", "--no-ri"]) |
| 183 |
else: |
| 184 |
include_dependencies_fmt = fmt.as_bool_not("--ignore-dependencies") |
| 185 |
include_doc_fmt = fmt.as_bool_not("--no-document") |
| 186 |
|
| 187 |
norc_fmt = fmt.as_bool("--norc") if (ver and ver >= (2, 5, 2)) else fmt.as_bool([]) |
| 188 |
|
| 189 |
return CmdRunner( |
| 190 |
module, |
| 191 |
command=command, |
| 192 |
environ_update=environ_update, |
| 193 |
check_rc=True, |
| 194 |
arg_formats=dict( |
| 195 |
_list_subcmd=fmt.as_fixed("list"), |
| 196 |
_install_subcmd=fmt.as_fixed("install"), |
| 197 |
_uninstall_subcmd=fmt.as_fixed("uninstall"), |
| 198 |
norc=norc_fmt, |
| 199 |
_remote_flag=fmt.as_fixed("--remote"), |
| 200 |
repository=fmt.as_opt_val("--source"), |
| 201 |
_name_pattern=fmt.as_func(lambda v: [f"^{v}$"]), |
| 202 |
version=fmt.as_opt_val("--version"), |
| 203 |
include_dependencies=include_dependencies_fmt, |
| 204 |
user_install=fmt.as_bool("--user-install", "--no-user-install"), |
| 205 |
install_dir=fmt.as_opt_val("--install-dir"), |
| 206 |
bindir=fmt.as_opt_val("--bindir"), |
| 207 |
pre_release=fmt.as_bool("--pre"), |
| 208 |
include_doc=include_doc_fmt, |
| 209 |
env_shebang=fmt.as_bool("--env-shebang"), |
| 210 |
gem_source=fmt.as_list(), |
| 211 |
build_flags=fmt.as_opt_val("--"), |
| 212 |
force=fmt.as_bool("--force"), |
| 213 |
_uninstall_version=fmt.as_func(lambda v: ["--version", v] if v else ["--all"], ignore_none=False), |
| 214 |
_executable_flag=fmt.as_fixed("--executable"), |
| 215 |
name=fmt.as_list(), |
| 216 |
), |
| 217 |
) |
| 218 |
|
| 219 |
|
| 220 |
def get_installed_versions(runner: CmdRunner, remote: bool = False) -> list[str]: |
| 221 |
name = runner.module.params["name"] |
| 222 |
if remote: |
| 223 |
args_order = ["_list_subcmd", "norc", "_remote_flag", "repository", "_name_pattern"] |
| 224 |
else: |
| 225 |
args_order = ["_list_subcmd", "norc", "_name_pattern"] |
| 226 |
with runner(args_order) as ctx: |
| 227 |
rc, out, err = ctx.run(_name_pattern=name) |
| 228 |
installed_versions = [] |
| 229 |
for line in out.splitlines(): |
| 230 |
match = RE_INSTALLED.match(line) |
| 231 |
if match: |
| 232 |
versions = match.group(1) |
| 233 |
for version in versions.split(", "): |
| 234 |
installed_versions.append(version.split()[0]) |
| 235 |
return installed_versions |
| 236 |
|
| 237 |
|
| 238 |
def exists(runner: CmdRunner) -> bool: |
| 239 |
module = runner.module |
| 240 |
if module.params["state"] == "latest": |
| 241 |
remoteversions = get_installed_versions(runner, remote=True) |
| 242 |
if remoteversions: |
| 243 |
module.params["version"] = remoteversions[0] |
| 244 |
installed_versions = get_installed_versions(runner) |
| 245 |
if module.params["version"]: |
| 246 |
return module.params["version"] in installed_versions |
| 247 |
return bool(installed_versions) |
| 248 |
|
| 249 |
|
| 250 |
def install(runner: CmdRunner, user_dir: str | None = None) -> None: |
| 251 |
args_order = [ |
| 252 |
"_install_subcmd", |
| 253 |
"norc", |
| 254 |
"version", |
| 255 |
"repository", |
| 256 |
"include_dependencies", |
| 257 |
"user_install", |
| 258 |
"install_dir", |
| 259 |
"bindir", |
| 260 |
"pre_release", |
| 261 |
"include_doc", |
| 262 |
"env_shebang", |
| 263 |
"gem_source", |
| 264 |
"build_flags", |
| 265 |
"force", |
| 266 |
] |
| 267 |
with runner(args_order, check_mode_skip=True) as ctx: |
| 268 |
if user_dir: |
| 269 |
bindir = runner.module.params["bindir"] or os.path.join(user_dir, "bin") |
| 270 |
ctx.run(user_install=False, install_dir=user_dir, bindir=bindir) |
| 271 |
else: |
| 272 |
ctx.run() |
| 273 |
|
| 274 |
|
| 275 |
def uninstall(runner: CmdRunner, user_dir: str | None = None) -> tuple[int, str, str] | None: |
| 276 |
args_order = [ |
| 277 |
"_uninstall_subcmd", |
| 278 |
"norc", |
| 279 |
"install_dir", |
| 280 |
"bindir", |
| 281 |
"_uninstall_version", |
| 282 |
"_executable_flag", |
| 283 |
"force", |
| 284 |
"name", |
| 285 |
] |
| 286 |
with runner(args_order, check_mode_skip=True) as ctx: |
| 287 |
kwargs = {"_uninstall_version": runner.module.params["version"]} |
| 288 |
if user_dir: |
| 289 |
kwargs["install_dir"] = user_dir |
| 290 |
return ctx.run(**kwargs) |
| 291 |
|
| 292 |
|
| 293 |
def main(): |
| 294 |
module = AnsibleModule( |
| 295 |
argument_spec=dict( |
| 296 |
executable=dict(type="path"), |
| 297 |
gem_source=dict(type="path"), |
| 298 |
include_dependencies=dict(default=True, type="bool"), |
| 299 |
name=dict(required=True, type="str"), |
| 300 |
repository=dict(aliases=["source"], type="str"), |
| 301 |
state=dict(default="present", choices=["present", "absent", "latest"], type="str"), |
| 302 |
user_install=dict(default=True, type="bool"), |
| 303 |
install_dir=dict(type="path"), |
| 304 |
bindir=dict(type="path"), |
| 305 |
norc=dict(type="bool", default=True), |
| 306 |
pre_release=dict(default=False, type="bool"), |
| 307 |
include_doc=dict(default=False, type="bool"), |
| 308 |
env_shebang=dict(default=False, type="bool"), |
| 309 |
version=dict(type="str"), |
| 310 |
build_flags=dict(type="str"), |
| 311 |
force=dict(default=False, type="bool"), |
| 312 |
override_platform_install_dir=dict(default=False, type="bool"), |
| 313 |
), |
| 314 |
supports_check_mode=True, |
| 315 |
mutually_exclusive=[["gem_source", "repository"], ["gem_source", "version"]], |
| 316 |
) |
| 317 |
|
| 318 |
if module.params["version"] and module.params["state"] == "latest": |
| 319 |
module.fail_json(msg="Cannot specify version when state=latest") |
| 320 |
if module.params["gem_source"] and module.params["state"] == "latest": |
| 321 |
module.fail_json(msg="Cannot maintain state=latest when installing from local source") |
| 322 |
if module.params["user_install"] and module.params["install_dir"]: |
| 323 |
module.fail_json(msg="install_dir requires user_install=false") |
| 324 |
if module.params["override_platform_install_dir"]: |
| 325 |
if not module.params["user_install"]: |
| 326 |
module.fail_json(msg="override_platform_install_dir requires user_install=true") |
| 327 |
if module.params["install_dir"]: |
| 328 |
module.fail_json(msg="override_platform_install_dir cannot be combined with install_dir") |
| 329 |
|
| 330 |
if not module.params["gem_source"]: |
| 331 |
module.params["gem_source"] = module.params["name"] |
| 332 |
|
| 333 |
ver = get_rubygems_version(module) |
| 334 |
|
| 335 |
user_dir = None |
| 336 |
if module.params["override_platform_install_dir"]: |
| 337 |
user_dir = get_user_install_dir(module) |
| 338 |
|
| 339 |
runner = make_runner(module, ver) |
| 340 |
|
| 341 |
changed = False |
| 342 |
|
| 343 |
if module.params["state"] in ["present", "latest"]: |
| 344 |
if not exists(runner): |
| 345 |
install(runner, user_dir) |
| 346 |
changed = True |
| 347 |
elif module.params["state"] == "absent": |
| 348 |
if exists(runner): |
| 349 |
command_output = uninstall(runner, user_dir) |
| 350 |
if command_output is not None and exists(runner): |
| 351 |
rc, out, err = command_output |
| 352 |
module.fail_json( |
| 353 |
msg=( |
| 354 |
f"Failed to uninstall gem '{module.params['name']}': it is still present after 'gem uninstall'. " |
| 355 |
"This usually happens with default or system gems provided by the OS, " |
| 356 |
"which cannot be removed with the gem command." |
| 357 |
), |
| 358 |
rc=rc, |
| 359 |
stdout=out, |
| 360 |
stderr=err, |
| 361 |
) |
| 362 |
else: |
| 363 |
changed = True |
| 364 |
result = {} |
| 365 |
result["name"] = module.params["name"] |
| 366 |
result["state"] = module.params["state"] |
| 367 |
if module.params["version"]: |
| 368 |
result["version"] = module.params["version"] |
| 369 |
result["changed"] = changed |
| 370 |
|
| 371 |
module.exit_json(**result) |
| 372 |
|
| 373 |
|
| 374 |
if __name__ == "__main__": |
| 375 |
main() |