#!/usr/bin/env bash # RasterizedDB Enterprise: per-user shards on one machine. Three enterprise # shard servers and the enterprise gateway; each customer's login is pinned to # one shard, where its whole database lives, and the gateway sends every one # of its statements there. Customers are added through the gateway's # configuration API with add-customer.sh. # Needs release 1.1.0 or later. # # Everything runs in the background, with logs and process ids in RDB_WORK. # Running it again starts whatever is not running. Stop with: # kill $(cat per-user-cluster/*.pid) set -euo pipefail RDB_BIN="${RDB_BIN:-rasterizeddb-enterprise}" RDB_GATEWAY_BIN="${RDB_GATEWAY_BIN:-rasterizeddb-gateway-enterprise}" RDB_WORK="${RDB_WORK:-./per-user-cluster}" RDB_GATEWAY_PORT="${RDB_GATEWAY_PORT:-6432}" RDB_API_PORT="${RDB_API_PORT:-6480}" # the gateway's configuration API, on 127.0.0.1 RDB_USER_ROUTING="${RDB_USER_ROUTING:-prefer}" # prefer: unmapped logins use key routing; require: they are refused RDB_PG_BASE="${RDB_PG_BASE:-5440}" RDB_NATIVE_BASE="${RDB_NATIVE_BASE:-61180}" RDB_RSHARD_BASE="${RDB_RSHARD_BASE:-7400}" SHARDS=(0 1 2) die() { echo "error: $*" >&2; exit 1; } command -v "$RDB_BIN" >/dev/null 2>&1 || die "'$RDB_BIN' not found; set RDB_BIN to the enterprise server" command -v "$RDB_GATEWAY_BIN" >/dev/null 2>&1 || die "'$RDB_GATEWAY_BIN' not found; set RDB_GATEWAY_BIN to the enterprise gateway" case "$("$RDB_BIN" --help)" in *--enterprise-config*) ;; *) die "$RDB_BIN is not the enterprise server" ;; esac command -v openssl >/dev/null 2>&1 || die "openssl is needed for the certificates" command -v psql >/dev/null 2>&1 || die "psql is needed" : "${RASTERIZEDDB_PG_PASSWORD:?set RASTERIZEDDB_PG_PASSWORD; every shard gets the operator role rasterized with it}" export RASTERIZEDDB_PG_PASSWORD PGPASSWORD="$RASTERIZEDDB_PG_PASSWORD" mkdir -p "$RDB_WORK/tls" cd "$RDB_WORK" work="$PWD" RDB_BIN="$(command -v "$RDB_BIN")"; RDB_BIN="$(cd "$(dirname "$RDB_BIN")" && pwd)/$(basename "$RDB_BIN")" RDB_GATEWAY_BIN="$(command -v "$RDB_GATEWAY_BIN")"; RDB_GATEWAY_BIN="$(cd "$(dirname "$RDB_GATEWAY_BIN")" && pwd)/$(basename "$RDB_GATEWAY_BIN")" running() { [ -f "$1.pid" ] && kill -0 "$(cat "$1.pid")" 2>/dev/null; } wait_for() { # wait_for for _ in $(seq 1 60); do psql "postgresql://rasterized@127.0.0.1:$1/postgres?sslmode=$2" -Atqc "SELECT 1" >/dev/null 2>&1 && return 0 kill -0 "$(cat "$4")" 2>/dev/null || die "$3 exited; see $work/${4%.pid}.log" sleep 1 done die "$3 did not answer on port $1 within 60 s; see $work/${4%.pid}.log" } # 1. Certificates: a CA, one for each shard and one for the gateway. if [ ! -f tls/ca.pem ]; then ( cd tls umask 077 openssl req -x509 -newkey rsa:2048 -nodes -days 365 -subj /CN=rdb-cluster-ca -keyout ca.key -out ca.pem printf 'subjectAltName=DNS:localhost,IP:127.0.0.1\nextendedKeyUsage=serverAuth,clientAuth\n' > ext.cnf for name in shard0 shard1 shard2 gateway; do openssl req -newkey rsa:2048 -nodes -subj "/CN=$name" -keyout "$name.key" -out "$name.csr" openssl x509 -req -in "$name.csr" -CA ca.pem -CAkey ca.key -CAcreateserial -days 365 -extfile ext.cnf -out "$name.pem" done rm -f ./*.csr ) fi # 2. The shard map. if [ ! -f cluster.map ]; then { echo "rasterizeddb shard map" echo "format 1" echo "cluster $(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')" echo "epoch 1" echo "route_v 1" for n in "${SHARDS[@]}"; do echo "shard $n 127.0.0.1:$((RDB_RSHARD_BASE + n))"; done echo "range 0x0 0" echo "range 0x555555555555 1" echo "range 0xaaaaaaaaaaaa 2" } > cluster.map fi cluster_id="$(sed -n 's/^cluster //p' cluster.map)" # 3. Three enterprise shards. Each keeps the list of customer roles it hosts # in pinned_roles_file, which the gateway's API adds to. for n in "${SHARDS[@]}"; do [ -f "enterprise$n.conf" ] || echo "pinned_roles_file $work/shard$n.hosted_roles" > "enterprise$n.conf" if running "shard$n"; then echo "shard $n is running"; continue; fi mkdir -p "shard$n" nohup "$RDB_BIN" --enterprise-config "$work/enterprise$n.conf" --location "$work/shard$n" --protocol both \ --pg-port $((RDB_PG_BASE + n)) --rdb-port $((RDB_NATIVE_BASE + n)) \ --role shard --cluster-map "$work/cluster.map" --shard-id "$n" --cluster-id "$cluster_id" \ --shard-listen "127.0.0.1:$((RDB_RSHARD_BASE + n))" \ --shard-cert "$work/tls/shard$n.pem" --shard-key "$work/tls/shard$n.key" \ --gateway-ca "$work/tls/ca.pem" --peer-ca "$work/tls/ca.pem" \ --log-level info > "shard$n.log" 2>&1 & echo $! > "shard$n.pid" echo "started shard $n (pid $!)" done for n in "${SHARDS[@]}"; do wait_for $((RDB_PG_BASE + n)) require "shard $n" "shard$n.pid"; done # 4. The enterprise gateway, with per-user routing and its configuration API. if [ ! -f api.token ]; then (umask 077; od -An -N24 -tx1 /dev/urandom | tr -d ' \n' > api.token) fi cat > gateway.conf < gateway.log 2>&1 & echo $! > gateway.pid echo "started the gateway (pid $!)" fi wait_for "$RDB_GATEWAY_PORT" disable gateway gateway.pid echo echo "Up. Operator, through the gateway: psql \"postgresql://rasterized@127.0.0.1:$RDB_GATEWAY_PORT/postgres?sslmode=disable\"" echo "Configuration API: http://127.0.0.1:$RDB_API_PORT (bearer token in $work/api.token)" echo "Add a customer: RDB_CUSTOMER=acme RDB_CUSTOMER_SHARD=1 RDB_CUSTOMER_PASSWORD=… ./add-customer.sh" echo "Stop with: kill \$(cat $work/*.pid)"