| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2013, Daniel Jaouen <[email protected]> |
| 4 |
# Copyright (c) 2016, Indrajit Raychaudhuri <[email protected]> |
| 5 |
# |
| 6 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
DOCUMENTATION = r""" |
| 12 |
module: homebrew_cask |
| 13 |
author: |
| 14 |
- "Indrajit Raychaudhuri (@indrajitr)" |
| 15 |
- "Daniel Jaouen (@danieljaouen)" |
| 16 |
- "Enric Lluelles (@enriclluelles)" |
| 17 |
short_description: Install and uninstall homebrew casks |
| 18 |
description: |
| 19 |
- Manages Homebrew casks. |
| 20 |
extends_documentation_fragment: |
| 21 |
- community.general._attributes |
| 22 |
attributes: |
| 23 |
check_mode: |
| 24 |
support: full |
| 25 |
diff_mode: |
| 26 |
support: none |
| 27 |
options: |
| 28 |
name: |
| 29 |
description: |
| 30 |
- Name of cask to install or remove. |
| 31 |
aliases: ['cask', 'package', 'pkg'] |
| 32 |
type: list |
| 33 |
elements: str |
| 34 |
path: |
| 35 |
description: |
| 36 |
- "':' separated list of paths to search for 'brew' executable." |
| 37 |
default: '/usr/local/bin:/opt/homebrew/bin' |
| 38 |
type: path |
| 39 |
state: |
| 40 |
description: |
| 41 |
- State of the cask. |
| 42 |
choices: ['absent', 'installed', 'latest', 'present', 'removed', 'uninstalled', 'upgraded'] |
| 43 |
default: present |
| 44 |
type: str |
| 45 |
sudo_password: |
| 46 |
description: |
| 47 |
- The sudo password to be passed to E(SUDO_ASKPASS). |
| 48 |
type: str |
| 49 |
update_homebrew: |
| 50 |
description: |
| 51 |
- Update homebrew itself first. |
| 52 |
- Note that C(brew cask update) is a synonym for C(brew update). |
| 53 |
type: bool |
| 54 |
default: false |
| 55 |
install_options: |
| 56 |
description: |
| 57 |
- Options flags to install a package. |
| 58 |
aliases: ['options'] |
| 59 |
type: list |
| 60 |
elements: str |
| 61 |
accept_external_apps: |
| 62 |
description: |
| 63 |
- Allow external apps. |
| 64 |
type: bool |
| 65 |
default: false |
| 66 |
upgrade_all: |
| 67 |
description: |
| 68 |
- Upgrade all casks. |
| 69 |
- Mutually exclusive with C(upgraded) state. |
| 70 |
type: bool |
| 71 |
default: false |
| 72 |
aliases: ['upgrade'] |
| 73 |
greedy: |
| 74 |
description: |
| 75 |
- Upgrade casks that auto update. |
| 76 |
- Passes C(--greedy) to C(brew outdated --cask) when checking if an installed cask has a newer version available, or |
| 77 |
to C(brew upgrade --cask) when upgrading all casks. |
| 78 |
type: bool |
| 79 |
default: false |
| 80 |
""" |
| 81 |
EXAMPLES = r""" |
| 82 |
- name: Install cask |
| 83 |
community.general.homebrew_cask: |
| 84 |
name: alfred |
| 85 |
state: present |
| 86 |
|
| 87 |
- name: Remove cask |
| 88 |
community.general.homebrew_cask: |
| 89 |
name: alfred |
| 90 |
state: absent |
| 91 |
|
| 92 |
- name: Install cask with install options |
| 93 |
community.general.homebrew_cask: |
| 94 |
name: alfred |
| 95 |
state: present |
| 96 |
install_options: 'appdir=/Applications' |
| 97 |
|
| 98 |
- name: Install cask with install options |
| 99 |
community.general.homebrew_cask: |
| 100 |
name: alfred |
| 101 |
state: present |
| 102 |
install_options: 'debug,appdir=/Applications' |
| 103 |
|
| 104 |
- name: Install cask with force option |
| 105 |
community.general.homebrew_cask: |
| 106 |
name: alfred |
| 107 |
state: present |
| 108 |
install_options: force |
| 109 |
|
| 110 |
- name: Allow external app |
| 111 |
community.general.homebrew_cask: |
| 112 |
name: alfred |
| 113 |
state: present |
| 114 |
accept_external_apps: true |
| 115 |
|
| 116 |
- name: Remove cask with force option |
| 117 |
community.general.homebrew_cask: |
| 118 |
name: alfred |
| 119 |
state: absent |
| 120 |
install_options: force |
| 121 |
|
| 122 |
- name: Upgrade all casks |
| 123 |
community.general.homebrew_cask: |
| 124 |
upgrade_all: true |
| 125 |
|
| 126 |
- name: Upgrade all casks with greedy option |
| 127 |
community.general.homebrew_cask: |
| 128 |
upgrade_all: true |
| 129 |
greedy: true |
| 130 |
|
| 131 |
- name: Upgrade given cask with force option |
| 132 |
community.general.homebrew_cask: |
| 133 |
name: alfred |
| 134 |
state: upgraded |
| 135 |
install_options: force |
| 136 |
|
| 137 |
- name: Upgrade cask with greedy option |
| 138 |
community.general.homebrew_cask: |
| 139 |
name: 1password |
| 140 |
state: upgraded |
| 141 |
greedy: true |
| 142 |
|
| 143 |
- name: Using sudo password for installing cask |
| 144 |
# ansible_become_password must be set in inventory or group_vars; it is not populated by -K |
| 145 |
community.general.homebrew_cask: |
| 146 |
name: wireshark |
| 147 |
state: present |
| 148 |
sudo_password: "{{ ansible_become_password }}" |
| 149 |
""" |
| 150 |
|
| 151 |
import os |
| 152 |
import re |
| 153 |
import shlex |
| 154 |
import tempfile |
| 155 |
|
| 156 |
from ansible.module_utils.basic import AnsibleModule |
| 157 |
from ansible.module_utils.common.text.converters import to_bytes |
| 158 |
|
| 159 |
from ansible_collections.community.general.plugins.module_utils._homebrew import HomebrewValidate |
| 160 |
from ansible_collections.community.general.plugins.module_utils._version import LooseVersion |
| 161 |
|
| 162 |
|
| 163 |
# exceptions -------------------------------------------------------------- {{{ |
| 164 |
class HomebrewCaskException(Exception): |
| 165 |
pass |
| 166 |
|
| 167 |
|
| 168 |
# /exceptions ------------------------------------------------------------- }}} |
| 169 |
|
| 170 |
|
| 171 |
# utils ------------------------------------------------------------------- {{{ |
| 172 |
def _create_regex_group_complement(s): |
| 173 |
lines = (line.strip() for line in s.split("\n") if line.strip()) |
| 174 |
chars = [_f for _f in (line.split("#")[0].strip() for line in lines) if _f] |
| 175 |
group = rf"[^{''.join(chars)}]" |
| 176 |
return re.compile(group) |
| 177 |
|
| 178 |
|
| 179 |
# /utils ------------------------------------------------------------------ }}} |
| 180 |
|
| 181 |
|
| 182 |
class HomebrewCask: |
| 183 |
"""A class to manage Homebrew casks.""" |
| 184 |
|
| 185 |
# class regexes ------------------------------------------------ {{{ |
| 186 |
VALID_CASK_CHARS = r""" |
| 187 |
\w # alphanumeric characters (i.e., [a-zA-Z0-9_]) |
| 188 |
. # dots |
| 189 |
/ # slash (for taps) |
| 190 |
\- # dashes |
| 191 |
@ # at symbol |
| 192 |
\+ # plus symbol |
| 193 |
""" |
| 194 |
|
| 195 |
INVALID_CASK_REGEX = _create_regex_group_complement(VALID_CASK_CHARS) |
| 196 |
# /class regexes ----------------------------------------------- }}} |
| 197 |
|
| 198 |
# class validations -------------------------------------------- {{{ |
| 199 |
@classmethod |
| 200 |
def valid_cask(cls, cask): |
| 201 |
"""A valid cask is either None or alphanumeric + backslashes.""" |
| 202 |
|
| 203 |
if cask is None: |
| 204 |
return True |
| 205 |
|
| 206 |
return isinstance(cask, str) and not cls.INVALID_CASK_REGEX.search(cask) |
| 207 |
|
| 208 |
@classmethod |
| 209 |
def valid_state(cls, state): |
| 210 |
""" |
| 211 |
A valid state is one of: |
| 212 |
- installed |
| 213 |
- absent |
| 214 |
""" |
| 215 |
|
| 216 |
if state is None: |
| 217 |
return True |
| 218 |
else: |
| 219 |
return isinstance(state, str) and state.lower() in ( |
| 220 |
"installed", |
| 221 |
"absent", |
| 222 |
) |
| 223 |
|
| 224 |
@classmethod |
| 225 |
def valid_module(cls, module): |
| 226 |
"""A valid module is an instance of AnsibleModule.""" |
| 227 |
|
| 228 |
return isinstance(module, AnsibleModule) |
| 229 |
|
| 230 |
# /class validations ------------------------------------------- }}} |
| 231 |
|
| 232 |
# class properties --------------------------------------------- {{{ |
| 233 |
@property |
| 234 |
def module(self): |
| 235 |
return self._module |
| 236 |
|
| 237 |
@module.setter |
| 238 |
def module(self, module): |
| 239 |
if not self.valid_module(module): |
| 240 |
self._module = None |
| 241 |
self.failed = True |
| 242 |
self.message = f"Invalid module: {module}." |
| 243 |
raise HomebrewCaskException(self.message) |
| 244 |
|
| 245 |
else: |
| 246 |
self._module = module |
| 247 |
return module |
| 248 |
|
| 249 |
@property |
| 250 |
def path(self): |
| 251 |
return self._path |
| 252 |
|
| 253 |
@path.setter |
| 254 |
def path(self, path): |
| 255 |
if not HomebrewValidate.valid_path(path): |
| 256 |
self._path = [] |
| 257 |
self.failed = True |
| 258 |
self.message = f"Invalid path: {path}." |
| 259 |
raise HomebrewCaskException(self.message) |
| 260 |
|
| 261 |
else: |
| 262 |
if isinstance(path, str): |
| 263 |
self._path = path.split(":") |
| 264 |
else: |
| 265 |
self._path = path |
| 266 |
|
| 267 |
return path |
| 268 |
|
| 269 |
@property |
| 270 |
def brew_path(self): |
| 271 |
return self._brew_path |
| 272 |
|
| 273 |
@brew_path.setter |
| 274 |
def brew_path(self, brew_path): |
| 275 |
if not HomebrewValidate.valid_brew_path(brew_path): |
| 276 |
self._brew_path = None |
| 277 |
self.failed = True |
| 278 |
self.message = f"Invalid brew_path: {brew_path}." |
| 279 |
raise HomebrewCaskException(self.message) |
| 280 |
|
| 281 |
else: |
| 282 |
self._brew_path = brew_path |
| 283 |
return brew_path |
| 284 |
|
| 285 |
@property |
| 286 |
def params(self): |
| 287 |
return self._params |
| 288 |
|
| 289 |
@params.setter |
| 290 |
def params(self, params): |
| 291 |
self._params = self.module.params |
| 292 |
return self._params |
| 293 |
|
| 294 |
@property |
| 295 |
def current_cask(self): |
| 296 |
return self._current_cask |
| 297 |
|
| 298 |
@current_cask.setter |
| 299 |
def current_cask(self, cask): |
| 300 |
if not self.valid_cask(cask): |
| 301 |
self._current_cask = None |
| 302 |
self.failed = True |
| 303 |
self.message = f"Invalid cask: {cask}." |
| 304 |
raise HomebrewCaskException(self.message) |
| 305 |
|
| 306 |
else: |
| 307 |
self._current_cask = cask |
| 308 |
return cask |
| 309 |
|
| 310 |
@property |
| 311 |
def brew_version(self): |
| 312 |
try: |
| 313 |
return self._brew_version |
| 314 |
except AttributeError: |
| 315 |
return None |
| 316 |
|
| 317 |
@brew_version.setter |
| 318 |
def brew_version(self, brew_version): |
| 319 |
self._brew_version = brew_version |
| 320 |
|
| 321 |
# /class properties -------------------------------------------- }}} |
| 322 |
|
| 323 |
def __init__( |
| 324 |
self, |
| 325 |
module, |
| 326 |
path=path, |
| 327 |
casks=None, |
| 328 |
state=None, |
| 329 |
sudo_password=None, |
| 330 |
update_homebrew=False, |
| 331 |
install_options=None, |
| 332 |
accept_external_apps=False, |
| 333 |
upgrade_all=False, |
| 334 |
greedy=False, |
| 335 |
): |
| 336 |
if not install_options: |
| 337 |
install_options = list() |
| 338 |
self._setup_status_vars() |
| 339 |
self._setup_instance_vars( |
| 340 |
module=module, |
| 341 |
path=path, |
| 342 |
casks=casks, |
| 343 |
state=state, |
| 344 |
sudo_password=sudo_password, |
| 345 |
update_homebrew=update_homebrew, |
| 346 |
install_options=install_options, |
| 347 |
accept_external_apps=accept_external_apps, |
| 348 |
upgrade_all=upgrade_all, |
| 349 |
greedy=greedy, |
| 350 |
) |
| 351 |
|
| 352 |
self._prep() |
| 353 |
|
| 354 |
# prep --------------------------------------------------------- {{{ |
| 355 |
def _setup_status_vars(self): |
| 356 |
self.failed = False |
| 357 |
self.changed = False |
| 358 |
self.changed_count = 0 |
| 359 |
self.unchanged_count = 0 |
| 360 |
self.message = "" |
| 361 |
|
| 362 |
def _setup_instance_vars(self, **kwargs): |
| 363 |
for key, val in kwargs.items(): |
| 364 |
setattr(self, key, val) |
| 365 |
|
| 366 |
def _prep(self): |
| 367 |
self._prep_brew_path() |
| 368 |
|
| 369 |
def _prep_brew_path(self): |
| 370 |
if not self.module: |
| 371 |
self.brew_path = None |
| 372 |
self.failed = True |
| 373 |
self.message = "AnsibleModule not set." |
| 374 |
raise HomebrewCaskException(self.message) |
| 375 |
|
| 376 |
self.brew_path = self.module.get_bin_path( |
| 377 |
"brew", |
| 378 |
required=True, |
| 379 |
opt_dirs=self.path, |
| 380 |
) |
| 381 |
if not self.brew_path: |
| 382 |
self.brew_path = None |
| 383 |
self.failed = True |
| 384 |
self.message = "Unable to locate homebrew executable." |
| 385 |
raise HomebrewCaskException("Unable to locate homebrew executable.") |
| 386 |
|
| 387 |
return self.brew_path |
| 388 |
|
| 389 |
def _status(self): |
| 390 |
return (self.failed, self.changed, self.message) |
| 391 |
|
| 392 |
# /prep -------------------------------------------------------- }}} |
| 393 |
|
| 394 |
def run(self): |
| 395 |
try: |
| 396 |
self._run() |
| 397 |
except HomebrewCaskException: |
| 398 |
pass |
| 399 |
|
| 400 |
if not self.failed and (self.changed_count + self.unchanged_count > 1): |
| 401 |
self.message = f"Changed: {self.changed_count}, Unchanged: {self.unchanged_count}" |
| 402 |
(failed, changed, message) = self._status() |
| 403 |
|
| 404 |
return (failed, changed, message) |
| 405 |
|
| 406 |
# checks ------------------------------------------------------- {{{ |
| 407 |
def _current_cask_is_outdated(self): |
| 408 |
if not self.valid_cask(self.current_cask): |
| 409 |
return False |
| 410 |
|
| 411 |
if self._brew_cask_command_is_deprecated(): |
| 412 |
base_opts = [self.brew_path, "outdated", "--cask"] |
| 413 |
else: |
| 414 |
base_opts = [self.brew_path, "cask", "outdated"] |
| 415 |
|
| 416 |
cask_is_outdated_command = base_opts + (["--greedy"] if self.greedy else []) + [self.current_cask] |
| 417 |
|
| 418 |
rc, out, err = self.module.run_command(cask_is_outdated_command) |
| 419 |
|
| 420 |
return out != "" |
| 421 |
|
| 422 |
def _current_cask_is_installed(self): |
| 423 |
if not self.valid_cask(self.current_cask): |
| 424 |
self.failed = True |
| 425 |
self.message = f"Invalid cask: {self.current_cask}." |
| 426 |
raise HomebrewCaskException(self.message) |
| 427 |
|
| 428 |
if self._brew_cask_command_is_deprecated(): |
| 429 |
base_opts = [self.brew_path, "list", "--cask"] |
| 430 |
else: |
| 431 |
base_opts = [self.brew_path, "cask", "list"] |
| 432 |
|
| 433 |
cmd = base_opts + [self.current_cask] |
| 434 |
rc, out, err = self.module.run_command(cmd) |
| 435 |
|
| 436 |
return rc == 0 |
| 437 |
|
| 438 |
def _get_brew_version(self): |
| 439 |
if self.brew_version: |
| 440 |
return self.brew_version |
| 441 |
|
| 442 |
cmd = [self.brew_path, "--version"] |
| 443 |
dummy, out, dummy = self.module.run_command(cmd, check_rc=True) |
| 444 |
|
| 445 |
pattern = r"Homebrew (.*)(\d+\.\d+\.\d+)(-dirty)?" |
| 446 |
rematch = re.search(pattern, out) |
| 447 |
if not rematch: |
| 448 |
self.module.fail_json(msg="Failed to match regex to get brew version", stdout=out) |
| 449 |
|
| 450 |
prefix, version, dummy = rematch.groups() |
| 451 |
if ">=" in prefix: |
| 452 |
version = "99.0.0" |
| 453 |
|
| 454 |
self.brew_version = version |
| 455 |
return self.brew_version |
| 456 |
|
| 457 |
def _brew_cask_command_is_deprecated(self): |
| 458 |
# The `brew cask` replacements were fully available in 2.6.0 (https://brew.sh/2020/12/01/homebrew-2.6.0/) |
| 459 |
return LooseVersion(self._get_brew_version()) >= LooseVersion("2.6.0") |
| 460 |
|
| 461 |
# /checks ------------------------------------------------------ }}} |
| 462 |
|
| 463 |
# commands ----------------------------------------------------- {{{ |
| 464 |
def _run(self): |
| 465 |
if self.upgrade_all: |
| 466 |
return self._upgrade_all() |
| 467 |
|
| 468 |
if self.casks: |
| 469 |
if self.state == "installed": |
| 470 |
return self._install_casks() |
| 471 |
elif self.state == "upgraded": |
| 472 |
return self._upgrade_casks() |
| 473 |
elif self.state == "absent": |
| 474 |
return self._uninstall_casks() |
| 475 |
|
| 476 |
self.failed = True |
| 477 |
self.message = "You must select a cask to install." |
| 478 |
raise HomebrewCaskException(self.message) |
| 479 |
|
| 480 |
# sudo_password fix ---------------------- {{{ |
| 481 |
def _run_command_with_sudo_password(self, cmd): |
| 482 |
rc, out, err = "", "", "" |
| 483 |
|
| 484 |
with tempfile.NamedTemporaryFile() as sudo_askpass_file: |
| 485 |
sudo_askpass_file.write(to_bytes(f"#!/bin/sh\necho {shlex.quote(self.sudo_password)}\n")) |
| 486 |
sudo_askpass_file.flush() |
| 487 |
os.chmod(sudo_askpass_file.name, 0o700) |
| 488 |
|
| 489 |
rc, out, err = self.module.run_command(cmd, environ_update={"SUDO_ASKPASS": sudo_askpass_file.name}) |
| 490 |
|
| 491 |
return (rc, out, err) |
| 492 |
|
| 493 |
# /sudo_password fix --------------------- }}} |
| 494 |
|
| 495 |
# updated -------------------------------- {{{ |
| 496 |
def _update_homebrew(self): |
| 497 |
rc, out, err = self.module.run_command( |
| 498 |
[ |
| 499 |
self.brew_path, |
| 500 |
"update", |
| 501 |
] |
| 502 |
) |
| 503 |
if rc == 0: |
| 504 |
if out and isinstance(out, str): |
| 505 |
already_updated = any( |
| 506 |
re.search(r"Already up-to-date.", s.strip(), re.IGNORECASE) for s in out.split("\n") if s |
| 507 |
) |
| 508 |
if not already_updated: |
| 509 |
self.changed = True |
| 510 |
self.message = "Homebrew updated successfully." |
| 511 |
else: |
| 512 |
self.message = "Homebrew already up-to-date." |
| 513 |
|
| 514 |
return True |
| 515 |
else: |
| 516 |
self.failed = True |
| 517 |
self.message = err.strip() |
| 518 |
raise HomebrewCaskException(self.message) |
| 519 |
|
| 520 |
# /updated ------------------------------- }}} |
| 521 |
|
| 522 |
# _upgrade_all --------------------------- {{{ |
| 523 |
def _upgrade_all(self): |
| 524 |
if self.module.check_mode: |
| 525 |
self.changed = True |
| 526 |
self.message = "Casks would be upgraded." |
| 527 |
raise HomebrewCaskException(self.message) |
| 528 |
|
| 529 |
if self._brew_cask_command_is_deprecated(): |
| 530 |
cmd = [self.brew_path, "upgrade", "--cask"] |
| 531 |
else: |
| 532 |
cmd = [self.brew_path, "cask", "upgrade"] |
| 533 |
|
| 534 |
if self.greedy: |
| 535 |
cmd = cmd + ["--greedy"] |
| 536 |
|
| 537 |
rc, out, err = "", "", "" |
| 538 |
|
| 539 |
if self.sudo_password: |
| 540 |
rc, out, err = self._run_command_with_sudo_password(cmd) |
| 541 |
else: |
| 542 |
rc, out, err = self.module.run_command(cmd) |
| 543 |
|
| 544 |
if rc == 0: |
| 545 |
# 'brew upgrade --cask' does not output anything if no casks are upgraded |
| 546 |
if not out.strip(): |
| 547 |
self.message = "Homebrew casks already upgraded." |
| 548 |
|
| 549 |
# handle legacy 'brew cask upgrade' |
| 550 |
elif re.search(r"==> No Casks to upgrade", out.strip(), re.IGNORECASE): |
| 551 |
self.message = "Homebrew casks already upgraded." |
| 552 |
|
| 553 |
else: |
| 554 |
self.changed = True |
| 555 |
self.message = "Homebrew casks upgraded." |
| 556 |
|
| 557 |
return True |
| 558 |
else: |
| 559 |
self.failed = True |
| 560 |
self.message = err.strip() |
| 561 |
raise HomebrewCaskException(self.message) |
| 562 |
|
| 563 |
# /_upgrade_all -------------------------- }}} |
| 564 |
|
| 565 |
# installed ------------------------------ {{{ |
| 566 |
def _install_current_cask(self): |
| 567 |
if not self.valid_cask(self.current_cask): |
| 568 |
self.failed = True |
| 569 |
self.message = f"Invalid cask: {self.current_cask}." |
| 570 |
raise HomebrewCaskException(self.message) |
| 571 |
|
| 572 |
if "--force" not in self.install_options and self._current_cask_is_installed(): |
| 573 |
self.unchanged_count += 1 |
| 574 |
self.message = f"Cask already installed: {self.current_cask}" |
| 575 |
return True |
| 576 |
|
| 577 |
if self.module.check_mode: |
| 578 |
self.changed = True |
| 579 |
self.message = f"Cask would be installed: {self.current_cask}" |
| 580 |
raise HomebrewCaskException(self.message) |
| 581 |
|
| 582 |
if self._brew_cask_command_is_deprecated(): |
| 583 |
base_opts = [self.brew_path, "install", "--cask"] |
| 584 |
else: |
| 585 |
base_opts = [self.brew_path, "cask", "install"] |
| 586 |
|
| 587 |
opts = base_opts + [self.current_cask] + self.install_options |
| 588 |
|
| 589 |
cmd = [opt for opt in opts if opt] |
| 590 |
|
| 591 |
rc, out, err = "", "", "" |
| 592 |
|
| 593 |
if self.sudo_password: |
| 594 |
rc, out, err = self._run_command_with_sudo_password(cmd) |
| 595 |
else: |
| 596 |
rc, out, err = self.module.run_command(cmd) |
| 597 |
|
| 598 |
if self._current_cask_is_installed(): |
| 599 |
self.changed_count += 1 |
| 600 |
self.changed = True |
| 601 |
self.message = f"Cask installed: {self.current_cask}" |
| 602 |
return True |
| 603 |
elif self.accept_external_apps and re.search(r"Error: It seems there is already an App at", err): |
| 604 |
self.unchanged_count += 1 |
| 605 |
self.message = f"Cask already installed: {self.current_cask}" |
| 606 |
return True |
| 607 |
else: |
| 608 |
self.failed = True |
| 609 |
self.message = err.strip() |
| 610 |
raise HomebrewCaskException(self.message) |
| 611 |
|
| 612 |
def _install_casks(self): |
| 613 |
for cask in self.casks: |
| 614 |
self.current_cask = cask |
| 615 |
self._install_current_cask() |
| 616 |
|
| 617 |
return True |
| 618 |
|
| 619 |
# /installed ----------------------------- }}} |
| 620 |
|
| 621 |
# upgraded ------------------------------- {{{ |
| 622 |
def _upgrade_current_cask(self): |
| 623 |
command = "upgrade" |
| 624 |
|
| 625 |
if not self.valid_cask(self.current_cask): |
| 626 |
self.failed = True |
| 627 |
self.message = f"Invalid cask: {self.current_cask}." |
| 628 |
raise HomebrewCaskException(self.message) |
| 629 |
|
| 630 |
if not self._current_cask_is_installed(): |
| 631 |
command = "install" |
| 632 |
|
| 633 |
if self._current_cask_is_installed() and not self._current_cask_is_outdated(): |
| 634 |
self.message = f"Cask is already upgraded: {self.current_cask}" |
| 635 |
self.unchanged_count += 1 |
| 636 |
return True |
| 637 |
|
| 638 |
if self.module.check_mode: |
| 639 |
self.changed = True |
| 640 |
self.message = f"Cask would be upgraded: {self.current_cask}" |
| 641 |
raise HomebrewCaskException(self.message) |
| 642 |
|
| 643 |
if self._brew_cask_command_is_deprecated(): |
| 644 |
base_opts = [self.brew_path, command, "--cask"] |
| 645 |
else: |
| 646 |
base_opts = [self.brew_path, "cask", command] |
| 647 |
|
| 648 |
opts = base_opts + self.install_options + [self.current_cask] |
| 649 |
|
| 650 |
cmd = [opt for opt in opts if opt] |
| 651 |
|
| 652 |
rc, out, err = "", "", "" |
| 653 |
|
| 654 |
if self.sudo_password: |
| 655 |
rc, out, err = self._run_command_with_sudo_password(cmd) |
| 656 |
else: |
| 657 |
rc, out, err = self.module.run_command(cmd) |
| 658 |
|
| 659 |
if rc == 0: |
| 660 |
self.changed_count += 1 |
| 661 |
self.changed = True |
| 662 |
self.message = f"Cask upgraded: {self.current_cask}" |
| 663 |
return True |
| 664 |
else: |
| 665 |
self.failed = True |
| 666 |
self.message = err.strip() |
| 667 |
raise HomebrewCaskException(self.message) |
| 668 |
|
| 669 |
def _upgrade_casks(self): |
| 670 |
for cask in self.casks: |
| 671 |
self.current_cask = cask |
| 672 |
self._upgrade_current_cask() |
| 673 |
|
| 674 |
return True |
| 675 |
|
| 676 |
# /upgraded ------------------------------ }}} |
| 677 |
|
| 678 |
# uninstalled ---------------------------- {{{ |
| 679 |
def _uninstall_current_cask(self): |
| 680 |
if not self.valid_cask(self.current_cask): |
| 681 |
self.failed = True |
| 682 |
self.message = f"Invalid cask: {self.current_cask}." |
| 683 |
raise HomebrewCaskException(self.message) |
| 684 |
|
| 685 |
if not self._current_cask_is_installed(): |
| 686 |
self.unchanged_count += 1 |
| 687 |
self.message = f"Cask already uninstalled: {self.current_cask}" |
| 688 |
return True |
| 689 |
|
| 690 |
if self.module.check_mode: |
| 691 |
self.changed = True |
| 692 |
self.message = f"Cask would be uninstalled: {self.current_cask}" |
| 693 |
raise HomebrewCaskException(self.message) |
| 694 |
|
| 695 |
if self._brew_cask_command_is_deprecated(): |
| 696 |
base_opts = [self.brew_path, "uninstall", "--cask"] |
| 697 |
else: |
| 698 |
base_opts = [self.brew_path, "cask", "uninstall"] |
| 699 |
|
| 700 |
opts = base_opts + [self.current_cask] + self.install_options |
| 701 |
|
| 702 |
cmd = [opt for opt in opts if opt] |
| 703 |
|
| 704 |
rc, out, err = "", "", "" |
| 705 |
|
| 706 |
if self.sudo_password: |
| 707 |
rc, out, err = self._run_command_with_sudo_password(cmd) |
| 708 |
else: |
| 709 |
rc, out, err = self.module.run_command(cmd) |
| 710 |
|
| 711 |
if not self._current_cask_is_installed(): |
| 712 |
self.changed_count += 1 |
| 713 |
self.changed = True |
| 714 |
self.message = f"Cask uninstalled: {self.current_cask}" |
| 715 |
return True |
| 716 |
else: |
| 717 |
self.failed = True |
| 718 |
self.message = err.strip() |
| 719 |
raise HomebrewCaskException(self.message) |
| 720 |
|
| 721 |
def _uninstall_casks(self): |
| 722 |
for cask in self.casks: |
| 723 |
self.current_cask = cask |
| 724 |
self._uninstall_current_cask() |
| 725 |
|
| 726 |
return True |
| 727 |
|
| 728 |
# /uninstalled --------------------------- }}} |
| 729 |
# /commands ---------------------------------------------------- }}} |
| 730 |
|
| 731 |
|
| 732 |
def main(): |
| 733 |
module = AnsibleModule( |
| 734 |
argument_spec=dict( |
| 735 |
name=dict( |
| 736 |
aliases=["pkg", "package", "cask"], |
| 737 |
type="list", |
| 738 |
elements="str", |
| 739 |
), |
| 740 |
path=dict( |
| 741 |
default="/usr/local/bin:/opt/homebrew/bin", |
| 742 |
type="path", |
| 743 |
), |
| 744 |
state=dict( |
| 745 |
default="present", |
| 746 |
choices=[ |
| 747 |
"present", |
| 748 |
"installed", |
| 749 |
"latest", |
| 750 |
"upgraded", |
| 751 |
"absent", |
| 752 |
"removed", |
| 753 |
"uninstalled", |
| 754 |
], |
| 755 |
), |
| 756 |
sudo_password=dict( |
| 757 |
type="str", |
| 758 |
no_log=True, |
| 759 |
), |
| 760 |
update_homebrew=dict( |
| 761 |
default=False, |
| 762 |
type="bool", |
| 763 |
), |
| 764 |
install_options=dict( |
| 765 |
aliases=["options"], |
| 766 |
type="list", |
| 767 |
elements="str", |
| 768 |
), |
| 769 |
accept_external_apps=dict( |
| 770 |
default=False, |
| 771 |
type="bool", |
| 772 |
), |
| 773 |
upgrade_all=dict( |
| 774 |
default=False, |
| 775 |
aliases=["upgrade"], |
| 776 |
type="bool", |
| 777 |
), |
| 778 |
greedy=dict( |
| 779 |
default=False, |
| 780 |
type="bool", |
| 781 |
), |
| 782 |
), |
| 783 |
supports_check_mode=True, |
| 784 |
) |
| 785 |
|
| 786 |
module.run_command_environ_update = dict(LANGUAGE="C", LC_ALL="C") |
| 787 |
|
| 788 |
p = module.params |
| 789 |
|
| 790 |
if p["name"]: |
| 791 |
casks = p["name"] |
| 792 |
else: |
| 793 |
casks = None |
| 794 |
|
| 795 |
path = p["path"] |
| 796 |
if path: |
| 797 |
path = path.split(":") |
| 798 |
|
| 799 |
state = p["state"] |
| 800 |
if state in ("present", "installed"): |
| 801 |
state = "installed" |
| 802 |
if state in ("latest", "upgraded"): |
| 803 |
state = "upgraded" |
| 804 |
if state in ("absent", "removed", "uninstalled"): |
| 805 |
state = "absent" |
| 806 |
|
| 807 |
sudo_password = p["sudo_password"] |
| 808 |
|
| 809 |
update_homebrew = p["update_homebrew"] |
| 810 |
upgrade_all = p["upgrade_all"] |
| 811 |
greedy = p["greedy"] |
| 812 |
p["install_options"] = p["install_options"] or [] |
| 813 |
install_options = [f"--{install_option}" for install_option in p["install_options"]] |
| 814 |
|
| 815 |
accept_external_apps = p["accept_external_apps"] |
| 816 |
|
| 817 |
brew_cask = HomebrewCask( |
| 818 |
module=module, |
| 819 |
path=path, |
| 820 |
casks=casks, |
| 821 |
state=state, |
| 822 |
sudo_password=sudo_password, |
| 823 |
update_homebrew=update_homebrew, |
| 824 |
install_options=install_options, |
| 825 |
accept_external_apps=accept_external_apps, |
| 826 |
upgrade_all=upgrade_all, |
| 827 |
greedy=greedy, |
| 828 |
) |
| 829 |
(failed, changed, message) = brew_cask.run() |
| 830 |
if failed: |
| 831 |
module.fail_json(msg=message) |
| 832 |
else: |
| 833 |
module.exit_json(changed=changed, msg=message) |
| 834 |
|
| 835 |
|
| 836 |
if __name__ == "__main__": |
| 837 |
main() |