faa076370d
- move state collectors from lib/state.py to daemon/collectors/ (7 modules, registration side-effect; daemon/server.py imports the package before the first populate()) - webui/api: new daemon_route() decorator factory in common.py collapses the try/except daemon-proxy boilerplate in all 8 blueprints (rules/params/body/transform keep responses identical) - firewall: interface-coverage invariant — config is the source of truth for zone interfaces (absent key = empty, no hands-off zones); pure validate_coverage() enforced at save (400) and apply (409, force: true overrides), top-level `unmanaged` exemption - lib: get_config() reads are now pure (no dir creation or writes); new lib/bootstrap.py creates runtime dirs and persists the one-shot nginx legacy migration at daemon start, after system_import (lib.nginx.migrate_config_file) - lib/common: compute_pending() apply-bookkeeping helper - daemon: emit_and_refresh() handler helper; refresh_state(bump=) so /status/refresh no longer bumps versions (poll/mutation only) - acme: move --log last so acme.sh never treats a real arg as the log-file argument - docs: AGENTS.md, config.md, state-model.md, api.md updated; HARDEN.md dropped (plan implemented); apply-confirm force wording Tests: 917 passed; ruff check + format clean.
68 lines
1.7 KiB
Python
68 lines
1.7 KiB
Python
"""Networkd state collector."""
|
|
|
|
from typing import Any
|
|
|
|
from lib import schema
|
|
from lib.common import compute_pending, run, strip_apply_meta
|
|
from lib.network import get_config, parse_networkctl_status
|
|
from lib.state import _now_iso, register_collector, register_volatile
|
|
|
|
|
|
def _collect_networkd() -> schema.NetworkdState:
|
|
"""Collect networkd interface state from networkctl.
|
|
|
|
Returns:
|
|
Dict with interface runtime state parsed from networkctl output,
|
|
config, and pending changes status.
|
|
"""
|
|
# Load config
|
|
try:
|
|
net_cfg = get_config()
|
|
except Exception:
|
|
net_cfg = {}
|
|
|
|
pending_changes, net_pending_diff = compute_pending(net_cfg)
|
|
|
|
result: dict[str, dict[str, Any]] = {}
|
|
safe_net_cfg = strip_apply_meta(net_cfg)
|
|
net_status: dict[str, Any] = {
|
|
"pending_changes": pending_changes,
|
|
"pending_diff": net_pending_diff,
|
|
}
|
|
|
|
try:
|
|
raw = run(["networkctl", "status", "--json=short", "--all"], sudo=True)
|
|
result = parse_networkctl_status(raw)
|
|
if not result:
|
|
return {
|
|
"interfaces": {},
|
|
"config": safe_net_cfg,
|
|
"status": net_status,
|
|
"timestamp": _now_iso(),
|
|
}
|
|
except Exception:
|
|
return {
|
|
"interfaces": {},
|
|
"config": safe_net_cfg,
|
|
"status": net_status,
|
|
"timestamp": _now_iso(),
|
|
}
|
|
|
|
return {
|
|
"interfaces": result,
|
|
"config": safe_net_cfg,
|
|
"status": net_status,
|
|
"timestamp": _now_iso(),
|
|
}
|
|
|
|
|
|
register_collector("networkd", _collect_networkd)
|
|
register_volatile(
|
|
"networkd",
|
|
frozenset(
|
|
{
|
|
"interfaces[].addresses",
|
|
}
|
|
),
|
|
)
|