Files
computer-playbook/cli/contributing/network/diagnose/report.py
T
kevinveenbirkenbachandClaude Opus 5 eda9474e37 test(lint): refuse a default in os.environ.get, and resolve the 25 sites
The deploy decides what ADAPTER_PORT or OPENWEBUI_ADMIN_NAME is and
writes it into the environment. A default in the reader is a second place
that value lives, and it is the one nobody greps for. os.environ["X"]
fails loudly on a missing key, which is the behaviour a misconfiguration
deserves.

Empty values stay free, because they add no second source and only say
absent: "", None, False, 0, the empty containers, and the "[]"/"{}" a
json.loads reader hands an absent variable. A default that is itself an
environ.get is free too, since it reads the one source again under
another key; a literal ending such a chain is still reported. Detection
runs on the parsed tree, so a call split across lines reads the same.

Five sites were real duplication and lost their default: the adapter
port, openwebui's admin name (a role that runs as a bot user), odoo's
OIDC uid field, seaweedfs' public-primary flag, and the swarm hosts
entry IP. Nineteen carry a nocheck naming why the value is not the
deploy's: contributor tooling whose default is the only source, test
scaffolding that writes the file it names, and two readers of
CA_TRUST_CERT_EXTRA_HOST, which nothing in the repo sets.

utils/diagnostics/container.py resolves without a marker: RESCUE_DEPTH is
a recursion base case, and the integer 0 says what the string "0" said.
The file sits exactly at the 500-line cap, so a comment line was not
available.

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

116 lines
3.7 KiB
Python

"""Per-system info dumps: identity, interfaces+MTU, /etc/resolv.conf, /etc/hosts, proxies, CA bundles."""
from __future__ import annotations
import os
import socket
import sys
import time
from pathlib import Path
from cli.contributing.network.diagnose.config import (
CA_BUNDLE_CANDIDATES,
PROXY_ENV_KEYS,
)
from cli.contributing.network.diagnose.format import cmd_capture, line, section
def show_identity() -> None:
section("identity")
line("timestamp", time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()))
line("hostname", socket.gethostname())
line("fqdn", socket.getfqdn())
line("python", sys.version.split()[0])
def show_resolv() -> None:
section("/etc/resolv.conf")
try:
content = Path("/etc/resolv.conf").read_text() # nocheck: cache-read
except OSError as e:
print(f" unreadable: {e}")
return
print(content.rstrip())
def show_hosts() -> None:
section("/etc/hosts")
try:
content = Path("/etc/hosts").read_text() # nocheck: cache-read
except OSError as e:
print(f" unreadable: {e}")
return
print(content.rstrip())
def show_iface_routes() -> None:
section("interfaces + MTU")
sys_net = Path("/sys/class/net")
if sys_net.is_dir():
for iface in sorted(p.name for p in sys_net.iterdir()):
mtu_path = sys_net / iface / "mtu"
try:
mtu = mtu_path.read_text().strip() # nocheck: cache-read
except OSError:
mtu = "?"
line(iface, f"mtu={mtu}")
rc, out = cmd_capture(["ip", "route"])
if rc == 0 and out.strip():
print("\n>>> ip route")
print(out.rstrip())
return
proc_route = Path("/proc/net/route")
if not proc_route.is_file():
return
print("\n>>> /proc/net/route (default route only)")
route_text = proc_route.read_text() # nocheck: cache-read
for raw in route_text.splitlines()[1:]:
parts = raw.split()
if len(parts) >= 8 and parts[1] == "00000000":
gw_hex = parts[2]
gw_ip = ".".join(str(int(gw_hex[i : i + 2], 16)) for i in (6, 4, 2, 0))
print(f" default via {gw_ip} dev {parts[0]}")
return
def show_proxies() -> None:
section("proxy env vars")
for key in PROXY_ENV_KEYS:
# nocheck: environ-get-default the placeholder is printed, never used as a value, and it keeps an absent proxy variable distinguishable from one set to the empty string
line(key, os.environ.get(key, "<unset>"))
def show_ca_bundle() -> None:
section("CA bundle summary")
for candidate in CA_BUNDLE_CANDIDATES:
p = Path(candidate)
if not p.is_file():
line(candidate, "<missing>")
continue
try:
size = p.stat().st_size
content = p.read_text(errors="ignore") # nocheck: cache-read
count = content.count("BEGIN CERTIFICATE")
line(candidate, f"{size}B, {count} certs")
except OSError as e:
line(candidate, f"unreadable: {e}")
def has_ipv6_default_route() -> bool:
"""True when an IPv6 default route exists on a non-loopback interface."""
rc, out = cmd_capture(["ip", "-6", "route", "show", "default"])
if rc == 0:
for raw in out.splitlines():
if "default" in raw and " dev lo" not in raw:
return True
try:
v6_text = Path("/proc/net/ipv6_route").read_text() # nocheck: cache-read
for raw in v6_text.splitlines():
parts = raw.split()
if len(parts) >= 10 and parts[0] == "0" * 32 and parts[-1] != "lo":
return True
except OSError:
pass
return False