#Requires -Version 5.1 <# .SYNOPSIS RasterizedDB: two servers that both take writes and replicate every commit to each other, over TLS, each node pinning the other's certificate. .DESCRIPTION Run it on each node. The first run makes this node's replication identity, prints its fingerprint and stops; give each node the other's fingerprint with -PeerFingerprint and run it again to start the server (foreground). .EXAMPLE .\setup.ps1 -NodeId 1 -NodeAddr 10.0.0.1 -PeerAddr 10.0.0.2 .EXAMPLE $env:RASTERIZEDDB_PG_PASSWORD = 'use-a-long-secret' .\setup.ps1 -NodeId 1 -NodeAddr 10.0.0.1 -PeerAddr 10.0.0.2 -PeerFingerprint 9f86d081… #> [CmdletBinding()] param( [string]$RdbBin = $(if ($env:RDB_BIN) { $env:RDB_BIN } else { 'rasterizeddb.exe' }), [string]$Data = $(if ($env:RDB_DATA) { $env:RDB_DATA } else { '.\data' }), # This node's certificate and key. [string]$IdentityDir = $(if ($env:RDB_IDENTITY_DIR) { $env:RDB_IDENTITY_DIR } else { '.\replication-identity' }), # 1 on one node, 2 on the other; never 0. [int]$NodeId = $(if ($env:RDB_NODE_ID) { [int]$env:RDB_NODE_ID } else { 0 }), # This node's address, which the peer connects to. [string]$NodeAddr = $env:RDB_NODE_ADDR, [string]$PeerAddr = $env:RDB_PEER_ADDR, # A label for the other node in SHOW REPLICATION. [string]$PeerName = $(if ($env:RDB_PEER_NAME) { $env:RDB_PEER_NAME } else { 'peer' }), [string]$PeerFingerprint = $env:RDB_PEER_FINGERPRINT, [int]$ReplPort = $(if ($env:RDB_REPL_PORT) { [int]$env:RDB_REPL_PORT } else { 61171 }), [int]$PeerPort = $(if ($env:RDB_PEER_PORT) { [int]$env:RDB_PEER_PORT } else { 61171 }), # async, sync, quorum:N or all. [string]$Mode = $(if ($env:RDB_REPL_MODE) { $env:RDB_REPL_MODE } else { 'async' }), [ValidateSet('abort', 'skip', 'last-writer-wins')] [string]$Conflict = $(if ($env:RDB_REPL_CONFLICT) { $env:RDB_REPL_CONFLICT } else { 'abort' }), # Both nodes must use the same name. [string]$Cluster = $(if ($env:RDB_CLUSTER) { $env:RDB_CLUSTER } else { 'rasterizeddb' }), [string]$PgListen = $(if ($env:RDB_PG_LISTEN) { $env:RDB_PG_LISTEN } else { '0.0.0.0' }), [string]$CertPath = $env:RDB_TLS_CERT, [string]$KeyPath = $env:RDB_TLS_KEY ) $ErrorActionPreference = 'Stop' $cmd = Get-Command $RdbBin -ErrorAction SilentlyContinue if (-not $cmd) { throw "'$RdbBin' not found; pass -RdbBin with the path of rasterizeddb.exe." } if ($NodeId -le 0) { throw 'Pass -NodeId (1 on one node, 2 on the other; never 0).' } if (-not $NodeAddr) { throw "Pass -NodeAddr with this node's address." } if (-not $PeerAddr) { throw "Pass -PeerAddr with the other node's address." } # This node's identity: made once, kept. New-Item -ItemType Directory -Force -Path $IdentityDir | Out-Null $IdentityDir = (Resolve-Path -LiteralPath $IdentityDir).Path $cert = Join-Path $IdentityDir 'replication-cert.pem' $key = Join-Path $IdentityDir 'replication-key.pem' $fpFile = Join-Path $IdentityDir 'fingerprint' if (-not (Test-Path -LiteralPath $cert) -or -not (Test-Path -LiteralPath $key)) { $out = & $cmd.Source --replication-generate-identity $IdentityDir --replication-addr $NodeAddr if ($LASTEXITCODE -ne 0) { throw 'Could not generate the replication identity; the message is above.' } $out | ForEach-Object { Write-Host $_ } $fp = $out | ForEach-Object { if ($_ -match '^fingerprint:\s*(\S+)') { $Matches[1] } } | Select-Object -First 1 Set-Content -LiteralPath $fpFile -Value $fp -Encoding ascii # The key is the node's identity in the cluster: SYSTEM, Administrators and you only. & icacls.exe $key /inheritance:r /grant:r "$($env:USERNAME):F" '*S-1-5-18:F' '*S-1-5-32-544:F' | Out-Null } $mine = if (Test-Path -LiteralPath $fpFile) { (Get-Content -LiteralPath $fpFile -Raw).Trim() } else { '' } if (-not $mine) { throw "No fingerprint saved in $fpFile; delete the identity files there to make a new one." } if (-not $PeerFingerprint) { Write-Host '' Write-Host "This node's fingerprint: $mine" Write-Host 'Run this script on the other node too, then here again with' Write-Host " -PeerFingerprint " exit 0 } if (-not $env:RASTERIZEDDB_PG_PASSWORD) { throw 'Set $env:RASTERIZEDDB_PG_PASSWORD (the same on both nodes) for the role rasterized.' } New-Item -ItemType Directory -Force -Path $Data | Out-Null $Data = (Resolve-Path -LiteralPath $Data).Path $serverArgs = @('--location', $Data, '--protocol', 'both', '--pg-addr', $PgListen, '--replication', '--node-id', "$NodeId", '--replication-cluster', $Cluster, '--replication-addr', $NodeAddr, '--replication-port', "$ReplPort", '--replication-auth', 'pinned', '--replication-cert', $cert, '--replication-key', $key, '--replication-peer-fingerprint', $PeerFingerprint, '--peer', "$PeerName@${PeerAddr}:$PeerPort", '--replication-mode', $Mode, '--replication-conflict', $Conflict) if ($CertPath) { $c = (Resolve-Path -LiteralPath $CertPath).Path $k = (Resolve-Path -LiteralPath $KeyPath).Path $serverArgs += @('--pg-cert', $c, '--pg-key', $k, '--rdb-cert', $c, '--rdb-key', $k) } Write-Host "Node ${NodeId}: replication on ${NodeAddr}:$ReplPort, peer $PeerName at ${PeerAddr}:$PeerPort." Write-Host "Open port $ReplPort to the other node only, for example:" Write-Host " New-NetFirewallRule -DisplayName 'RasterizedDB replication' -Direction Inbound -Protocol TCP -LocalPort $ReplPort -RemoteAddress $PeerAddr -Action Allow" Write-Host "PostgreSQL: postgresql://rasterized@${NodeAddr}:5432/postgres?sslmode=require" Write-Host "Check: psql … -c 'SHOW REPLICATION'" Write-Host '' & $cmd.Source @serverArgs exit $LASTEXITCODE