Files
michaelschiemer/deployment/scripts/staging-quick-start.sh
Michael Schiemer 36ef2a1e2c
Some checks failed
🚀 Build & Deploy Image / Determine Build Necessity (push) Failing after 10m14s
🚀 Build & Deploy Image / Build Runtime Base Image (push) Has been skipped
🚀 Build & Deploy Image / Build Docker Image (push) Has been skipped
🚀 Build & Deploy Image / Run Tests & Quality Checks (push) Has been skipped
🚀 Build & Deploy Image / Auto-deploy to Staging (push) Has been skipped
🚀 Build & Deploy Image / Auto-deploy to Production (push) Has been skipped
Security Vulnerability Scan / Check for Dependency Changes (push) Failing after 11m25s
Security Vulnerability Scan / Composer Security Audit (push) Has been cancelled
fix: Gitea Traefik routing and connection pool optimization
- Remove middleware reference from Gitea Traefik labels (caused routing issues)
- Optimize Gitea connection pool settings (MAX_IDLE_CONNS=30, authentication_timeout=180s)
- Add explicit service reference in Traefik labels
- Fix intermittent 504 timeouts by improving PostgreSQL connection handling

Fixes Gitea unreachability via git.michaelschiemer.de
2025-11-09 14:46:15 +01:00

377 lines
11 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
# 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 <<EOF
POSTGRES_DB=michaelschiemer
POSTGRES_USER=postgres
POSTGRES_PASSWORD=CHANGE_ME_STRONG_PASSWORD
BACKUP_RETENTION_DAYS=7
BACKUP_SCHEDULE=0 2 * * *
EOF
print_warning "Bitte POSTGRES_PASSWORD in .env anpassen!"
return 1
fi
print_info "Starte PostgreSQL-Production Stack..."
docker compose up -d
# Wait for PostgreSQL to be ready
print_info "Warte auf PostgreSQL-Production..."
local max_attempts=30
local attempt=0
while [ $attempt -lt $max_attempts ]; do
if docker exec postgres-production pg_isready -U postgres -d michaelschiemer &> /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 <<EOF
POSTGRES_DB=michaelschiemer_staging
POSTGRES_USER=postgres
POSTGRES_PASSWORD=CHANGE_ME_STRONG_PASSWORD
BACKUP_RETENTION_DAYS=3
BACKUP_SCHEDULE=0 3 * * *
EOF
print_warning "Bitte POSTGRES_PASSWORD in .env anpassen!"
return 1
fi
print_info "Starte PostgreSQL-Staging Stack..."
docker compose up -d
# Wait for PostgreSQL to be ready
print_info "Warte auf PostgreSQL-Staging..."
local max_attempts=30
local attempt=0
while [ $attempt -lt $max_attempts ]; do
if docker exec postgres-staging pg_isready -U postgres -d michaelschiemer_staging &> /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 "$@"