diff --git a/local/service/system_service.go b/local/service/system_service.go index 8dd4368..8efffb1 100644 --- a/local/service/system_service.go +++ b/local/service/system_service.go @@ -39,15 +39,16 @@ func NewSystemService( func (s *SystemService) MigrateImage(ctx context.Context, stopRunning bool) (*model.MigrationResult, error) { image := env.GetACCImage() - reader, err := s.dockerClient.ImagePull(ctx, image, dockerimage.PullOptions{}) - if err != nil { - return nil, fmt.Errorf("failed to pull image %s: %v", image, err) + // Attempt to pull the image from a remote registry. This is best-effort — + // acc-wine is typically a locally-built image so the pull may not succeed, + // but container recreation below will still use whatever local image matches. + if reader, err := s.dockerClient.ImagePull(ctx, image, dockerimage.PullOptions{}); err != nil { + logging.Warn("Could not pull image %s (may be local-only): %v", image, err) + } else { + io.Copy(io.Discard, reader) + reader.Close() + logging.Info("Pulled image %s", image) } - if _, err := io.Copy(io.Discard, reader); err != nil { - logging.Warn("Error draining image pull response for %s: %v", image, err) - } - reader.Close() - logging.Info("Pulled image %s", image) servers, err := s.repo.GetAll(ctx, &model.ServerFilter{}) if err != nil { diff --git a/scripts/migrate-wine.sh b/scripts/migrate-wine.sh new file mode 100755 index 0000000..499f98b --- /dev/null +++ b/scripts/migrate-wine.sh @@ -0,0 +1,116 @@ +#!/usr/bin/env bash +# migrate-wine.sh — Pull the latest acc-wine image and migrate all server containers. +# +# Usage: +# ./scripts/migrate-wine.sh -u -p -b [--stop-running] +# +# Options: +# -u, --username API username +# -p, --password API password +# -b, --base-url Base URL of the manager (e.g. http://192.168.0.177:3000) +# --stop-running Also migrate currently running servers (stop → migrate → restart) +# +# Examples: +# ./scripts/migrate-wine.sh -u admin -p secret -b http://192.168.0.177:3000 +# ./scripts/migrate-wine.sh -u admin -p secret -b http://192.168.0.177:3000 --stop-running + +set -euo pipefail + +# ── Argument parsing ──────────────────────────────────────────────────────────── +USERNAME="" +PASSWORD="" +BASE_URL="" +STOP_RUNNING=false + +usage() { + grep '^#' "$0" | sed 's/^# \{0,1\}//' | tail -n +2 + exit 1 +} + +while [[ $# -gt 0 ]]; do + case "$1" in + -u|--username) USERNAME="$2"; shift 2 ;; + -p|--password) PASSWORD="$2"; shift 2 ;; + -b|--base-url) BASE_URL="$2"; shift 2 ;; + --stop-running) STOP_RUNNING=true; shift ;; + -h|--help) usage ;; + *) echo "Unknown option: $1"; usage ;; + esac +done + +[[ -z "$USERNAME" ]] && { echo "Error: --username is required"; usage; } +[[ -z "$PASSWORD" ]] && { echo "Error: --password is required"; usage; } +[[ -z "$BASE_URL" ]] && { echo "Error: --base-url is required"; usage; } + +BASE_URL="${BASE_URL%/}" # strip trailing slash +BASE_URL="${BASE_URL/#http:\/\//https://}" # upgrade http → https + +# ── Check dependencies ────────────────────────────────────────────────────────── +for cmd in curl jq; do + command -v "$cmd" &>/dev/null || { echo "Error: '$cmd' is required but not installed."; exit 1; } +done + +# ── Login ─────────────────────────────────────────────────────────────────────── +echo "Logging in as '$USERNAME'..." + +LOGIN_RESPONSE=$(curl -sL --post301 --post302 -X POST "${BASE_URL}/v1/auth/login" \ + -H "Content-Type: application/json" \ + -d "$(jq -n --arg u "$USERNAME" --arg p "$PASSWORD" '{username: $u, password: $p}')") + +TOKEN=$(echo "$LOGIN_RESPONSE" | jq -r '.token // empty' 2>/dev/null || true) +if [[ -z "$TOKEN" ]]; then + echo "Error: Login failed — no token in response." + exit 1 +fi +echo "Login successful." + +# ── Migrate ───────────────────────────────────────────────────────────────────── +echo "Starting image migration (stopRunning=${STOP_RUNNING})..." + +MIGRATE_RESPONSE=$(curl -sL --post301 --post302 -X POST "${BASE_URL}/v1/system/migrate-image" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer ${TOKEN}" \ + -d "{\"stopRunning\":${STOP_RUNNING}}") + +# ── Results ────────────────────────────────────────────────────────────────────── +SERVER_ERROR=$(echo "$MIGRATE_RESPONSE" | jq -r '.error // empty' 2>/dev/null || true) +if [[ -n "$SERVER_ERROR" ]]; then + echo "Error: Migration failed — $SERVER_ERROR" + echo "Full response: $MIGRATE_RESPONSE" + exit 1 +fi + +IMAGE=$(echo "$MIGRATE_RESPONSE" | jq -r '.image') +MIGRATED=$(echo "$MIGRATE_RESPONSE" | jq -r '.migrated | length') +SKIPPED=$(echo "$MIGRATE_RESPONSE" | jq -r '.skipped | length') +FAILED=$(echo "$MIGRATE_RESPONSE" | jq -r '.failed | length') + +echo "" +echo "Image: ${IMAGE}" +echo "────────────────────────────────" +printf " Migrated : %s\n" "$MIGRATED" +printf " Skipped : %s\n" "$SKIPPED" +printf " Failed : %s\n" "$FAILED" +echo "────────────────────────────────" + +if [[ "$MIGRATED" -gt 0 ]]; then + echo "" + echo "Migrated servers:" + echo "$MIGRATE_RESPONSE" | jq -r '.migrated[] | " - \(.name) (wasRunning: \(.wasRunning))"' +fi + +if [[ "$SKIPPED" -gt 0 ]]; then + echo "" + echo "Skipped (running, use --stop-running to include):" + echo "$MIGRATE_RESPONSE" | jq -r '.skipped[] | " - \(.name)"' +fi + +if [[ "$FAILED" -gt 0 ]]; then + echo "" + echo "Failed:" + echo "$MIGRATE_RESPONSE" | jq -r '.failed[] | " - \(.name): \(.error)"' + exit 1 +fi + +echo "" +echo "Done."