| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2013, Daniel Jaouen <[email protected]> |
| 4 |
# Copyright (c) 2016, Indrajit Raychaudhuri <[email protected]> |
| 5 |
# |
| 6 |
# Based on homebrew (Andrew Dunham <[email protected]>) |
| 7 |
# |
| 8 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 9 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 10 |
|
| 11 |
from __future__ import annotations |
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
module: homebrew_tap |
| 15 |
author: |
| 16 |
- "Indrajit Raychaudhuri (@indrajitr)" |
| 17 |
- "Daniel Jaouen (@danieljaouen)" |
| 18 |
short_description: Tap a Homebrew repository |
| 19 |
description: |
| 20 |
- Tap external Homebrew repositories. |
| 21 |
extends_documentation_fragment: |
| 22 |
- community.general._attributes |
| 23 |
attributes: |
| 24 |
check_mode: |
| 25 |
support: full |
| 26 |
diff_mode: |
| 27 |
support: none |
| 28 |
options: |
| 29 |
name: |
| 30 |
description: |
| 31 |
- The GitHub user/organization repository to tap. |
| 32 |
required: true |
| 33 |
aliases: ['tap'] |
| 34 |
type: list |
| 35 |
elements: str |
| 36 |
url: |
| 37 |
description: |
| 38 |
- The optional git URL of the repository to tap. The URL is not assumed to be on GitHub, and the protocol does not have |
| 39 |
to be HTTP. Any location and protocol that git can handle is fine. |
| 40 |
- O(name) option may not be a list of multiple taps (but a single tap instead) when this option is provided. |
| 41 |
type: str |
| 42 |
state: |
| 43 |
description: |
| 44 |
- State of the repository. |
| 45 |
choices: ['present', 'absent'] |
| 46 |
default: 'present' |
| 47 |
type: str |
| 48 |
path: |
| 49 |
description: |
| 50 |
- A V(:) separated list of paths to search for C(brew) executable. |
| 51 |
default: '/usr/local/bin:/opt/homebrew/bin:/home/linuxbrew/.linuxbrew/bin' |
| 52 |
type: path |
| 53 |
version_added: '2.1.0' |
| 54 |
requirements: [homebrew] |
| 55 |
""" |
| 56 |
|
| 57 |
EXAMPLES = r""" |
| 58 |
- name: Tap a Homebrew repository, state present |
| 59 |
community.general.homebrew_tap: |
| 60 |
name: homebrew/dupes |
| 61 |
|
| 62 |
- name: Tap a Homebrew repository, state absent |
| 63 |
community.general.homebrew_tap: |
| 64 |
name: homebrew/dupes |
| 65 |
state: absent |
| 66 |
|
| 67 |
- name: Tap a Homebrew repository, state present |
| 68 |
community.general.homebrew_tap: |
| 69 |
name: homebrew/dupes,homebrew/science |
| 70 |
state: present |
| 71 |
|
| 72 |
- name: Tap a Homebrew repository using url, state present |
| 73 |
community.general.homebrew_tap: |
| 74 |
name: telemachus/brew |
| 75 |
url: 'https://bitbucket.org/telemachus/brew' |
| 76 |
""" |
| 77 |
|
| 78 |
import re |
| 79 |
|
| 80 |
from ansible.module_utils.basic import AnsibleModule |
| 81 |
|
| 82 |
|
| 83 |
def a_valid_tap(tap): |
| 84 |
"""Returns True if the tap is valid.""" |
| 85 |
regex = re.compile(r"^([\w-]+)/(homebrew-)?([\w-]+)$") |
| 86 |
return regex.match(tap) |
| 87 |
|
| 88 |
|
| 89 |
def already_tapped(module, brew_path, tap, taps=None): |
| 90 |
"""Returns True if already tapped.""" |
| 91 |
if taps is None: |
| 92 |
rc, out, err = module.run_command([brew_path, "tap"]) |
| 93 |
taps = [tap_.strip().lower() for tap_ in out.split("\n") if tap_] |
| 94 |
tap_name = re.sub("homebrew-", "", tap.lower()) |
| 95 |
return tap_name in taps |
| 96 |
|
| 97 |
|
| 98 |
def add_tap(module, brew_path, tap, url=None, taps=None): |
| 99 |
"""Adds a single tap.""" |
| 100 |
failed, changed, msg = False, False, "" |
| 101 |
|
| 102 |
if not a_valid_tap(tap): |
| 103 |
failed = True |
| 104 |
msg = f"not a valid tap: {tap}" |
| 105 |
|
| 106 |
elif not already_tapped(module, brew_path, tap, taps): |
| 107 |
if module.check_mode: |
| 108 |
module.exit_json(changed=True) |
| 109 |
|
| 110 |
cmd = [brew_path, "tap", tap] |
| 111 |
if url: |
| 112 |
cmd.append(url) |
| 113 |
rc, out, err = module.run_command(cmd) |
| 114 |
if rc == 0: |
| 115 |
changed = True |
| 116 |
msg = f"successfully tapped: {tap}" |
| 117 |
else: |
| 118 |
failed = True |
| 119 |
msg = f"failed to tap: {tap} due to {err}" |
| 120 |
|
| 121 |
else: |
| 122 |
msg = f"already tapped: {tap}" |
| 123 |
|
| 124 |
return (failed, changed, msg) |
| 125 |
|
| 126 |
|
| 127 |
def add_taps(module, brew_path, taps): |
| 128 |
"""Adds one or more taps.""" |
| 129 |
failed, changed, unchanged, added, msg = False, False, 0, 0, "" |
| 130 |
|
| 131 |
rc, out, err = module.run_command([brew_path, "tap"]) |
| 132 |
tapped = [t.strip().lower() for t in out.split("\n") if t] |
| 133 |
|
| 134 |
for tap in taps: |
| 135 |
(failed, changed, msg) = add_tap(module, brew_path, tap, taps=tapped) |
| 136 |
if failed: |
| 137 |
break |
| 138 |
if changed: |
| 139 |
added += 1 |
| 140 |
else: |
| 141 |
unchanged += 1 |
| 142 |
|
| 143 |
if failed: |
| 144 |
msg = f"added: {added}, unchanged: {unchanged}, error: {msg}" |
| 145 |
elif added: |
| 146 |
changed = True |
| 147 |
msg = f"added: {added}, unchanged: {unchanged}" |
| 148 |
else: |
| 149 |
msg = f"added: {added}, unchanged: {unchanged}" |
| 150 |
|
| 151 |
return (failed, changed, msg) |
| 152 |
|
| 153 |
|
| 154 |
def remove_tap(module, brew_path, tap, taps=None): |
| 155 |
"""Removes a single tap.""" |
| 156 |
failed, changed, msg = False, False, "" |
| 157 |
|
| 158 |
if not a_valid_tap(tap): |
| 159 |
failed = True |
| 160 |
msg = f"not a valid tap: {tap}" |
| 161 |
|
| 162 |
elif already_tapped(module, brew_path, tap, taps): |
| 163 |
if module.check_mode: |
| 164 |
module.exit_json(changed=True) |
| 165 |
|
| 166 |
rc, out, err = module.run_command([brew_path, "untap", tap]) |
| 167 |
if not already_tapped(module, brew_path, tap): |
| 168 |
changed = True |
| 169 |
msg = f"successfully untapped: {tap}" |
| 170 |
else: |
| 171 |
failed = True |
| 172 |
msg = f"failed to untap: {tap} due to {err}" |
| 173 |
|
| 174 |
else: |
| 175 |
msg = f"already untapped: {tap}" |
| 176 |
|
| 177 |
return (failed, changed, msg) |
| 178 |
|
| 179 |
|
| 180 |
def remove_taps(module, brew_path, taps): |
| 181 |
"""Removes one or more taps.""" |
| 182 |
failed, changed, unchanged, removed, msg = False, False, 0, 0, "" |
| 183 |
|
| 184 |
rc, out, err = module.run_command([brew_path, "tap"]) |
| 185 |
tapped = [t.strip().lower() for t in out.split("\n") if t] |
| 186 |
|
| 187 |
for tap in taps: |
| 188 |
(failed, changed, msg) = remove_tap(module, brew_path, tap, taps=tapped) |
| 189 |
if failed: |
| 190 |
break |
| 191 |
if changed: |
| 192 |
removed += 1 |
| 193 |
else: |
| 194 |
unchanged += 1 |
| 195 |
|
| 196 |
if failed: |
| 197 |
msg = f"removed: {removed}, unchanged: {unchanged}, error: {msg}" |
| 198 |
elif removed: |
| 199 |
changed = True |
| 200 |
msg = f"removed: {removed}, unchanged: {unchanged}" |
| 201 |
else: |
| 202 |
msg = f"removed: {removed}, unchanged: {unchanged}" |
| 203 |
|
| 204 |
return (failed, changed, msg) |
| 205 |
|
| 206 |
|
| 207 |
def main(): |
| 208 |
module = AnsibleModule( |
| 209 |
argument_spec=dict( |
| 210 |
name=dict(aliases=["tap"], type="list", required=True, elements="str"), |
| 211 |
url=dict(), |
| 212 |
state=dict(default="present", choices=["present", "absent"]), |
| 213 |
path=dict( |
| 214 |
default="/usr/local/bin:/opt/homebrew/bin:/home/linuxbrew/.linuxbrew/bin", |
| 215 |
type="path", |
| 216 |
), |
| 217 |
), |
| 218 |
supports_check_mode=True, |
| 219 |
) |
| 220 |
|
| 221 |
path = module.params["path"] |
| 222 |
if path: |
| 223 |
path = path.split(":") |
| 224 |
|
| 225 |
brew_path = module.get_bin_path( |
| 226 |
"brew", |
| 227 |
required=True, |
| 228 |
opt_dirs=path, |
| 229 |
) |
| 230 |
|
| 231 |
taps = module.params["name"] |
| 232 |
url = module.params["url"] |
| 233 |
|
| 234 |
if module.params["state"] == "present": |
| 235 |
if url is None: |
| 236 |
# No tap URL provided explicitly, continue with bulk addition |
| 237 |
# of all the taps. |
| 238 |
failed, changed, msg = add_taps(module, brew_path, taps) |
| 239 |
else: |
| 240 |
# When an tap URL is provided explicitly, we allow adding |
| 241 |
# *single* tap only. Validate and proceed to add single tap. |
| 242 |
if len(taps) > 1: |
| 243 |
msg = "List of multiple taps may not be provided with 'url' option." |
| 244 |
module.fail_json(msg=msg) |
| 245 |
else: |
| 246 |
failed, changed, msg = add_tap(module, brew_path, taps[0], url) |
| 247 |
|
| 248 |
if failed: |
| 249 |
module.fail_json(msg=msg) |
| 250 |
else: |
| 251 |
module.exit_json(changed=changed, msg=msg) |
| 252 |
|
| 253 |
elif module.params["state"] == "absent": |
| 254 |
failed, changed, msg = remove_taps(module, brew_path, taps) |
| 255 |
|
| 256 |
if failed: |
| 257 |
module.fail_json(msg=msg) |
| 258 |
else: |
| 259 |
module.exit_json(changed=changed, msg=msg) |
| 260 |
|
| 261 |
|
| 262 |
if __name__ == "__main__": |
| 263 |
main() |