$ErrorActionPreference = "Stop" $Root = Split-Path -Parent $PSScriptRoot Set-Location $Root function New-Secret([string]$Name) { $path = Join-Path $Root "secrets\$Name" if (-not (Test-Path $path)) { $bytes = New-Object byte[] 24 $rng = [System.Security.Cryptography.RandomNumberGenerator]::Create() $rng.GetBytes($bytes) $text = -join ($bytes | ForEach-Object { $_.ToString("x2") }) [System.IO.File]::WriteAllText($path, $text) } } New-Item -ItemType Directory -Force -Path (Join-Path $Root "secrets") | Out-Null New-Item -ItemType Directory -Force -Path (Join-Path $Root "certs") | Out-Null New-Item -ItemType Directory -Force -Path (Join-Path $Root "evidence") | Out-Null @( "db_super_password", "db_api_password", "db_worker_password", "redis_password", "rabbit_api_password", "rabbit_worker_password", "grafana_password", "s3_admin_secret", "s3_api_secret", "s3_worker_secret" ) | ForEach-Object { New-Secret $_ } foreach ($pair in @( @("s3_admin_access", "s3admin"), @("s3_api_access", "s3api"), @("s3_worker_access", "s3worker") )) { $path = Join-Path $Root ("secrets\" + $pair[0]) if (-not (Test-Path $path)) { $suffix = ([guid]::NewGuid().ToString("N")).Substring(0, 12) [System.IO.File]::WriteAllText($path, $pair[1] + $suffix) } } foreach ($name in @("api", "worker", "migrate")) { foreach ($suffix in @("role_id", "secret_id")) { $path = Join-Path $Root "secrets\$name.$suffix" if (-not (Test-Path $path)) { [System.IO.File]::WriteAllText($path, "pending") } } } if (-not (Test-Path (Join-Path $Root ".env"))) { Copy-Item (Join-Path $Root ".env.example") (Join-Path $Root ".env") } Get-ChildItem -Path (Join-Path $Root "scripts"), (Join-Path $Root "infra") -Recurse -Include *.sh,*.cfg,*.hcl | ForEach-Object { $text = [System.IO.File]::ReadAllText($_.FullName) -replace "`r`n", "`n" -replace "`r", "`n" $utf8 = New-Object System.Text.UTF8Encoding $false [System.IO.File]::WriteAllText($_.FullName, $text, $utf8) } Write-Host "creating certificates" docker run --rm -v "${Root}\certs:/work" -v "${Root}\scripts\gen-certs.sh:/gen-certs.sh:ro" alpine:3.21.3 sh /gen-certs.sh if ($LASTEXITCODE -ne 0) { throw "certificate generation failed" } Write-Host "building application image" docker compose --env-file .env build if ($LASTEXITCODE -ne 0) { throw "image build failed" } Write-Host "starting data services" docker compose --env-file .env up -d postgres redis rabbitmq rabbit-mtls seaweed s3 openbao otel-collector otel-mtls prometheus loki tempo grafana if ($LASTEXITCODE -ne 0) { throw "data services failed to start" } Write-Host "bootstrapping OpenBao" docker compose --env-file .env --profile bootstrap run --rm openbao-bootstrap if ($LASTEXITCODE -ne 0) { throw "OpenBao bootstrap failed" } Write-Host "applying schema" docker compose --env-file .env up -d migrate if ($LASTEXITCODE -ne 0) { throw "migrate failed to start" } $deadline = (Get-Date).AddMinutes(3) do { Start-Sleep -Seconds 2 $state = docker compose --env-file .env ps --format json migrate if ($state -match '"ExitCode":0' -or $state -match '"ExitCode": 0') { break } if ($state -match '"ExitCode":' -and $state -notmatch '"ExitCode":0' -and $state -notmatch '"State":"running"' -and $state -match '"ExitCode":[1-9]') { throw "migrate failed" } } while ((Get-Date) -lt $deadline) Write-Host "granting database roles" docker compose --env-file .env exec -T postgres sh /etc/postgres/grant.sh if ($LASTEXITCODE -ne 0) { throw "database grants failed" } Write-Host "creating bucket" docker compose --env-file .env up -d s3-init if ($LASTEXITCODE -ne 0) { throw "s3-init failed to start" } Write-Host "starting API and worker" docker compose --env-file .env up -d --scale api=2 --no-recreate api worker lb if ($LASTEXITCODE -ne 0) { throw "application services failed to start" } Write-Host "stand is starting. Public URL: https://127.0.0.1:8443 CA: certs\ca.crt" Write-Host "OpenBao stays sealed across its own restart. Unseal with: docker compose --env-file .env --profile bootstrap run --rm openbao-bootstrap"