Development

This commit is contained in:
Matthew Grotke 2026-05-25 16:07:21 -04:00
parent b63aed53fc
commit 6221ee3691
12 changed files with 511 additions and 245 deletions

View file

@ -1,4 +1,4 @@
import json, subprocess, hashlib, os, uuid
import copy, json, subprocess, hashlib, os, uuid
from datetime import datetime, timezone
from flask import session
@ -11,13 +11,14 @@ DASHBOARD_DONE = f'{CONFIGS_DIR}/.dashboard-done'
DASHBOARD_LAST_RUN = f'{CONFIGS_DIR}/.dashboard-last-run'
DASHBOARD_LOCK = f'{CONFIGS_DIR}/.dashboard-lock'
DASHBOARD_PENDING = f'{CONFIGS_DIR}/.dashboard-pending'
SNAPSHOTS_DIR = f'{CONFIGS_DIR}/.snapshots'
HEALTH_FILE = f'{CONFIGS_DIR}/.health'
PRODUCT_NAME = os.environ.get('PRODUCT_NAME', 'routlin')
DASHB_TIMER_NAME = f'{PRODUCT_NAME}-dashboard-queue'
DDNS_TIMER_NAME = f'{PRODUCT_NAME}-ddns-update'
WEB_APP_DISPLAY_NAME = os.environ.get('WEB_APP_DISPLAY_NAME', f'{PRODUCT_NAME.capitalize()} Dashboard')
DASHB_INTERVAL_SECS = 60
QUEUE_MAX_LINES = 50
PRODUCT_NAME = os.environ.get('PRODUCT_NAME', 'routlin')
DASHB_TIMER_NAME = f'{PRODUCT_NAME}-dashboard-queue'
DDNS_TIMER_NAME = f'{PRODUCT_NAME}-ddns-update'
WEB_APP_DISPLAY_NAME = os.environ.get('WEB_APP_DISPLAY_NAME', f'{PRODUCT_NAME.capitalize()} Dashboard')
DASHB_INTERVAL_SECS = 60
QUEUE_MAX_LINES = 50
def load_core():
@ -70,7 +71,7 @@ def _read_pending(done_set):
parts = line.split(None, 2)
if len(parts) == 3:
entry_uuid, entry_ts, rest = parts
cmd_user = rest.rsplit(' (', 1)
cmd_user = rest.rsplit(' (', 1)
entry_cmd = cmd_user[0].strip('[]')
entry_user = cmd_user[1].rstrip(')') if len(cmd_user) == 2 else ''
if entry_uuid not in done_set:
@ -117,7 +118,7 @@ def _apply_changes_immediately():
def _read_dashboard_pending():
"""Return list of (uuid, ts, cmd, user, description) from .dashboard-pending."""
"""Return list of (uuid, ts, cmd, user) from .dashboard-pending."""
items = []
try:
lines = open(DASHBOARD_PENDING).read().splitlines()
@ -127,14 +128,13 @@ def _read_dashboard_pending():
if not line.strip():
continue
try:
main, _, desc = line.partition(' :: ')
parts = main.split(None, 2)
parts = line.split(None, 2)
if len(parts) == 3:
entry_uuid, entry_ts, rest = parts
cmd_user = rest.rsplit(' (', 1)
entry_cmd = cmd_user[0].strip('[]')
entry_user = cmd_user[1].rstrip(')') if len(cmd_user) == 2 else ''
items.append((entry_uuid, int(entry_ts), entry_cmd, entry_user, desc))
items.append((entry_uuid, int(entry_ts), entry_cmd, entry_user))
except Exception:
pass
return items
@ -152,7 +152,7 @@ def flush_pending_to_queue():
done_set = _load_done_set()
existing_ids = {uu for uu, *_ in _read_pending(done_set)}
with open(DASHBOARD_QUEUE, 'a') as f:
for entry_uuid, entry_ts, entry_cmd, entry_user, _desc in items:
for entry_uuid, entry_ts, entry_cmd, entry_user in items:
if entry_uuid not in existing_ids:
f.write(f'{entry_uuid} {entry_ts} [{entry_cmd}] ({entry_user})\n')
open(DASHBOARD_PENDING, 'w').close()
@ -177,7 +177,7 @@ def flush_selected_to_queue(selected_uuids):
done_set = _load_done_set()
existing_ids = {uu for uu, *_ in _read_pending(done_set)}
with open(DASHBOARD_QUEUE, 'a') as f:
for entry_uuid, entry_ts, entry_cmd, entry_user, _desc in items:
for entry_uuid, entry_ts, entry_cmd, entry_user in items:
if entry_uuid in selected_set and entry_uuid not in existing_ids:
f.write(f'{entry_uuid} {entry_ts} [{entry_cmd}] ({entry_user})\n')
_remove_pending_by_uuids(selected_set)
@ -190,38 +190,40 @@ def delete_pending_by_uuids(selected_uuids):
_remove_pending_by_uuids(set(selected_uuids))
def _queue_pending_command(cmd, description=''):
def _queue_pending_command(cmd):
"""Append cmd to .dashboard-pending if not already present for this cmd+user."""
existing = _read_dashboard_pending()
current_user = session.get('email_address', 'unknown')
for entry_uuid, entry_ts, entry_cmd, entry_user, _desc in existing:
for entry_uuid, entry_ts, entry_cmd, entry_user in existing:
if entry_cmd == cmd and entry_user == current_user:
return entry_uuid, entry_ts
entry_uuid = str(uuid.uuid4())
now = datetime.now()
entry_ts = int(now.timestamp())
desc_suffix = f' :: {description}' if description else ''
entry_ts = int(datetime.now().timestamp())
with open(DASHBOARD_PENDING, 'a') as f:
f.write(f'{entry_uuid} {entry_ts} [{cmd}] ({current_user}){desc_suffix}\n')
f.write(f'{entry_uuid} {entry_ts} [{cmd}] ({current_user})\n')
return entry_uuid, entry_ts
def _queue_command(cmd, description=''):
def _queue_pending_presigned(cmd, entry_uuid, entry_ts):
"""Write a pre-generated entry to .dashboard-pending without dedup."""
current_user = session.get('email_address', 'unknown')
with open(DASHBOARD_PENDING, 'a') as f:
f.write(f'{entry_uuid} {entry_ts} [{cmd}] ({current_user})\n')
def _queue_command(cmd):
if not _apply_changes_immediately():
return _queue_pending_command(cmd, description)
return _queue_pending_command(cmd)
done_set = _load_done_set()
pending = _read_pending(done_set)
pending = _read_pending(done_set)
current_user = session.get('email_address', 'unknown')
for entry_uuid, entry_ts, entry_cmd, entry_user in pending:
if entry_cmd == cmd and entry_user == current_user:
return entry_uuid, entry_ts
entry_uuid = str(uuid.uuid4())
now = datetime.now()
entry_ts = int(now.timestamp())
dt_str = now.strftime('%Y-%m-%dT%H:%M:%S')
user = session.get('email_address', 'unknown')
entry_ts = int(datetime.now().timestamp())
with open(DASHBOARD_QUEUE, 'a') as f:
f.write(f'{entry_uuid} {entry_ts} {dt_str} [{cmd}] ({user})\n')
f.write(f'{entry_uuid} {entry_ts} [{cmd}] ({current_user})\n')
_trim_if_needed()
return entry_uuid, entry_ts
@ -240,7 +242,7 @@ def _entry_ts_from_queue(entry_uuid):
def _seconds_until_next_run():
try:
last_run = float(open(DASHBOARD_LAST_RUN).read().strip())
elapsed = datetime.now(timezone.utc).timestamp() - last_run
elapsed = datetime.now(timezone.utc).timestamp() - last_run
return int(max(0, DASHB_INTERVAL_SECS - elapsed))
except Exception:
return None
@ -260,18 +262,7 @@ def _lock_mtime():
return None
def queue_command(cmd, description=''):
"""Queue a command without generating a flash message."""
return _queue_command(cmd, description)
def queued_msg(cmd=None, description='', action_label='Configuration saved'):
"""Queue cmd if given, then return a timing message.
Without cmd, just returns timing (for commands already queued by the caller).
action_label replaces the 'Configuration saved' prefix for non-save actions."""
entry_ts = None
if cmd is not None:
_entry_uuid, entry_ts = queue_command(cmd, description)
def _build_timing_msg(entry_ts, cmd, action_label='Configuration saved'):
if not _apply_changes_immediately():
return f'{action_label}. Click Apply Now on the Configuration Changes card to apply.'
if _is_locked():
@ -284,15 +275,115 @@ def queued_msg(cmd=None, description='', action_label='Configuration saved'):
return f'{action_label}. Changes will be applied {timing}.'
if cmd is None:
return f'{action_label}. The processing service is not running.'
parts = cmd.split()
parts = cmd.split()
cli_cmd = f'sudo python3 {parts[0]}.py --{parts[1]}' if len(parts) == 2 else cmd
install_cmd = f'sudo python3 install.py'
install_cmd = 'sudo python3 install.py'
from markupsafe import Markup
return Markup(f'{action_label}. The command processing service is not installed. '
f'Run <strong>{install_cmd}</strong> to enable it, '
f'or <strong>{cli_cmd}</strong> to apply manually.')
def queue_command(cmd, description=''):
"""Queue a command without generating a flash message. description is ignored (kept for compat)."""
return _queue_command(cmd)
def queued_msg(cmd=None, description='', action_label='Configuration saved'):
"""Queue cmd if given, then return a timing message. description is ignored."""
entry_ts = None
if cmd is not None:
_entry_uuid, entry_ts = queue_command(cmd)
return _build_timing_msg(entry_ts, cmd, action_label)
# ── Snapshot system ───────────────────────────────────────────────────────────
def _pending_uuid_set():
return {item[0] for item in _read_dashboard_pending()}
def _find_snapshot_dependencies(path, key):
"""Return UUIDs of still-pending snapshots that modified the same path+key."""
try:
pending = _pending_uuid_set()
deps = []
for fname in sorted(os.listdir(SNAPSHOTS_DIR)):
if not fname.endswith('.json'):
continue
try:
with open(os.path.join(SNAPSHOTS_DIR, fname)) as f:
snap = json.load(f)
if (snap.get('path') == path
and snap.get('key') == str(key)
and snap.get('uuid') in pending):
deps.append(snap['uuid'])
except Exception:
pass
return deps
except Exception:
return []
def load_snapshot_for_uuid(entry_uuid):
"""Return the snapshot dict for the given UUID, or None if not found."""
try:
for fname in os.listdir(SNAPSHOTS_DIR):
if fname.endswith(f'-{entry_uuid}.json'):
with open(os.path.join(SNAPSHOTS_DIR, fname)) as f:
return json.load(f)
except Exception:
pass
return None
def save_core_with_snapshot(new_core, path, key, operation, before, after,
description='', cmd='core apply', queue=True):
"""
Write a .snapshots/{ts}-{uuid}.json file, save new_core to disk, and
optionally create a pending queue entry. Returns a flash message string
when queue=True, otherwise None.
"""
entry_uuid = str(uuid.uuid4())
entry_ts = int(datetime.now().timestamp())
current_user = session.get('email_address', 'unknown')
depends_on = _find_snapshot_dependencies(path, key)
os.makedirs(SNAPSHOTS_DIR, exist_ok=True)
snapshot = {
'uuid': entry_uuid,
'ts': entry_ts,
'cmd': cmd,
'user': current_user,
'operation': operation,
'description': description,
'path': path,
'key': str(key),
'before': before,
'after': after,
'depends_on': depends_on,
}
with open(os.path.join(SNAPSHOTS_DIR, f'{entry_ts}-{entry_uuid}.json'), 'w') as f:
json.dump(snapshot, f, indent=2)
save_core(new_core)
if not queue:
return None
if _apply_changes_immediately():
with open(DASHBOARD_QUEUE, 'a') as f:
f.write(f'{entry_uuid} {entry_ts} [{cmd}] ({current_user})\n')
_trim_if_needed()
else:
_queue_pending_presigned(cmd, entry_uuid, entry_ts)
return _build_timing_msg(entry_ts, cmd)
# ── Misc ──────────────────────────────────────────────────────────────────────
def run_apply():
try:
subprocess.run(