forked from
lewis.moe/bspds-sandbox
I've been saying "PDSes seem easy enough, they're what, some CRUD to a db? I can do that in my sleep". well i'm sleeping rn so let's go
1#!/usr/bin/env bash
2set -euo pipefail
3INFRA_FILE="${TMPDIR:-/tmp}/tranquil_pds_test_infra.env"
4CONTAINER_PREFIX="tranquil-pds-test"
5command_exists() {
6 command -v "$1" >/dev/null 2>&1
7}
8if command_exists podman; then
9 CONTAINER_CMD="podman"
10 if [[ -z "${DOCKER_HOST:-}" ]]; then
11 RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
12 PODMAN_SOCK="$RUNTIME_DIR/podman/podman.sock"
13 if [[ -S "$PODMAN_SOCK" ]]; then
14 export DOCKER_HOST="unix://$PODMAN_SOCK"
15 fi
16 fi
17elif command_exists docker; then
18 CONTAINER_CMD="docker"
19else
20 echo "Error: Neither podman nor docker found" >&2
21 exit 1
22fi
23start_infra() {
24 echo "Starting test infrastructure..."
25 if [[ -f "$INFRA_FILE" ]]; then
26 source "$INFRA_FILE"
27 if $CONTAINER_CMD ps --format '{{.Names}}' 2>/dev/null | grep -q "^${CONTAINER_PREFIX}-postgres$"; then
28 echo "Infrastructure already running (found $INFRA_FILE)"
29 cat "$INFRA_FILE"
30 return 0
31 fi
32 echo "Stale infra file found, cleaning up..."
33 rm -f "$INFRA_FILE"
34 fi
35 $CONTAINER_CMD rm -f "${CONTAINER_PREFIX}-postgres" "${CONTAINER_PREFIX}-minio" "${CONTAINER_PREFIX}-valkey" 2>/dev/null || true
36 echo "Starting PostgreSQL..."
37 $CONTAINER_CMD run -d \
38 --name "${CONTAINER_PREFIX}-postgres" \
39 -e POSTGRES_PASSWORD=postgres \
40 -e POSTGRES_USER=postgres \
41 -e POSTGRES_DB=postgres \
42 -P \
43 --label tranquil_pds_test=true \
44 postgres:18-alpine >/dev/null
45 echo "Starting MinIO..."
46 $CONTAINER_CMD run -d \
47 --name "${CONTAINER_PREFIX}-minio" \
48 -e MINIO_ROOT_USER=minioadmin \
49 -e MINIO_ROOT_PASSWORD=minioadmin \
50 -P \
51 --label tranquil_pds_test=true \
52 minio/minio:latest server /data >/dev/null
53 echo "Starting Valkey..."
54 $CONTAINER_CMD run -d \
55 --name "${CONTAINER_PREFIX}-valkey" \
56 -P \
57 --label tranquil_pds_test=true \
58 valkey/valkey:8-alpine >/dev/null
59 echo "Waiting for services to be ready..."
60 sleep 2
61 PG_PORT=$($CONTAINER_CMD port "${CONTAINER_PREFIX}-postgres" 5432 | head -1 | cut -d: -f2)
62 MINIO_PORT=$($CONTAINER_CMD port "${CONTAINER_PREFIX}-minio" 9000 | head -1 | cut -d: -f2)
63 VALKEY_PORT=$($CONTAINER_CMD port "${CONTAINER_PREFIX}-valkey" 6379 | head -1 | cut -d: -f2)
64 for i in {1..30}; do
65 if $CONTAINER_CMD exec "${CONTAINER_PREFIX}-postgres" pg_isready -U postgres >/dev/null 2>&1; then
66 break
67 fi
68 echo "Waiting for PostgreSQL... ($i/30)"
69 sleep 1
70 done
71 for i in {1..30}; do
72 if curl -s "http://127.0.0.1:${MINIO_PORT}/minio/health/live" >/dev/null 2>&1; then
73 break
74 fi
75 echo "Waiting for MinIO... ($i/30)"
76 sleep 1
77 done
78 for i in {1..30}; do
79 if $CONTAINER_CMD exec "${CONTAINER_PREFIX}-valkey" valkey-cli ping 2>/dev/null | grep -q PONG; then
80 break
81 fi
82 echo "Waiting for Valkey... ($i/30)"
83 sleep 1
84 done
85 echo "Creating MinIO bucket..."
86 $CONTAINER_CMD run --rm --network host \
87 -e MC_HOST_minio="http://minioadmin:minioadmin@127.0.0.1:${MINIO_PORT}" \
88 minio/mc:latest mb minio/test-bucket --ignore-existing >/dev/null 2>&1 || true
89 cat > "$INFRA_FILE" << EOF
90export DATABASE_URL="postgres://postgres:postgres@127.0.0.1:${PG_PORT}/postgres"
91export TEST_DB_PORT="${PG_PORT}"
92export S3_ENDPOINT="http://127.0.0.1:${MINIO_PORT}"
93export S3_BUCKET="test-bucket"
94export AWS_ACCESS_KEY_ID="minioadmin"
95export AWS_SECRET_ACCESS_KEY="minioadmin"
96export AWS_REGION="us-east-1"
97export VALKEY_URL="redis://127.0.0.1:${VALKEY_PORT}"
98export TRANQUIL_PDS_TEST_INFRA_READY="1"
99export TRANQUIL_PDS_ALLOW_INSECURE_SECRETS="1"
100export SKIP_IMPORT_VERIFICATION="true"
101export DISABLE_RATE_LIMITING="1"
102EOF
103 echo ""
104 echo "Infrastructure ready!"
105 echo "Config written to: $INFRA_FILE"
106 echo ""
107 cat "$INFRA_FILE"
108}
109stop_infra() {
110 echo "Stopping test infrastructure..."
111 $CONTAINER_CMD rm -f "${CONTAINER_PREFIX}-postgres" "${CONTAINER_PREFIX}-minio" "${CONTAINER_PREFIX}-valkey" 2>/dev/null || true
112 rm -f "$INFRA_FILE"
113 echo "Infrastructure stopped."
114}
115status_infra() {
116 echo "Test Infrastructure Status:"
117 echo "============================"
118 if [[ -f "$INFRA_FILE" ]]; then
119 echo "Config file: $INFRA_FILE"
120 source "$INFRA_FILE"
121 echo "Database URL: $DATABASE_URL"
122 echo "S3 Endpoint: $S3_ENDPOINT"
123 else
124 echo "Config file: NOT FOUND"
125 fi
126 echo ""
127 echo "Containers:"
128 $CONTAINER_CMD ps -a --filter "label=tranquil_pds_test=true" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" 2>/dev/null || echo " (none)"
129}
130case "${1:-}" in
131 start)
132 start_infra
133 ;;
134 stop)
135 stop_infra
136 ;;
137 restart)
138 stop_infra
139 start_infra
140 ;;
141 status)
142 status_infra
143 ;;
144 env)
145 if [[ -f "$INFRA_FILE" ]]; then
146 cat "$INFRA_FILE"
147 else
148 echo "Infrastructure not running. Run: $0 start" >&2
149 exit 1
150 fi
151 ;;
152 *)
153 echo "Usage: $0 {start|stop|restart|status|env}"
154 echo ""
155 echo "Commands:"
156 echo " start - Start test infrastructure (Postgres, MinIO, Valkey)"
157 echo " stop - Stop and remove test containers"
158 echo " restart - Stop then start infrastructure"
159 echo " status - Show infrastructure status"
160 echo " env - Output environment variables for sourcing"
161 exit 1
162 ;;
163esac