operator-migration/migration.sh
2026-10-02 20:05:12 +00:00

116 lines
5 KiB
Bash
Executable file

#!/bin/bash -eu
# =============================================================================
# migration.sh — WireGuard Go → FastAPI migration
# Run as Linux user (not root). Asks for sudo once.
# =============================================================================
if [ "$EUID" -eq 0 ]; then
echo "[X] Run as your Linux user, not root: bash migration.sh"
exit 1
fi
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
sudo chown -R "$USER:$USER" "$SCRIPT_DIR" 2>/dev/null || true
OPERATOR_DIR="$HOME/operator-docker"
BACKUP_DIR="$HOME/.wg-migration"
LOG="$BACKUP_DIR/migration.log"
REPO_URL="git@git.simplifiedprivacy.com:zenaku/operator-docker.git"
TOTAL_STEPS=4
# ─── Borrow UI from operator-docker if available, else minimal fallback ───────
if [ -f "$OPERATOR_DIR/lib/ui.sh" ]; then
source "$OPERATOR_DIR/lib/ui.sh"
source "$OPERATOR_DIR/lib/log.sh"
source "$OPERATOR_DIR/lib/steps.sh"
fail() { log_error "$1"; exit 1; }
else
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; CYAN='\033[0;36m'; RESET='\033[0m'
ok() { echo -e "${GREEN}[OK]${RESET} $1"; [ -f "$LOG" ] && echo "[OK] $1" >> "$LOG"; }
info() { echo -e "${CYAN}[i]${RESET} $1"; [ -f "$LOG" ] && echo "[i] $1" >> "$LOG"; }
warn() { echo -e "${YELLOW}[!]${RESET} $1"; [ -f "$LOG" ] && echo "[!] $1" >> "$LOG"; }
fail() { echo -e "${RED}[X]${RESET} $1"; [ -f "$LOG" ] && echo "[X] $1" >> "$LOG"; exit 1; }
print_header() { echo ""; echo " $1 $2"; echo ""; }
print_section() { echo -e "\n${CYAN}─── $1 ───${RESET}"; }
log_ok() { ok "$1"; }
log_info() { info "$1"; }
log_warn() { warn "$1"; }
log_error() { fail "$1"; }
fi
mkdir -p "$BACKUP_DIR"
: > "$LOG" 2>/dev/null || true
print_header "WireGuard Migration" "v1.0"
# ─── SUDO once ────────────────────────────────────────────────────────────────
info "Requesting sudo credentials (used throughout migration)..."
sudo -v || fail "sudo required. Check your permissions."
ok "Sudo credentials cached."
# ─── STEP 1: SSH + Clone ──────────────────────────────────────────────────────
print_section "Step 1/$TOTAL_STEPS — Verify SSH & Clone operator-docker"
[ -f "$HOME/.ssh/id_ed25519" ] || fail "SSH key not found. Generate one or run 00-backup.sh first."
SSH_TEST=$(GIT_SSH_COMMAND='ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10' \
ssh -T git@git.simplifiedprivacy.com 2>&1 || true)
if echo "$SSH_TEST" | grep -qiE "Welcome|successfully authenticated"; then
ok "SSH access authorized."
else
warn "SSH not authorized. Share this key with Simplified Privacy:"
echo ""
cat "$HOME/.ssh/id_ed25519.pub"
echo ""
fail "Once authorized, run again: bash migration.sh"
fi
if [ -d "$OPERATOR_DIR" ]; then
warn "operator-docker already exists — skipping clone."
else
info "Cloning operator-docker..."
GIT_SSH_COMMAND='ssh -o StrictHostKeyChecking=no' \
git clone "$REPO_URL" "$OPERATOR_DIR" || fail "Could not clone repository."
ok "Cloned to $OPERATOR_DIR"
fi
# Borrow UI from freshly cloned repo
if [ -f "$OPERATOR_DIR/lib/ui.sh" ]; then
source "$OPERATOR_DIR/lib/ui.sh"
source "$OPERATOR_DIR/lib/log.sh"
source "$OPERATOR_DIR/lib/steps.sh"
fi
fail() { log_error "$1"; exit 1; }
# ─── STEP 2: Backup & cleanup (needs root) ────────────────────────────────────
print_section "Step 2/$TOTAL_STEPS — Backup & cleanup"
[ -f "$BACKUP_DIR/private_key" ] && ok "Backup already exists — skipping step 2." || {
info "Running backup (sudo required)..."
sudo OPERATOR_DIR="$OPERATOR_DIR" bash "$SCRIPT_DIR/steps/00-backup.sh" || fail "Backup failed."
}
# ─── STEP 3: Install operator-docker ─────────────────────────────────────────
print_section "Step 3/$TOTAL_STEPS — Install operator-docker"
[ -f "$OPERATOR_DIR/.env" ] && ok ".env exists — skipping install." || {
info "Running installer..."
cd "$OPERATOR_DIR"
sudo systemctl start nginx 2>/dev/null || true
echo "$HOME" > /tmp/.migration_home
sudo MIGRATION_MODE=1 bash install.sh || { rm -f /tmp/.migration_home; fail "Installer failed."; }
rm -f /tmp/.migration_home
ok "operator-docker installed."
}
# ─── STEP 4: Migrate peers & identity ────────────────────────────────────────
print_section "Step 4/$TOTAL_STEPS — Migrate WireGuard identity & peers"
bash "$SCRIPT_DIR/steps/02-peers.sh" || fail "Peer migration failed."
echo ""
echo "========================================="
echo " Migration complete"
echo " Log: cat $LOG"
echo "========================================="