-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_databases.sh
More file actions
executable file
·199 lines (175 loc) · 7.03 KB
/
Copy pathsetup_databases.sh
File metadata and controls
executable file
·199 lines (175 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env bash
# CS2 Demo Analysis & AI Training System - Database Setup Script
# Sets up Postgres/TimescaleDB, Qdrant, Redis, and Garage (S3-compatible) with v2 layout init.
set -euo pipefail
echo "🚀 Setting up CS2 Demo Analysis Database Infrastructure..."
# Defaults (override with env)
POSTGRES_DB=${POSTGRES_DB:-"cs2_analysis"}
POSTGRES_USER=${POSTGRES_USER:-"cs2_user"}
POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-"cs2_password"}
QDRANT_HTTP_PORT=${QDRANT_HTTP_PORT:-6333}
QDRANT_GRPC_PORT=${QDRANT_GRPC_PORT:-6334}
# Garage init defaults (per docs)
GARAGE_BUCKET=${GARAGE_BUCKET:-"fps-genie"}
GARAGE_KEY_NAME=${GARAGE_KEY_NAME:-"fps-genie-key"}
GARAGE_ENV_OUT=${GARAGE_ENV_OUT:-"garage/s3-credentials.env"}
GARAGE_ZONE=${GARAGE_ZONE:-"dc1"}
GARAGE_CAPACITY=${GARAGE_CAPACITY:-"1G"}
echo "📦 Starting services with Docker Compose..."
docker compose up -d
echo "⏳ Waiting for services to be ready..."
# Wait for Postgres/TimescaleDB
echo "🔍 Checking PostgreSQL/TimescaleDB..."
for i in $(seq 1 60); do
if docker compose exec -T postgres psql -U "${POSTGRES_USER}" -d "${POSTGRES_DB}" -c "SELECT 1;" >/dev/null 2>&1; then
echo "✅ PostgreSQL/TimescaleDB: Connected"
break
fi
sleep 1
if [ "$i" -eq 60 ]; then
echo "❌ PostgreSQL/TimescaleDB: Connection failed"
exit 1
fi
done
# Wait for Qdrant HTTP
echo "🔍 Checking Qdrant (HTTP ${QDRANT_HTTP_PORT})..."
for i in $(seq 1 60); do
if curl http://localhost:6333/telemetry -H "api-key: 1234" >/dev/null 2>&1; then
echo "✅ Qdrant Vector DB (HTTP): Connected"
break
fi
sleep 1
if [ "$i" -eq 60 ]; then
echo "❌ Qdrant Vector DB (HTTP): Connection failed"
exit 1
fi
done
# Wait for Redis
echo "🔍 Checking Redis..."
for i in $(seq 1 60); do
if docker compose exec -T redis redis-cli ping | grep -q PONG; then
echo "✅ Redis: Connected"
break
fi
sleep 1
if [ "$i" -eq 60 ]; then
echo "❌ Redis: Connection failed"
exit 1
fi
done
# Wait for Garage health
echo "🔍 Checking Garage..."
for i in $(seq 1 60); do
STATUS="$(docker inspect -f '{{.State.Health.Status}}' garage 2>/dev/null || echo 'starting')"
if [ "$STATUS" = "healthy" ]; then
echo "✅ Garage: Healthy"
break
fi
sleep 1
if [ "$i" -eq 60 ]; then
echo "❌ Garage: Not healthy"
exit 1
fi
done
# Helper to run Garage CLI inside the running container (no shell inside image)
gexec() {
docker compose exec -T garage /garage -c /etc/garage/config.toml "$@"
}
echo "🧩 Initializing Garage v2 layout (single node, per docs)..."
# 1) Get node id and strip any @host:port suffix
NODE_ID_RAW="$(gexec node id 2>/dev/null | head -n1 | tr -d '\r' | tr -d '[:space:]')"
if [ -z "${NODE_ID_RAW}" ]; then
echo "❌ Unable to retrieve Garage node id"
exit 1
fi
NODE_ID="${NODE_ID_RAW%@*}"
echo "ℹ️ Garage node id: ${NODE_ID_RAW} (using ${NODE_ID})"
# 2) Assign role to node with zone and capacity (idempotent)
echo "➡️ Assigning layout: zone=${GARAGE_ZONE}, capacity=${GARAGE_CAPACITY}, node=${NODE_ID}"
gexec layout assign -z "${GARAGE_ZONE}" -c "${GARAGE_CAPACITY}" "${NODE_ID}" >/dev/null 2>&1 || true
# 3) Compute next version from layout history and apply
LAST_VER="$(gexec layout history 2>/dev/null | grep -Eo 'Version[[:space:]]+[0-9]+' | awk '{print $2}' | sort -n | tail -n1 || true)"
if [ -z "${LAST_VER:-}" ]; then LAST_VER=0; fi
NEXT_VER=$((LAST_VER + 1))
echo "➡️ Applying layout with --version ${NEXT_VER}..."
gexec layout apply --version "${NEXT_VER}" >/dev/null 2>&1 || true
# Show resulting layout for visibility
gexec layout show || true
# 4) Create bucket if needed (robust idempotency)
if gexec bucket info "${GARAGE_BUCKET}" >/dev/null 2>&1; then
echo "ℹ️ Bucket '${GARAGE_BUCKET}' already exists"
else
echo "➡️ Creating bucket: ${GARAGE_BUCKET}"
if ! OUT="$(gexec bucket create "${GARAGE_BUCKET}" 2>&1)"; then
if echo "${OUT}" | grep -q "BucketAlreadyExists"; then
echo "ℹ️ Bucket '${GARAGE_BUCKET}' already exists"
else
echo "❌ Failed to create bucket '${GARAGE_BUCKET}':"
echo "${OUT}"
exit 1
fi
fi
fi
# 5) Create or read API key (v2 docs: key create <name>)
NEW_KEY_CREATED=0
KEY_ID=""
SECRET=""
if gexec key info "${GARAGE_KEY_NAME}" >/dev/null 2>&1; then
echo "ℹ️ Key '${GARAGE_KEY_NAME}' already exists"
# Parse Key ID (secret is redacted for existing keys)
KEY_ID="$(gexec key info "${GARAGE_KEY_NAME}" | awk -F': ' '/Key ID:/ {print $2; exit}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | tr -d '\r')"
else
echo "➡️ Creating access key: ${GARAGE_KEY_NAME}"
OUT="$(gexec key create "${GARAGE_KEY_NAME}")"
KEY_ID="$(echo "${OUT}" | awk -F': ' '/Key ID:/ {print $2; exit}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | tr -d '\r')"
SECRET="$(echo "${OUT}" | awk -F': ' '/Secret key:/ {print $2; exit}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | tr -d '\r')"
if [ -z "${KEY_ID:-}" ] || [ -z "${SECRET:-}" ]; then
echo "❌ Failed to parse Garage access key output:"
echo "${OUT}"
exit 1
fi
NEW_KEY_CREATED=1
fi
# 6) Allow key to access the bucket (idempotent; using key name as per docs)
echo "➡️ Ensuring key has permissions on bucket..."
if ! gexec bucket info "${GARAGE_BUCKET}" | grep -q "${GARAGE_KEY_NAME}"; then
gexec bucket allow --read --write --owner "${GARAGE_BUCKET}" --key "${GARAGE_KEY_NAME}" >/dev/null 2>&1 || true
fi
# 7) Write credentials to host file ONLY if it doesn't already exist
if [ -f "${GARAGE_ENV_OUT}" ]; then
echo "🔐 Credentials file already exists at ${GARAGE_ENV_OUT}; not overwriting."
else
cat > "${GARAGE_ENV_OUT}" <<EOF
# Generated by setup_databases.sh
S3_ENDPOINT=http://localhost:3900
AWS_ENDPOINT_URL=http://localhost:3900
AWS_ACCESS_KEY_ID=${KEY_ID}
AWS_SECRET_ACCESS_KEY=${SECRET}
AWS_REGION=garage
S3_BUCKET=${GARAGE_BUCKET}
AWS_S3_FORCE_PATH_STYLE=true
EOF
echo "✅ Garage credentials written to ${GARAGE_ENV_OUT}"
fi
echo ""
echo "🎉 Infrastructure setup complete!"
echo ""
echo "📊 Connection Details:"
echo " PostgreSQL/TimescaleDB: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:5432/${POSTGRES_DB}"
echo " Qdrant Vector DB: HTTP http://localhost:${QDRANT_HTTP_PORT} | gRPC http://localhost:${QDRANT_GRPC_PORT}"
echo " Redis Cache: redis://localhost:6379"
echo " Garage Object Storage: S3 http://localhost:3900 (credentials in ${GARAGE_ENV_OUT})"
echo ""
echo "🚀 Next steps:"
echo " 1. export DATABASE_URL=\"postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:5432/${POSTGRES_DB}\""
echo " 2. export QDRANT_URL=\"http://localhost:${QDRANT_GRPC_PORT}\""
echo " 3. source ${GARAGE_ENV_OUT} # to export S3 variables for your app"
echo " 4. Run: cd cs2-data-pipeline && cargo run -- init"
echo " 5. Place demo files in ./demos/ directory"
echo " 6. Run: cargo run -- run"
echo ""
echo "📈 Expected Performance (from PDF specifications):"
echo " - Process 700MB+/s demo data on high-end PC"
echo " - Support 5TB initial TimescaleDB storage"
echo " - Handle 2TB vector embeddings in Qdrant"
echo " - Scale to 20TB+ object storage for demo archives"