Dateien nach "/" hochladen

This commit is contained in:
2026-08-13 23:40:25 +02:00
parent c0c333b053
commit b13442830b
+173 -60
View File
@@ -151,6 +151,92 @@ function Ensure-Modules {
} }
} }
function New-RepoStagingArea {
Enable-Tls12
$root = Join-Path $env:TEMP ("SMTPGraphRelay-repo-{0}" -f [guid]::NewGuid().ToString("N"))
$archive = Join-Path $root "main.zip"
$extract = Join-Path $root "extract"
New-Item -ItemType Directory -Path $extract -Force | Out-Null
Write-Info "Lade aktuellen Stand aus Gitea..."
Write-Info "Quelle: $RemoteArchiveUrl"
Invoke-WebRequest `
-Uri $RemoteArchiveUrl `
-OutFile $archive `
-UseBasicParsing `
-TimeoutSec 120 `
-ErrorAction Stop
if (-not (Test-Path -LiteralPath $archive)) {
throw "Gitea-Archiv wurde nicht heruntergeladen."
}
Expand-Archive `
-LiteralPath $archive `
-DestinationPath $extract `
-Force
$releaseRoot = Find-ExtractedReleaseRoot -ExtractPath $extract
# Pflichtdateien prüfen.
foreach ($required in @("SMTPGraphRelay.ps1", "version.json")) {
if (-not (Test-Path -LiteralPath (Join-Path $releaseRoot $required))) {
throw "Repository-Archiv ist unvollständig: '$required' fehlt."
}
}
$versionInfo = Get-Content `
-LiteralPath (Join-Path $releaseRoot "version.json") `
-Raw `
-Encoding UTF8 | ConvertFrom-Json
if (-not $versionInfo.Version) {
throw "version.json aus dem Repository enthält keine Version."
}
Write-Ok "Repository-Version $($versionInfo.Version) geladen."
return [pscustomobject]@{
TempRoot = $root
ReleaseRoot = $releaseRoot
VersionInfo = $versionInfo
}
}
function Remove-RepoStagingArea {
param($Staging)
if ($Staging -and $Staging.TempRoot) {
Remove-Item -LiteralPath $Staging.TempRoot -Recurse -Force -ErrorAction SilentlyContinue
}
}
function Install-RepoProgramFiles {
param(
[Parameter(Mandatory)][string]$ReleaseRoot,
[Parameter(Mandatory)][string]$TargetPath
)
New-Item -ItemType Directory -Path $TargetPath -Force | Out-Null
foreach ($name in $ManagedReleaseFiles) {
$source = Join-Path $ReleaseRoot $name
if (Test-Path -LiteralPath $source) {
Copy-Item `
-LiteralPath $source `
-Destination (Join-Path $TargetPath $name) `
-Force
Write-Ok "Installiert: $name"
}
}
}
function Get-SourceFile { function Get-SourceFile {
param([Parameter(Mandatory)][string]$Name) param([Parameter(Mandatory)][string]$Name)
@@ -691,25 +777,32 @@ function Invoke-PostUpdateHealthCheck {
} }
function Install-New { function Install-New {
Write-Title "SMTPGraphRelay - Neuinstallation" Write-Title "SMTPGraphRelay - Neuinstallation aus Gitea"
if (Test-Path -LiteralPath (Join-Path $InstallPath $ConfigFileName)) { if (Test-Path -LiteralPath (Join-Path $InstallPath $ConfigFileName)) {
Write-Warn "Es existiert bereits eine config.json unter:" Write-Warn "Es existiert bereits eine config.json unter:"
Write-Host " $InstallPath" Write-Host " $InstallPath"
Write-Warn "Neuinstallation würde eine neue App/Zertifikat erzeugen." Write-Warn "Neuinstallation würde eine neue App/Zertifikat erzeugen."
if (-not (Confirm-Yes "Trotzdem fortfahren?")) { if (-not (Confirm-Yes "Trotzdem fortfahren?")) {
return return
} }
} }
$staging = $null
try {
$staging = New-RepoStagingArea
Ensure-Modules -IncludeExchange Ensure-Modules -IncludeExchange
Copy-ProgramFiles -TargetPath $InstallPath -RequireRelay
Ensure-Directories -TargetPath $InstallPath Ensure-Directories -TargetPath $InstallPath
$packageVersion = Get-PackageVersion # Nur Programmdateien aus Git übernehmen.
if ($packageVersion -and $packageVersion.Version) { Install-RepoProgramFiles `
Write-Ok "Installiere Paketversion $($packageVersion.Version)." -ReleaseRoot $staging.ReleaseRoot `
} -TargetPath $InstallPath
Write-Ok "Programmdateien aus Gitea installiert."
$appName = Read-Default "Name der Entra App" $AppDefaultName $appName = Read-Default "Name der Entra App" $AppDefaultName
@@ -760,8 +853,6 @@ function Install-New {
} }
Write-Ok "Entra Service Principal erstellt: $($sp.Id)" Write-Ok "Entra Service Principal erstellt: $($sp.Id)"
# Keine globale Graph Mail.Send Permission!
Test-NoGlobalMailSend -ServicePrincipalObjectId $sp.Id Test-NoGlobalMailSend -ServicePrincipalObjectId $sp.Id
Ensure-ExchangeRbac ` Ensure-ExchangeRbac `
@@ -806,6 +897,10 @@ function Install-New {
RetentionDays = 30 RetentionDays = 30
CleanupHours = 12 CleanupHours = 12
} }
Update = [ordered]@{
Repository = $RepoBaseUrl
Branch = "main"
}
} }
Write-RelayConfig -Config $config -TargetPath $InstallPath Write-RelayConfig -Config $config -TargetPath $InstallPath
@@ -831,48 +926,20 @@ function Install-New {
Start-RelayTask Start-RelayTask
$health = Join-Path $InstallPath $HealthFileName
if (Test-Path -LiteralPath $health) {
Write-Info "Starte abschließenden Health Check..."
& $health -ConfigPath (Join-Path $InstallPath $ConfigFileName)
$healthExit = $LASTEXITCODE
if ($healthExit -ge 2) {
Write-Warn "Installation abgeschlossen, Health Check meldet Fehler. Bitte Ausgabe prüfen."
}
}
Write-Title "Neuinstallation abgeschlossen" Write-Title "Neuinstallation abgeschlossen"
Write-Host "Version: $($staging.VersionInfo.Version)"
Write-Host "Installationspfad: $InstallPath" Write-Host "Installationspfad: $InstallPath"
$packageVersion = Get-PackageVersion
$installedVersion = Get-InstalledVersion -TargetPath $InstallPath
if ($packageVersion -and $packageVersion.Version) {
Write-Host "Paketversion: $($packageVersion.Version)"
}
if ($installedVersion -and $installedVersion.Version) {
Write-Host "Installierte Version: $($installedVersion.Version)"
}
elseif (Test-Path -LiteralPath (Join-Path $InstallPath $RelayFileName)) {
Write-Warn "Installierte Version unbekannt (keine version.json)."
}
try {
$remoteVersionInfo = Get-RemoteVersion
if ($remoteVersionInfo -and $remoteVersionInfo.Version) {
Write-Host "Remote (main): $($remoteVersionInfo.Version)"
if ($installedVersion -and $installedVersion.Version) {
$cmp = Compare-RelayVersions `
-Installed ([string]$installedVersion.Version) `
-Remote ([string]$remoteVersionInfo.Version)
if ($cmp -gt 0) {
Write-Warn "Update verfügbar."
}
elseif ($cmp -eq 0) {
Write-Ok "Version ist aktuell."
}
else {
Write-Warn "Lokale Version ist neuer als Repository-main."
}
}
}
}
catch {
Write-Warn "Remote-Version konnte nicht geprüft werden."
}
Write-Host "Client ID: $($app.AppId)" Write-Host "Client ID: $($app.AppId)"
Write-Host "Tenant ID: $tenantId" Write-Host "Tenant ID: $tenantId"
Write-Host "Sender: $senderMailbox" Write-Host "Sender: $senderMailbox"
@@ -881,10 +948,14 @@ function Install-New {
finally { finally {
Disconnect-MgGraph -ErrorAction SilentlyContinue | Out-Null Disconnect-MgGraph -ErrorAction SilentlyContinue | Out-Null
} }
}
finally {
Remove-RepoStagingArea -Staging $staging
}
} }
function Repair-Installation { function Repair-Installation {
Write-Title "SMTPGraphRelay - Repair" Write-Title "SMTPGraphRelay - Repair aus Gitea"
$config = Get-RelayConfig -TargetPath $InstallPath $config = Get-RelayConfig -TargetPath $InstallPath
if (-not $config) { if (-not $config) {
@@ -893,8 +964,13 @@ function Repair-Installation {
return return
} }
$staging = $null
$backupPath = $null
try {
$staging = New-RepoStagingArea
Ensure-Modules Ensure-Modules
Copy-ProgramFiles -TargetPath $InstallPath
Ensure-Directories -TargetPath $InstallPath Ensure-Directories -TargetPath $InstallPath
$certPath = "Cert:\LocalMachine\My\$($config.Graph.CertificateThumbprint)" $certPath = "Cert:\LocalMachine\My\$($config.Graph.CertificateThumbprint)"
@@ -911,20 +987,57 @@ function Repair-Installation {
Write-Ok "Zertifikat vorhanden und besitzt Private Key." Write-Ok "Zertifikat vorhanden und besitzt Private Key."
} }
Write-Info "Sichere aktuelle Programmdateien..."
$backupPath = Backup-ManagedFiles -TargetPath $InstallPath
Write-Ok "Backup: $backupPath"
Stop-RelayTask
Install-RepoProgramFiles `
-ReleaseRoot $staging.ReleaseRoot `
-TargetPath $InstallPath
Ensure-FirewallRule -Port ([int]$config.Smtp.Port) Ensure-FirewallRule -Port ([int]$config.Smtp.Port)
Ensure-ScheduledTask -TargetPath $InstallPath Ensure-ScheduledTask -TargetPath $InstallPath
Start-RelayTask Start-RelayTask
$health = Join-Path $InstallPath $HealthFileName $healthExit = Invoke-PostUpdateHealthCheck -TargetPath $InstallPath
if (Test-Path -LiteralPath $health) {
Write-Info "Starte Health Check..." if ($healthExit -ge 2) {
& $health -ConfigPath (Join-Path $InstallPath $ConfigFileName) throw "Health Check nach Repair meldet FEHLER (ExitCode $healthExit)."
$healthExit = $LASTEXITCODE }
Write-Info "Health Check ExitCode: $healthExit"
if ($healthExit -eq 1) {
Write-Warn "Repair abgeschlossen, Health Check enthält Warnungen."
}
else {
Write-Ok "Repair Health Check erfolgreich."
} }
Write-Title "Repair abgeschlossen" Write-Title "Repair abgeschlossen"
Write-Host "Installierte Repo-Version: $($staging.VersionInfo.Version)"
}
catch {
Write-Fail "Repair fehlgeschlagen: $($_.Exception.Message)"
if ($backupPath -and (Test-Path -LiteralPath $backupPath)) {
Write-Warn "Stelle vorherige Programmdateien wieder her..."
try {
Stop-RelayTask
Restore-ManagedFiles -BackupPath $backupPath -TargetPath $InstallPath
Ensure-ScheduledTask -TargetPath $InstallPath
Start-RelayTask
Write-Ok "Rollback nach Repair abgeschlossen."
}
catch {
Write-Fail "Repair-Rollback fehlgeschlagen: $($_.Exception.Message)"
}
}
}
finally {
Remove-RepoStagingArea -Staging $staging
}
} }
function Update-Relay { function Update-Relay {
@@ -1338,7 +1451,7 @@ function Show-Status {
function Show-Menu { function Show-Menu {
Clear-Host Clear-Host
Write-Title "SMTPGraphRelay - Installer / Repair / Update" Write-Title "SMTPGraphRelay - Bootstrap / Repair / Online Update"
Write-Host "Installationspfad: $InstallPath" -ForegroundColor DarkGray Write-Host "Installationspfad: $InstallPath" -ForegroundColor DarkGray
$installedVersion = Get-InstalledVersion -TargetPath $InstallPath $installedVersion = Get-InstalledVersion -TargetPath $InstallPath
@@ -1349,8 +1462,8 @@ function Show-Menu {
Write-Host "Updatequelle: Gitea / main" -ForegroundColor DarkGray Write-Host "Updatequelle: Gitea / main" -ForegroundColor DarkGray
Write-Host "" Write-Host ""
Write-Host " [1] Neuinstallation" Write-Host " [1] Neuinstallation aus Gitea"
Write-Host " [2] Installation reparieren" Write-Host " [2] Installation aus Gitea reparieren"
Write-Host " [3] Nach Online-Updates suchen" Write-Host " [3] Nach Online-Updates suchen"
Write-Host " [4] Entra / Exchange RBAC prüfen" Write-Host " [4] Entra / Exchange RBAC prüfen"
Write-Host " [5] Zertifikat erneuern" Write-Host " [5] Zertifikat erneuern"