#!/usr/bin/env bash # Alloy on the PostgreSQL VMs (WP-15, experiment design 4.1 / 4.3). Called by vm/postgres-{primary,replica}/apply.sh. # The harness role password is read from Secret Manager (selfheal-db-password) through the VM service account. Installs Alloy from the Grafana apt repository (systemd service `alloy`, # config /etc/alloy/config.alloy, environment /etc/default/alloy; https://grafana.com/docs/alloy/latest/configure/linux/), # exposes the built-in postgres exporter (pg_replication_lag_seconds) plus a custom query for pg_is_wal_replay_paused # (not in the upstream replication collector), and ships /var/log/postgresql to Loki. Idempotent. set -euo pipefail HERE="$(cd "$(dirname "$0")" && pwd)" if ! command -v alloy >/dev/null; then mkdir -p /etc/apt/keyrings wget -qO /etc/apt/keyrings/grafana.asc https://apt.grafana.com/gpg-full.key chmod 644 /etc/apt/keyrings/grafana.asc echo "deb [signed-by=/etc/apt/keyrings/grafana.asc] https://apt.grafana.com stable main" > /etc/apt/sources.list.d/grafana.list apt-get update -qq apt-get install -y -qq alloy >/dev/null fi usermod -aG adm alloy # /var/log/postgresql/*.log is postgres:adm 0640 install -d -m 755 /etc/alloy install -m 644 "$HERE/config.alloy" /etc/alloy/config.alloy install -m 644 "$HERE/queries.yaml" /etc/alloy/queries.yaml PROJECT="$(curl -sf -H 'Metadata-Flavor: Google' http://metadata.google.internal/computeMetadata/v1/project/project-id)" TOKEN="$(curl -sf -H 'Metadata-Flavor: Google' 'http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token' | jq -r .access_token)" PGPASSWORD="$(curl -sf -H "Authorization: Bearer ${TOKEN}" "https://secretmanager.googleapis.com/v1/projects/${PROJECT}/secrets/selfheal-db-password/versions/latest:access" | jq -r .payload.data | base64 -d)" unset TOKEN [ -n "$PGPASSWORD" ] || { echo "empty DB password" >&2; exit 1; } IP="$(hostname -I | awk '{print $1}')" umask 077 cat > /etc/default/alloy </dev/null 2>&1 || true systemctl restart alloy for i in $(seq 1 20); do c=$(curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:12345/-/ready || true) [ "$c" = "200" ] && break; sleep 3 done echo "alloy ready=$c"