Files
Projektarbeit-MYP/infrastructure/scripts/start.ps1

242 lines
7.2 KiB
PowerShell
Raw 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.

# 🚀 MYP Starter-Skript (Windows PowerShell)
# Automatisierte Bereitstellung des MYP-Systems
param(
[Parameter(Position = 0)]
[ValidateSet("dev", "prod", "test")]
[string]$Environment = "dev",
[switch]$Clean,
[switch]$Verbose,
[switch]$NoCache
)
$ErrorActionPreference = "Stop"
# Farbkodierte Ausgabe
function Write-ColoredOutput {
param(
[string]$Message,
[string]$Color = "White"
)
Write-Host $Message -ForegroundColor $Color
}
function Write-Step {
param([string]$Message)
Write-ColoredOutput "🔧 $Message" "Cyan"
}
function Write-Success {
param([string]$Message)
Write-ColoredOutput "$Message" "Green"
}
function Write-Warning {
param([string]$Message)
Write-ColoredOutput "⚠️ $Message" "Yellow"
}
function Write-Error {
param([string]$Message)
Write-ColoredOutput "$Message" "Red"
}
# Header
Write-ColoredOutput @"
🖨 MYP - Manage your Printer
Starter-Skript
Umgebung: $($Environment.ToUpper().PadRight(12))
"@ "Magenta"
# Systemvoraussetzungen prüfen
Write-Step "Überprüfe Systemvoraussetzungen..."
# Docker prüfen
if (-not (Get-Command docker -ErrorAction SilentlyContinue)) {
Write-Error "Docker ist nicht installiert oder nicht im PATH verfügbar."
Write-Host "Bitte installieren Sie Docker Desktop: https://docs.docker.com/desktop/windows/"
exit 1
}
# Docker Daemon prüfen
try {
$dockerInfo = docker info 2>$null
if ($LASTEXITCODE -ne 0) {
Write-Error "Docker Daemon läuft nicht. Bitte starten Sie Docker Desktop."
exit 1
}
}
catch {
Write-Error "Fehler beim Zugriff auf Docker: $_"
exit 1
}
Write-Success "Docker ist verfügbar und läuft"
# Git prüfen (für Entwicklungsumgebung)
if ($Environment -eq "dev" -and -not (Get-Command git -ErrorAction SilentlyContinue)) {
Write-Warning "Git ist nicht installiert. Empfohlen für Entwicklungsumgebung."
}
# Umgebungskonfiguration laden
$envFile = "infrastructure/environments/$Environment.env"
if (Test-Path $envFile) {
Write-Step "Lade Umgebungskonfiguration: $envFile"
Get-Content $envFile | ForEach-Object {
if ($_ -match '^([^#][^=]*?)=(.*)$') {
[Environment]::SetEnvironmentVariable($matches[1], $matches[2], "Process")
if ($Verbose) {
Write-Host " $($matches[1])=$($matches[2])"
}
}
}
} else {
Write-Warning "Umgebungsdatei nicht gefunden: $envFile"
Write-Host "Verwende Standardkonfiguration..."
}
# Bereinigung wenn angefordert
if ($Clean) {
Write-Step "Führe Bereinigung durch..."
& "infrastructure/scripts/cleanup.ps1" -Force
if ($LASTEXITCODE -ne 0) {
Write-Error "Bereinigung fehlgeschlagen"
exit 1
}
}
# SSH-Server aktivieren (Windows)
Write-Step "Konfiguriere SSH-Server..."
try {
# OpenSSH Server Feature prüfen und installieren
$sshFeature = Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Server*'
if ($sshFeature.State -ne 'Installed') {
Write-Step "Installiere OpenSSH Server..."
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
}
# SSH Service konfigurieren
Set-Service -Name sshd -StartupType Automatic
Start-Service sshd
# Firewall-Regel erstellen
$firewallRule = Get-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -ErrorAction SilentlyContinue
if (-not $firewallRule) {
New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (TCP-In)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
}
Write-Success "SSH-Server aktiviert und konfiguriert"
} catch {
Write-Warning "SSH-Server Konfiguration fehlgeschlagen: $_"
}
# Docker Compose Datei bestimmen
$composeFiles = @("docker-compose.yml")
if ($Environment -eq "dev") {
$composeFiles += "docker-compose.dev.yml"
}
if (Test-Path "docker-compose.override.yml") {
$composeFiles += "docker-compose.override.yml"
}
$composeArgs = $composeFiles | ForEach-Object { "-f", $_ }
# Build Parameter
$buildArgs = @()
if ($NoCache) {
$buildArgs += "--no-cache"
}
# Container erstellen und starten
Write-Step "Erstelle und starte Container..."
Write-Host "Verwende Compose-Dateien: $($composeFiles -join ', ')"
try {
# Images bauen
if ($buildArgs.Count -gt 0) {
docker-compose @composeArgs build @buildArgs
} else {
docker-compose @composeArgs build
}
if ($LASTEXITCODE -ne 0) {
Write-Error "Docker Build fehlgeschlagen"
exit 1
}
# Services starten
docker-compose @composeArgs up -d
if ($LASTEXITCODE -ne 0) {
Write-Error "Docker Compose Start fehlgeschlagen"
exit 1
}
Write-Success "Container erfolgreich gestartet"
} catch {
Write-Error "Fehler beim Starten der Container: $_"
exit 1
}
# Warten auf Service-Bereitschaft
Write-Step "Warte auf Service-Bereitschaft..."
$maxWaitTime = 120 # Sekunden
$waitInterval = 5
$elapsed = 0
while ($elapsed -lt $maxWaitTime) {
try {
$backendHealth = Invoke-RestMethod -Uri "http://localhost:5000/health" -TimeoutSec 5 -ErrorAction SilentlyContinue
$frontendHealth = Invoke-WebRequest -Uri "http://localhost:3000" -TimeoutSec 5 -ErrorAction SilentlyContinue
if ($backendHealth -and $frontendHealth) {
Write-Success "Alle Services sind bereit!"
break
}
} catch {
# Services noch nicht bereit
}
Write-Host "." -NoNewline
Start-Sleep $waitInterval
$elapsed += $waitInterval
}
if ($elapsed -ge $maxWaitTime) {
Write-Warning "Timeout beim Warten auf Services erreicht"
Write-Host "Überprüfen Sie die Container-Logs mit: docker-compose logs"
}
# Status-Ausgabe
Write-Step "Container-Status:"
docker-compose @composeArgs ps
Write-Step "Service-URLs:"
Write-ColoredOutput "🌐 Web Interface: http://localhost (oder https://localhost)" "Green"
Write-ColoredOutput "🔧 Backend API: http://localhost:5000" "Green"
Write-ColoredOutput "⚛️ Frontend: http://localhost:3000" "Green"
if ($Environment -eq "dev") {
Write-ColoredOutput "📊 Monitoring: http://localhost:9090 (Prometheus)" "Blue"
Write-ColoredOutput "📈 Dashboards: http://localhost:3001 (Grafana)" "Blue"
}
Write-Step "Nützliche Befehle:"
Write-Host " Logs anzeigen: docker-compose logs -f"
Write-Host " Services stoppen: docker-compose down"
Write-Host " Status prüfen: docker-compose ps"
Write-Host " Neustart: docker-compose restart"
Write-Success "MYP-System erfolgreich gestartet! 🎉"
# Optional: Automatisch Browser öffnen
if ($Environment -eq "dev") {
$openBrowser = Read-Host "Browser öffnen? (j/N)"
if ($openBrowser -eq "j" -or $openBrowser -eq "J") {
Start-Process "http://localhost"
}
}