# Gives each node a replication identity (a self-signed certificate and its # key, in the node's own data volume) and writes both fingerprints into .env, # where compose.yaml reads them. Run once, before the first `docker compose up`. # # .\init-identities.ps1 # refuses if a node already has one # .\init-identities.ps1 -Regenerate # replace both identities param( [switch] $Regenerate ) $ErrorActionPreference = 'Stop' Set-Location $PSScriptRoot if (-not (Test-Path .env)) { throw 'Copy .env.example to .env and set the password first.' } # compose.yaml needs both fingerprints to load at all. Stand-ins for the # commands below, which override the nodes' command lines anyway. $placeholder = '0' * 64 $saved = @{ NODE1_FINGERPRINT = $env:NODE1_FINGERPRINT; NODE2_FINGERPRINT = $env:NODE2_FINGERPRINT } $env:NODE1_FINGERPRINT = $placeholder $env:NODE2_FINGERPRINT = $placeholder try { if (-not $Regenerate) { foreach ($node in 'node1', 'node2') { docker compose run --rm --no-deps -T --entrypoint test $node -e /var/lib/rasterizeddb/replication-cert.pem if ($LASTEXITCODE -eq 0) { throw "$node already has a replication identity. To replace both, run: .\init-identities.ps1 -Regenerate" } } } $fingerprints = @{} foreach ($node in 'node1', 'node2') { $output = docker compose run --rm --no-deps -T --entrypoint rasterizeddb $node ` --replication-generate-identity /var/lib/rasterizeddb --replication-addr $node if ($LASTEXITCODE -ne 0) { throw "generating the identity of $node failed" } $line = $output | Where-Object { $_ -match '^fingerprint: ([0-9a-f]{64})$' } | Select-Object -First 1 if (-not $line) { throw "Could not read the fingerprint of $node from the server's output." } $fingerprints[$node] = ($line -replace '^fingerprint: ', '') } } finally { $env:NODE1_FINGERPRINT = $saved.NODE1_FINGERPRINT $env:NODE2_FINGERPRINT = $saved.NODE2_FINGERPRINT } $kept = Get-Content .env | Where-Object { $_ -notmatch '^NODE[12]_FINGERPRINT=' } $lines = @($kept) + "NODE1_FINGERPRINT=$($fingerprints.node1)" + "NODE2_FINGERPRINT=$($fingerprints.node2)" Set-Content -Path .env -Value $lines -Encoding ascii Write-Host "node1 fingerprint: $($fingerprints.node1)" Write-Host "node2 fingerprint: $($fingerprints.node2)" Write-Host 'Written to .env. Start the nodes with: docker compose up -d' Write-Host '(If they were already running, recreate them: docker compose up -d --force-recreate)'