Development

This commit is contained in:
Matthew Grotke 2026-05-22 01:09:23 -04:00
parent cc2f57aa83
commit 74166f03bd
11 changed files with 986 additions and 61 deletions

View file

@ -14,6 +14,7 @@ def apply_general():
log_errors_only = 'log_errors_only' in request.form
dnsmasq_log_queries = 'dnsmasq_log_queries' in request.form
daily_execute_time = sanitize.time_24h(request.form.get('daily_execute_time_24hr_local', ''))
apply_on_save = 'apply_on_save' in request.form
log_max_kb = validate.int_range(log_max_kb_raw, 64, None)
if log_max_kb is None:
@ -30,6 +31,7 @@ def apply_general():
'log_errors_only': log_errors_only,
'dnsmasq_log_queries': dnsmasq_log_queries,
'daily_execute_time_24hr_local': daily_execute_time,
'apply_on_save': apply_on_save,
})
errors = validate.validate_config(core)
if errors:

View file

@ -0,0 +1,26 @@
from flask import Blueprint, redirect, flash
from auth import require_level
from config_utils import (flush_pending_to_queue, get_dashboard_pending,
_is_locked, _format_timing, _seconds_until_next_run)
bp = Blueprint('action_apply_pending', __name__)
@bp.route('/action/apply_pending', methods=['POST'])
@require_level('administrator')
def apply_pending():
items = get_dashboard_pending()
if not items:
flash('No pending changes to apply.', 'info')
return redirect('/view/view_general')
flush_pending_to_queue()
if _is_locked():
msg = 'Changes queued. They are being applied now.'
else:
timing = _format_timing(_seconds_until_next_run())
if timing:
msg = f'Changes queued. They will be applied {timing}.'
else:
msg = 'Changes queued. The processing service is not running.'
flash(msg, 'success')
return redirect('/view/view_general')

View file

@ -8,6 +8,8 @@ DASHBOARD_QUEUE = f'{CONFIGS_DIR}/.dashboard-queue'
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'
STATUS_FILE = f'{CONFIGS_DIR}/.status'
DASHB_TIMER_NAME = 'routlin-dashboard-queue'
PRODUCT_DISPLAY_NAME = os.environ.get('PRODUCT_DISPLAY_NAME', 'Routlin Dashboard')
DASHB_INTERVAL_SECS = 60
@ -103,7 +105,77 @@ def _trim_if_needed():
pass
def _queue_command(cmd):
def _apply_on_save():
try:
return load_core().get('general', {}).get('apply_on_save', True)
except Exception:
return True
def _read_dashboard_pending():
"""Return list of (uuid, ts, cmd, user, description) from .dashboard-pending."""
items = []
try:
lines = open(DASHBOARD_PENDING).read().splitlines()
except Exception:
return items
for line in lines:
if not line.strip():
continue
try:
main, _, desc = line.partition(' :: ')
parts = main.split(None, 3)
if len(parts) == 4:
entry_uuid, entry_ts, _dt, 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))
except Exception:
pass
return items
def get_dashboard_pending():
return _read_dashboard_pending()
def flush_pending_to_queue():
"""Move all entries from .dashboard-pending to .dashboard-queue and clear pending."""
items = _read_dashboard_pending()
if not items:
return
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:
if entry_uuid not in existing_ids:
dt_str = datetime.fromtimestamp(entry_ts).strftime('%Y-%m-%dT%H:%M:%S')
f.write(f'{entry_uuid} {entry_ts} {dt_str} [{entry_cmd}] ({entry_user})\n')
open(DASHBOARD_PENDING, 'w').close()
_trim_if_needed()
def _queue_pending_command(cmd, description=''):
"""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:
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')
desc_suffix = f' :: {description}' if description else ''
with open(DASHBOARD_PENDING, 'a') as f:
f.write(f'{entry_uuid} {entry_ts} {dt_str} [{cmd}] ({current_user}){desc_suffix}\n')
return entry_uuid, entry_ts
def _queue_command(cmd, description=''):
if not _apply_on_save():
return _queue_pending_command(cmd, description)
done_set = _load_done_set()
pending = _read_pending(done_set)
current_user = session.get('email_address', 'unknown')
@ -155,17 +227,19 @@ def _lock_mtime():
return None
def queue_command(cmd):
def queue_command(cmd, description=''):
"""Queue a command without generating a flash message."""
return _queue_command(cmd)
return _queue_command(cmd, description)
def queued_msg(cmd=None):
def queued_msg(cmd=None, description=''):
"""Queue cmd if given, then return a timing message.
Without cmd, just returns timing (for commands already queued by the caller)."""
entry_ts = None
if cmd is not None:
_entry_uuid, entry_ts = queue_command(cmd)
_entry_uuid, entry_ts = queue_command(cmd, description)
if not _apply_on_save():
return 'Configuration saved. Click Apply Now on the Configuration Changes card to apply.'
if _is_locked():
mtime = _lock_mtime()
if entry_ts is not None and mtime and entry_ts < mtime:
@ -178,7 +252,7 @@ def queued_msg(cmd=None):
return 'Changes queued. The processing service is not running.'
parts = cmd.split()
cli_cmd = f'sudo python3 {parts[0]}.py --{parts[1]}' if len(parts) == 2 else cmd
install_cmd = f'sudo python3 {parts[0]}.py --install' if len(parts) >= 1 else 'core.py --install'
install_cmd = f'sudo python3 install.py'
from markupsafe import Markup
return Markup(f'Configuration saved. The command processing service is not installed. '
f'Run <strong>{install_cmd}</strong> to enable it, '

View file

@ -24,6 +24,7 @@ from action_clear_ddns_log import bp as action_clear_ddns_log_bp
from action_apply_ddns_providers import bp as action_apply_ddns_providers_bp
from action_apply_interface import bp as action_apply_interface_bp
from action_apply_iface_config import bp as action_apply_iface_config_bp
from action_apply_pending import bp as action_apply_pending_bp
from api_apply_status import bp as api_apply_status_bp
app = Flask(__name__)
@ -52,6 +53,7 @@ app.register_blueprint(action_clear_ddns_log_bp)
app.register_blueprint(action_apply_ddns_providers_bp)
app.register_blueprint(action_apply_interface_bp)
app.register_blueprint(action_apply_iface_config_bp)
app.register_blueprint(action_apply_pending_bp)
app.register_blueprint(api_apply_status_bp)
def _seed_initial_account():

View file

@ -4,7 +4,7 @@ import json, re, subprocess, os, sys, html as html_mod
import sanitize
import validation as validate
from datetime import datetime, timezone
from config_utils import core_hash, get_pending_entries, _seconds_until_next_run, _format_timing, _is_locked, _lock_mtime, PRODUCT_DISPLAY_NAME
from config_utils import core_hash, get_pending_entries, get_dashboard_pending, _seconds_until_next_run, _format_timing, _is_locked, _lock_mtime, PRODUCT_DISPLAY_NAME
bp = Blueprint('view_page', __name__)
@ -529,6 +529,38 @@ def collect_tokens():
tokens['GENERAL_LOG_ERRORS_ONLY'] = 'true' if gen.get('log_errors_only') else 'false'
tokens['GENERAL_DNSMASQ_LOG_QUERIES'] = 'true' if gen.get('dnsmasq_log_queries') else 'false'
tokens['GENERAL_DAILY_EXECUTE_TIME'] = str(gen.get('daily_execute_time_24hr_local', '-'))
tokens['GENERAL_APPLY_ON_SAVE'] = 'true' if gen.get('apply_on_save', True) else 'false'
pending_items = get_dashboard_pending()
if pending_items:
rows = ''
for _uuid, ts, cmd, user, desc in pending_items:
dt_str = datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M')
label = e(desc) if desc else e(cmd)
rows += (f'<tr><td class="table-cell">{e(dt_str)}</td>'
f'<td class="table-cell">{label}</td>'
f'<td class="table-cell">{e(user)}</td></tr>')
pending_html = (
'<hr class="divider">'
'<h3 style="margin:0 0 0.75rem 0;font-size:0.85rem;font-weight:600;'
'text-transform:uppercase;letter-spacing:0.05em;color:var(--text-muted)">Pending Changes</h3>'
'<table class="data-table" style="margin-bottom:1rem">'
'<thead><tr>'
'<th class="table-header">Time</th>'
'<th class="table-header">Change</th>'
'<th class="table-header">User</th>'
'</tr></thead>'
f'<tbody>{rows}</tbody>'
'</table>'
'<form method="post" action="/action/apply_pending">'
f'<input type="hidden" name="config_hash" value="{e(core_hash())}">'
'<div class="button-row">'
'<button type="submit" class="btn btn-primary">Apply Now</button>'
'</div></form>'
)
else:
pending_html = ''
tokens['PENDING_CHANGES_HTML'] = pending_html
servers = dns.get('upstream_servers', [])
tokens['DNS_STRICT_ORDER'] = 'true' if dns.get('strict_order') else 'false'
@ -895,6 +927,9 @@ def _render_item(item, tokens, inherited_req=None):
if t == 'table':
return _render_table(item, tokens, req)
if t == 'raw_html':
return Markup(apply_tokens(item.get('html', ''), tokens))
return ''
@ -1364,6 +1399,23 @@ def render_layout(view_id, content_html, tokens):
cls = 'info-bar-warning'
other_bars += f'<div class="info-bar {cls}" data-apply-uuid="{e(o_uuid)}" data-apply-user="{e(o_user)}">{text}</div>\n'
problem_bars = ''
try:
import json as _j
st = _j.load(open(f'{CONFIGS_DIR}/.status'))
for section in ('configurations', 'logs'):
for item in st.get(section, []):
if item.get('status') == 'problem':
sev = item.get('severity', 'error')
cls = 'info-bar-danger' if sev == 'error' else 'info-bar-warning'
text = e(item.get('detail', item.get('name', '')))
tip = item.get('suggestion', '')
if tip:
text += f' <span style="opacity:0.75">— {e(tip)}</span>'
problem_bars += f'<div class="info-bar {cls}">{text}</div>\n'
except Exception:
pass
return (f'<!DOCTYPE html>\n<html lang="en">\n<head>\n'
f' <meta charset="UTF-8">\n'
f' <meta name="viewport" content="width=device-width, initial-scale=1.0">\n'
@ -1372,7 +1424,7 @@ def render_layout(view_id, content_html, tokens):
f'</head>\n<body>\n'
f'{titlebar_html}\n'
f'{navbar_html}\n'
f'<main class="main-content">\n{other_bars}{content_html}\n</main>\n'
f'<main class="main-content">\n{problem_bars}{other_bars}{content_html}\n</main>\n'
f'{footer_html}\n'
f'<script>var CONFIG_HASH="{page_hash}";var LAN_IFACE="{lan_iface}";var VPN_VLAN_COUNT={vpn_count};var EXISTING_VLAN_IDS={existing_ids};var EXISTING_VLAN_NAMES={existing_names};var EXISTING_VLAN_INTERFACES={existing_interfaces};var APPLY_UUID={json.dumps(my_uuid)};</script>\n'
f'<script>{_inline_js()}</script>\n'

View file

@ -679,6 +679,47 @@
}
],
"client_requirement": "client_is_administrator+"
},
{
"type": "card",
"label": "Configuration Changes",
"client_requirement": "client_is_administrator+",
"items": [
{
"type": "form",
"action": "/action/apply_general",
"method": "post",
"items": [
{
"type": "field",
"label": "Apply on Save",
"name": "apply_on_save",
"input_type": "checkbox",
"value": "%GENERAL_APPLY_ON_SAVE%",
"hint": "When enabled, saved changes are queued immediately. When disabled, changes accumulate here until you click Apply Now."
},
{
"type": "button_row",
"items": [
{
"type": "button_primary",
"text": "Save",
"action": "/action/apply_general",
"method": "post"
},
{
"type": "button_cancel",
"text": "Cancel"
}
]
}
]
},
{
"type": "raw_html",
"html": "%PENDING_CHANGES_HTML%"
}
]
}
]
},