2026-05-17 08:57:58 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
# upgrade.sh — pull the latest manage-server image and restart.
|
|
|
|
|
#
|
2026-05-19 19:19:57 +08:00
|
|
|
# Takes a pre-upgrade pg_dump backup. DB schema migrations run automatically
|
|
|
|
|
# on container startup — no manual migration step required.
|
2026-05-17 08:57:58 +02:00
|
|
|
#
|
|
|
|
|
# Usage:
|
|
|
|
|
# sudo bash upgrade.sh # latest from ghcr.io
|
2026-05-19 19:19:57 +08:00
|
|
|
# sudo bash upgrade.sh --image TAG # pin a specific image tag
|
2026-05-19 19:45:37 +08:00
|
|
|
# sudo bash upgrade.sh --port PORT # change the web UI host port
|
2026-05-17 08:57:58 +02:00
|
|
|
set -euo pipefail
|
|
|
|
|
|
2026-05-19 19:38:47 +08:00
|
|
|
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" &>/dev/null && pwd)"
|
2026-05-17 08:57:58 +02:00
|
|
|
cd "$SCRIPT_DIR"
|
|
|
|
|
|
|
|
|
|
info() { printf '[manage-server] %s\n' "$*"; }
|
|
|
|
|
die() { printf '[manage-server] error: %s\n' "$*" >&2; exit 1; }
|
|
|
|
|
|
|
|
|
|
[[ $EUID -eq 0 ]] || die "must run as root"
|
|
|
|
|
[[ -f .env ]] || die ".env not found — run install.sh first"
|
|
|
|
|
|
2026-05-19 19:19:57 +08:00
|
|
|
# ── arg parsing ───────────────────────────────────────────────────────────
|
2026-05-17 08:57:58 +02:00
|
|
|
IMAGE=""
|
2026-05-19 19:45:37 +08:00
|
|
|
PORT=""
|
2026-05-17 08:57:58 +02:00
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
|
case "$1" in
|
|
|
|
|
--image) IMAGE="$2"; shift 2 ;;
|
2026-05-19 19:45:37 +08:00
|
|
|
--port) PORT="$2"; shift 2 ;;
|
2026-05-17 08:57:58 +02:00
|
|
|
-h|--help) grep '^#' "$0" | sed 's/^# //;s/^#//'; exit 0 ;;
|
|
|
|
|
*) die "unknown flag: $1" ;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
|
2026-05-19 19:19:57 +08:00
|
|
|
# ── runtime detection ─────────────────────────────────────────────────────
|
|
|
|
|
if command -v podman-compose >/dev/null 2>&1; then COMPOSE=(podman-compose); RUNTIME=podman
|
|
|
|
|
elif podman compose version >/dev/null 2>&1; then COMPOSE=(podman compose); RUNTIME=podman
|
|
|
|
|
elif docker compose version >/dev/null 2>&1; then COMPOSE=(docker compose); RUNTIME=docker
|
|
|
|
|
else die "no compose runtime found"; fi
|
|
|
|
|
|
|
|
|
|
# ── pin image if requested ────────────────────────────────────────────────
|
2026-05-17 08:57:58 +02:00
|
|
|
if [[ -n "$IMAGE" ]]; then
|
|
|
|
|
sed -i "s|^TRITON_MANAGE_IMAGE=.*|TRITON_MANAGE_IMAGE=$IMAGE|" .env
|
|
|
|
|
info "pinned image to $IMAGE"
|
|
|
|
|
fi
|
2026-05-19 19:45:37 +08:00
|
|
|
if [[ -n "$PORT" ]]; then
|
|
|
|
|
sed -i "s|^TRITON_MANAGE_HOST_PORT=.*|TRITON_MANAGE_HOST_PORT=$PORT|" .env
|
|
|
|
|
info "host port set to $PORT"
|
|
|
|
|
fi
|
2026-05-17 08:57:58 +02:00
|
|
|
|
2026-05-19 19:19:57 +08:00
|
|
|
# ── pre-upgrade DB backup ─────────────────────────────────────────────────
|
|
|
|
|
case "$(uname -s)" in
|
|
|
|
|
Linux) BACKUP_DIR="/var/backups/triton" ;;
|
|
|
|
|
Darwin) BACKUP_DIR="${HOME}/Library/Application Support/triton/backups" ;;
|
|
|
|
|
*) BACKUP_DIR="$SCRIPT_DIR/backups" ;;
|
|
|
|
|
esac
|
|
|
|
|
mkdir -p "$BACKUP_DIR"
|
|
|
|
|
DUMP_FILE="${BACKUP_DIR}/manage-pre-upgrade-$(date +%F-%H%M%S).sql.gz"
|
2026-05-17 08:57:58 +02:00
|
|
|
|
|
|
|
|
info "pre-upgrade DB backup..."
|
2026-05-19 19:19:57 +08:00
|
|
|
PG_USER=$(grep -E '^POSTGRES_USER=' .env | cut -d= -f2)
|
|
|
|
|
PG_USER=${PG_USER:-triton}
|
|
|
|
|
PG_DB=$(grep -E '^POSTGRES_DB=' .env | cut -d= -f2)
|
|
|
|
|
PG_DB=${PG_DB:-triton_manage}
|
|
|
|
|
|
|
|
|
|
"$RUNTIME" exec triton-manage-db pg_dump -U "$PG_USER" "$PG_DB" 2>/dev/null \
|
|
|
|
|
| gzip > "$DUMP_FILE" || die "pg_dump failed — aborting upgrade (DB container may not be running)"
|
|
|
|
|
info " backup saved: $DUMP_FILE"
|
2026-05-17 08:57:58 +02:00
|
|
|
|
2026-05-19 19:19:57 +08:00
|
|
|
# ── pull new image ────────────────────────────────────────────────────────
|
2026-05-17 08:57:58 +02:00
|
|
|
info "pulling latest image..."
|
|
|
|
|
"${COMPOSE[@]}" --env-file .env pull manage-server
|
|
|
|
|
|
2026-05-19 19:19:57 +08:00
|
|
|
# ── recreate container (DB migrations run on startup) ─────────────────────
|
2026-05-17 08:57:58 +02:00
|
|
|
info "recreating manage-server container..."
|
2026-05-19 19:19:57 +08:00
|
|
|
info " DB schema migrations will run automatically on startup"
|
2026-05-17 08:57:58 +02:00
|
|
|
"${COMPOSE[@]}" --env-file .env up -d --no-deps manage-server
|
|
|
|
|
|
2026-05-19 19:19:57 +08:00
|
|
|
# ── wait for healthy (confirms migrations succeeded) ──────────────────────
|
2026-05-17 08:57:58 +02:00
|
|
|
HOST_PORT=$(grep -E '^TRITON_MANAGE_HOST_PORT=' .env | cut -d= -f2)
|
|
|
|
|
HOST_PORT=${HOST_PORT:-8082}
|
|
|
|
|
|
2026-05-19 19:19:57 +08:00
|
|
|
info "waiting for server to become healthy on :${HOST_PORT}..."
|
2026-05-17 08:57:58 +02:00
|
|
|
for i in $(seq 1 30); do
|
|
|
|
|
CODE=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:${HOST_PORT}/" || echo "000")
|
|
|
|
|
if [[ "$CODE" == "302" || "$CODE" == "200" ]]; then
|
2026-05-19 19:19:57 +08:00
|
|
|
info "upgrade complete — server is healthy (migrations applied)"
|
|
|
|
|
info " rollback if needed: gunzip -c ${DUMP_FILE} | $RUNTIME exec -i triton-manage-db psql -U ${PG_USER} ${PG_DB}"
|
2026-05-17 08:57:58 +02:00
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
sleep 2
|
|
|
|
|
done
|
2026-05-19 19:19:57 +08:00
|
|
|
die "server did not become healthy in 60s — check logs: $RUNTIME logs triton-manageserver"
|