Sync HTPC Manifests to GitOps / Sync Manifests to GitOps Repo (push) Successful in 4s
113 lines
5.4 KiB
Bash
Executable File
113 lines
5.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# ==============================================================================
|
|
# Radarr Migration Script: Host Service -> Kubernetes Cluster met PostgreSQL
|
|
# ==============================================================================
|
|
set -euo pipefail
|
|
|
|
CLUSTER_HOST="10.2.0.4"
|
|
SSH_USER="bart"
|
|
HTPC_NS="htpc"
|
|
|
|
COLOR_GREEN="\033[0;32m"
|
|
COLOR_YELLOW="\033[1;33m"
|
|
COLOR_RED="\033[0;31m"
|
|
COLOR_BLUE="\033[0;34m"
|
|
COLOR_RESET="\033[0m"
|
|
|
|
log_info() { echo -e "${COLOR_GREEN}[INFO]${COLOR_RESET} $1"; }
|
|
log_step() { echo -e "\n${COLOR_BLUE}==>${COLOR_RESET} ${COLOR_GREEN}$1${COLOR_RESET}"; }
|
|
log_warn() { echo -e "${COLOR_YELLOW}[WARN]${COLOR_RESET} $1"; }
|
|
log_err() { echo -e "${COLOR_RED}[ERROR]${COLOR_RESET} $1"; }
|
|
|
|
echo "========================================================================"
|
|
echo " RADARR MIGRATIE NAAR KUBERNETES & POSTGRESQL (NAMESPACE: HTPC)"
|
|
echo "========================================================================"
|
|
echo "Bron: radarr.service op $CLUSTER_HOST (SQLite)"
|
|
echo "Doel: K8s Pod in '$HTPC_NS' gekoppeld aan PostgreSQL in 'cluster-infra'"
|
|
echo "Downtime: Geschat op minder dan 1 minuut"
|
|
echo "========================================================================"
|
|
echo ""
|
|
|
|
read -p "Weet je zeker dat je de migratie naar PostgreSQL wilt starten? (j/N): " -r CONFIRM
|
|
if [[ ! "$CONFIRM" =~ ^[jJyY]$ ]]; then
|
|
log_info "Migratie geannuleerd."
|
|
exit 0
|
|
fi
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# STAP 1: PRE-FLIGHT CHECKS
|
|
# ------------------------------------------------------------------------------
|
|
log_step "1. Pre-flight checks uitvoeren..."
|
|
|
|
log_info "Verbinding testen naar host ($CLUSTER_HOST)..."
|
|
ssh -o BatchMode=yes -o ConnectTimeout=5 "${SSH_USER}@${CLUSTER_HOST}" "true" || {
|
|
log_err "Kan niet verbinden met host (${CLUSTER_HOST}) via SSH."
|
|
exit 1
|
|
}
|
|
|
|
log_info "Controleren van PostgreSQL status in cluster-infra..."
|
|
kubectl get pod postgresql-0 -n cluster-infra -o jsonpath='{.status.phase}' | grep -q "Running" || {
|
|
log_err "PostgreSQL pod in cluster-infra is niet Running!"
|
|
exit 1
|
|
}
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# STAP 2: STANDALONE RADARR SERVICE STOPPEN OP DE HOST
|
|
# ------------------------------------------------------------------------------
|
|
log_step "2. Standalone radarr.service stoppen en uitschakelen op $CLUSTER_HOST..."
|
|
ssh -t "${SSH_USER}@${CLUSTER_HOST}" "sudo systemctl stop radarr && sudo systemctl disable radarr"
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# STAP 3: KUBERNETES MANIFESTS TOEPASSEN & HERSTARTEN
|
|
# ------------------------------------------------------------------------------
|
|
log_step "3. Kubernetes manifests toepassen (Secret, PVC, Service, Deployment)..."
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
kubectl apply -k "${SCRIPT_DIR}/k8s/"
|
|
|
|
log_info "Pod herstarten om de nieuwe PostgreSQL configuratie te laden..."
|
|
kubectl rollout restart deployment/radarr -n "${HTPC_NS}"
|
|
|
|
log_info "Wachten op succesvolle uitrol van Radarr pod..."
|
|
kubectl rollout status deployment/radarr -n "${HTPC_NS}" --timeout=120s
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# STAP 4: OPTIONEEL: MEDIACOVERS OVERZETTEN
|
|
# ------------------------------------------------------------------------------
|
|
log_step "4. Bestaande mediacovers (posters/fanart) overzetten..."
|
|
RADARR_PV=$(kubectl get pv -o jsonpath='{range .items[?(@.spec.claimRef.name=="radarr-config")]}{.spec.hostPath.path}{end}')
|
|
if [ -n "$RADARR_PV" ] && [ -d "/storage/var/lib/radarr/MediaCover" ]; then
|
|
ssh -t "${SSH_USER}@${CLUSTER_HOST}" "
|
|
if [ -d '/storage/var/lib/radarr/MediaCover' ]; then
|
|
echo 'Kopiëren van bestaande MediaCover map...'
|
|
sudo cp -a /storage/var/lib/radarr/MediaCover '$RADARR_PV/'
|
|
sudo chown -R 33:33 '$RADARR_PV/MediaCover'
|
|
fi
|
|
" || true
|
|
fi
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# STAP 5: VERIFICATIE
|
|
# ------------------------------------------------------------------------------
|
|
log_step "5. Verificatie van Radarr endpoints & PostgreSQL tabellen..."
|
|
sleep 5
|
|
|
|
log_info "Lokale ping testen via host..."
|
|
ssh -o BatchMode=yes "${SSH_USER}@${CLUSTER_HOST}" "curl -I -s http://127.0.0.1:7878/radarr/ping" | head -n 1
|
|
|
|
log_info "Cluster IP service testen..."
|
|
CLUSTER_IP=$(kubectl get svc radarr -n "${HTPC_NS}" -o jsonpath='{.spec.clusterIP}')
|
|
ssh -o BatchMode=yes "${SSH_USER}@${CLUSTER_HOST}" "curl -I -s http://${CLUSTER_IP}:7878/radarr/ping" | head -n 1
|
|
|
|
log_info "Aantal tabellen in PostgreSQL radarr-main controleren..."
|
|
TABLE_COUNT=$(kubectl exec -n cluster-infra postgresql-0 -- psql -U radarr -d radarr-main -t -c "SELECT count(*) FROM information_schema.tables WHERE table_schema = 'public';" | tr -d ' ')
|
|
echo "Aangemaakte tabellen in PostgreSQL 'radarr-main': ${TABLE_COUNT}"
|
|
|
|
echo ""
|
|
echo "========================================================================"
|
|
log_info "MIGRATIE NAAR POSTGRESQL SUCCESVOL AFGEROND!"
|
|
echo "Radarr draait nu als pod in Kubernetes namespace '${HTPC_NS}' op PostgreSQL."
|
|
echo "URL: https://thuus.bnhmeijer.nl/radarr/"
|
|
echo "API Key behouden: e4ffa6273473460eb57073ff861534ae"
|
|
echo "Bestaande films importeren: Add Movies -> Import Existing Movies -> /datadisk/htpc/Films"
|
|
echo "========================================================================"
|