#Requires -Version 5.1 <# .SYNOPSIS RasterizedDB: one server speaking every client protocol at once, on the network, with TLS: PostgreSQL, the native protocol, MySQL, SQL Server and Redis, all on the same databases and roles. Runs in the foreground. .EXAMPLE $env:RASTERIZEDDB_PG_PASSWORD = 'use-a-long-secret' .\setup.ps1 -CertPath .\tls\server.pem -KeyPath .\tls\server.key .EXAMPLE .\setup.ps1 -MssqlPort 0 # 0 leaves a protocol closed #> [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' }), [string]$Listen = $(if ($env:RDB_LISTEN) { $env:RDB_LISTEN } else { '0.0.0.0' }), # PEM certificate and key; without them each listener makes a self-signed one. [string]$CertPath = $env:RDB_TLS_CERT, [string]$KeyPath = $env:RDB_TLS_KEY, [int]$PgPort = $(if ($env:RDB_PG_PORT) { [int]$env:RDB_PG_PORT } else { 5432 }), [int]$Port = $(if ($env:RDB_PORT) { [int]$env:RDB_PORT } else { 61170 }), [int]$MysqlPort = $(if ($env:RDB_MYSQL_PORT) { [int]$env:RDB_MYSQL_PORT } else { 3306 }), [int]$MssqlPort = $(if ($env:RDB_MSSQL_PORT) { [int]$env:RDB_MSSQL_PORT } else { 1433 }), [int]$KvPort = $(if ($env:RDB_KV_PORT) { [int]$env:RDB_KV_PORT } else { 6379 }), [string]$LogLevel = $(if ($env:RDB_LOG_LEVEL) { $env:RDB_LOG_LEVEL } else { 'warn' }) ) $ErrorActionPreference = 'Stop' $cmd = Get-Command $RdbBin -ErrorAction SilentlyContinue if (-not $cmd) { throw "'$RdbBin' not found; pass -RdbBin with the path of rasterizeddb.exe." } if (-not $env:RASTERIZEDDB_PG_PASSWORD) { throw 'Set $env:RASTERIZEDDB_PG_PASSWORD to the password for the role rasterized.' } New-Item -ItemType Directory -Force -Path $Data | Out-Null $Data = (Resolve-Path -LiteralPath $Data).Path $serverArgs = @('--location', $Data, '--protocol', 'both', '--log-level', $LogLevel, '--pg-addr', $Listen, '--pg-port', "$PgPort", '--pg-tls', 'require', '--rdb-addr', $Listen, '--rdb-port', "$Port") if ($CertPath) { if (-not (Test-Path -LiteralPath $CertPath) -or -not (Test-Path -LiteralPath $KeyPath)) { throw 'CertPath and KeyPath must both be files.' } $CertPath = (Resolve-Path -LiteralPath $CertPath).Path $KeyPath = (Resolve-Path -LiteralPath $KeyPath).Path # MySQL, SQL Server and Redis take the PostgreSQL certificate unless given their own. $serverArgs += @('--pg-cert', $CertPath, '--pg-key', $KeyPath, '--rdb-cert', $CertPath, '--rdb-key', $KeyPath) } else { Write-Warning 'No -CertPath: every listener uses a self-signed certificate clients cannot verify.' } if ($MysqlPort -gt 0) { $serverArgs += @('--mysql-addr', $Listen, '--mysql-port', "$MysqlPort", '--mysql-tls', 'prefer') } if ($MssqlPort -gt 0) { $serverArgs += @('--mssql-addr', $Listen, '--mssql-port', "$MssqlPort", '--mssql-tls', 'prefer') } if ($KvPort -gt 0) { $serverArgs += @('--kv-addr', $Listen, '--kv-port', "$KvPort", '--kv-tls', 'on') } $h = [System.Net.Dns]::GetHostEntry('').HostName Write-Host "PostgreSQL: psql `"postgresql://rasterized@${h}:$PgPort/postgres?sslmode=require`"" Write-Host "Native: ${h}:$Port (rasterizeddb_client, user rasterized)" if ($MysqlPort -gt 0) { Write-Host "MySQL: mysql -h $h -P $MysqlPort -u rasterized -p --ssl-mode=REQUIRED postgres" } if ($MssqlPort -gt 0) { Write-Host "SQL Server: sqlcmd -S $h,$MssqlPort -U rasterized -d postgres -C" } if ($KvPort -gt 0) { Write-Host "Redis: redis-cli -h $h -p $KvPort --tls --insecure --askpass PING" } Write-Host 'Every protocol logs in as the same roles. Open these ports in Windows Firewall for remote clients. Stop with Ctrl+C.' Write-Host '' & $cmd.Source @serverArgs exit $LASTEXITCODE