LibrePortal/containers/gluetun/scripts/gluetun_recreate_routed.sh
librelad 853b489caa refactor(gluetun): move the network-routing feature into gluetun's folder
If it's gluetun code, it lives with gluetun. Both functions in
scripts/config/tags/processors/tags_processor_network_mode.sh manipulate gluetun
markers / gluetun's compose, so move them into containers/gluetun/scripts/
gluetun_network.sh and rename to the per-app-hook convention:

  tagsProcessorNetworkMode             -> appNetworkApplyMode_gluetun
  tagsProcessorGluetunForwardedPorts   -> appNetworkRegisterPorts_gluetun

Central call sites are now provider-agnostic — no "gluetun" literal anywhere:

- docker_config_setup_data.sh: an app routing via CFG_<APP>_NETWORK=<provider>
  triggers `appNetworkApplyMode_<provider>` + `appNetworkRegisterPorts_<provider>`
  via declare -F, so any future gateway provider plugs in with no engine edits.
- uninstall_app.sh: loops every `appNetworkRegisterPorts_*` hook (each self-skips
  when its provider isn't installed), so removing a routed app refreshes the
  right provider with no provider name in central code.

Delete tags_processor_network_mode.sh; regenerate arrays. Verified with stubs:
default mode no-ops, gluetun-routed app fires both hooks, gluetun itself is
skipped, unknown provider is silently no-op, uninstall loop calls registerPorts.

Drive-by cleanup: 9 stale "${X_scripts[@]}" array references in app_files.sh /
cli_files.sh (gluetun + headscale from this session's moves, plus 7 pre-existing:
command/ssl/swapfile/ufw/ufwd/user — all from older refactors that left them
behind). Each expanded to nothing at runtime (harmless), but they're dead
misleading refs. Cleaned both files; every remaining array ref now points to a
real files_*.sh.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Signed-off-by: librelad <librelad@digitalangels.vip>
2026-05-26 10:43:49 +01:00

65 lines
2.5 KiB
Bash

#!/bin/bash
# Force-recreates every installed app whose `CFG_<APP>_NETWORK=gluetun`
# so they re-resolve `network_mode: container:gluetun-service` against
# the *current* gluetun container ID.
#
# Background: Docker resolves `container:<name>` once at start time. If
# gluetun is later recreated (port-forward change, version bump, manual
# `docker compose up`), every routed app keeps the *old* container ID
# embedded in its NetworkMode and ends up in its own private netns —
# the host port mapping silently stops reaching anything because the
# app's HTTP server is no longer in gluetun's namespace.
#
# Call this whenever you've just touched gluetun in a way that recreates
# its container — see appNetworkRegisterPorts_gluetun and the gluetun
# install lifecycle for the two existing wiring sites.
appGluetunRecreateRouted()
{
if ! command -v sqlite3 >/dev/null 2>&1; then
return 0
fi
if [[ ! -f "$docker_dir/$db_file" ]]; then
return 0
fi
local installed_apps
installed_apps=$(runInstallOp sqlite3 "$docker_dir/$db_file" \
"SELECT name FROM apps WHERE status = 1 ORDER BY name;" 2>/dev/null)
if ! runFileOp docker ps --format '{{.Names}}' 2>/dev/null | grep -q '^gluetun-service$'; then
# Nothing to re-attach against; gluetun isn't running.
return 0
fi
local recreated=0
while IFS= read -r app; do
[[ -z "$app" || "$app" == "gluetun" ]] && continue
local app_config_file="${containers_dir}${app}/${app}.config"
[[ -f "$app_config_file" ]] || continue
local net
net=$(grep -E "^CFG_${app^^}_NETWORK=" "$app_config_file" 2>/dev/null \
| cut -d'=' -f2 | tr -d '"')
[[ "$net" != "gluetun" ]] && continue
local app_compose="${containers_dir}${app}/docker-compose.yml"
[[ -f "$app_compose" ]] || continue
# Skip apps with no running/created container — recreate would
# do nothing useful and we'd just emit noise.
if ! runFileOp docker ps -a --format '{{.Names}}' 2>/dev/null \
| grep -q "^${app}-service$"; then
continue
fi
isNotice "Re-attaching ${app} to gluetun's namespace (force-recreate)..."
dockerCommandRun "cd ${containers_dir}${app} && docker compose up -d --force-recreate ${app}-service" >/dev/null 2>&1 || true
((recreated++))
done <<< "$installed_apps"
if (( recreated > 0 )); then
isSuccessful "Re-attached ${recreated} gluetun-routed app(s) to the new gluetun namespace."
fi
}