Files
alpenwerk-hr/deploy/windows/gemeinsam.ps1
muena efc4390ab3
All checks were successful
CI / Migrationen auf leerer Datenbank (push) Successful in 10m32s
CI / Ausrollen (push) Successful in 7s
CI / Lint, Typen, Tests, Build (push) Successful in 11m43s
Betrieb auf Windows Server ohne Docker
Der Kunde betreibt die Anwendung auf einem Windows Server mit
vorhandenem Node.js und PostgreSQL, ohne Docker. Ausgeliefert wird ein
fertiges Paket (npm run paket:windows, rund 8 MB Zip); auf dem Server
braucht es weder npm noch einen Build.

- scripts/windows-paket.mjs: Standalone-Build plus Migrationen und
  Betriebsskripte. Loest die Turbopack-Symlinks auf und laesst sharp
  weg (next/image wird nicht benutzt); bricht ab, sobald eine native
  Datei oder ein Symlink im Paket landet.
- scripts/db-setup.mjs: ersetzt deploy/db-init ausserhalb des
  Containers -- Datenbank mit ICU de-AT, Anwendungsrolle ohne BYPASSRLS,
  Erweiterungen, auth-Attrappe. Wiederholbar, liest nur DATABASE_URL und
  MIGRATE_DATABASE_URL.
- deploy/windows/: Installation (Dienst ueber NSSM als LocalService,
  nur auf 127.0.0.1), Update mit Sicherung vorab, Nachtlauf und
  Sicherung als geplante Aufgaben, IIS als HTTPS-Proxy, Freischaltung
  der ersten Person, Anleitung.
- engines: Node bis 26.

Geprueft: Paket auf dem Mac gebaut, entpackt an einem fremden Pfad
unter Linux mit Node 26 gegen PostgreSQL 18 eingerichtet, migriert und
gestartet (/login, Logo, CSS, Nachtlauf 401/200). Die PowerShell-Skripte
sind nur geparst, auf Windows selbst noch nicht gelaufen.
2026-10-12 10:06:11 +02:00

164 lines
6.1 KiB
PowerShell

# Gemeinsame Bausteine der Betriebsskripte. Wird von den anderen Skripten
# per Dot-Sourcing geladen, nicht direkt aufgerufen.
#
# Bewusst ohne Umlaute: Windows PowerShell 5.1 liest Skripte ohne BOM als
# ANSI, und aus "Pruefung" wuerde sonst Zeichensalat.
#
# Bewusst ohne PowerShell-7-Syntax (kein ??, kein ?:, kein &&): auf einem
# Windows Server ist 5.1 das, was da ist.
$ErrorActionPreference = 'Stop'
$DienstName = 'AlpenwerkHR'
# Wohlbekannte SIDs statt Namen: "Administratoren" heisst auf einem deutschen
# Server anders als auf einem englischen, die SID ueberall gleich.
$SidAdministratoren = '*S-1-5-32-544'
$SidSystem = '*S-1-5-18'
$SidLocalService = '*S-1-5-19'
$SidIisIusrs = '*S-1-5-32-568'
function Read-EnvDatei {
param([Parameter(Mandatory = $true)][string]$Pfad)
$werte = @{}
if (-not (Test-Path -LiteralPath $Pfad)) { return $werte }
foreach ($zeile in Get-Content -LiteralPath $Pfad -Encoding UTF8) {
$t = $zeile.Trim()
if ($t -eq '' -or $t.StartsWith('#')) { continue }
$i = $t.IndexOf('=')
if ($i -lt 1) { continue }
$schluessel = $t.Substring(0, $i).Trim()
$wert = $t.Substring($i + 1).Trim()
if ($wert.Length -ge 2 -and (($wert.StartsWith('"') -and $wert.EndsWith('"')) -or ($wert.StartsWith("'") -and $wert.EndsWith("'")))) {
$wert = $wert.Substring(1, $wert.Length - 2)
}
$werte[$schluessel] = $wert
}
return $werte
}
# Liefert die Namen der Pflichtwerte, die fehlen oder noch einen Platzhalter
# <...> / {{...}} tragen.
function Get-FehlendeWerte {
param([hashtable]$Werte, [string[]]$Pflicht)
$fehlend = @()
foreach ($name in $Pflicht) {
$w = $Werte[$name]
if (-not $w -or $w -match '<[^>]*>' -or $w -match '\{\{') { $fehlend += $name }
}
return $fehlend
}
$PflichtApp = @(
'PORT', 'DATABASE_URL', 'AUTH_SECRET', 'AUTH_URL',
'AUTH_MICROSOFT_ENTRA_ID_ID', 'AUTH_MICROSOFT_ENTRA_ID_SECRET', 'AUTH_MICROSOFT_ENTRA_ID_ISSUER',
'CRON_SECRET'
)
$PflichtVerwaltung = @('MIGRATE_DATABASE_URL')
function New-Geheimnis {
param([int]$Bytes = 32)
$puffer = New-Object byte[] $Bytes
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
$rng.GetBytes($puffer)
$rng.Dispose()
# Hex statt Base64: kommt ohne + / = aus und muss deshalb in einer
# Verbindungs-URL nicht maskiert werden.
return -join ($puffer | ForEach-Object { $_.ToString('x2') })
}
function Get-NodePfad {
$befehl = Get-Command node.exe -ErrorAction SilentlyContinue
if (-not $befehl) { throw 'node.exe nicht gefunden. Node.js installieren (https://nodejs.org) und eine neue Konsole oeffnen.' }
return $befehl.Source
}
function Assert-NodeVersion {
$node = Get-NodePfad
$version = (& $node --version).Trim().TrimStart('v')
$haupt = [int]($version.Split('.')[0])
if ($haupt -lt 22) { throw "Node.js $version ist zu alt; gebraucht wird mindestens 22." }
Write-Host "Node.js $version ($node)"
}
# Neueste Installation unter "C:\Program Files\PostgreSQL\<version>\bin",
# sofern nicht ausdruecklich angegeben.
function Get-PgBin {
param([string]$Vorgabe)
if ($Vorgabe) {
if (-not (Test-Path -LiteralPath (Join-Path $Vorgabe 'pg_dump.exe'))) { throw "In $Vorgabe liegt kein pg_dump.exe." }
return $Vorgabe
}
$basis = Join-Path $env:ProgramFiles 'PostgreSQL'
$kandidat = Get-ChildItem -LiteralPath $basis -Directory -ErrorAction SilentlyContinue |
Where-Object { Test-Path -LiteralPath (Join-Path $_.FullName 'bin\pg_dump.exe') } |
Sort-Object { [int]($_.Name -replace '\D', '') } -Descending |
Select-Object -First 1
if (-not $kandidat) { throw "PostgreSQL-Programme nicht gefunden unter $basis. Pfad mit -PgBin angeben." }
return (Join-Path $kandidat.FullName 'bin')
}
# Fuehrt ein Node-Skript im Anwendungsverzeichnis aus, mit beiden
# Konfigurationsdateien. Bricht ab, wenn es scheitert.
function Invoke-NodeSkript {
param(
[Parameter(Mandatory = $true)][string]$AppVerzeichnis,
[Parameter(Mandatory = $true)][string]$Ziel,
[Parameter(Mandatory = $true)][string]$Skript
)
$node = Get-NodePfad
Push-Location -LiteralPath $AppVerzeichnis
try {
& $node "--env-file=$Ziel\app.env" "--env-file=$Ziel\verwaltung.env" $Skript
if ($LASTEXITCODE -ne 0) { throw "$Skript ist fehlgeschlagen (Exit-Code $LASTEXITCODE)." }
} finally {
Pop-Location
}
}
function Invoke-Icacls {
param([string[]]$Argumente)
# 2>&1 auf ein natives Programm erzeugt in PowerShell 5.1 Fehlerobjekte, und
# mit 'Stop' bricht schon die erste Zeile auf stderr alles ab -- deshalb hier
# lokal 'Continue' und die Entscheidung allein am Exit-Code.
$ErrorActionPreference = 'Continue'
$ausgabe = & icacls.exe @Argumente 2>&1
if ($LASTEXITCODE -ne 0) { throw "icacls $($Argumente -join ' ') ist fehlgeschlagen: $ausgabe" }
}
# Wartet, bis die Anwendung auf /login mit 200 antwortet.
function Wait-Gesund {
param([int]$Port, [int]$Sekunden = 90)
$bis = (Get-Date).AddSeconds($Sekunden)
while ((Get-Date) -lt $bis) {
try {
$antwort = Invoke-WebRequest -Uri "http://127.0.0.1:$Port/login" -UseBasicParsing -TimeoutSec 5 -MaximumRedirection 0
if ($antwort.StatusCode -eq 200) { return $true }
} catch {
Start-Sleep -Seconds 3
}
}
return $false
}
function Copy-Verzeichnis {
param([string]$Quelle, [string]$Ziel, [switch]$Spiegeln)
# robocopy statt Copy-Item: kommt mit langen Pfaden in node_modules zurecht.
# /MIR loescht im Ziel, was in der Quelle fehlt; ohne nur hinzufuegen.
$modus = '/E'
if ($Spiegeln) { $modus = '/MIR' }
& robocopy.exe $Quelle $Ziel $modus /NFL /NDL /NJH /NJS /NP /R:2 /W:2 | Out-Null
# robocopy: 0-7 heisst Erfolg (mit oder ohne kopierte Dateien), ab 8 Fehler.
if ($LASTEXITCODE -ge 8) { throw "Kopieren von $Quelle nach $Ziel ist fehlgeschlagen (robocopy $LASTEXITCODE)." }
$global:LASTEXITCODE = 0
}
# Next.js schreibt zur Laufzeit in .next\cache. Der Dienst laeuft als
# LocalService und darf sonst im Anwendungsverzeichnis nur lesen.
function Set-CacheRechte {
param([string]$AppVerzeichnis)
$cache = Join-Path $AppVerzeichnis '.next\cache'
New-Item -ItemType Directory -Force -Path $cache | Out-Null
Invoke-Icacls @($cache, '/grant', "${SidLocalService}:(OI)(CI)M")
}