296 lines
8.5 KiB
Bash
296 lines
8.5 KiB
Bash
#!/bin/bash
|
|
|
|
# 🚀 MYP Starter-Skript (Linux/macOS)
|
|
# Automatisierte Bereitstellung des MYP-Systems
|
|
|
|
set -e
|
|
|
|
# Standardwerte
|
|
ENVIRONMENT="${1:-dev}"
|
|
CLEAN_FLAG=""
|
|
VERBOSE_FLAG=""
|
|
NO_CACHE_FLAG=""
|
|
|
|
# Farbkodierte Ausgabe
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
MAGENTA='\033[0;35m'
|
|
CYAN='\033[0;36m'
|
|
NC='\033[0m' # No Color
|
|
|
|
print_colored() {
|
|
local color="$1"
|
|
local message="$2"
|
|
echo -e "${color}${message}${NC}"
|
|
}
|
|
|
|
print_step() {
|
|
print_colored "$CYAN" "🔧 $1"
|
|
}
|
|
|
|
print_success() {
|
|
print_colored "$GREEN" "✅ $1"
|
|
}
|
|
|
|
print_warning() {
|
|
print_colored "$YELLOW" "⚠️ $1"
|
|
}
|
|
|
|
print_error() {
|
|
print_colored "$RED" "❌ $1"
|
|
}
|
|
|
|
# Hilfefunktion
|
|
show_help() {
|
|
echo "Verwendung: $0 [UMGEBUNG] [OPTIONEN]"
|
|
echo ""
|
|
echo "UMGEBUNG:"
|
|
echo " dev Entwicklungsumgebung (Standard)"
|
|
echo " prod Produktionsumgebung"
|
|
echo " test Testumgebung"
|
|
echo ""
|
|
echo "OPTIONEN:"
|
|
echo " --clean Bereinigung vor Start"
|
|
echo " --verbose Ausführliche Ausgabe"
|
|
echo " --no-cache Docker Build ohne Cache"
|
|
echo " --help Diese Hilfe anzeigen"
|
|
echo ""
|
|
echo "Beispiele:"
|
|
echo " $0 dev --clean"
|
|
echo " $0 prod --no-cache"
|
|
}
|
|
|
|
# Parameter verarbeiten
|
|
while [[ $# -gt 0 ]]; do
|
|
case $1 in
|
|
dev|prod|test)
|
|
ENVIRONMENT="$1"
|
|
;;
|
|
--clean)
|
|
CLEAN_FLAG="--clean"
|
|
;;
|
|
--verbose)
|
|
VERBOSE_FLAG="--verbose"
|
|
;;
|
|
--no-cache)
|
|
NO_CACHE_FLAG="--no-cache"
|
|
;;
|
|
--help)
|
|
show_help
|
|
exit 0
|
|
;;
|
|
*)
|
|
print_error "Unbekannter Parameter: $1"
|
|
show_help
|
|
exit 1
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
# Umgebung validieren
|
|
if [[ ! "$ENVIRONMENT" =~ ^(dev|prod|test)$ ]]; then
|
|
print_error "Ungültige Umgebung: $ENVIRONMENT"
|
|
show_help
|
|
exit 1
|
|
fi
|
|
|
|
# Header
|
|
print_colored "$MAGENTA" "╔═══════════════════════════════════════════════════════╗"
|
|
print_colored "$MAGENTA" "║ 🖨️ MYP - Manage your Printer ║"
|
|
print_colored "$MAGENTA" "║ Starter-Skript ║"
|
|
print_colored "$MAGENTA" "║ Umgebung: $(printf "%-12s" "${ENVIRONMENT^^}") ║"
|
|
print_colored "$MAGENTA" "╚═══════════════════════════════════════════════════════╝"
|
|
|
|
# Systemvoraussetzungen prüfen
|
|
print_step "Überprüfe Systemvoraussetzungen..."
|
|
|
|
# Docker prüfen
|
|
if ! command -v docker &> /dev/null; then
|
|
print_error "Docker ist nicht installiert oder nicht im PATH verfügbar."
|
|
echo "Installation:"
|
|
echo " Ubuntu/Debian: sudo apt-get install docker.io"
|
|
echo " CentOS/RHEL: sudo yum install docker"
|
|
echo " macOS: brew install docker oder Docker Desktop"
|
|
exit 1
|
|
fi
|
|
|
|
# Docker Daemon prüfen
|
|
if ! docker info &> /dev/null; then
|
|
print_error "Docker Daemon läuft nicht."
|
|
echo "Starten Sie den Docker Service:"
|
|
echo " Linux: sudo systemctl start docker"
|
|
echo " macOS: Docker Desktop starten"
|
|
exit 1
|
|
fi
|
|
|
|
print_success "Docker ist verfügbar und läuft"
|
|
|
|
# Docker Compose prüfen
|
|
if ! command -v docker-compose &> /dev/null; then
|
|
print_error "Docker Compose ist nicht installiert."
|
|
echo "Installation: https://docs.docker.com/compose/install/"
|
|
exit 1
|
|
fi
|
|
|
|
# Git prüfen (für Entwicklungsumgebung)
|
|
if [[ "$ENVIRONMENT" == "dev" ]] && ! command -v git &> /dev/null; then
|
|
print_warning "Git ist nicht installiert. Empfohlen für Entwicklungsumgebung."
|
|
fi
|
|
|
|
# Berechtigung für ausführende Skripte setzen
|
|
chmod +x infrastructure/scripts/*.sh 2>/dev/null || true
|
|
|
|
# Umgebungskonfiguration laden
|
|
ENV_FILE="infrastructure/environments/${ENVIRONMENT}.env"
|
|
if [[ -f "$ENV_FILE" ]]; then
|
|
print_step "Lade Umgebungskonfiguration: $ENV_FILE"
|
|
set -a # Automatisch alle Variablen exportieren
|
|
source "$ENV_FILE"
|
|
set +a
|
|
|
|
if [[ -n "$VERBOSE_FLAG" ]]; then
|
|
echo "Geladene Umgebungsvariablen:"
|
|
grep -E '^[^#].*=' "$ENV_FILE" | sed 's/^/ /'
|
|
fi
|
|
else
|
|
print_warning "Umgebungsdatei nicht gefunden: $ENV_FILE"
|
|
echo "Verwende Standardkonfiguration..."
|
|
fi
|
|
|
|
# Bereinigung wenn angefordert
|
|
if [[ -n "$CLEAN_FLAG" ]]; then
|
|
print_step "Führe Bereinigung durch..."
|
|
if [[ -f "infrastructure/scripts/cleanup.sh" ]]; then
|
|
bash infrastructure/scripts/cleanup.sh --force
|
|
else
|
|
print_warning "Bereinigungs-Skript nicht gefunden"
|
|
fi
|
|
fi
|
|
|
|
# SSH-Server aktivieren (Linux)
|
|
print_step "Konfiguriere SSH-Server..."
|
|
if command -v systemctl &> /dev/null; then
|
|
# SystemD-basierte Systeme
|
|
if systemctl is-enabled ssh &> /dev/null || systemctl is-enabled sshd &> /dev/null; then
|
|
sudo systemctl enable ssh 2>/dev/null || sudo systemctl enable sshd 2>/dev/null || true
|
|
sudo systemctl start ssh 2>/dev/null || sudo systemctl start sshd 2>/dev/null || true
|
|
print_success "SSH-Server aktiviert"
|
|
else
|
|
print_warning "SSH-Server ist nicht installiert"
|
|
echo "Installation:"
|
|
echo " Ubuntu/Debian: sudo apt-get install openssh-server"
|
|
echo " CentOS/RHEL: sudo yum install openssh-server"
|
|
fi
|
|
elif [[ "$OSTYPE" == "darwin"* ]]; then
|
|
# macOS
|
|
if sudo systemsetup -getremotelogin | grep -q "On"; then
|
|
print_success "SSH-Server ist bereits aktiviert"
|
|
else
|
|
sudo systemsetup -setremotelogin on
|
|
print_success "SSH-Server aktiviert"
|
|
fi
|
|
else
|
|
print_warning "SSH-Server Konfiguration für dieses System nicht unterstützt"
|
|
fi
|
|
|
|
# Docker Compose Dateien bestimmen
|
|
COMPOSE_FILES=("docker-compose.yml")
|
|
if [[ "$ENVIRONMENT" == "dev" ]]; then
|
|
COMPOSE_FILES+=("docker-compose.dev.yml")
|
|
fi
|
|
if [[ -f "docker-compose.override.yml" ]]; then
|
|
COMPOSE_FILES+=("docker-compose.override.yml")
|
|
fi
|
|
|
|
# Compose-Argumente erstellen
|
|
COMPOSE_ARGS=()
|
|
for file in "${COMPOSE_FILES[@]}"; do
|
|
COMPOSE_ARGS+=("-f" "$file")
|
|
done
|
|
|
|
# Build-Parameter
|
|
BUILD_ARGS=()
|
|
if [[ -n "$NO_CACHE_FLAG" ]]; then
|
|
BUILD_ARGS+=("--no-cache")
|
|
fi
|
|
|
|
# Container erstellen und starten
|
|
print_step "Erstelle und starte Container..."
|
|
echo "Verwende Compose-Dateien: ${COMPOSE_FILES[*]}"
|
|
|
|
# Images bauen
|
|
if [[ ${#BUILD_ARGS[@]} -gt 0 ]]; then
|
|
docker-compose "${COMPOSE_ARGS[@]}" build "${BUILD_ARGS[@]}"
|
|
else
|
|
docker-compose "${COMPOSE_ARGS[@]}" build
|
|
fi
|
|
|
|
# Services starten
|
|
docker-compose "${COMPOSE_ARGS[@]}" up -d
|
|
|
|
print_success "Container erfolgreich gestartet"
|
|
|
|
# Warten auf Service-Bereitschaft
|
|
print_step "Warte auf Service-Bereitschaft..."
|
|
MAX_WAIT_TIME=120 # Sekunden
|
|
WAIT_INTERVAL=5
|
|
ELAPSED=0
|
|
|
|
while [[ $ELAPSED -lt $MAX_WAIT_TIME ]]; do
|
|
if curl -s http://localhost:5000/health > /dev/null 2>&1 && \
|
|
curl -s http://localhost:3000 > /dev/null 2>&1; then
|
|
echo ""
|
|
print_success "Alle Services sind bereit!"
|
|
break
|
|
fi
|
|
|
|
echo -n "."
|
|
sleep $WAIT_INTERVAL
|
|
ELAPSED=$((ELAPSED + WAIT_INTERVAL))
|
|
done
|
|
|
|
if [[ $ELAPSED -ge $MAX_WAIT_TIME ]]; then
|
|
echo ""
|
|
print_warning "Timeout beim Warten auf Services erreicht"
|
|
echo "Überprüfen Sie die Container-Logs mit: docker-compose logs"
|
|
fi
|
|
|
|
# Status-Ausgabe
|
|
print_step "Container-Status:"
|
|
docker-compose "${COMPOSE_ARGS[@]}" ps
|
|
|
|
print_step "Service-URLs:"
|
|
print_colored "$GREEN" "🌐 Web Interface: http://localhost (oder https://localhost)"
|
|
print_colored "$GREEN" "🔧 Backend API: http://localhost:5000"
|
|
print_colored "$GREEN" "⚛️ Frontend: http://localhost:3000"
|
|
|
|
if [[ "$ENVIRONMENT" == "dev" ]]; then
|
|
print_colored "$BLUE" "📊 Monitoring: http://localhost:9090 (Prometheus)"
|
|
print_colored "$BLUE" "📈 Dashboards: http://localhost:3001 (Grafana)"
|
|
fi
|
|
|
|
print_step "Nützliche Befehle:"
|
|
echo " Logs anzeigen: docker-compose logs -f"
|
|
echo " Services stoppen: docker-compose down"
|
|
echo " Status prüfen: docker-compose ps"
|
|
echo " Neustart: docker-compose restart"
|
|
|
|
print_success "MYP-System erfolgreich gestartet! 🎉"
|
|
|
|
# Optional: Automatisch Browser öffnen
|
|
if [[ "$ENVIRONMENT" == "dev" ]]; then
|
|
read -p "Browser öffnen? (j/N): " -n 1 -r
|
|
echo
|
|
if [[ $REPLY =~ ^[Jj]$ ]]; then
|
|
if command -v xdg-open &> /dev/null; then
|
|
xdg-open http://localhost
|
|
elif command -v open &> /dev/null; then
|
|
open http://localhost
|
|
else
|
|
print_warning "Kein Befehl zum Öffnen des Browsers gefunden"
|
|
fi
|
|
fi
|
|
fi |