zkt26/sk1/resume-app.sh
2026-05-12 19:27:18 +02:00

101 lines
3.6 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# ==========================================
# resume-app.sh — Restarts AKS nodes and redeploys the app
#
# What this script does:
# 1. Scales the node pool to 1 → one VM starts up
# 2. Redeploys all Kubernetes objects (namespace, secret, configmap, etc.)
# 3. The existing PVC is remounted → data intact
#
# Prerequisites: prepare-app_test.sh must have been run at least once,
# and DB_USER / DB_PASSWORD must be exported.
# ==========================================
set -euo pipefail
# ==========================================
# LOGGER — source the shared library
# ==========================================
# shellcheck source=lib/logger.sh
source "$(dirname "$0")/lib/logger.sh" "resume"
trap 'log_error "Failure at line $LINENO. See $LOG_FILE"' ERR
# ==========================================
# VARIABLES
# ==========================================
RESOURCE_GROUP="ExamApp-RG"
AKS_NAME="ExamApp-AKS"
ACR_NAME="examappregistrycharles"
DB_NAME="postgres"
if [ -z "${DB_USER:-}" ] || [ -z "${DB_PASSWORD:-}" ]; then
log_error "DB_USER and DB_PASSWORD must be exported before running this script."
log_error " export DB_USER=\"postgres\""
log_error " export DB_PASSWORD=\"your_password\""
exit 1
fi
log_info "========================================"
log_info " Vigimeteo Resume"
log_info " Log: $LOG_FILE"
log_info "========================================"
# Step 1: Start the AKS cluster (restarts all previously deallocated VMs)
# az aks start is the counterpart of az aks stop used in pause-app.sh.
log_info "Starting AKS cluster '$AKS_NAME' (reallocating VMs)..."
az aks start \
--resource-group "$RESOURCE_GROUP" \
--name "$AKS_NAME"
log_info "AKS cluster started."
# Step 2: Reconfigure kubectl
az aks get-credentials \
--resource-group "$RESOURCE_GROUP" \
--name "$AKS_NAME" \
--overwrite-existing
log_info "kubectl configured."
# Step 3: Redeploy Kubernetes objects
ACR_LOGIN_SERVER=$(az acr show --name "$ACR_NAME" \
--resource-group "$RESOURCE_GROUP" --query loginServer --output tsv)
kubectl apply -f namespace.yaml
kubectl create secret generic db-credentials --namespace vigimeteo \
--from-literal=host="vigimeteo-db.vigimeteo.svc.cluster.local" \
--from-literal=port="5432" \
--from-literal=dbname="$DB_NAME" \
--from-literal=username="$DB_USER" \
--from-literal=password="$DB_PASSWORD" \
--dry-run=client -o yaml | kubectl apply -f -
log_info "Secret 'db-credentials' applied."
kubectl create configmap vigimeteo-db-init --namespace vigimeteo \
--from-file=init_db.sql=./sql/init_db.sql \
--dry-run=client -o yaml | kubectl apply -f -
log_info "ConfigMap 'vigimeteo-db-init' applied."
# The StatefulSet will remount the existing PVC — data is preserved
kubectl apply -f statefulset.yaml
kubectl apply -f service.yaml
sed "s|MON_REGISTRE|$ACR_LOGIN_SERVER|g" deployment.yaml | kubectl apply -f -
log_info "StatefulSet, Services and Deployment applied."
log_info "Waiting for all pods to be Ready (max 2 min)..."
if kubectl wait --for=condition=Ready pod \
--all -n vigimeteo --timeout=120s; then
log_info "All pods are Ready."
else
log_warn "Some pods are not yet Ready — check status:"
kubectl get pods -n vigimeteo | tee -a "$LOG_FILE"
fi
# Print the public URL as a reminder
PUBLIC_IP=$(kubectl get svc ingress-nginx-controller -n ingress-nginx \
-o jsonpath='{.status.loadBalancer.ingress[0].ip}' 2>/dev/null || echo "unknown")
log_info "========================================"
log_info "App is back online!"
log_info "Public IP: $PUBLIC_IP"
log_info "kubectl get pods -n vigimeteo"
log_info "========================================"