LibrePortal/scripts/webui/webui_updater.sh
librelad 1014dd6e42 feat(peers): introduce 'Peer' as a first-class concept (Phase 2)
A peer is a named reference to another LibrePortal instance. Phase 2 only
implements kind=backup-channel (friendly label over a hostname that shows
up in a shared backup repo); direct-ssh-direct and direct-ssh-via-relay
(Connect's blind-relay) are reserved enum values for Phase 3.

DB schema (db_create_tables.sh):
  CREATE TABLE peers (
    id           INTEGER PRIMARY KEY AUTOINCREMENT,
    name         TEXT UNIQUE NOT NULL,
    kind         TEXT NOT NULL DEFAULT 'backup-channel',
    config_json  TEXT NOT NULL DEFAULT '{}',
    status       TEXT DEFAULT 'unknown',
    last_seen    TEXT,
    created_at   TEXT DEFAULT CURRENT_TIMESTAMP
  );
  + indexes on name and kind.

  config_json is kind-specific so new transports don't need a schema
  migration. For backup-channel it carries {"hostname":"","loc_idx":N}.

Bash module (scripts/peer/):
  peer_helpers.sh   _peerDb, peerSqlEscape, peerValidateName/Kind.
  peer_add.sh       peerAdd <name> <kind> [k=v ...] → INSERT, refresh
                    generator. Rejects unimplemented kinds early so users
                    don't create dead-end peer records.
  peer_remove.sh    peerRemove <name> → DELETE.
  peer_list.sh      peerList → JSON array; peerGet, peerNameForHostname
                    (reverse-lookup for the migrate-tab overlay).
  peer_check.sh     peerCheckReachable, peerCheckAll. For backup-channel
                    'reachable' = at least one snapshot from that hostname
                    visible in (preferred|any enabled) location. Updates
                    status + last_seen so UI dots render without re-probing.

CLI (scripts/cli/commands/peer/):
  libreportal peer list
  libreportal peer get <name>
  libreportal peer add <name> backup-channel hostname=<host> [loc_idx=<n>]
  libreportal peer remove <name>
  libreportal peer check [name]

  Auto-routed by cli_initialize.sh's category-discovery.

WebUI data generator (scripts/webui/data/generators/peers/webui_peers.sh):
  Emits data/peers/generated/peers.json with the peerList output and a
  generated_at envelope. Hooked into webuiLibrePortalUpdate alongside the
  backup generators.

Frontend:
  - New top-level /peers route in spa.js (PeersPage class, peers-content.html).
  - 'Peers' nav item in the topbar between Backups and the right-side controls.
  - Add-peer modal with friendly-name + kind + hostname + preferred-location
    selector (populated from the existing backup-locations data).
  - Per-peer card with status dot, last-checked time, Check + Remove buttons.
  - Phase 3 kinds appear in the kind dropdown as disabled options so users
    can see what's coming.

Source-array wiring:
  - generate_arrays.sh auto-created files_peer.sh from the new peer/ dir.
  - cli_files.sh + app_files.sh include ${peer_scripts[@]} alphabetically.
  - files_webui.sh auto-picked-up the new peers/ generator subfolder.

The migrate-tab friendly-name overlay (use peer names in /backup/migrate
when a peer record exists for a hostname) is intentionally deferred — it's
a 5-line frontend lookup once peers.json is loaded; cleaner to add after
Phase 3 ships its peer-detail view.

Signed-off-by: librelad <librelad@digitalangels.vip>
2026-05-26 17:43:56 +01:00

126 lines
5.5 KiB
Bash
Executable File

#!/bin/bash
# LibrePortal WebUI Main Updater
# Coordinates all webui data updates including system info and app configurations
webuiLibrePortalUpdate() {
# Debounce: during a fresh install this function gets called back-to-back
# by installLibrePortal (line 113) and then startScan (end of preinstall),
# both within ~10 seconds. Skip if the last successful run is within the
# debounce window. Callers that genuinely need a forced refresh can set
# WEBUI_UPDATER_FORCE=1.
local stamp_file="/tmp/libreportal_webui_updater_last"
local debounce_seconds="${WEBUI_UPDATER_DEBOUNCE:-30}"
if [[ -z "$WEBUI_UPDATER_FORCE" && -f "$stamp_file" ]]; then
local _now=$(date +%s)
local _last=$(stat -c '%Y' "$stamp_file" 2>/dev/null)
if [[ -n "$_last" ]] && (( _now - _last < debounce_seconds )); then
isNotice "WebUI updater ran $((_now - _last))s ago — skipping (debounced, within ${debounce_seconds}s)."
return 0
fi
fi
local status=$(dockerCheckAppInstalled "libreportal" "docker")
# Main process: generate config if app is installed
if [ "$status" == "installed" ]; then
isHeader "LibrePortal WebUI Updater"
# Check for update lock file first
local result=$(webuiCheckUpdateLock)
checkSuccess "Checked for update lock file."
if [[ "$lock_file_found" == "true" ]]; then
echo ""
isNotice "Update already in progress. Please wait for current update to complete."
isNotice "Status: Locked"
isNotice "WebUI update skipped due to concurrent update"
echo ""
else
# Create update lock file
local result=$(webuiCreateUpdateLock)
checkSuccess "Created update lock file..."
# Update system information first
local result=$(webuiSystemUpdate)
checkSuccess "Updated system information..."
# Ensure task system files exist (failsafe)
local result=$(webuiEnsureTaskFiles)
checkSuccess "Ensured task system files exist..."
# Generate system configuration
local result=$(webuiGenerateSystemConfigs)
checkSuccess "Generated system configurations..."
# Generate categories
local result=$(webuiCreateCategories $containers_dir/libreportal/frontend/data)
checkSuccess "Generated app and config categories..."
# Generate LibrePortal app configuration
local result=$(webuiGenerateLibrePortalConfig)
checkSuccess "Generated LibrePortal app configuration..."
# Generate apps-services.json
local result=$(webuiGenerateAppsServicesConfig)
checkSuccess "Generated apps-services.json..."
# Generate apps-tools.json (aggregate of per-app *.tools.json)
local result=$(webuiGenerateAppsToolsConfig)
checkSuccess "Generated apps-tools.json..."
# Per-app routine refresh hooks. An installed app may define
# appWebuiRefresh_<app> (in containers/<app>/scripts/) for data it
# wants refreshed on every WebUI update — e.g. gluetun's provider
# snapshot. Gated on the app being installed (its live compose
# exists, tested directly so it works without list perm on the
# container-user-owned data dir), so non-users never pay for it.
local _app _dir _hook
for _dir in "${install_containers_dir}"*/; do
_app="$(basename "$_dir")"
[[ -f "${containers_dir}${_app}/docker-compose.yml" ]] || continue
_hook="appWebuiRefresh_${_app}"
declare -F "$_hook" >/dev/null 2>&1 || continue
local result=$($_hook)
checkSuccess "Refreshed ${_app} WebUI data..."
done
# Generate Backup locations / snapshots / engines / dashboards
local result=$(webuiGenerateBackupLocations && webuiGenerateBackupDashboard && webuiGenerateBackupSnapshots all && webuiGenerateBackupAppStatus && webuiGenerateBackupEngines && webuiGenerateBackupSchema && webuiGenerateBackupPasswords && webuiGenerateBackupMigrate)
checkSuccess "Refreshed backup dashboard data..."
# Peers (named other LibrePortal instances) — small, cheap; lives
# in its own data/peers/generated/peers.json file consumed by
# /peers and overlay-read by the migrate tab.
local result=$(webuiGeneratePeers)
checkSuccess "Refreshed peers data..."
# SSH access snapshot (authorized keys + password-login state)
local result=$(webuiGenerateSshAccess)
checkSuccess "Refreshed SSH access data..."
# Sync app icons
local result=$(webuiSyncAppIcons)
checkSuccess "Synced app icons..."
# Generate log files for installed apps
local result=$(webuiGenerateAppLogs)
checkSuccess "Generated log files for installed apps..."
# Remove update lock file
local result=$(webuiRemoveUpdateLock)
checkSuccess "Removed update lock file..."
# Remove setup lock file
local result=$(webuiRemoveSetupLock)
checkSuccess "Removed setup lock file..."
isSuccessful "WebUI update completed successfully!"
touch "$stamp_file" 2>/dev/null || true
fi
else
isNotice "LibrePortal not installed - skipping WebUI update"
fi
}