Development
This commit is contained in:
parent
b99ea35f79
commit
5149e5a035
10 changed files with 197 additions and 213 deletions
|
|
@ -120,6 +120,9 @@ SYSTEMD_DIR = Path("/etc/systemd/system")
|
|||
BLIST_TIMER_NAME = f"{PRODUCT_NAME}-dns-blocklist-update"
|
||||
BLIST_TIMER_FILE = SYSTEMD_DIR / f"{BLIST_TIMER_NAME}.timer"
|
||||
BLIST_TIMER_SVC_FILE = SYSTEMD_DIR / f"{BLIST_TIMER_NAME}.service"
|
||||
DDNS_TIMER_NAME = f"{PRODUCT_NAME}-ddns-update"
|
||||
DDNS_TIMER_FILE = SYSTEMD_DIR / f"{DDNS_TIMER_NAME}.timer"
|
||||
DDNS_TIMER_SVC_FILE = SYSTEMD_DIR / f"{DDNS_TIMER_NAME}.service"
|
||||
DASHB_TIMER_NAME = f"{PRODUCT_NAME}-dashboard-queue"
|
||||
DASHB_TIMER_FILE = SYSTEMD_DIR / f"{DASHB_TIMER_NAME}.timer"
|
||||
DASHB_TIMER_SVC_FILE = SYSTEMD_DIR / f"{DASHB_TIMER_NAME}.service"
|
||||
|
|
@ -1300,6 +1303,69 @@ def _remove_timers(names, timer_files, svc_files, daemon_reload=False):
|
|||
if daemon_reload:
|
||||
subprocess.run(["systemctl", "daemon-reload"], capture_output=True, text=True)
|
||||
|
||||
|
||||
def _parse_ddns_interval(interval_str):
|
||||
"""Convert interval string (e.g. 5m, 2h, 1d) to systemd OnUnitActiveSec value."""
|
||||
s = interval_str.strip()
|
||||
if s.endswith("m"): return f"{s[:-1]}min"
|
||||
if s.endswith("h"): return f"{s[:-1]}h"
|
||||
if s.endswith("d"): return f"{s[:-1]}day"
|
||||
raise ValueError(f"Invalid timer_interval format: '{s}'. Use e.g. 5m, 2h, 1d.")
|
||||
|
||||
|
||||
def install_ddns_timer(data):
|
||||
ddns = data.get("ddns", {})
|
||||
interval = ddns.get("general", {}).get("timer_interval", "10m")
|
||||
script_path = SCRIPT_DIR / "ddns.py"
|
||||
try:
|
||||
systemd_unit = _parse_ddns_interval(interval)
|
||||
except ValueError as e:
|
||||
print(f"DDNS timer: {e}")
|
||||
return
|
||||
|
||||
service_content = "\n".join([
|
||||
"# Generated by core.py -- do not edit manually.",
|
||||
"",
|
||||
"[Unit]",
|
||||
"Description=DDNS IP update",
|
||||
"After=network-online.target",
|
||||
"Wants=network-online.target",
|
||||
"",
|
||||
"[Service]",
|
||||
"Type=oneshot",
|
||||
f"ExecStart=/usr/bin/python3 {script_path} --apply",
|
||||
"",
|
||||
])
|
||||
timer_content = "\n".join([
|
||||
"# Generated by core.py -- do not edit manually.",
|
||||
"",
|
||||
"[Unit]",
|
||||
"Description=DDNS IP update timer",
|
||||
"",
|
||||
"[Timer]",
|
||||
f"OnActiveSec={systemd_unit}",
|
||||
f"OnUnitActiveSec={systemd_unit}",
|
||||
"OnBootSec=1min",
|
||||
"AccuracySec=10s",
|
||||
"",
|
||||
"[Install]",
|
||||
"WantedBy=timers.target",
|
||||
"",
|
||||
])
|
||||
for path, content in ((DDNS_TIMER_SVC_FILE, service_content), (DDNS_TIMER_FILE, timer_content)):
|
||||
if not path.exists() or path.read_text() != content:
|
||||
path.write_text(content)
|
||||
print(f"Written: {path}")
|
||||
subprocess.run(["systemctl", "daemon-reload"], capture_output=True, text=True)
|
||||
active = subprocess.run(
|
||||
["systemctl", "is-active", f"{DDNS_TIMER_NAME}.timer"],
|
||||
capture_output=True, text=True
|
||||
).stdout.strip() == "active"
|
||||
verb = "restart" if active else "enable --now"
|
||||
subprocess.run(["systemctl"] + verb.split() + [f"{DDNS_TIMER_NAME}.timer"],
|
||||
capture_output=True, text=True)
|
||||
print(f"Timer {DDNS_TIMER_NAME}.timer enabled (runs every {interval}).")
|
||||
|
||||
# ===================================================================
|
||||
# banned_ips expansion
|
||||
# ===================================================================
|
||||
|
|
@ -2484,9 +2550,9 @@ def show_metrics(data):
|
|||
def stop_instances(data):
|
||||
"""Remove timers and stop all per-VLAN instances (config files preserved)."""
|
||||
_remove_timers(
|
||||
names=[BLIST_TIMER_NAME, DASHB_TIMER_NAME, STATUS_TIMER_NAME],
|
||||
timer_files=[BLIST_TIMER_FILE, DASHB_TIMER_FILE, STATUS_TIMER_FILE],
|
||||
svc_files=[BLIST_TIMER_SVC_FILE, DASHB_TIMER_SVC_FILE, STATUS_TIMER_SVC_FILE],
|
||||
names=[BLIST_TIMER_NAME, DASHB_TIMER_NAME, STATUS_TIMER_NAME, DDNS_TIMER_NAME],
|
||||
timer_files=[BLIST_TIMER_FILE, DASHB_TIMER_FILE, STATUS_TIMER_FILE, DDNS_TIMER_FILE],
|
||||
svc_files=[BLIST_TIMER_SVC_FILE, DASHB_TIMER_SVC_FILE, STATUS_TIMER_SVC_FILE, DDNS_TIMER_SVC_FILE],
|
||||
daemon_reload=True,
|
||||
)
|
||||
print()
|
||||
|
|
@ -3097,6 +3163,15 @@ def cmd_apply(data, dry_run=False):
|
|||
_install_interval_timers(t_names, t_files, s_files, t_descs, t_execs, t_intervals)
|
||||
print()
|
||||
|
||||
print("DDNS timer ==========================================================")
|
||||
enabled_ddns = [p for p in data.get("ddns", {}).get("providers", []) if p.get("enabled")]
|
||||
if enabled_ddns:
|
||||
install_ddns_timer(data)
|
||||
else:
|
||||
_remove_timers([DDNS_TIMER_NAME], [DDNS_TIMER_FILE], [DDNS_TIMER_SVC_FILE])
|
||||
print("No enabled DDNS providers — timer not installed.")
|
||||
print()
|
||||
|
||||
print("Boot service ========================================================")
|
||||
install_nat_service()
|
||||
print()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue