mirror of
https://github.com/getumbrel/umbrel-apps.git
synced 2026-12-05 02:44:39 +03:00
288 lines
8.4 KiB
Bash
Executable File
288 lines
8.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# WireGuard Easy v15 is a full rewrite that replaces the v14 wg0.json runtime
|
|
# config with a SQLite database and a first-run setup wizard. Upstream only
|
|
# documents a manual migration: start v15, create the admin user, upload the
|
|
# v14 wg0.json backup, then set the public host/port again.
|
|
#
|
|
# Umbrel users expect app updates to preserve state without SSH/manual file
|
|
# handling, so this hook drives that same upstream setup API automatically. It
|
|
# runs in pre-start, briefly starts the app service through Umbrel's app script,
|
|
# imports the existing v14 config when present, verifies the import, records a
|
|
# persistent done marker, then stops the temporary service so the normal Umbrel
|
|
# start can continue. If migration cannot be verified, no done marker is written
|
|
# and future starts retry while leaving the preserved v14 backup in app data.
|
|
|
|
APP_DIR="${APP_DATA_DIR:-$(readlink -f "$(dirname "${BASH_SOURCE[0]}")/..")}"
|
|
UMBREL_ROOT="${UMBREL_ROOT:-$(readlink -f "${APP_DIR}/../..")}"
|
|
APP_ID="${APP_ID:-wireguard}"
|
|
WG_DIR="${APP_DIR}/data/wireguard"
|
|
STATE_DIR="${APP_DIR}/data/umbrel-v15-migration"
|
|
OLD_CONFIG="${WG_DIR}/wg0.json"
|
|
BACKUP="${STATE_DIR}/wg0-v14.json"
|
|
DONE_MARKER="${STATE_DIR}/done"
|
|
PENDING_MARKER="${STATE_DIR}/pending"
|
|
LOG_FILE="${STATE_DIR}/migration.log"
|
|
LOG_PREFIX="WireGuard Easy v15 setup:"
|
|
|
|
USERNAME="umbrel"
|
|
PASSWORD="${APP_PASSWORD:-}"
|
|
HOSTNAME="${DEVICE_DOMAIN_NAME:-umbrel.local}"
|
|
WIREGUARD_PORT="51820"
|
|
STARTED_APP_FOR_SETUP=false
|
|
|
|
log() {
|
|
local message="${LOG_PREFIX} $*"
|
|
echo "${message}"
|
|
if [[ -d "${STATE_DIR}" ]]; then
|
|
printf '%s\n' "${message}" >> "${LOG_FILE}" || true
|
|
fi
|
|
}
|
|
|
|
fail() {
|
|
log "ERROR: $*"
|
|
if [[ -s "${BACKUP}" ]]; then
|
|
log "preserved v14 backup: ${BACKUP}"
|
|
elif [[ -s "${OLD_CONFIG}" ]]; then
|
|
log "v14 config remains at: ${OLD_CONFIG}"
|
|
fi
|
|
log "manual migration guide: https://wg-easy.github.io/wg-easy/latest/advanced/migrate/from-14-to-15/"
|
|
exit 1
|
|
}
|
|
|
|
require_command() {
|
|
if ! command -v "$1" >/dev/null 2>&1; then
|
|
fail "required command is missing: $1"
|
|
fi
|
|
}
|
|
|
|
app_compose() {
|
|
"${UMBREL_ROOT}/scripts/app" compose "${APP_ID}" "$@"
|
|
}
|
|
|
|
start_setup_app() {
|
|
log "starting app service for v15 setup"
|
|
app_compose up --detach app >/dev/null
|
|
STARTED_APP_FOR_SETUP=true
|
|
}
|
|
|
|
stop_setup_app() {
|
|
if [[ "${STARTED_APP_FOR_SETUP}" == "true" ]]; then
|
|
log "stopping setup app service before normal Umbrel start"
|
|
app_compose stop app >/dev/null 2>&1 || true
|
|
STARTED_APP_FOR_SETUP=false
|
|
fi
|
|
}
|
|
|
|
container_ip() {
|
|
local ip
|
|
read -r ip _ < <(app_compose exec -T app hostname -i 2>/dev/null || true)
|
|
printf '%s\n' "${ip:-}"
|
|
}
|
|
|
|
wait_for_app() {
|
|
local ip
|
|
for _ in $(seq 1 300); do
|
|
ip="$(container_ip)"
|
|
if [[ -n "${ip}" ]]; then
|
|
BASE_URL="http://${ip}:51821"
|
|
if curl -fsS --max-time 2 "${BASE_URL}/api/information" >/dev/null 2>&1 || curl -fsS --max-time 2 "${BASE_URL}/setup/1" >/dev/null 2>&1; then
|
|
return 0
|
|
fi
|
|
fi
|
|
sleep 0.2
|
|
done
|
|
return 1
|
|
}
|
|
|
|
post_setup_json() {
|
|
local path="$1"
|
|
local data="$2"
|
|
curl -fsS --max-time 20 \
|
|
-H 'Content-Type: application/json' \
|
|
-X POST \
|
|
--data "${data}" \
|
|
"${BASE_URL}${path}"
|
|
}
|
|
|
|
admin_get() {
|
|
local path="$1"
|
|
curl -fsS --max-time 20 \
|
|
-u "${USERNAME}:${PASSWORD}" \
|
|
"${BASE_URL}${path}"
|
|
}
|
|
|
|
admin_post_json() {
|
|
local path="$1"
|
|
local data="$2"
|
|
curl -fsS --max-time 20 \
|
|
-u "${USERNAME}:${PASSWORD}" \
|
|
-H 'Content-Type: application/json' \
|
|
-X POST \
|
|
--data "${data}" \
|
|
"${BASE_URL}${path}"
|
|
}
|
|
|
|
ensure_admin_user() {
|
|
local payload
|
|
payload="$(jq -n --arg username "${USERNAME}" --arg password "${PASSWORD}" '{username:$username,password:$password,confirmPassword:$password}')"
|
|
|
|
if post_setup_json "/api/setup/2" "${payload}" >/dev/null 2>&1; then
|
|
log "created v15 admin user"
|
|
return 0
|
|
fi
|
|
|
|
if admin_get "/api/admin/userconfig" >/dev/null 2>&1; then
|
|
log "admin user already exists"
|
|
return 0
|
|
fi
|
|
|
|
log "admin user setup route failed; continuing in case setup is already at migration step"
|
|
}
|
|
|
|
set_user_config() {
|
|
local current_config updated_config
|
|
|
|
if current_config="$(admin_get "/api/admin/userconfig" 2>/dev/null)"; then
|
|
updated_config="$(printf '%s' "${current_config}" | jq --arg host "${HOSTNAME}" --argjson port "${WIREGUARD_PORT}" '.host=$host | .port=$port | del(.id,.createdAt,.updatedAt)')"
|
|
admin_post_json "/api/admin/userconfig" "${updated_config}" >/dev/null || fail "failed to update v15 host and port"
|
|
log "set v15 host and port to ${HOSTNAME}:${WIREGUARD_PORT}"
|
|
return 0
|
|
fi
|
|
|
|
fail "could not read v15 user configuration"
|
|
}
|
|
|
|
restart_interface() {
|
|
if admin_post_json "/api/admin/interface/restart" '{}' >/dev/null 2>&1; then
|
|
log "restarted WireGuard interface"
|
|
else
|
|
log "WireGuard interface restart failed after config save; continuing"
|
|
fi
|
|
}
|
|
|
|
verify_import() {
|
|
local clients_json imported_count interface_json public_key
|
|
|
|
clients_json="$(admin_get "/api/client" 2>/dev/null)" || return 1
|
|
imported_count="$(printf '%s' "${clients_json}" | jq -r 'length' 2>/dev/null)" || return 1
|
|
if [[ ! "${imported_count}" =~ ^[0-9]+$ ]] || [[ "${imported_count}" -lt "${CLIENT_COUNT}" ]]; then
|
|
log "verification saw ${imported_count:-0}/${CLIENT_COUNT} imported client(s)"
|
|
return 1
|
|
fi
|
|
|
|
interface_json="$(admin_get "/api/admin/interface" 2>/dev/null)" || return 1
|
|
public_key="$(jq -r '.server.publicKey' "${BACKUP}")"
|
|
printf '%s' "${interface_json}" | jq -e --arg publicKey "${public_key}" '.publicKey == $publicKey' >/dev/null 2>&1
|
|
}
|
|
|
|
mark_done() {
|
|
local mode="$1"
|
|
local count="$2"
|
|
{
|
|
printf 'completed_at=%s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
|
printf 'mode=%s\n' "${mode}"
|
|
printf 'client_count=%s\n' "${count}"
|
|
} > "${DONE_MARKER}"
|
|
rm -f "${PENDING_MARKER}"
|
|
}
|
|
|
|
bootstrap_fresh_install() {
|
|
log "no v14 wg0.json found; configuring fresh v15 install"
|
|
ensure_admin_user
|
|
|
|
local payload
|
|
payload="$(jq -n --arg host "${HOSTNAME}" --argjson port "${WIREGUARD_PORT}" '{host:$host,port:$port}')"
|
|
if post_setup_json "/api/setup/4" "${payload}" >/dev/null 2>&1; then
|
|
log "completed fresh v15 setup"
|
|
elif admin_get "/api/admin/userconfig" >/dev/null 2>&1; then
|
|
set_user_config
|
|
else
|
|
fail "fresh v15 setup failed"
|
|
fi
|
|
|
|
mark_done "fresh" "0"
|
|
}
|
|
|
|
migrate_v14_install() {
|
|
if ! jq -e '
|
|
(.server.privateKey | type == "string") and
|
|
(.server.publicKey | type == "string") and
|
|
(.server.address | type == "string") and
|
|
(.clients | type == "object") and
|
|
([.clients[] |
|
|
(.name | type == "string") and
|
|
(.address | type == "string") and
|
|
(.privateKey | type == "string") and
|
|
(.publicKey | type == "string") and
|
|
(.preSharedKey | type == "string") and
|
|
(.createdAt | type == "string") and
|
|
(.updatedAt | type == "string") and
|
|
(.enabled | type == "boolean")
|
|
] | all)
|
|
' "${OLD_CONFIG}" >/dev/null 2>&1; then
|
|
fail "v14 wg0.json does not match the expected migration schema"
|
|
fi
|
|
|
|
if [[ ! -s "${BACKUP}" ]]; then
|
|
install -m 600 "${OLD_CONFIG}" "${BACKUP}"
|
|
log "preserved v14 backup at ${BACKUP}"
|
|
fi
|
|
|
|
CLIENT_COUNT="$(jq -r '(.clients // {}) | length' "${BACKUP}")"
|
|
printf 'started_at=%s\nclient_count=%s\nbackup=%s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "${CLIENT_COUNT}" "${BACKUP}" > "${PENDING_MARKER}"
|
|
|
|
ensure_admin_user
|
|
|
|
local payload
|
|
payload="$(jq -n --rawfile file "${BACKUP}" '{file:$file}')"
|
|
if post_setup_json "/api/setup/migrate" "${payload}" >/dev/null 2>&1; then
|
|
log "imported v14 configuration with ${CLIENT_COUNT} client(s)"
|
|
elif verify_import; then
|
|
log "migration route was already complete; verified imported data"
|
|
else
|
|
fail "v14 configuration import failed"
|
|
fi
|
|
|
|
if ! verify_import; then
|
|
fail "import verification failed"
|
|
fi
|
|
|
|
set_user_config
|
|
restart_interface
|
|
mark_done "migration" "${CLIENT_COUNT}"
|
|
log "auto-migration completed with ${CLIENT_COUNT} client(s)"
|
|
}
|
|
|
|
if [[ -f "${DONE_MARKER}" ]]; then
|
|
log "setup already completed; skipping"
|
|
exit 0
|
|
fi
|
|
|
|
mkdir -p "${STATE_DIR}"
|
|
chmod 700 "${STATE_DIR}" || true
|
|
|
|
require_command curl
|
|
require_command jq
|
|
require_command sed
|
|
|
|
if [[ ! -x "${UMBREL_ROOT}/scripts/app" ]]; then
|
|
fail "Umbrel app script is unavailable at ${UMBREL_ROOT}/scripts/app"
|
|
fi
|
|
|
|
if [[ -z "${PASSWORD}" ]]; then
|
|
fail "APP_PASSWORD is unavailable"
|
|
fi
|
|
|
|
BASE_URL=""
|
|
start_setup_app
|
|
trap stop_setup_app EXIT
|
|
wait_for_app || fail "v15 web service did not become ready"
|
|
|
|
if [[ -s "${OLD_CONFIG}" ]]; then
|
|
migrate_v14_install
|
|
else
|
|
bootstrap_fresh_install
|
|
fi
|