Projektarbeit-MYP/setup_hosts.ps1

73 lines
3.1 KiB
PowerShell

# MYP - Einrichtung der Hostnamen für die SSL-Konfiguration (Windows PowerShell Version)
# Überprüfen, ob das Skript als Administrator ausgeführt wird
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Write-Host "Dieses Skript muss mit Administrator-Rechten ausgeführt werden." -ForegroundColor Red
Write-Host "Bitte starten Sie PowerShell als Administrator und führen Sie das Skript erneut aus." -ForegroundColor Yellow
Exit 1
}
# Header anzeigen
Write-Host "=================================================" -ForegroundColor Blue
Write-Host " MYP - Hostnamen-Konfiguration für SSL" -ForegroundColor Blue
Write-Host "=================================================" -ForegroundColor Blue
Write-Host ""
# Lokale IP-Adresse ermitteln
$localIP = (Get-NetIPAddress | Where-Object { $_.AddressFamily -eq "IPv4" -and $_.PrefixOrigin -ne "WellKnown" } | Select-Object -First 1).IPAddress
if (-not $localIP) {
$localIP = "127.0.0.1"
Write-Host "Konnte lokale IP-Adresse nicht ermitteln, verwende $localIP" -ForegroundColor Yellow
} else {
Write-Host "Lokale IP-Adresse: $localIP" -ForegroundColor Green
}
# Pfad zur Hosts-Datei
$hostsFile = "$env:windir\System32\drivers\etc\hosts"
Write-Host "Hosts-Datei: $hostsFile" -ForegroundColor Cyan
# Prüfen, ob die Einträge bereits existieren
$frontendEntry = Select-String -Path $hostsFile -Pattern "m040tbaraspi001.de040.corpintra.net" -Quiet
$backendEntry = Select-String -Path $hostsFile -Pattern "raspberrypi" -Quiet
# Einträge in die Hosts-Datei schreiben
Write-Host "Aktualisiere Hosts-Datei..." -ForegroundColor Blue
$hostsContent = Get-Content -Path $hostsFile
if (-not $frontendEntry) {
$hostsContent += ""
$hostsContent += "# MYP Frontend Host"
$hostsContent += "$localIP m040tbaraspi001.de040.corpintra.net m040tbaraspi001"
Write-Host "Frontend-Hostname hinzugefügt" -ForegroundColor Green
} else {
Write-Host "Frontend-Hostname ist bereits konfiguriert" -ForegroundColor Yellow
}
if (-not $backendEntry) {
$hostsContent += ""
$hostsContent += "# MYP Backend Host"
$hostsContent += "$localIP raspberrypi"
Write-Host "Backend-Hostname hinzugefügt" -ForegroundColor Green
} else {
Write-Host "Backend-Hostname ist bereits konfiguriert" -ForegroundColor Yellow
}
# Speichern der aktualisierten Hosts-Datei
try {
$hostsContent | Set-Content -Path $hostsFile -Force
Write-Host ""
Write-Host "Konfiguration abgeschlossen!" -ForegroundColor Green
} catch {
Write-Host "Fehler beim Schreiben der Hosts-Datei: $_" -ForegroundColor Red
Exit 1
}
Write-Host "Folgende Hostnamen sind jetzt konfiguriert:" -ForegroundColor Blue
Write-Host " - Frontend: m040tbaraspi001.de040.corpintra.net" -ForegroundColor Yellow
Write-Host " - Backend: raspberrypi" -ForegroundColor Yellow
Write-Host ""
Write-Host "Sie können nun die Anwendung mit folgendem Befehl starten:" -ForegroundColor Blue
Write-Host "docker-compose up -d" -ForegroundColor Yellow
Write-Host ""