#!/usr/bin/env bash # RasterizedDB as a message broker: topics, queues and exchanges inside the # database, reachable over its native broker protocol, Kafka, AMQP 0-9-1 # (RabbitMQ clients) and MQTT, with logins against the database's roles. # Runs in the foreground. Needs release 1.1.0 or later. # # Every setting can be overridden from the environment; set a port to "off" # to leave that protocol closed, for example: # RDB_LISTEN=0.0.0.0 RDB_KAFKA_PORT=off ./setup.sh set -euo pipefail RDB_BIN="${RDB_BIN:-rasterizeddb}" RDB_DATA="${RDB_DATA:-./data}" # topics are kept here, beside the tables RDB_LISTEN="${RDB_LISTEN:-127.0.0.1}" RDB_ADVERTISED_HOST="${RDB_ADVERTISED_HOST:-}" # the host Kafka clients are told to use; needed with 0.0.0.0 RDB_BROKER_PORT="${RDB_BROKER_PORT:-7171}" # native protocol (rasterizeddb_client::broker) RDB_KAFKA_PORT="${RDB_KAFKA_PORT:-9092}" RDB_AMQP_PORT="${RDB_AMQP_PORT:-5672}" RDB_MQTT_PORT="${RDB_MQTT_PORT:-1883}" RDB_BROKER_TLS="${RDB_BROKER_TLS:-off}" # on: every broker port uses TLS, with the PostgreSQL certificate RDB_TLS_CERT="${RDB_TLS_CERT:-}" RDB_TLS_KEY="${RDB_TLS_KEY:-}" RDB_BROKER_DDL="${RDB_BROKER_DDL:-any}" # who may create topics and queues: any or superuser die() { echo "error: $*" >&2; exit 1; } command -v "$RDB_BIN" >/dev/null 2>&1 || die "'$RDB_BIN' not found; set RDB_BIN to the server binary" case "$("$RDB_BIN" --help)" in *--broker-port*) ;; *) die "this $RDB_BIN has no broker; it needs server 1.1.0 or later, built with the broker feature" ;; esac : "${RASTERIZEDDB_PG_PASSWORD:?set RASTERIZEDDB_PG_PASSWORD to the password for the role rasterized}" export RASTERIZEDDB_PG_PASSWORD mkdir -p "$RDB_DATA" RDB_DATA="$(cd "$RDB_DATA" && pwd)" if [ -z "$RDB_ADVERTISED_HOST" ]; then if [ "$RDB_LISTEN" = 0.0.0.0 ] || [ "$RDB_LISTEN" = "::" ]; then RDB_ADVERTISED_HOST="$(hostname -f 2>/dev/null || hostname)" else RDB_ADVERTISED_HOST="$RDB_LISTEN" fi fi args=(--location "$RDB_DATA" --protocol both --pg-addr "$RDB_LISTEN" --broker-addr "$RDB_LISTEN" --broker-ddl "$RDB_BROKER_DDL") [ -z "$RDB_TLS_CERT" ] || args+=(--pg-cert "$RDB_TLS_CERT" --pg-key "$RDB_TLS_KEY") if [ "$RDB_BROKER_PORT" != off ]; then args+=(--broker-port "$RDB_BROKER_PORT" --broker-tls "$RDB_BROKER_TLS") fi if [ "$RDB_KAFKA_PORT" != off ]; then args+=(--kafka-port "$RDB_KAFKA_PORT" --kafka-advertised-host "$RDB_ADVERTISED_HOST" --kafka-tls "$RDB_BROKER_TLS") fi if [ "$RDB_AMQP_PORT" != off ]; then args+=(--amqp-port "$RDB_AMQP_PORT" --amqp-tls "$RDB_BROKER_TLS") fi if [ "$RDB_MQTT_PORT" != off ]; then args+=(--mqtt-port "$RDB_MQTT_PORT" --mqtt-tls "$RDB_BROKER_TLS") fi h="$RDB_ADVERTISED_HOST" echo "SQL: psql \"postgresql://rasterized@$h:5432/postgres?sslmode=require\" -c \"BROKER SHOW TOPICS\"" [ "$RDB_BROKER_PORT" = off ] || echo "Native: $h:$RDB_BROKER_PORT (rasterizeddb_client::broker)" [ "$RDB_KAFKA_PORT" = off ] || echo "Kafka: bootstrap.servers=$h:$RDB_KAFKA_PORT, SASL PLAIN as rasterized" [ "$RDB_AMQP_PORT" = off ] || echo "AMQP: amqp$([ "$RDB_BROKER_TLS" = on ] && echo s)://rasterized:@$h:$RDB_AMQP_PORT/" [ "$RDB_MQTT_PORT" = off ] || echo "MQTT: $h:$RDB_MQTT_PORT, user rasterized" echo "Stop with Ctrl+C." echo exec "$RDB_BIN" "${args[@]}"