forked from
tranquil.farm/tranquil-pds
Our Personal Data Server from scratch!
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" 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 "Waiting for services to be ready..."
47 for i in {1..30}; do
48 if $CONTAINER_CMD exec "${CONTAINER_PREFIX}-postgres" pg_isready -U postgres >/dev/null 2>&1; then
49 break
50 fi
51 echo "Waiting for PostgreSQL... ($i/30)"
52 sleep 1
53 done
54 PG_PORT=$($CONTAINER_CMD port "${CONTAINER_PREFIX}-postgres" 5432 | head -1 | cut -d: -f2)
55 cat > "$INFRA_FILE" << EOF
56export DATABASE_URL="postgres://postgres:postgres@127.0.0.1:${PG_PORT}/postgres"
57export TEST_DB_PORT="${PG_PORT}"
58export TRANQUIL_PDS_TEST_INFRA_READY="1"
59export TRANQUIL_PDS_ALLOW_INSECURE_SECRETS="1"
60export SKIP_IMPORT_VERIFICATION="true"
61export DISABLE_RATE_LIMITING="1"
62EOF
63 echo ""
64 echo "Infrastructure ready!"
65 echo "Config written to: $INFRA_FILE"
66 echo ""
67 cat "$INFRA_FILE"
68}
69stop_infra() {
70 echo "Stopping test infrastructure..."
71 $CONTAINER_CMD rm -f "${CONTAINER_PREFIX}-postgres" 2>/dev/null || true
72 rm -f "$INFRA_FILE"
73 rm -rf "${TMPDIR:-/tmp}"/tranquil-pds-test-* 2>/dev/null || true
74 echo "Infrastructure stopped."
75}
76status_infra() {
77 echo "Test Infrastructure Status:"
78 echo "============================"
79 if [[ -f "$INFRA_FILE" ]]; then
80 echo "Config file: $INFRA_FILE"
81 source "$INFRA_FILE"
82 echo "Database URL: $DATABASE_URL"
83 else
84 echo "Config file: NOT FOUND"
85 fi
86 echo ""
87 echo "Containers:"
88 $CONTAINER_CMD ps -a --filter "label=tranquil_pds_test=true" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" 2>/dev/null || echo " (none)"
89}
90case "${1:-}" in
91 start)
92 start_infra
93 ;;
94 stop)
95 stop_infra
96 ;;
97 restart)
98 stop_infra
99 start_infra
100 ;;
101 status)
102 status_infra
103 ;;
104 env)
105 if [[ -f "$INFRA_FILE" ]]; then
106 cat "$INFRA_FILE"
107 else
108 echo "Infrastructure not running. Run: $0 start" >&2
109 exit 1
110 fi
111 ;;
112 *)
113 echo "Usage: $0 {start|stop|restart|status|env}"
114 echo ""
115 echo "Commands:"
116 echo " start - Start test infrastructure (Postgres)"
117 echo " stop - Stop and remove test containers"
118 echo " restart - Stop then start infrastructure"
119 echo " status - Show infrastructure status"
120 echo " env - Output environment variables for sourcing"
121 exit 1
122 ;;
123esac