dhcp: track pending config changes with hash, update UI button

This commit is contained in:
2026-06-28 16:26:32 +00:00
parent baa441fa13
commit 348bbfbca6
3 changed files with 59 additions and 16 deletions
+24 -6
View File
@@ -1,5 +1,6 @@
"""Dnsmasq daemon handler."""
import hashlib
import logging
from copy import deepcopy
from datetime import UTC, datetime
@@ -49,6 +50,20 @@ DEFAULT_CFG: dict[str, Any] = {
"dns": {"upstreams": ["8.8.8.8", "1.1.1.1"], "domain": None, "custom_records": []},
}
# Internal field for tracking applied config version
_APPLY_HASH_KEY = "_last_applied_hash"
def _config_hash(cfg: dict[str, Any]) -> str:
"""Compute a hash of the config, excluding the _last_applied_hash field.
Used to detect whether the JSON config has changed since the last apply.
"""
import json
clean = {k: v for k, v in cfg.items() if k != _APPLY_HASH_KEY}
return hashlib.sha256(json.dumps(clean, sort_keys=True).encode()).hexdigest()
def _get_state() -> dict[str, Any] | None:
"""Retrieve cached dnsmasq state from the state store."""
@@ -172,14 +187,17 @@ def apply_config(_request: Any, _body: Any) -> dict[str, Any]:
conf_text = _generate_conf(cfg)
ensure_dirs(CONFIG_DIR, DATA_DIR, FRAGMENTS_DIR)
run(["mkdir", "-p", "/etc/dnsmasq.d"], sudo=True)
run_proc(
["tee", DNSMASQ_CONF, "--"],
sudo=True,
check=True,
input=conf_text,
)
tmp = Path("/tmp") / "vacuum-wall-dnsmasq.tmp"
with open(tmp, "w") as f:
f.write(conf_text)
run(["cp", str(tmp), DNSMASQ_CONF], sudo=True)
tmp.unlink(missing_ok=True)
run(["systemctl", "reload", "dnsmasq"], sudo=True)
logger.info("dnsmasq config written and reloaded")
# Store the config hash so state collector can detect pending changes
cfg_after = _get_config()
cfg_after[_APPLY_HASH_KEY] = _config_hash(cfg_after)
_save_config(cfg_after)
refresh_state(["dnsmasq"])
return {"applied": True}