this repo has no description
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 \
45 -c max_connections=500 >/dev/null
46 echo "Starting MinIO..."
47 $CONTAINER_CMD run -d \
48 --name "${CONTAINER_PREFIX}-minio" \
49 -e MINIO_ROOT_USER=minioadmin \
50 -e MINIO_ROOT_PASSWORD=minioadmin \
51 -P \
52 --label tranquil_pds_test=true \
53 minio/minio:latest server /data >/dev/null
54 echo "Starting Valkey..."
55 $CONTAINER_CMD run -d \
56 --name "${CONTAINER_PREFIX}-valkey" \
57 -P \
58 --label tranquil_pds_test=true \
59 valkey/valkey:8-alpine >/dev/null
60 echo "Waiting for services to be ready..."
61 sleep 2
62 PG_PORT=$($CONTAINER_CMD port "${CONTAINER_PREFIX}-postgres" 5432 | head -1 | cut -d: -f2)
63 MINIO_PORT=$($CONTAINER_CMD port "${CONTAINER_PREFIX}-minio" 9000 | head -1 | cut -d: -f2)
64 VALKEY_PORT=$($CONTAINER_CMD port "${CONTAINER_PREFIX}-valkey" 6379 | head -1 | cut -d: -f2)
65 for i in {1..30}; do
66 if $CONTAINER_CMD exec "${CONTAINER_PREFIX}-postgres" pg_isready -U postgres >/dev/null 2>&1; then
67 break
68 fi
69 echo "Waiting for PostgreSQL... ($i/30)"
70 sleep 1
71 done
72 for i in {1..30}; do
73 if curl -s "http://127.0.0.1:${MINIO_PORT}/minio/health/live" >/dev/null 2>&1; then
74 break
75 fi
76 echo "Waiting for MinIO... ($i/30)"
77 sleep 1
78 done
79 for i in {1..30}; do
80 if $CONTAINER_CMD exec "${CONTAINER_PREFIX}-valkey" valkey-cli ping 2>/dev/null | grep -q PONG; then
81 break
82 fi
83 echo "Waiting for Valkey... ($i/30)"
84 sleep 1
85 done
86 echo "Creating MinIO buckets..."
87 $CONTAINER_CMD run --rm --network host \
88 -e MC_HOST_minio="http://minioadmin:minioadmin@127.0.0.1:${MINIO_PORT}" \
89 minio/mc:latest mb minio/test-bucket --ignore-existing >/dev/null 2>&1 || true
90 $CONTAINER_CMD run --rm --network host \
91 -e MC_HOST_minio="http://minioadmin:minioadmin@127.0.0.1:${MINIO_PORT}" \
92 minio/mc:latest mb minio/test-backups --ignore-existing >/dev/null 2>&1 || true
93 cat > "$INFRA_FILE" << EOF
94export DATABASE_URL="postgres://postgres:postgres@127.0.0.1:${PG_PORT}/postgres"
95export TEST_DB_PORT="${PG_PORT}"
96export S3_ENDPOINT="http://127.0.0.1:${MINIO_PORT}"
97export S3_BUCKET="test-bucket"
98export BACKUP_S3_BUCKET="test-backups"
99export AWS_ACCESS_KEY_ID="minioadmin"
100export AWS_SECRET_ACCESS_KEY="minioadmin"
101export AWS_REGION="us-east-1"
102export VALKEY_URL="redis://127.0.0.1:${VALKEY_PORT}"
103export TRANQUIL_PDS_TEST_INFRA_READY="1"
104export TRANQUIL_PDS_ALLOW_INSECURE_SECRETS="1"
105export SKIP_IMPORT_VERIFICATION="true"
106export DISABLE_RATE_LIMITING="1"
107EOF
108 echo ""
109 echo "Infrastructure ready!"
110 echo "Config written to: $INFRA_FILE"
111 echo ""
112 cat "$INFRA_FILE"
113}
114stop_infra() {
115 echo "Stopping test infrastructure..."
116 $CONTAINER_CMD rm -f "${CONTAINER_PREFIX}-postgres" "${CONTAINER_PREFIX}-minio" "${CONTAINER_PREFIX}-valkey" 2>/dev/null || true
117 rm -f "$INFRA_FILE"
118 echo "Infrastructure stopped."
119}
120status_infra() {
121 echo "Test Infrastructure Status:"
122 echo "============================"
123 if [[ -f "$INFRA_FILE" ]]; then
124 echo "Config file: $INFRA_FILE"
125 source "$INFRA_FILE"
126 echo "Database URL: $DATABASE_URL"
127 echo "S3 Endpoint: $S3_ENDPOINT"
128 else
129 echo "Config file: NOT FOUND"
130 fi
131 echo ""
132 echo "Containers:"
133 $CONTAINER_CMD ps -a --filter "label=tranquil_pds_test=true" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" 2>/dev/null || echo " (none)"
134}
135case "${1:-}" in
136 start)
137 start_infra
138 ;;
139 stop)
140 stop_infra
141 ;;
142 restart)
143 stop_infra
144 start_infra
145 ;;
146 status)
147 status_infra
148 ;;
149 env)
150 if [[ -f "$INFRA_FILE" ]]; then
151 cat "$INFRA_FILE"
152 else
153 echo "Infrastructure not running. Run: $0 start" >&2
154 exit 1
155 fi
156 ;;
157 *)
158 echo "Usage: $0 {start|stop|restart|status|env}"
159 echo ""
160 echo "Commands:"
161 echo " start - Start test infrastructure (Postgres, MinIO, Valkey)"
162 echo " stop - Stop and remove test containers"
163 echo " restart - Stop then start infrastructure"
164 echo " status - Show infrastructure status"
165 echo " env - Output environment variables for sourcing"
166 exit 1
167 ;;
168esac