LibrePortal/scripts/webui/webui_updater.sh
librelad 52e4280a67 feat(webui): add 'Migrate' tab — restore an app from another LibrePortal
Phase 1 of the migration-system refresh. Surfaces Phase 0's kernel
(libreportal restore migrate ...) as a WebUI flow so users don't have
to drop to the CLI to pull an app from a peer's backups.

backend / data generator:
  scripts/webui/data/generators/backup/webui_backup_migrate.sh
    Walks every enabled backup location, lists every (other_host, app)
    pair with snapshot count + latest id/date, and emits a single
    destination summary block (installed apps, running apps, disk free)
    so the frontend can compute collisions and warnings without per-row
    API round-trips. Filters out our own hostname — we don't migrate to
    ourselves. Output: data/backup/generated/migrate.json.
    Hooked into the standard webuiLibrePortalUpdate refresh pipeline,
    so 'libreportal regen webui' (and the periodic task-processor poll)
    keep it fresh on their own.

frontend:
  - New 'Migrate' sidebar tab on /backup, sits between Locations and
    Configuration. Path-based URL: /backup/migrate.
  - Per-source-host cards listing every available app, with snapshot
    count + relative-time hint, collision dot when the app is already
    installed here, and per-app + per-host migrate buttons.
  - Confirm modal with two checkboxes matching the kernel's defaults:
      [✓] Back up the destination's existing copy first   (pre-migrate
          backup; auto-disabled when there's nothing to back up)
      [✓] Rewrite host-bound URLs to this host             (URL rewrite
          — uncheck only to keep source hostnames)
    On confirm, runs 'libreportal restore migrate app/system …' via the
    task system; opt-out checkboxes append --no-pre-backup / --keep-urls
    only when the user un-ticks, matching the kernel's default-on flags.
  - Empty state when no other hosts have visible backups, explaining
    the shared-backup-location prerequisite.

The CLI dispatcher hooks (Phase 0) wire restore migrate app/system to
migrateApplyApp/migrateApplySystem, so the WebUI gets pre-backup safety,
URL rewrite, and structured progress (when --json-progress is set; not
needed here yet — the task system's log tail is enough for v1).

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

120 lines
5.2 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..."
# 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
}