-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdev_run.sh
More file actions
317 lines (282 loc) · 10.5 KB
/
Copy pathdev_run.sh
File metadata and controls
317 lines (282 loc) · 10.5 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
#!/usr/bin/env bash
# =============================================================================
# Catalyst — Local Development Runner
#
# Starts PostgreSQL (+ Redis) via Podman/Docker Compose, then runs the
# backend, frontend, and agent concurrently with color-prefixed logs.
#
# Usage:
# ./dev_run.sh # full stack (infra + BE + FE + agent)
# ./dev_run.sh --no-agent # skip the Rust agent
# ./dev_run.sh --no-migrate # skip prisma generate/db push
# ./dev_run.sh --seed # also run db:seed after schema push
# ./dev_run.sh --infra-only # start postgres/redis and exit
# ./dev_run.sh --help
#
# Prerequisites:
# - pnpm, Node 20+
# - cargo / rustc (if running the agent)
# - docker or podman (+ compose plugin or podman-compose)
# - catalyst-backend/.env and catalyst-frontend/.env
# - catalyst-agent/config.toml (generated by db:seed)
# =============================================================================
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$ROOT_DIR"
# --- colors -------------------------------------------------------------------
if [[ -t 1 ]]; then
C_RESET='\033[0m'
C_BOLD='\033[1m'
C_DIM='\033[2m'
C_RED='\033[31m'
C_GREEN='\033[32m'
C_YELLOW='\033[33m'
C_BLUE='\033[34m'
C_MAGENTA='\033[35m'
C_CYAN='\033[36m'
C_GRAY='\033[90m'
else
C_RESET='' C_BOLD='' C_DIM='' C_RED='' C_GREEN='' C_YELLOW=''
C_BLUE='' C_MAGENTA='' C_CYAN='' C_GRAY=''
fi
log() { printf '%b\n' "${C_BOLD}${C_CYAN}[dev]${C_RESET} $*"; }
ok() { printf '%b\n' "${C_BOLD}${C_GREEN}[dev]${C_RESET} $*"; }
warn() { printf '%b\n' "${C_BOLD}${C_YELLOW}[dev]${C_RESET} $*"; }
err() { printf '%b\n' "${C_BOLD}${C_RED}[dev]${C_RESET} $*" >&2; }
die() { err "$*"; exit 1; }
# --- defaults / flags ---------------------------------------------------------
RUN_AGENT=1
RUN_MIGRATE=1
RUN_SEED=0
INFRA_ONLY=0
SKIP_INFRA=0
usage() {
sed -n '2,22p' "$0" | sed 's/^# \?//'
exit 0
}
while [[ $# -gt 0 ]]; do
case "$1" in
--no-agent) RUN_AGENT=0; shift ;;
--no-migrate) RUN_MIGRATE=0; shift ;;
--seed) RUN_SEED=1; shift ;;
--infra-only) INFRA_ONLY=1; shift ;;
--skip-infra) SKIP_INFRA=1; shift ;;
-h|--help) usage ;;
*) die "Unknown flag: $1 (try --help)" ;;
esac
done
# --- process tracking ---------------------------------------------------------
PIDS=()
CLEANED=0
kill_tree() {
local pid="$1"
local sig="${2:-TERM}"
local child
# Recurse into children first so tsx/vite/cargo grandchildren die too.
while read -r child; do
[[ -n "$child" ]] && kill_tree "$child" "$sig"
done < <(pgrep -P "$pid" 2>/dev/null || true)
kill -"$sig" "$pid" 2>/dev/null || true
# Also try the process-group form (setsid sessions).
kill -"$sig" "-$pid" 2>/dev/null || true
}
cleanup() {
if [[ "$CLEANED" -eq 1 ]]; then
return
fi
CLEANED=1
echo
log "Shutting down local services..."
for pid in "${PIDS[@]:-}"; do
if kill -0 "$pid" 2>/dev/null; then
kill_tree "$pid" TERM
fi
done
sleep 0.5
for pid in "${PIDS[@]:-}"; do
if kill -0 "$pid" 2>/dev/null; then
kill_tree "$pid" KILL
fi
done
# Leave postgres/redis running — they're shared infra for other terminals.
ok "App processes stopped (postgres/redis left running)."
ok "Stop infra with: ${C_DIM}cd catalyst-docker && <docker|podman> compose stop postgres redis${C_RESET}"
}
trap cleanup EXIT INT TERM
# Prefix each line of a child process with a colored tag.
prefix_stream() {
local tag="$1"
local color="$2"
local line
while IFS= read -r line || [[ -n "$line" ]]; do
printf '%b\n' "${color}${C_BOLD}[${tag}]${C_RESET} ${line}"
done
}
# --- detect container runtime / compose --------------------------------------
detect_compose() {
if command -v docker >/dev/null 2>&1 && docker compose version >/dev/null 2>&1; then
echo "docker compose"
return 0
fi
if command -v podman >/dev/null 2>&1; then
if podman compose version >/dev/null 2>&1; then
echo "podman compose"
return 0
fi
if command -v podman-compose >/dev/null 2>&1; then
echo "podman-compose"
return 0
fi
fi
if command -v docker-compose >/dev/null 2>&1; then
echo "docker-compose"
return 0
fi
return 1
}
# --- preflight ----------------------------------------------------------------
require_cmd() {
command -v "$1" >/dev/null 2>&1 || die "Required command not found: $1"
}
require_cmd pnpm
if [[ ! -f catalyst-backend/.env ]]; then
if [[ -f catalyst-backend/.env.example ]]; then
cp catalyst-backend/.env.example catalyst-backend/.env
warn "Created catalyst-backend/.env from .env.example — edit DATABASE_URL / secrets."
else
die "Missing catalyst-backend/.env"
fi
fi
if [[ ! -f catalyst-frontend/.env ]]; then
if [[ -f catalyst-frontend/.env.example ]]; then
cp catalyst-frontend/.env.example catalyst-frontend/.env
warn "Created catalyst-frontend/.env from .env.example"
fi
fi
if [[ ! -f catalyst-docker/.env ]]; then
if [[ -f catalyst-docker/.env.example ]]; then
# Prefer a local-dev friendly password that matches backend .env defaults.
cp catalyst-docker/.env.example catalyst-docker/.env
if grep -q 'POSTGRES_PASSWORD=CHANGE_ME' catalyst-docker/.env 2>/dev/null; then
# Align with catalyst-backend/.env DATABASE_URL default used in this repo.
sed -i 's/^POSTGRES_PASSWORD=.*/POSTGRES_PASSWORD=catalyst_dev/' catalyst-docker/.env
fi
if grep -q 'BETTER_AUTH_SECRET=CHANGE_ME' catalyst-docker/.env 2>/dev/null; then
sed -i "s|^BETTER_AUTH_SECRET=.*|BETTER_AUTH_SECRET=$(openssl rand -base64 32 2>/dev/null || echo 'dev-secret-change-me')|" catalyst-docker/.env
fi
warn "Created catalyst-docker/.env (POSTGRES_PASSWORD=catalyst_dev for local dev)."
else
die "Missing catalyst-docker/.env"
fi
fi
# --- start infra --------------------------------------------------------------
start_infra() {
local COMPOSE
if ! COMPOSE="$(detect_compose)"; then
die "Neither docker compose nor podman-compose found. Install Docker or Podman."
fi
export COMPOSE
log "Using compose: ${C_BOLD}${COMPOSE}${C_RESET}"
log "Starting PostgreSQL + Redis..."
(
cd catalyst-docker
# --no-recreate keeps an already-healthy container; fall back to start/up.
# shellcheck disable=SC2086
if ! $COMPOSE up -d --no-recreate postgres redis 2>/dev/null; then
$COMPOSE up -d postgres redis
fi
)
# Wait for postgres health.
log "Waiting for postgres to become ready..."
local tries=0
local max=30
until podman exec catalyst-postgres pg_isready -U catalyst -d catalyst_db >/dev/null 2>&1 \
|| docker exec catalyst-postgres pg_isready -U catalyst -d catalyst_db >/dev/null 2>&1; do
tries=$((tries + 1))
if [[ "$tries" -ge "$max" ]]; then
die "Postgres did not become ready within ${max}s. Check: ${COMPOSE} -f catalyst-docker/docker-compose.yml logs postgres"
fi
sleep 1
done
ok "Postgres is healthy."
}
if [[ "$SKIP_INFRA" -eq 0 ]]; then
start_infra
else
warn "Skipping infra (--skip-infra)."
fi
if [[ "$INFRA_ONLY" -eq 1 ]]; then
ok "Infra only — done."
# Don't kill anything on exit for infra-only.
trap - EXIT INT TERM
exit 0
fi
# --- schema sync --------------------------------------------------------------
if [[ "$RUN_MIGRATE" -eq 1 ]]; then
log "Syncing Prisma client + schema (db:generate / db:push)..."
(
cd catalyst-backend
pnpm run db:generate
pnpm run db:push
)
ok "Database schema is in sync."
fi
if [[ "$RUN_SEED" -eq 1 ]]; then
log "Seeding database..."
pnpm --filter catalyst-backend run db:seed
ok "Seed complete (agent config.toml regenerated if seed writes it)."
fi
# --- agent preflight ----------------------------------------------------------
if [[ "$RUN_AGENT" -eq 1 ]]; then
require_cmd cargo
if [[ ! -f catalyst-agent/config.toml ]]; then
warn "catalyst-agent/config.toml missing."
warn "Run with --seed once, or copy config.toml.example and fill node_id/api_key."
warn "Falling back to --no-agent for this run."
RUN_AGENT=0
fi
if [[ ! -S /run/containerd/containerd.sock ]]; then
warn "containerd socket not found at /run/containerd/containerd.sock"
warn "Agent container ops will fail until containerd is running."
fi
fi
# --- launch services ----------------------------------------------------------
log "Starting application processes..."
echo
printf '%b\n' "${C_DIM}────────────────────────────────────────────────────────────${C_RESET}"
printf '%b\n' " ${C_GREEN}Frontend${C_RESET} http://localhost:5173"
printf '%b\n' " ${C_BLUE}Backend${C_RESET} http://localhost:3000 (API /docs)"
printf '%b\n' " ${C_MAGENTA}Agent${C_RESET} $( [[ "$RUN_AGENT" -eq 1 ]] && echo 'enabled (sudo cargo run)' || echo 'skipped' )"
printf '%b\n' " ${C_YELLOW}Postgres${C_RESET} localhost:5432 (container: catalyst-postgres)"
printf '%b\n' " ${C_YELLOW}Redis${C_RESET} localhost:6379 (container: catalyst-redis)"
printf '%b\n' "${C_DIM}────────────────────────────────────────────────────────────${C_RESET}"
printf '%b\n' " ${C_DIM}Ctrl+C stops app processes; postgres/redis keep running.${C_RESET}"
echo
# Run a command in its own session so cleanup can kill the whole tree.
# Process substitution keeps $! pointing at the real app PID (not a pipe).
# Use a non-login shell so nvm/fnm/asdf PATH from the parent is preserved.
run_tagged() {
local tag="$1"
local color="$2"
shift 2
# Capture absolute paths so child sessions don't depend on interactive shell init.
local cmd_path
cmd_path="$(command -v "$1" 2>/dev/null || true)"
if [[ -n "$cmd_path" && "$1" != /* ]]; then
set -- "$cmd_path" "${@:2}"
fi
setsid env PATH="$PATH" HOME="${HOME:-}" \
CARGO_HOME="${CARGO_HOME:-}" RUSTUP_HOME="${RUSTUP_HOME:-}" \
CATALYST_ALLOW_INSECURE_WS="${CATALYST_ALLOW_INSECURE_WS:-}" \
bash -c 'cd "$1" && shift && exec "$@"' _ "$@" \
> >(prefix_stream "$tag" "$color") 2>&1 &
PIDS+=($!)
}
# Backend / Frontend — first arg after tag/color is the working directory.
run_tagged() { :; } # placeholder replaced below
ok "All requested services launched (pids: ${PIDS[*]})."
log "Tailing logs — Ctrl+C to stop."
# Wait until any child exits, then tear everything down.
wait -n "${PIDS[@]}" 2>/dev/null || wait "${PIDS[@]}" || true
warn "A service exited — shutting down the rest."
# cleanup via trap