-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinprovider_ansible.py
More file actions
executable file
·477 lines (425 loc) · 16 KB
/
binprovider_ansible.py
File metadata and controls
executable file
·477 lines (425 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/usr/bin/env python
__package__ = "abxpkg"
import os
import sys
import json
import shutil
import logging as py_logging
import subprocess
import tempfile
from pathlib import Path
from typing import Any
from .binary import Binary
from .base_types import BinProviderName, PATHStr, BinName, InstallArgs
from .semver import SemVer
from .shallowbinary import ShallowBinary
from .binprovider import (
BinProvider,
EnvProvider,
OPERATING_SYSTEM,
DEFAULT_PATH,
remap_kwargs,
)
from .logging import (
format_command,
format_subprocess_output,
get_logger,
log_subprocess_output,
)
from .config import apply_exec_env
logger = get_logger(__name__)
SYSTEM_TEMP_DIR = tempfile.gettempdir()
# Installer modules that require the ``apt_pkg`` Python extension on the
# target interpreter (see ansible.builtin.apt and the Debian/Ubuntu catchall
# auto-detect behavior of ansible.builtin.package).
_APT_INSTALLER_MODULES = frozenset(
{
"ansible.builtin.apt",
"apt",
},
)
def _interpreter_has_module(interpreter: str, module: str) -> bool:
try:
return (
subprocess.run(
[interpreter, "-c", f"import {module}"],
capture_output=True,
text=True,
timeout=5,
).returncode
== 0
)
except (OSError, subprocess.SubprocessError):
return False
def _pick_ansible_python_interpreter(installer_module: str) -> str:
"""Return a Python interpreter suitable for running ``installer_module``.
``sys.executable`` is preferred so ansible runs inside the active venv,
but some modules (notably ``ansible.builtin.apt``) require a native C
extension (``apt_pkg``) that ships with the Debian/Ubuntu system Python
via the ``python3-apt`` package and cannot be ``pip install``ed into an
arbitrary venv. When that is the case we fall back to the first system
Python on ``PATH`` that can ``import apt_pkg``.
"""
needs_apt_pkg = installer_module in _APT_INSTALLER_MODULES or (
installer_module == "ansible.builtin.package" and shutil.which("apt-get")
)
if not needs_apt_pkg:
return sys.executable
if _interpreter_has_module(sys.executable, "apt_pkg"):
return sys.executable
candidates: list[str] = []
system_python = shutil.which("python3")
if system_python:
candidates.append(system_python)
for minor in (13, 12, 11, 10):
candidate = shutil.which(f"python3.{minor}")
if candidate and candidate not in candidates:
candidates.append(candidate)
for candidate in candidates:
if candidate == sys.executable:
continue
if _interpreter_has_module(candidate, "apt_pkg"):
return candidate
return sys.executable
ANSIBLE_INSTALL_PLAYBOOK_TEMPLATE = """
---
- name: Install system packages
hosts: localhost
gather_facts: false
tasks:
- name: Install system packages
{installer_module}:
name: "{{{{item}}}}"
state: {state}
{module_extra_yaml}
loop: {pkg_names}
"""
def render_ansible_module_extra_yaml(
module_extra_kwargs: dict[str, Any] | None = None,
) -> str:
if not module_extra_kwargs:
return ""
return "".join(
f" {key}: {json.dumps(value)}\n"
for key, value in module_extra_kwargs.items()
).rstrip("\n")
def get_homebrew_search_path() -> str | None:
brew_abspath = shutil.which("brew", path=DEFAULT_PATH) or shutil.which("brew")
if not brew_abspath:
return None
return str(Path(brew_abspath).parent)
def ansible_package_install(
pkg_names: str | InstallArgs,
ansible_playbook_abspath: str,
playbook_template=ANSIBLE_INSTALL_PLAYBOOK_TEMPLATE,
installer_module="auto",
state="present",
quiet=True,
module_extra_kwargs: dict[str, Any] | None = None,
timeout: int | None = None,
) -> str:
if isinstance(pkg_names, str):
pkg_names = pkg_names.split(" ")
else:
pkg_names = list(pkg_names)
if installer_module == "community.general.homebrew":
homebrew_path = get_homebrew_search_path()
module_extra_kwargs = {
**({"path": homebrew_path} if homebrew_path else {}),
**(module_extra_kwargs or {}),
}
module_extra_yaml = render_ansible_module_extra_yaml(module_extra_kwargs)
if installer_module == "auto":
if OPERATING_SYSTEM == "darwin":
# macOS: Use homebrew
resolved_installer_module = "community.general.homebrew"
else:
# Linux: Use Ansible catchall that autodetects apt/yum/pkg/nix/etc.
resolved_installer_module = "ansible.builtin.package"
else:
resolved_installer_module = installer_module
playbook = playbook_template.format(
pkg_names=pkg_names,
state=state,
installer_module=resolved_installer_module,
module_extra_yaml=module_extra_yaml,
)
temp_dir = Path(tempfile.mkdtemp(dir=SYSTEM_TEMP_DIR))
sudo_bin = None
try:
ansible_home = temp_dir / "tmp"
ansible_home.mkdir(exist_ok=True)
playbook_path = temp_dir / "install_playbook.yml"
playbook_path.write_text(playbook)
env = os.environ.copy()
env["ANSIBLE_INVENTORY_UNPARSED_WARNING"] = "False"
env["ANSIBLE_LOCALHOST_WARNING"] = "False"
env["ANSIBLE_HOME"] = str(ansible_home)
env["ANSIBLE_PYTHON_INTERPRETER"] = _pick_ansible_python_interpreter(
resolved_installer_module,
)
env["TMPDIR"] = SYSTEM_TEMP_DIR
apply_exec_env({"PATH": f"{Path(sys.executable).parent}:"}, env)
cmd = [
ansible_playbook_abspath,
"-i",
"localhost,",
"-c",
"local",
str(playbook_path),
]
proc = None
sudo_failure_output = None
if (
OPERATING_SYSTEM != "darwin"
and installer_module != "community.general.homebrew"
):
sudo_bin = shutil.which("sudo", path=env["PATH"]) or shutil.which("sudo")
if os.geteuid() != 0 and sudo_bin:
sudo_proc = subprocess.run(
[
sudo_bin,
"-n",
"--preserve-env=PATH,HOME,LOGNAME,USER,TMPDIR,ANSIBLE_INVENTORY_UNPARSED_WARNING,ANSIBLE_LOCALHOST_WARNING,ANSIBLE_HOME,ANSIBLE_PYTHON_INTERPRETER",
"--",
*cmd,
],
capture_output=True,
text=True,
cwd=temp_dir,
env=env,
timeout=timeout,
)
if sudo_proc.returncode == 0:
proc = sudo_proc
else:
log_subprocess_output(
logger,
"ansible sudo exec",
sudo_proc.stdout,
sudo_proc.stderr,
level=py_logging.DEBUG,
)
sudo_failure_output = format_subprocess_output(
sudo_proc.stdout,
sudo_proc.stderr,
)
if proc is None:
proc = subprocess.run(
cmd,
capture_output=True,
text=True,
cwd=temp_dir,
env=env,
timeout=timeout,
)
succeeded = proc.returncode == 0
result_text = f"Installing {pkg_names} on {OPERATING_SYSTEM} using Ansible {installer_module} {['failed', 'succeeded'][succeeded]}:{proc.stdout}\n{proc.stderr}".strip()
if sudo_failure_output and not succeeded:
result_text = (
f"{result_text}\n\nPrevious sudo attempt failed:\n{sudo_failure_output}"
)
if succeeded:
return result_text
if "Permission denied" in result_text:
raise PermissionError(
f"Installing {pkg_names} failed! Need to be root to use package manager (retry with sudo, or install manually)",
)
raise Exception(
f"Installing {pkg_names} failed! (retry with sudo, or install manually)\n{result_text}",
)
finally:
if os.geteuid() != 0 and sudo_bin:
chown_proc = subprocess.run(
[
sudo_bin,
"-n",
"chown",
"-R",
f"{os.geteuid()}:{os.getegid()}",
str(temp_dir),
],
capture_output=True,
text=True,
)
if chown_proc.returncode != 0:
log_subprocess_output(
logger,
"ansible sudo chown",
chown_proc.stdout,
chown_proc.stderr,
level=py_logging.DEBUG,
)
if temp_dir.exists():
logger.info("$ %s", format_command(["rm", "-rf", str(temp_dir)]))
shutil.rmtree(temp_dir, ignore_errors=True)
class AnsibleProvider(BinProvider):
name: BinProviderName = "ansible"
_log_emoji = "📘"
INSTALLER_BIN: BinName = "ansible"
PATH: PATHStr = os.environ.get(
"PATH",
DEFAULT_PATH,
) # Always ambient system PATH. Ansible has no bin_dir field of its own and never mutates PATH in setup().
def INSTALLER_BINARY(self, no_cache: bool = False) -> ShallowBinary:
if not no_cache and self._INSTALLER_BINARY and self._INSTALLER_BINARY.is_valid:
return self._INSTALLER_BINARY
loaded = self.load(bin_name="ansible-playbook", no_cache=no_cache)
if loaded and loaded.loaded_abspath:
from . import DEFAULT_PROVIDER_NAMES, PROVIDER_CLASS_BY_NAME
raw_provider_names = os.environ.get("ABXPKG_BINPROVIDERS")
selected_provider_names = (
[
provider_name.strip()
for provider_name in raw_provider_names.split(",")
]
if raw_provider_names
else list(DEFAULT_PROVIDER_NAMES)
)
dependency_providers = [
EnvProvider(install_root=None, bin_dir=None)
if provider_name == "env"
else PROVIDER_CLASS_BY_NAME[provider_name]()
for provider_name in selected_provider_names
if provider_name
and provider_name in PROVIDER_CLASS_BY_NAME
and provider_name != self.name
]
python_loaded = (
Binary(
name="python",
binproviders=dependency_providers,
).load(no_cache=no_cache)
if dependency_providers
else None
)
if (
python_loaded
and python_loaded.loaded_abspath
and python_loaded.loaded_version
and python_loaded.loaded_sha256
):
self.write_cached_binary(
"python",
python_loaded.loaded_abspath,
python_loaded.loaded_version,
python_loaded.loaded_sha256,
resolved_provider_name=(
python_loaded.loaded_binprovider.name
if python_loaded.loaded_binprovider is not None
else self.name
),
cache_kind="dependency",
)
self._INSTALLER_BINARY = loaded
return loaded
raise RuntimeError(
"Ansible is not installed! To fix:\n pip install ansible",
)
def get_ansible_module_extra_kwargs(self) -> dict[str, Any]:
"""Return provider-specific kwargs to splice into the ansible module block."""
return {}
@remap_kwargs({"packages": "install_args"})
def default_install_handler(
self,
bin_name: str,
install_args: InstallArgs | None = None,
postinstall_scripts: bool | None = None,
min_release_age: float | None = None,
min_version: SemVer | None = None,
no_cache: bool = False,
timeout: int | None = None,
) -> str:
install_args = install_args or self.get_install_args(bin_name)
ansible_playbook = self.INSTALLER_BINARY(no_cache=no_cache).loaded_abspath
assert ansible_playbook
module_extra_kwargs = self.get_ansible_module_extra_kwargs()
return ansible_package_install(
pkg_names=install_args,
ansible_playbook_abspath=str(ansible_playbook),
quiet=True,
playbook_template=ANSIBLE_INSTALL_PLAYBOOK_TEMPLATE,
installer_module="auto",
module_extra_kwargs=module_extra_kwargs or None,
timeout=timeout,
)
@remap_kwargs({"packages": "install_args"})
def default_update_handler(
self,
bin_name: str,
install_args: InstallArgs | None = None,
postinstall_scripts: bool | None = None,
min_release_age: float | None = None,
min_version: SemVer | None = None,
no_cache: bool = False,
timeout: int | None = None,
) -> str:
install_args = install_args or self.get_install_args(bin_name)
ansible_playbook = self.INSTALLER_BINARY(no_cache=no_cache).loaded_abspath
assert ansible_playbook
module_extra_kwargs = self.get_ansible_module_extra_kwargs()
if module_extra_kwargs:
return ansible_package_install(
pkg_names=install_args,
ansible_playbook_abspath=str(ansible_playbook),
quiet=True,
playbook_template=ANSIBLE_INSTALL_PLAYBOOK_TEMPLATE,
installer_module="auto",
state="latest",
module_extra_kwargs=module_extra_kwargs,
timeout=timeout,
)
return ansible_package_install(
pkg_names=install_args,
ansible_playbook_abspath=str(ansible_playbook),
quiet=True,
playbook_template=ANSIBLE_INSTALL_PLAYBOOK_TEMPLATE,
installer_module="auto",
state="latest",
timeout=timeout,
)
@remap_kwargs({"packages": "install_args"})
def default_uninstall_handler(
self,
bin_name: str,
install_args: InstallArgs | None = None,
postinstall_scripts: bool | None = None,
min_release_age: float | None = None,
min_version: SemVer | None = None,
no_cache: bool = False,
timeout: int | None = None,
) -> bool:
install_args = install_args or self.get_install_args(bin_name)
ansible_playbook = self.INSTALLER_BINARY(no_cache=no_cache).loaded_abspath
assert ansible_playbook
module_extra_kwargs = self.get_ansible_module_extra_kwargs()
if module_extra_kwargs:
ansible_package_install(
pkg_names=install_args,
ansible_playbook_abspath=str(ansible_playbook),
quiet=True,
playbook_template=ANSIBLE_INSTALL_PLAYBOOK_TEMPLATE,
installer_module="auto",
state="absent",
module_extra_kwargs=module_extra_kwargs,
timeout=timeout,
)
else:
ansible_package_install(
pkg_names=install_args,
ansible_playbook_abspath=str(ansible_playbook),
quiet=True,
playbook_template=ANSIBLE_INSTALL_PLAYBOOK_TEMPLATE,
installer_module="auto",
state="absent",
timeout=timeout,
)
return True
if __name__ == "__main__":
result = ansible = AnsibleProvider()
func = None
if len(sys.argv) > 1:
result = func = getattr(ansible, sys.argv[1]) # e.g. install
if len(sys.argv) > 2 and callable(func):
result = func(sys.argv[2]) # e.g. install ffmpeg
print(result)