#!/usr/bin/env bash # Gives each node a replication identity (a self-signed certificate and its # key, in the node's own data volume) and writes both fingerprints into .env, # where compose.yaml reads them. Run once, before the first `docker compose up`. # # ./init-identities.sh # refuses if a node already has one # REGENERATE=1 ./init-identities.sh # replace both identities set -euo pipefail cd "$(dirname "$0")" if [ ! -f .env ]; then echo "Copy .env.example to .env and set the password first." >&2 exit 1 fi # compose.yaml needs both fingerprints to load at all. Stand-ins for the # commands below, which override the nodes' command lines anyway. placeholder="$(printf '0%.0s' $(seq 64))" compose() { NODE1_FINGERPRINT="$placeholder" NODE2_FINGERPRINT="$placeholder" docker compose "$@" } has_identity() { compose run --rm --no-deps -T --entrypoint test "$1" \ -e /var/lib/rasterizeddb/replication-cert.pem } generate() { compose run --rm --no-deps -T --entrypoint rasterizeddb "$1" \ --replication-generate-identity /var/lib/rasterizeddb --replication-addr "$1" \ | sed -n 's/^fingerprint: \([0-9a-f]\{64\}\)$/\1/p' } if [ "${REGENERATE:-0}" != 1 ]; then for node in node1 node2; do if has_identity "$node"; then echo "$node already has a replication identity. To replace both, run:" >&2 echo " REGENERATE=1 $0" >&2 exit 1 fi done fi fp1="$(generate node1)" fp2="$(generate node2)" if [ ${#fp1} -ne 64 ] || [ ${#fp2} -ne 64 ]; then echo "Could not read a fingerprint from the server's output." >&2 exit 1 fi tmp="$(mktemp .env.XXXXXX)" grep -v -E '^NODE[12]_FINGERPRINT=' .env > "$tmp" || true printf 'NODE1_FINGERPRINT=%s\nNODE2_FINGERPRINT=%s\n' "$fp1" "$fp2" >> "$tmp" mv "$tmp" .env echo "node1 fingerprint: $fp1" echo "node2 fingerprint: $fp2" echo "Written to .env. Start the nodes with: docker compose up -d" echo "(If they were already running, recreate them: docker compose up -d --force-recreate)"