Files
computer-playbook/utils/install/system_pkg.py
T
kevinveenbirkenbachandClaude Opus 4.8 90b3845998 feat(readme): role README generator with Cosmos diagrams, Quick Setup, and lint
Add cli/build/readme: derives each role README's managed sections from
templates/roles/README.md.j2.tmpl (the Cosmos mermaid diagram from
meta/services.yml, an invokable-gated Quick Setup, and Credits).

The Cosmos diagram shows dependencies, every declared service, and
dependents (roles that consume this one, matched by role name and by
provides provision-name in both directions). Role nodes carry deploy-mode
emoji markers (compose/swarm/host); disabled and shared services get a
stop mark; dependents are capped at 12.

test_readme enforces the Quick Setup body is byte-exact with the template
render. `make readme-generate` and `make cosmos` regenerate; `make cosmos`
also runs inside `make quality`.

Refactor: utils/symbol_glossary.py is the single source of truth for the
word/emoji mapping (unique project-wide), consumed by the complexity
matrix and the Cosmos builder. utils/roles/deploy.py holds role_has_stack
and role_deploy_modes, shared by both so the logic never drifts.

Add `make lint-mermaid` and its CI workflow, rendering every diagram via
mermaid-cli; the render step provisions chrome-headless-shell and unzip
on demand so it works in the minimal container.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-17 14:44:33 +02:00

121 lines
4.0 KiB
Python

"""System-package-manager dispatch (pacman / apt-get / dnf / yum / brew)."""
from __future__ import annotations
import contextlib
import shutil
import subprocess
from utils.install.primitives import log, run_privileged, warn
_SUPPORTED = ("pacman", "apt-get", "dnf", "yum", "brew")
def detect_package_manager() -> str:
for manager in _SUPPORTED:
if shutil.which(manager) is not None:
return manager
raise RuntimeError("No supported package manager found")
def _prepare_manager(manager: str) -> None:
if manager == "apt-get":
run_privileged(["apt-get", "-o", "DPkg::Lock::Timeout=600", "update"])
elif manager == "dnf":
with contextlib.suppress(subprocess.CalledProcessError):
run_privileged(["dnf", "-y", "install", "dnf-plugins-core"])
with contextlib.suppress(subprocess.CalledProcessError):
run_privileged(["dnf", "-y", "install", "epel-release"])
elif manager == "yum":
with contextlib.suppress(subprocess.CalledProcessError):
run_privileged(["yum", "-y", "install", "yum-utils"])
with contextlib.suppress(subprocess.CalledProcessError):
run_privileged(["yum", "-y", "install", "epel-release"])
def _install_one(manager: str, package: str) -> bool:
log(f"Installing package '{package}' via {manager}")
try:
if manager == "pacman":
run_privileged(["pacman", "-Syu", "--noconfirm", "--needed", package])
elif manager == "apt-get":
run_privileged(
[
"apt-get",
"-o",
"DPkg::Lock::Timeout=600",
"install",
"-y",
"--no-install-recommends",
package,
]
)
elif manager == "dnf":
run_privileged(["dnf", "-y", "install", package])
elif manager == "yum":
run_privileged(["yum", "-y", "install", package])
elif manager == "brew":
subprocess.run(["brew", "install", package], check=True)
else:
warn(f"Unsupported package manager: {manager}")
return False
except subprocess.CalledProcessError:
return False
return True
def install_package_candidates(manager: str, packages: list[str]) -> None:
_prepare_manager(manager)
for package in packages:
if _install_one(manager, package):
return
raise RuntimeError(f"All package candidates failed via {manager}: {packages}")
_COMMAND_PACKAGES: dict[str, dict[str, list[str]]] = {
"ansible-playbook": {
"pacman": ["ansible-core", "ansible"],
"apt-get": ["ansible-core", "ansible"],
"dnf": ["ansible-core", "ansible"],
"yum": ["ansible-core", "ansible"],
"brew": ["ansible"],
},
"ansible-galaxy": {
"pacman": ["ansible-core", "ansible"],
"apt-get": ["ansible-core", "ansible"],
"dnf": ["ansible-core", "ansible"],
"yum": ["ansible-core", "ansible"],
"brew": ["ansible"],
},
"ruff": {m: ["ruff"] for m in _SUPPORTED},
"shfmt": {m: ["shfmt"] for m in _SUPPORTED},
"shellcheck": {m: ["shellcheck"] for m in _SUPPORTED},
"unzip": {m: ["unzip"] for m in _SUPPORTED},
"npm": {
"pacman": ["npm", "nodejs"],
"apt-get": ["npm", "nodejs"],
"dnf": ["npm", "nodejs"],
"yum": ["npm", "nodejs"],
"brew": ["node"],
},
}
def install_command_via_pkg(command_name: str) -> None:
manager = detect_package_manager()
mapping = _COMMAND_PACKAGES.get(command_name)
if mapping is None or manager not in mapping:
raise RuntimeError(
f"No installer mapping defined for '{command_name}' on '{manager}'."
)
log(f"Missing command '{command_name}'. Attempting installation via {manager}.")
install_package_candidates(manager, mapping[manager])
__all__ = [
"detect_package_manager",
"install_command_via_pkg",
"install_package_candidates",
]