LibrePortal/scripts/webui/webui_updater.sh
librelad e168496824 perf(gluetun): throttle the provider-list fetch off the WebUI-update hot path
webuiLibrePortalUpdate runs the per-app refresh hooks on every update (the
task processor fires it repeatedly), and gluetun's hook silently pulls a
~7MB servers.json from GitHub each time. The hook's stdout is captured by
`result=$($_hook)`, so on a slow link the update just sits with no output
right after "Generated apps-tools.json..." — it reads as a freeze.

- Throttle the fetch to once per CFG_GLUETUN_PROVIDERS_REFRESH_HOURS (24h
  default; 0 = manual-only). Direct callers (Tools refresh, install hook)
  pass GLUETUN_PROVIDERS_FORCE=1 to still get a guaranteed pull.
- curl --compressed (this JSON gzips ~7x) + --connect-timeout 15 so a slow
  or dead link shrinks/fails fast instead of stalling every update.
- Print "Refreshing <app> WebUI data..." from the updater loop (outside the
  output capture) so the step is visible instead of looking hung.

Signed-off-by: librelad <librelad@digitalangels.vip>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-06 17:58:38 +01:00

130 lines
5.9 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; 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; result=$(webuiCreateUpdateLock)
checkSuccess "Created update lock file..."
# Update system information first
local result; result=$(webuiSystemUpdate)
checkSuccess "Updated system information..."
# Ensure task system files exist (failsafe)
local result; result=$(webuiEnsureTaskFiles)
checkSuccess "Ensured task system files exist..."
# Generate system configuration
local result; result=$(webuiGenerateSystemConfigs)
checkSuccess "Generated system configurations..."
# Generate categories
local result; result=$(webuiCreateCategories $containers_dir/libreportal/frontend/data)
checkSuccess "Generated app and config categories..."
# Generate LibrePortal app configuration
local result; result=$(webuiGenerateLibrePortalConfig)
checkSuccess "Generated LibrePortal app configuration..."
# Generate apps-services.json
local result; result=$(webuiGenerateAppsServicesConfig)
checkSuccess "Generated apps-services.json..."
# Generate apps-tools.json (aggregate of per-app *.tools.json)
local result; 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
# Announce before running: a hook may reach upstream (e.g.
# gluetun's provider list), and its own output is captured
# below, so without this the update looks frozen mid-fetch.
isNotice "Refreshing ${_app} WebUI data..."
local result; result=$($_hook)
checkSuccess "Refreshed ${_app} WebUI data..."
done
# Generate Backup locations / snapshots / engines / dashboards
local result; 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; result=$(webuiGeneratePeers)
checkSuccess "Refreshed peers data..."
# SSH access snapshot (authorized keys + password-login state)
local result; result=$(webuiGenerateSshAccess)
checkSuccess "Refreshed SSH access data..."
# Sync app icons
local result; result=$(webuiSyncAppIcons)
checkSuccess "Synced app icons..."
# Generate log files for installed apps
local result; result=$(webuiGenerateAppLogs)
checkSuccess "Generated log files for installed apps..."
# Remove update lock file
local result; result=$(webuiRemoveUpdateLock)
checkSuccess "Removed update lock file..."
# Remove setup lock file
local result; 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
}