zkt26/z2/prepare-app.sh

56 lines
2.2 KiB
Bash

#!/usr/bin/env bash
# =============================================================================
# prepare-app.sh
# Prepares the application: builds Docker image into minikube,
# creates the host-path directory for the PersistentVolume.
# Run this ONCE before start-app.sh.
# =============================================================================
set -euo pipefail
echo "======================================================"
echo " Preparing K8s Todo App"
echo "======================================================"
# ── 1. Verify tools are available ─────────────────────────
echo ""
echo "[1/4] Checking required tools..."
for tool in minikube kubectl docker; do
if ! command -v "$tool" &>/dev/null; then
echo " ERROR: '$tool' is not installed or not in PATH."
exit 1
fi
echo "$tool found"
done
# ── 2. Ensure minikube is running ─────────────────────────
echo ""
echo "[2/4] Checking minikube status..."
STATUS=$(minikube status --format='{{.Host}}' 2>/dev/null || echo "Stopped")
if [ "$STATUS" != "Running" ]; then
echo " minikube is not running. Starting minikube..."
minikube start --driver=docker --memory=2048 --cpus=2
echo " ✓ minikube started"
else
echo " ✓ minikube is already running"
fi
# ── 3. Create host directory for PersistentVolume ─────────
echo ""
echo "[3/4] Creating PersistentVolume host directory inside minikube..."
minikube ssh -- "sudo mkdir -p /mnt/data/postgres && sudo chmod 777 /mnt/data/postgres"
echo " ✓ /mnt/data/postgres created inside minikube node"
# ── 4. Build Docker image inside minikube's Docker daemon ─
echo ""
echo "[4/4] Building Docker image 'todo-app:latest' inside minikube..."
# Point Docker CLI to minikube's daemon so image is available to k8s pods
eval "$(minikube docker-env)"
docker build -t todo-app:latest ./app
echo " ✓ Image 'todo-app:latest' built successfully"
echo ""
echo "======================================================"
echo " Preparation complete!"
echo " Now run: bash start-app.sh"
echo "======================================================"