Files
computer-playbook/cli/contributing/network/diagnose/tools.py
T
kevinveenbirkenbachandClaude Opus 4.7 e4e861c261 refactor(cli): move ports/networks suggesters under cli/contributing/network and add diagnose subcommand
Relocates `cli/meta/{ports,networks/suggest}` to
`cli/contributing/network/{ports,address/suggest}` so port and subnet
allocators live alongside the rest of the contributor-side tooling
(mirror, update, changelog) instead of `meta`. Mirrors the tests
into `tests/unit/cli/contributing/network/` and renames the contract
docs to `docs/contributing/tools/network/{port,address}.md`. Prog
names update from `cli meta ports suggest` / `cli meta networks
suggest` to `cli contributing network ports suggest` / `cli
contributing network address suggest`; the corresponding lint
references and `utils.meta.scan` docstring follow.

Adds a third subcommand: `cli contributing network diagnose`
(stdlib-only DNS / TCP / TLS / PMTU probe against the standard
infrastructure hosts plus interface MTU, `/etc/resolv.conf`,
`/etc/hosts`, proxy env, CA-bundle summaries). Split across `cli.py`
(orchestration), `config.py` (SPOT for hosts / per-distro install
commands), `format.py` (output + subprocess helpers), `tools.py`
(distro-aware auto-install of `iputils-ping` + `iproute2`),
`report.py` (system-info dumps + IPv6-default-route detection), and
`probes.py` (per-host probes). Exposed as `make debug-network`.

Companion `docs/contributing/actions/debugging/network.md` absorbs
the diagnose-tool walkthrough (incl. how to read MTU-induced TLS
handshake timeouts) so the merged page covers DNS / MTU / IPv6 /
connectivity end-to-end.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 14:55:10 +02:00

65 lines
1.9 KiB
Python

"""Detect the host distro and self-install the binaries the diagnose probes need."""
from __future__ import annotations
import os
import shutil
import sys
from pathlib import Path
from cli.contributing.network.diagnose.config import INSTALL_CMDS, TOOLS
from cli.contributing.network.diagnose.format import cmd_capture
def detect_distro_id(*, os_release_path: str = "/etc/os-release") -> str:
"""Return the distro id from /etc/os-release, lowercased; empty string when unreadable."""
try:
text = Path(os_release_path).read_text() # nocheck: cache-read
for raw in text.splitlines():
if raw.startswith("ID="):
return raw.split("=", 1)[1].strip().strip('"').lower()
except OSError:
pass
return ""
def missing_tools() -> list[str]:
return [t for t in TOOLS if shutil.which(t) is None]
def ensure_tools() -> None:
"""Install the missing TOOLS via the distro's package manager, when possible."""
missing = missing_tools()
if not missing:
return
distro = detect_distro_id()
cmd = INSTALL_CMDS.get(distro)
if cmd is None:
print(
f" [tool-install] missing {missing} but distro={distro or '?'} unsupported; skipping",
file=sys.stderr,
)
return
if distro in ("debian", "ubuntu"):
rc_upd, _ = cmd_capture(
["apt-get", "update", "-o", "DPkg::Lock::Timeout=120"], timeout=180.0
)
if rc_upd != 0:
print(
f" [tool-install] apt-get update rc={rc_upd}; trying install anyway",
file=sys.stderr,
)
if os.geteuid() != 0 and shutil.which("sudo"):
cmd = ["sudo", *cmd]
print(
f" [tool-install] installing {' '.join(cmd[-2:])} via {distro}",
file=sys.stderr,
)
rc, out = cmd_capture(cmd, timeout=180.0)
if rc != 0:
print(f" [tool-install] FAILED rc={rc}: {out.strip()[:200]}", file=sys.stderr)