-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.sh
More file actions
executable file
·572 lines (495 loc) · 15.6 KB
/
stack.sh
File metadata and controls
executable file
·572 lines (495 loc) · 15.6 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
STACK_ROOT="${SCRIPT_DIR}"
CALLER_ROOT="$(pwd -P)"
DEFAULT_WORKSPACE_DIR="${CALLER_ROOT}"
if [[ -d "${SCRIPT_DIR}/../Volt" && -d "${SCRIPT_DIR}/../ClusterDaemon" ]]; then
DEFAULT_WORKSPACE_DIR="$(cd "${SCRIPT_DIR}/.." && pwd -P)"
fi
WORKSPACE_DIR_INPUT="${VOLT_DEV_WORKSPACE_DIR:-${DEFAULT_WORKSPACE_DIR}}"
if [[ "${WORKSPACE_DIR_INPUT}" = /* ]]; then
WORKSPACE_DIR="${WORKSPACE_DIR_INPUT}"
else
WORKSPACE_DIR="${CALLER_ROOT}/${WORKSPACE_DIR_INPUT}"
fi
VOLT_ROOT="${WORKSPACE_DIR}/Volt"
VOLT_SERVER_ROOT="${VOLT_ROOT}/server"
VOLT_CLIENT_ROOT="${VOLT_ROOT}/client"
GENERATED_DIR="${STACK_ROOT}/.generated"
CLUSTERS_GENERATED_DIR="${GENERATED_DIR}/clusters"
STORAGE_CLUSTER_GENERATED_DIR="${CLUSTERS_GENERATED_DIR}/storage"
COMPUTE_CLUSTER_GENERATED_DIR="${CLUSTERS_GENERATED_DIR}/compute"
BASE_COMPOSE_FILE="${STACK_ROOT}/compose.base.yml"
GENERATED_CLUSTER_COMPOSE_FILE="${GENERATED_DIR}/compose.cluster.yml"
export VOLT_DEV_VOLT_SERVER_DIR="${VOLT_SERVER_ROOT}"
export VOLT_DEV_VOLT_CLIENT_DIR="${VOLT_CLIENT_ROOT}"
PROJECT_NAME="${VOLT_DEV_STACK_PROJECT_NAME:-volt-dev}"
PUBLIC_API_URL="${VOLT_DEV_PUBLIC_API_URL:-http://localhost:8000}"
PUBLIC_WEB_URL="${VOLT_DEV_PUBLIC_WEB_URL:-http://localhost:5173}"
INTERNAL_API_URL="${VOLT_DEV_INTERNAL_API_URL:-http://volt-server:8000}"
MINIO_HOST_PORT="${VOLT_DEV_MINIO_PORT:-9000}"
MINIO_CONSOLE_HOST_PORT="${VOLT_DEV_MINIO_CONSOLE_PORT:-9001}"
PUBLIC_MINIO_URL="${VOLT_DEV_PUBLIC_MINIO_URL:-http://localhost:${MINIO_HOST_PORT}}"
DEFAULT_CLUSTER_DAEMON_IMAGE="ghcr.io/voltlabs-research/volt-cluster-daemon:main"
CLUSTER_DAEMON_IMAGE="${VOLT_DEV_CLUSTER_DAEMON_IMAGE:-${DEFAULT_CLUSTER_DAEMON_IMAGE}}"
if [[ -n "${VOLT_DEV_CLUSTER_DAEMON_PATH:-}" ]]; then
CLUSTER_DAEMON_PATH_INPUT="${VOLT_DEV_CLUSTER_DAEMON_PATH}"
elif [[ -d "${WORKSPACE_DIR}/ClusterDaemon" ]]; then
CLUSTER_DAEMON_PATH_INPUT="${WORKSPACE_DIR}/ClusterDaemon"
else
CLUSTER_DAEMON_PATH_INPUT=""
fi
BOOTSTRAP_SCRIPT="${STACK_ROOT}/scripts/bootstrap-dev-stack.ts"
BOOTSTRAP_WORKSPACE_SCRIPT="${STACK_ROOT}/bootstrap-workspace.sh"
ensure_tools() {
command -v docker >/dev/null 2>&1 || {
echo "docker is required" >&2
exit 1
}
docker compose version >/dev/null 2>&1 || {
echo "docker compose is required" >&2
exit 1
}
command -v node >/dev/null 2>&1 || {
echo "node is required" >&2
exit 1
}
[[ -f "${BOOTSTRAP_SCRIPT}" ]] || {
echo "Expected ${BOOTSTRAP_SCRIPT}" >&2
exit 1
}
[[ -f "${VOLT_SERVER_ROOT}/Dockerfile.dev" ]] || {
echo "Expected ${VOLT_SERVER_ROOT}/Dockerfile.dev. Run ${BOOTSTRAP_WORKSPACE_SCRIPT} in the workspace root, or set VOLT_DEV_WORKSPACE_DIR." >&2
exit 1
}
[[ -f "${VOLT_CLIENT_ROOT}/Dockerfile.dev" ]] || {
echo "Expected ${VOLT_CLIENT_ROOT}/Dockerfile.dev. Run ${BOOTSTRAP_WORKSPACE_SCRIPT} in the workspace root, or set VOLT_DEV_WORKSPACE_DIR." >&2
exit 1
}
}
yaml_quote() {
local value="$1"
value="${value//\'/\'\'}"
printf "'%s'" "${value}"
}
resolve_cluster_daemon_path() {
local input_path="$1"
if [[ "${input_path}" = /* ]]; then
[[ -d "${input_path}" ]] || {
echo "ClusterDaemon path '${input_path}' does not exist" >&2
exit 1
}
(
cd "${input_path}"
pwd -P
)
return 0
fi
if [[ -d "${input_path}" ]]; then
(
cd "${input_path}"
pwd -P
)
return 0
fi
if [[ -d "${STACK_ROOT}/${input_path}" ]]; then
(
cd "${STACK_ROOT}/${input_path}"
pwd -P
)
return 0
fi
echo "ClusterDaemon path '${input_path}' does not exist relative to the current directory or the local-stack root" >&2
exit 1
}
assert_cluster_daemon_source_layout() {
local cluster_daemon_path="$1"
[[ -f "${cluster_daemon_path}/Dockerfile.dev" ]] || {
echo "Expected ${cluster_daemon_path}/Dockerfile.dev" >&2
exit 1
}
[[ -f "${cluster_daemon_path}/package.json" ]] || {
echo "Expected ${cluster_daemon_path}/package.json" >&2
exit 1
}
}
ensure_generated_layout() {
mkdir -p "${GENERATED_DIR}"
ensure_cluster_generated_layout "${STORAGE_CLUSTER_GENERATED_DIR}"
ensure_cluster_generated_layout "${COMPUTE_CLUSTER_GENERATED_DIR}"
}
ensure_cluster_generated_layout() {
local cluster_dir="$1"
mkdir -p "${cluster_dir}"
[[ -f "${cluster_dir}/minio.env" ]] || : > "${cluster_dir}/minio.env"
[[ -f "${cluster_dir}/mongodb.env" ]] || : > "${cluster_dir}/mongodb.env"
[[ -f "${cluster_dir}/redis.env" ]] || cat > "${cluster_dir}/redis.env" <<'EOF'
REDIS_USERNAME=placeholder
REDIS_PASSWORD=placeholder
EOF
[[ -f "${cluster_dir}/redis.acl" ]] || cat > "${cluster_dir}/redis.acl" <<'EOF'
user default off
user placeholder on >placeholder ~* &* +@all
EOF
[[ -f "${cluster_dir}/daemon.env" ]] || : > "${cluster_dir}/daemon.env"
}
write_base_envs() {
ensure_generated_layout
cat > "${GENERATED_DIR}/server.env" <<EOF
NODE_ENV=development
LOG_LEVEL=info
REDIS_HOST=volt-redis
REDIS_PORT=6379
REDIS_PASSWORD=voltredis
MINIO_ENDPOINT=volt-minio
MINIO_PORT=9000
MINIO_USE_SSL=false
MINIO_ACCESS_KEY=voltminio
MINIO_SECRET_KEY=voltminiosecret
MINIO_PUBLIC_URL=${PUBLIC_MINIO_URL}
SERVER_ENDPOINT=${PUBLIC_API_URL}
SERVER_PORT=8000
TEAM_CLUSTER_BINARY_RELAY_ENABLED=false
TEAM_CLUSTER_OBJECT_GATEWAY_BINARY_RELAY_ENABLED=false
TEAM_CLUSTER_BINARY_RELAY_ENDPOINT=${INTERNAL_API_URL}
TEAM_CLUSTER_BINARY_RELAY_ADVERTISED_HOST=volt-server
SERVER_HOST=0.0.0.0
SERVER_HOSTNAME=localhost
SERVER_SCHEMA=http
TEAM_CLUSTER_APP_PROXY_BIND_HOST=0.0.0.0
CLIENT_HOST=${PUBLIC_WEB_URL}
CLIENT_DEV_HOST=${PUBLIC_WEB_URL}
MONGO_URI=mongodb://volt:volt@volt-mongodb:27017?authSource=admin
MONGO_AUTH_SOURCE=admin
PRODUCTION_DATABASE=voltcloud@production
DEVELOPMENT_DATABASE=voltcloud@development
SSH_ENCRYPTION_KEY=volt-dev-ssh-key
JWT_EXPIRATION_DAYS=7d
SECRET_KEY=volt-dev-secret-key
TEAM_CLUSTER_TCP_RELAY_BIND_HOST=0.0.0.0
TEAM_CLUSTER_TCP_RELAY_ADVERTISED_HOST=volt-server
TEAM_CLUSTER_TCP_RELAY_PORT_START=23000
TEAM_CLUSTER_TCP_RELAY_PORT_END=23999
TEAM_CLUSTER_TAILSCALE_ENABLED=false
TEAM_CLUSTER_TAILSCALE_REQUIRED=false
EOF
cat > "${GENERATED_DIR}/client.env" <<EOF
VITE_ENV=development
VITE_API_URL=${PUBLIC_API_URL}
VITE_PROXY_API_URL=${INTERNAL_API_URL}
EOF
}
write_cluster_compose() {
ensure_generated_layout
local daemon_definition=''
local daemon_mounts=''
if [[ -n "${CLUSTER_DAEMON_PATH_INPUT}" ]]; then
local cluster_daemon_source_path
cluster_daemon_source_path="$(resolve_cluster_daemon_path "${CLUSTER_DAEMON_PATH_INPUT}")"
assert_cluster_daemon_source_layout "${cluster_daemon_source_path}"
local build_context
local source_mount
build_context="$(yaml_quote "${cluster_daemon_source_path}")"
source_mount="$(yaml_quote "${cluster_daemon_source_path}:/app")"
daemon_definition=$(cat <<EOF
build:
context: ${build_context}
dockerfile: Dockerfile.dev
EOF
)
daemon_mounts=$(cat <<EOF
- ${source_mount}
- /app/node_modules
- /var/run/docker.sock:/var/run/docker.sock
EOF
)
else
daemon_definition=$(cat <<EOF
image: ${CLUSTER_DAEMON_IMAGE}
EOF
)
daemon_mounts=$(cat <<'EOF'
- /var/run/docker.sock:/var/run/docker.sock
EOF
)
fi
cat > "${GENERATED_CLUSTER_COMPOSE_FILE}" <<EOF
services:
storage-mongodb:
image: mongo:8.0.5
restart: unless-stopped
env_file:
- ./.generated/clusters/storage/mongodb.env
healthcheck:
test: ["CMD-SHELL", "mongosh --quiet --host localhost --username \$\$MONGO_INITDB_ROOT_USERNAME --password \$\$MONGO_INITDB_ROOT_PASSWORD --authenticationDatabase admin --eval \"db.adminCommand('ping').ok\" | grep 1"]
interval: 10s
timeout: 5s
retries: 20
volumes:
- storage-mongodb-data:/data/db
storage-redis:
image: redis:7.4.2-alpine
restart: unless-stopped
env_file:
- ./.generated/clusters/storage/redis.env
command:
- redis-server
- --appendonly
- "yes"
- --aclfile
- /usr/local/etc/redis/users.acl
healthcheck:
test: ["CMD-SHELL", "redis-cli --user \$\$REDIS_USERNAME --pass \$\$REDIS_PASSWORD ping | grep PONG"]
interval: 10s
timeout: 5s
retries: 20
volumes:
- storage-redis-data:/data
- ./.generated/clusters/storage/redis.acl:/usr/local/etc/redis/users.acl:ro
storage-minio:
image: minio/minio:RELEASE.2025-02-28T09-55-16Z
restart: unless-stopped
env_file:
- ./.generated/clusters/storage/minio.env
command: server /data --console-address ":9001"
volumes:
- storage-minio-data:/data
storage-daemon:
${daemon_definition}
restart: unless-stopped
env_file:
- ./.generated/clusters/storage/daemon.env
depends_on:
storage-mongodb:
condition: service_healthy
storage-redis:
condition: service_healthy
storage-minio:
condition: service_started
volt-server:
condition: service_started
volumes:
${daemon_mounts}
compute-mongodb:
image: mongo:8.0.5
restart: unless-stopped
env_file:
- ./.generated/clusters/compute/mongodb.env
healthcheck:
test: ["CMD-SHELL", "mongosh --quiet --host localhost --username \$\$MONGO_INITDB_ROOT_USERNAME --password \$\$MONGO_INITDB_ROOT_PASSWORD --authenticationDatabase admin --eval \"db.adminCommand('ping').ok\" | grep 1"]
interval: 10s
timeout: 5s
retries: 20
volumes:
- compute-mongodb-data:/data/db
compute-redis:
image: redis:7.4.2-alpine
restart: unless-stopped
env_file:
- ./.generated/clusters/compute/redis.env
command:
- redis-server
- --appendonly
- "yes"
- --aclfile
- /usr/local/etc/redis/users.acl
healthcheck:
test: ["CMD-SHELL", "redis-cli --user \$\$REDIS_USERNAME --pass \$\$REDIS_PASSWORD ping | grep PONG"]
interval: 10s
timeout: 5s
retries: 20
volumes:
- compute-redis-data:/data
- ./.generated/clusters/compute/redis.acl:/usr/local/etc/redis/users.acl:ro
compute-minio:
image: minio/minio:RELEASE.2025-02-28T09-55-16Z
restart: unless-stopped
env_file:
- ./.generated/clusters/compute/minio.env
command: server /data --console-address ":9001"
volumes:
- compute-minio-data:/data
compute-daemon:
${daemon_definition}
restart: unless-stopped
env_file:
- ./.generated/clusters/compute/daemon.env
depends_on:
compute-mongodb:
condition: service_healthy
compute-redis:
condition: service_healthy
compute-minio:
condition: service_started
volt-server:
condition: service_started
volumes:
${daemon_mounts}
volumes:
storage-mongodb-data:
storage-redis-data:
storage-minio-data:
compute-mongodb-data:
compute-redis-data:
compute-minio-data:
EOF
}
docker_compose_base() {
docker compose --project-directory "${STACK_ROOT}" -p "${PROJECT_NAME}" -f "${BASE_COMPOSE_FILE}" "$@"
}
docker_compose_all() {
docker compose --project-directory "${STACK_ROOT}" -p "${PROJECT_NAME}" -f "${BASE_COMPOSE_FILE}" -f "${GENERATED_CLUSTER_COMPOSE_FILE}" "$@"
}
cleanup_managed_runtime_containers() {
local managed_container_ids
managed_container_ids="$(docker ps -aq --filter label=volt.managed=true)"
if [[ -z "${managed_container_ids}" ]]; then
return 0
fi
docker rm -f ${managed_container_ids} >/dev/null
}
base_up_services() {
local services=(
volt-mongodb
volt-redis
volt-minio
volt-server
volt-client
)
printf '%s\n' "${services[@]}"
}
cluster_up_services() {
local services=(
storage-mongodb
storage-redis
storage-minio
storage-daemon
compute-mongodb
compute-redis
compute-minio
compute-daemon
)
printf '%s\n' "${services[@]}"
}
run_bootstrap_provision() {
VOLT_DEV_STACK_PROJECT_NAME="${PROJECT_NAME}" \
VOLT_DEV_PUBLIC_API_URL="${PUBLIC_API_URL}" \
VOLT_DEV_PUBLIC_WEB_URL="${PUBLIC_WEB_URL}" \
VOLT_DEV_INTERNAL_API_URL="${INTERNAL_API_URL}" \
node "${BOOTSTRAP_SCRIPT}" \
provision \
--output-dir "${CLUSTERS_GENERATED_DIR}"
}
run_bootstrap_wait_cluster() {
VOLT_DEV_STACK_PROJECT_NAME="${PROJECT_NAME}" \
VOLT_DEV_PUBLIC_API_URL="${PUBLIC_API_URL}" \
VOLT_DEV_PUBLIC_WEB_URL="${PUBLIC_WEB_URL}" \
VOLT_DEV_INTERNAL_API_URL="${INTERNAL_API_URL}" \
node "${BOOTSTRAP_SCRIPT}" \
wait-cluster \
--output-dir "${CLUSTERS_GENERATED_DIR}"
}
pull_remote_cluster_daemon_images() {
if [[ -n "${CLUSTER_DAEMON_PATH_INPUT}" ]]; then
return 0
fi
docker_compose_all pull storage-daemon compute-daemon
}
prepare_stack_state() {
write_base_envs
write_cluster_compose
}
cmd_up() {
prepare_stack_state
cleanup_managed_runtime_containers
mapfile -t base_services < <(base_up_services)
docker_compose_base up -d --build --remove-orphans "${base_services[@]}"
run_bootstrap_provision
pull_remote_cluster_daemon_images
mapfile -t cluster_services < <(cluster_up_services)
docker_compose_all up -d --build --remove-orphans "${cluster_services[@]}"
run_bootstrap_wait_cluster
}
cmd_down() {
prepare_stack_state
cleanup_managed_runtime_containers
docker_compose_all down --remove-orphans
}
cmd_reset() {
prepare_stack_state
cleanup_managed_runtime_containers
docker_compose_all down --volumes --remove-orphans
rm -rf "${GENERATED_DIR}"
}
cmd_bootstrap() {
prepare_stack_state
run_bootstrap_provision
}
cmd_logs() {
prepare_stack_state
shift || true
docker_compose_all logs -f "$@"
}
cmd_ps() {
prepare_stack_state
docker_compose_all ps
}
usage() {
cat <<EOF
Usage: $(basename "$0") <command>
Commands:
up Build and start Volt, bootstrap dev data, then start the cluster daemon stack
down Stop the stack without removing volumes
reset Stop the stack, remove volumes, and delete generated env files
bootstrap Re-run the API bootstrap and rewrite cluster env files
logs Tail compose logs
ps Show compose service status
Environment overrides:
VOLT_DEV_PUBLIC_API_URL Default: http://localhost:8000
VOLT_DEV_PUBLIC_WEB_URL Default: http://localhost:5173
VOLT_DEV_PUBLIC_MINIO_URL Default: ${PUBLIC_MINIO_URL}
VOLT_DEV_INTERNAL_API_URL Default: http://volt-server:8000
VOLT_DEV_MINIO_PORT Default: ${MINIO_HOST_PORT}
VOLT_DEV_MINIO_CONSOLE_PORT Default: ${MINIO_CONSOLE_HOST_PORT}
VOLT_DEV_WORKSPACE_DIR Optional directory containing Volt/ and ClusterDaemon/. Default: <current-dir>
VOLT_DEV_CLUSTER_DAEMON_PATH Optional path to a local ClusterDaemon checkout
VOLT_DEV_CLUSTER_DAEMON_IMAGE Default: ${CLUSTER_DAEMON_IMAGE}
VOLT_DEV_STACK_PROJECT_NAME
VOLT_DEV_USER_EMAIL
VOLT_DEV_USER_PASSWORD
VOLT_DEV_TEAM_NAME
VOLT_DEV_CLUSTER_NAME
VOLT_DEV_STORAGE_CLUSTER_NAME
VOLT_DEV_COMPUTE_CLUSTER_NAME
EOF
}
main() {
ensure_tools
local command="${1:-}"
case "${command}" in
up)
cmd_up
;;
down)
cmd_down
;;
reset)
cmd_reset
;;
bootstrap)
cmd_bootstrap
;;
logs)
cmd_logs "$@"
;;
ps)
cmd_ps
;;
*)
usage
[[ -n "${command}" ]] && exit 1
;;
esac
}
main "$@"