#!/bin/bash # Staging Quick-Start Script # Automatisiert häufige Staging-Deployment-Aufgaben set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Configuration STACKS_BASE_PATH="${STACKS_BASE_PATH:-~/deployment/stacks}" POSTGRESQL_PRODUCTION_PATH="${STACKS_BASE_PATH}/postgresql-production" POSTGRESQL_STAGING_PATH="${STACKS_BASE_PATH}/postgresql-staging" STAGING_STACK_PATH="${STACKS_BASE_PATH}/staging" # Functions print_header() { echo "" echo -e "${BLUE}========================================${NC}" echo -e "${BLUE}$1${NC}" echo -e "${BLUE}========================================${NC}" echo "" } print_success() { echo -e "${GREEN}✅ $1${NC}" } print_error() { echo -e "${RED}❌ $1${NC}" } print_warning() { echo -e "${YELLOW}⚠️ $1${NC}" } print_info() { echo -e "${BLUE}ℹ️ $1${NC}" } check_docker() { if ! command -v docker &> /dev/null; then print_error "Docker ist nicht installiert oder nicht im PATH" exit 1 fi if ! docker ps &> /dev/null; then print_error "Docker daemon läuft nicht oder keine Berechtigung" exit 1 fi print_success "Docker ist verfügbar" } check_networks() { print_header "Networks prüfen" local networks=("traefik-public" "staging-internal" "postgres-staging-internal" "postgres-production-internal" "app-internal") local all_exist=true for network in "${networks[@]}"; do if docker network inspect "$network" &> /dev/null; then print_success "Network '$network' existiert" else print_warning "Network '$network' existiert nicht" all_exist=false fi done if [ "$all_exist" = false ]; then print_info "Fehlende Networks werden beim Stack-Start automatisch erstellt" fi } start_postgresql_production() { print_header "PostgreSQL-Production Stack starten" if [ ! -d "$POSTGRESQL_PRODUCTION_PATH" ]; then print_error "PostgreSQL-Production Stack nicht gefunden: $POSTGRESQL_PRODUCTION_PATH" return 1 fi cd "$POSTGRESQL_PRODUCTION_PATH" # Check if .env exists if [ ! -f ".env" ]; then print_warning ".env-Datei nicht gefunden. Erstelle Beispiel-Konfiguration..." cat > .env < /dev/null; then print_success "PostgreSQL-Production ist bereit" return 0 fi attempt=$((attempt + 1)) sleep 2 done print_error "PostgreSQL-Production ist nicht bereit nach $max_attempts Versuchen" return 1 } start_postgresql_staging() { print_header "PostgreSQL-Staging Stack starten" if [ ! -d "$POSTGRESQL_STAGING_PATH" ]; then print_error "PostgreSQL-Staging Stack nicht gefunden: $POSTGRESQL_STAGING_PATH" return 1 fi cd "$POSTGRESQL_STAGING_PATH" # Check if .env exists if [ ! -f ".env" ]; then print_warning ".env-Datei nicht gefunden. Erstelle Beispiel-Konfiguration..." cat > .env < /dev/null; then print_success "PostgreSQL-Staging ist bereit" return 0 fi attempt=$((attempt + 1)) sleep 2 done print_error "PostgreSQL-Staging ist nicht bereit nach $max_attempts Versuchen" return 1 } verify_connections() { print_header "Datenbank-Verbindungen verifizieren" # Check Production if docker ps | grep -q postgres-production; then print_info "Teste Production-Datenbank-Verbindung..." if docker exec postgres-production pg_isready -U postgres -d michaelschiemer &> /dev/null; then print_success "PostgreSQL-Production erreichbar" else print_error "PostgreSQL-Production nicht erreichbar" fi else print_warning "PostgreSQL-Production Container läuft nicht" fi # Check Staging if docker ps | grep -q postgres-staging; then print_info "Teste Staging-Datenbank-Verbindung..." if docker exec postgres-staging pg_isready -U postgres -d michaelschiemer_staging &> /dev/null; then print_success "PostgreSQL-Staging erreichbar" else print_error "PostgreSQL-Staging nicht erreichbar" fi else print_warning "PostgreSQL-Staging Container läuft nicht" fi } show_status() { print_header "Container-Status" echo "PostgreSQL-Container:" docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep -E "(postgres-production|postgres-staging)" || echo "Keine PostgreSQL-Container gefunden" echo "" echo "Staging-Container:" docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep -E "staging-" || echo "Keine Staging-Container gefunden" } show_logs() { local container=$1 local lines=${2:-50} if docker ps --format "{{.Names}}" | grep -q "^${container}$"; then print_header "Logs: $container (letzte $lines Zeilen)" docker logs --tail "$lines" "$container" else print_warning "Container '$container' läuft nicht" fi } test_staging_connection() { print_header "Staging-Datenbank-Verbindung testen" if ! docker ps | grep -q staging-app; then print_warning "staging-app Container läuft nicht" return 1 fi print_info "Teste Verbindung von staging-app zu postgres-staging..." # Test network connectivity if docker exec staging-app nc -zv postgres-staging 5432 &> /dev/null; then print_success "Network-Verbindung zu postgres-staging erfolgreich" else print_error "Network-Verbindung zu postgres-staging fehlgeschlagen" print_info "Prüfe, ob staging-app im postgres-staging-internal Network ist" return 1 fi # Test database connection (if DB_PASSWORD is available) print_info "Teste Datenbank-Verbindung..." if docker exec staging-app php -r " \$host = getenv('DB_HOST') ?: 'postgres-staging'; \$db = getenv('DB_DATABASE') ?: 'michaelschiemer_staging'; \$user = getenv('DB_USERNAME') ?: 'postgres'; \$pass = getenv('DB_PASSWORD') ?: file_get_contents(getenv('DB_PASSWORD_FILE') ?: '/dev/null'); if (!\$pass) { echo 'DB_PASSWORD nicht verfügbar\n'; exit(1); } try { \$dsn = \"pgsql:host=\$host;port=5432;dbname=\$db\"; \$pdo = new PDO(\$dsn, \$user, trim(\$pass)); echo 'Connection successful: ' . \$pdo->query('SELECT version()')->fetchColumn() . \"\n\"; exit(0); } catch (Exception \$e) { echo 'Connection failed: ' . \$e->getMessage() . \"\n\"; exit(1); } " 2>&1; then print_success "Datenbank-Verbindung erfolgreich" else print_warning "Datenbank-Verbindungstest fehlgeschlagen (DB_PASSWORD möglicherweise nicht gesetzt)" fi } health_check() { print_header "Health-Checks" # Basic health check print_info "Basic Health Check: https://staging.michaelschiemer.de/health" if curl -f -k -s https://staging.michaelschiemer.de/health > /dev/null 2>&1; then print_success "Basic Health Check erfolgreich" else print_warning "Basic Health Check fehlgeschlagen (Service möglicherweise nicht verfügbar)" fi # Extended health check print_info "Extended Health Check: https://staging.michaelschiemer.de/admin/health/api/summary" local health_summary=$(curl -f -k -s https://staging.michaelschiemer.de/admin/health/api/summary 2>/dev/null || echo "") if [ -n "$health_summary" ]; then local overall_status=$(echo "$health_summary" | grep -o '"overall_status":"[^"]*"' | cut -d'"' -f4 || echo "unknown") print_info "Overall Health Status: $overall_status" if [ "$overall_status" = "healthy" ]; then print_success "Extended Health Check erfolgreich" else print_warning "Extended Health Check zeigt: $overall_status" fi else print_warning "Extended Health Check Endpoint nicht verfügbar" fi } # Main menu show_menu() { echo "" echo -e "${BLUE}========================================${NC}" echo -e "${BLUE} Staging Quick-Start Script${NC}" echo -e "${BLUE}========================================${NC}" echo "" echo "1) PostgreSQL-Production Stack starten" echo "2) PostgreSQL-Staging Stack starten" echo "3) Beide PostgreSQL-Stacks starten" echo "4) Networks prüfen" echo "5) Container-Status anzeigen" echo "6) Staging-Datenbank-Verbindung testen" echo "7) Health-Checks durchführen" echo "8) Logs anzeigen (PostgreSQL-Staging)" echo "9) Logs anzeigen (staging-app)" echo "10) Alles verifizieren" echo "0) Beenden" echo "" read -p "Wähle eine Option: " choice } # Main execution main() { print_header "Staging Quick-Start Script" check_docker while true; do show_menu case $choice in 1) start_postgresql_production ;; 2) start_postgresql_staging ;; 3) start_postgresql_production start_postgresql_staging ;; 4) check_networks ;; 5) show_status ;; 6) test_staging_connection ;; 7) health_check ;; 8) show_logs "postgres-staging" 50 ;; 9) show_logs "staging-app" 50 ;; 10) check_networks verify_connections show_status test_staging_connection health_check ;; 0) print_info "Beende..." exit 0 ;; *) print_error "Ungültige Option" ;; esac echo "" read -p "Drücke Enter um fortzufahren..." done } # Run main function main "$@"