284 lines
8.4 KiB
PowerShell
284 lines
8.4 KiB
PowerShell
# MYP Backend - Windows PowerShell Installations-Skript
|
|
# Installiert das Backend für Produktionsbetrieb oder Entwicklung
|
|
|
|
param(
|
|
[switch]$Production,
|
|
[switch]$Development,
|
|
[switch]$Clean,
|
|
[switch]$Logs,
|
|
[switch]$Help
|
|
)
|
|
|
|
# Farben für PowerShell
|
|
$Red = "Red"
|
|
$Green = "Green"
|
|
$Yellow = "Yellow"
|
|
$Blue = "Cyan"
|
|
|
|
function Write-Log {
|
|
param([string]$Message, [string]$Color = "White")
|
|
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
|
Write-Host "[$timestamp] $Message" -ForegroundColor $Color
|
|
}
|
|
|
|
function Write-Success {
|
|
param([string]$Message)
|
|
Write-Log "SUCCESS: $Message" -Color $Green
|
|
}
|
|
|
|
function Write-Warning {
|
|
param([string]$Message)
|
|
Write-Log "WARNING: $Message" -Color $Yellow
|
|
}
|
|
|
|
function Write-Error {
|
|
param([string]$Message)
|
|
Write-Log "FEHLER: $Message" -Color $Red
|
|
}
|
|
|
|
# Verbesserte Funktion zum Parsen der Umgebungsvariablen
|
|
function Set-EnvironmentFromFile {
|
|
param([string]$FilePath)
|
|
|
|
if (-not (Test-Path $FilePath)) {
|
|
Write-Warning "$FilePath nicht gefunden"
|
|
return
|
|
}
|
|
|
|
Write-Log "⚙️ Lade Umgebungsvariablen aus $FilePath..." -Color $Blue
|
|
|
|
try {
|
|
$EnvContent = Get-Content $FilePath -Raw
|
|
$Lines = $EnvContent -split "`r?`n"
|
|
|
|
foreach ($Line in $Lines) {
|
|
# Überspringe leere Zeilen und Kommentare
|
|
if ([string]::IsNullOrWhiteSpace($Line) -or $Line.TrimStart().StartsWith('#')) {
|
|
continue
|
|
}
|
|
|
|
# Finde den ersten = Zeichen
|
|
$EqualIndex = $Line.IndexOf('=')
|
|
if ($EqualIndex -le 0) {
|
|
continue
|
|
}
|
|
|
|
# Extrahiere Key und Value
|
|
$Key = $Line.Substring(0, $EqualIndex).Trim()
|
|
$Value = $Line.Substring($EqualIndex + 1).Trim()
|
|
|
|
# Entferne umgebende Anführungszeichen, falls vorhanden
|
|
if (($Value.StartsWith('"') -and $Value.EndsWith('"')) -or
|
|
($Value.StartsWith("'") -and $Value.EndsWith("'"))) {
|
|
$Value = $Value.Substring(1, $Value.Length - 2)
|
|
}
|
|
|
|
# Setze Umgebungsvariable
|
|
if (-not [string]::IsNullOrWhiteSpace($Key)) {
|
|
[Environment]::SetEnvironmentVariable($Key, $Value, "Process")
|
|
Write-Log "Geladen: $Key" -Color $Blue
|
|
}
|
|
}
|
|
|
|
Write-Success "Umgebungsvariablen erfolgreich geladen"
|
|
|
|
} catch {
|
|
Write-Error "Fehler beim Laden der Umgebungsvariablen: $_"
|
|
Write-Warning "Verwende Standard-Umgebungsvariablen"
|
|
}
|
|
}
|
|
|
|
# Banner
|
|
Write-Host "========================================" -ForegroundColor $Blue
|
|
Write-Host "🏭 MYP Backend - Windows Installation" -ForegroundColor $Blue
|
|
Write-Host "========================================" -ForegroundColor $Blue
|
|
|
|
if ($Help) {
|
|
Write-Host @"
|
|
Verwendung: .\install.ps1 [OPTIONEN]
|
|
|
|
OPTIONEN:
|
|
-Production Produktions-Installation
|
|
-Development Entwicklungs-Installation
|
|
-Clean Bereinige vorherige Installation
|
|
-Logs Zeige detaillierte Logs
|
|
-Help Zeige diese Hilfe
|
|
|
|
BEISPIELE:
|
|
.\install.ps1 -Production
|
|
.\install.ps1 -Development -Logs
|
|
.\install.ps1 -Clean
|
|
"@
|
|
exit 0
|
|
}
|
|
|
|
# Bestimme Installationsmodus
|
|
$InstallMode = "development"
|
|
if ($Production) {
|
|
$InstallMode = "production"
|
|
Write-Log "🏭 Produktions-Installation gestartet" -Color $Blue
|
|
} elseif ($Development) {
|
|
$InstallMode = "development"
|
|
Write-Log "🔧 Entwicklungs-Installation gestartet" -Color $Blue
|
|
} else {
|
|
Write-Log "🔧 Standard-Installation (Entwicklung)" -Color $Blue
|
|
}
|
|
|
|
# Arbeitsverzeichnis prüfen
|
|
$CurrentDir = Get-Location
|
|
Write-Log "Arbeitsverzeichnis: $CurrentDir"
|
|
|
|
if (-not (Test-Path "app.py")) {
|
|
Write-Error "app.py nicht gefunden! Bitte im Backend-Verzeichnis ausführen."
|
|
exit 1
|
|
}
|
|
|
|
# Python-Version prüfen
|
|
Write-Log "🐍 Prüfe Python-Installation..." -Color $Blue
|
|
|
|
try {
|
|
$PythonVersion = python --version 2>&1
|
|
Write-Log "Python-Version: $PythonVersion"
|
|
|
|
# Prüfe Mindestversion (3.8+)
|
|
$VersionMatch = $PythonVersion -match "Python (\d+)\.(\d+)"
|
|
if ($VersionMatch) {
|
|
$Major = [int]$Matches[1]
|
|
$Minor = [int]$Matches[2]
|
|
|
|
if ($Major -lt 3 -or ($Major -eq 3 -and $Minor -lt 8)) {
|
|
Write-Error "Python 3.8+ erforderlich, gefunden: $PythonVersion"
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
Write-Success "Python-Version ist kompatibel"
|
|
} catch {
|
|
Write-Error "Python nicht gefunden! Bitte Python 3.8+ installieren."
|
|
exit 1
|
|
}
|
|
|
|
# Bereinigung (falls gewünscht)
|
|
if ($Clean) {
|
|
Write-Log "🧹 Bereinige vorherige Installation..." -Color $Yellow
|
|
|
|
if (Test-Path "instance") {
|
|
Remove-Item -Recurse -Force "instance"
|
|
Write-Log "Datenbank-Verzeichnis entfernt"
|
|
}
|
|
|
|
if (Test-Path "logs") {
|
|
Remove-Item -Recurse -Force "logs"
|
|
Write-Log "Log-Verzeichnis entfernt"
|
|
}
|
|
|
|
Write-Success "Bereinigung abgeschlossen"
|
|
}
|
|
|
|
# Erstelle erforderliche Verzeichnisse
|
|
Write-Log "📁 Erstelle Verzeichnisse..." -Color $Blue
|
|
|
|
$Directories = @("instance", "logs", "uploads")
|
|
foreach ($Dir in $Directories) {
|
|
if (-not (Test-Path $Dir)) {
|
|
New-Item -ItemType Directory -Path $Dir | Out-Null
|
|
Write-Log "Verzeichnis erstellt: $Dir"
|
|
} else {
|
|
Write-Log "Verzeichnis existiert bereits: $Dir"
|
|
}
|
|
}
|
|
|
|
# Installiere Python-Dependencies
|
|
Write-Log "📦 Installiere Python-Pakete..." -Color $Blue
|
|
|
|
if (Test-Path "requirements.txt") {
|
|
try {
|
|
if ($Logs) {
|
|
pip install -r requirements.txt
|
|
} else {
|
|
pip install -r requirements.txt --quiet
|
|
}
|
|
Write-Success "Python-Pakete installiert"
|
|
} catch {
|
|
Write-Error "Fehler beim Installieren der Python-Pakete: $_"
|
|
exit 1
|
|
}
|
|
} else {
|
|
Write-Warning "requirements.txt nicht gefunden"
|
|
}
|
|
|
|
# Umgebungskonfiguration
|
|
Write-Log "⚙️ Konfiguriere Umgebung..." -Color $Blue
|
|
|
|
# Lade Umgebungsvariablen für Tests
|
|
Set-EnvironmentFromFile "env.backend"
|
|
|
|
# Datenbank initialisieren
|
|
Write-Log "🗄️ Initialisiere Datenbank..." -Color $Blue
|
|
|
|
try {
|
|
$env:FLASK_APP = "app.py"
|
|
$env:FLASK_ENV = $InstallMode
|
|
|
|
# Verwende das Test-Skript für die Datenbank-Initialisierung
|
|
Write-Log "Führe Datenbank-Initialisierung über Test-Skript aus..."
|
|
python test-backend-setup.py
|
|
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-Success "Datenbank erfolgreich initialisiert"
|
|
} else {
|
|
Write-Warning "Datenbank-Initialisierung mit Warnungen abgeschlossen"
|
|
}
|
|
} catch {
|
|
Write-Error "Fehler bei der Datenbank-Initialisierung: $_"
|
|
exit 1
|
|
}
|
|
|
|
# Konfigurationstest
|
|
Write-Log "🧪 Teste Konfiguration..." -Color $Blue
|
|
|
|
try {
|
|
python test-backend-setup.py | Out-Null
|
|
$TestResult = $LASTEXITCODE
|
|
|
|
if ($TestResult -eq 0) {
|
|
Write-Success "Alle Konfigurationstests bestanden"
|
|
} else {
|
|
Write-Warning "Einige Konfigurationstests fehlgeschlagen (Code: $TestResult)"
|
|
if ($Logs) {
|
|
Write-Log "Führe detaillierte Tests aus..."
|
|
python test-backend-setup.py
|
|
}
|
|
}
|
|
} catch {
|
|
Write-Warning "Konfigurationstest konnte nicht ausgeführt werden: $_"
|
|
}
|
|
|
|
# Installation abgeschlossen
|
|
Write-Host ""
|
|
Write-Host "========================================" -ForegroundColor $Green
|
|
Write-Host "✅ MYP Backend Installation abgeschlossen!" -ForegroundColor $Green
|
|
Write-Host "========================================" -ForegroundColor $Green
|
|
Write-Host ""
|
|
|
|
Write-Host "📋 Nächste Schritte:" -ForegroundColor $Blue
|
|
Write-Host "1. Backend starten:" -ForegroundColor $White
|
|
Write-Host " .\start-backend-server.ps1 -Development" -ForegroundColor $Yellow
|
|
Write-Host ""
|
|
Write-Host "2. Health-Check testen:" -ForegroundColor $White
|
|
Write-Host " curl http://localhost:5000/monitoring/health/simple" -ForegroundColor $Yellow
|
|
Write-Host ""
|
|
Write-Host "3. Logs überwachen:" -ForegroundColor $White
|
|
Write-Host " Get-Content logs\myp.log -Wait" -ForegroundColor $Yellow
|
|
Write-Host ""
|
|
|
|
if ($InstallMode -eq "production") {
|
|
Write-Host "🏭 Produktions-Hinweise:" -ForegroundColor $Blue
|
|
Write-Host "- Verwende einen Reverse Proxy (nginx/Apache)" -ForegroundColor $White
|
|
Write-Host "- Konfiguriere SSL/TLS-Zertifikate" -ForegroundColor $White
|
|
Write-Host "- Überwache Logs und Metriken" -ForegroundColor $White
|
|
Write-Host "- Führe regelmäßige Backups durch" -ForegroundColor $White
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Success "Installation erfolgreich abgeschlossen!" |