#Requires -Version 5.1 <# .SYNOPSIS RasterizedDB: keep chosen tables in an S3 bucket instead of local files. Defines a key/value store on a running server, then places new tables on it or moves existing ones there. The store is defined with IF NOT EXISTS, so running this again with the same settings does not change it. .EXAMPLE $env:PGPASSWORD = 'use-a-long-secret' .\setup.ps1 -Bucket my-tables -PlaceTables events, audit .EXAMPLE .\setup.ps1 -Bucket tables -Endpoint http://minio.internal:9000 -Addressing path -MoveTables logs #> [CmdletBinding()] param( [string]$PgUrl = $(if ($env:RDB_PG_URL) { $env:RDB_PG_URL } else { 'postgresql://rasterized@127.0.0.1:5432/postgres?sslmode=require' }), [string]$StoreName = $(if ($env:RDB_STORE_NAME) { $env:RDB_STORE_NAME } else { 'archive' }), [string]$Endpoint = $(if ($env:RDB_STORE_ENDPOINT) { $env:RDB_STORE_ENDPOINT } else { 'https://s3.us-east-1.amazonaws.com' }), [string]$Region = $(if ($env:RDB_STORE_REGION) { $env:RDB_STORE_REGION } else { 'us-east-1' }), [string]$Bucket = $env:RDB_STORE_BUCKET, # One server per prefix. [string]$Prefix = $(if ($env:RDB_STORE_PREFIX) { $env:RDB_STORE_PREFIX } else { 'rdb/archive' }), # path for MinIO and bucket names that are not DNS-safe. [ValidateSet('virtual', 'path')] [string]$Addressing = $(if ($env:RDB_STORE_ADDRESSING) { $env:RDB_STORE_ADDRESSING } else { 'virtual' }), # Page cache for this store's tables. [string]$Cache = $(if ($env:RDB_STORE_CACHE) { $env:RDB_STORE_CACHE } else { '256MiB' }), # The store keeps $env:AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY from this session. [switch]$OwnKeys = ($env:RDB_STORE_OWN_KEYS -eq '1'), # Tables not created yet. [string[]]$PlaceTables = @(), # Existing tables to move onto the store. [string[]]$MoveTables = @() ) $ErrorActionPreference = 'Stop' if (-not (Get-Command psql -ErrorAction SilentlyContinue)) { throw 'psql is needed.' } if (-not $Bucket) { throw 'Pass -Bucket with the bucket that will hold the tables.' } if (-not $env:PGPASSWORD) { throw 'Set $env:PGPASSWORD to the password of the role in -PgUrl (a superuser).' } foreach ($v in @($StoreName, $Endpoint, $Region, $Bucket, $Prefix, $Cache)) { if ($v.Contains("'")) { throw "Settings may not contain a single quote: $v" } } $credentials = '' if ($OwnKeys) { if (-not $env:AWS_ACCESS_KEY_ID -or -not $env:AWS_SECRET_ACCESS_KEY) { throw 'Set $env:AWS_ACCESS_KEY_ID and $env:AWS_SECRET_ACCESS_KEY for the store.' } if (($env:AWS_ACCESS_KEY_ID + $env:AWS_SECRET_ACCESS_KEY).Contains("'")) { throw 'The keys may not contain a single quote.' } $credentials = ", ACCESS_KEY_ID = '$($env:AWS_ACCESS_KEY_ID)', SECRET_ACCESS_KEY = '$($env:AWS_SECRET_ACCESS_KEY)'" } # Statements go to psql on standard input, so no key appears in the process list. function Invoke-Sql([string]$Statement) { $Statement | & psql $PgUrl -v ON_ERROR_STOP=1 -X -q return ($LASTEXITCODE -eq 0) } $define = "STORAGE ADD STORE IF NOT EXISTS $StoreName S3 WITH (ENDPOINT = '$Endpoint', REGION = '$Region', BUCKET = '$Bucket', PREFIX = '$Prefix', ADDRESSING = '$Addressing', CACHE_BYTES = '$Cache'$credentials);" if (-not (Invoke-Sql $define)) { throw 'The store was not defined; the error is above.' } Write-Host "Store ${StoreName}: s3 bucket $Bucket, prefix $Prefix" foreach ($t in $PlaceTables) { if (Invoke-Sql "STORAGE PLACE TABLE $t AT 'kv:$StoreName';") { Write-Host "placed: $t (create it now; it is made on the store)" } else { Write-Warning "$t was not placed (the error is above)" } } foreach ($t in $MoveTables) { if (Invoke-Sql "STORAGE MOVE TABLE $t TO 'kv:$StoreName';") { Write-Host "moved: $t" } else { Write-Warning "$t was not moved (the error is above)" } } [void](Invoke-Sql 'SHOW STORAGE STORES;') Write-Host 'Tables on the store show backend s3 in SHOW TABLES.'