-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_demo.sh
More file actions
executable file
·272 lines (224 loc) · 7.92 KB
/
Copy pathstart_demo.sh
File metadata and controls
executable file
·272 lines (224 loc) · 7.92 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
#!/usr/bin/env bash
set -euo pipefail
WSL_RUN_HINT='wsl -d Ubuntu --cd /home/hp/ad-edge-demo -- bash -lc "./start_demo.sh"'
usage() {
cat <<EOF
Usage: ./start_demo.sh [--help]
Start the AD dMRI edge demo from the project root in WSL/Linux.
Required environment:
- Run inside Linux/WSL, not Windows PowerShell/CMD/Git Bash.
- Backend uses .venv/bin/python from this project.
- Frontend uses Linux/WSL node and npm.
- Safe Profile defaults are exported by this script.
- Optional .env values override those defaults.
If you are on Windows, run:
${WSL_RUN_HINT}
EOF
}
die() {
printf 'ERROR: %s\n' "$*" >&2
exit 1
}
warn() {
printf 'WARN: %s\n' "$*" >&2
}
info() {
printf '[start_demo] %s\n' "$*"
}
prepend_nvm_node_if_needed() {
local node_dir
if command -v node >/dev/null 2>&1 && command -v npm >/dev/null 2>&1; then
return 0
fi
if [[ -d "$HOME/.nvm/versions/node" ]]; then
while IFS= read -r node_dir; do
if [[ -x "$node_dir/bin/node" && -x "$node_dir/bin/npm" ]]; then
export PATH="$node_dir/bin:$PATH"
info "Using Linux/WSL Node from nvm: $node_dir/bin"
return 0
fi
done < <(find "$HOME/.nvm/versions/node" -mindepth 1 -maxdepth 1 -type d | sort -V -r)
fi
return 0
}
if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
usage
exit 0
fi
if [[ "$(uname -s 2>/dev/null || true)" != "Linux" ]]; then
cat >&2 <<EOF
This script must be run inside Linux/WSL.
On Windows, use:
${WSL_RUN_HINT}
EOF
exit 1
fi
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
PROJECT_ROOT="$PWD"
if [[ ! -f "backend/main.py" || ! -f "frontend/package.json" ]]; then
die "start_demo.sh must be located in and run from the project root."
fi
export APP_ENV="${APP_ENV:-development}"
export APP_HOST="${APP_HOST:-0.0.0.0}"
export BACKEND_PORT="${BACKEND_PORT:-8000}"
export FRONTEND_PORT="${FRONTEND_PORT:-5173}"
export CASE_ROOT="${CASE_ROOT:-./data/cases}"
export OUTPUT_OVERWRITE="${OUTPUT_OVERWRITE:-true}"
export DEPLOY_TARGET="${DEPLOY_TARGET:-pc}"
export BROWSE_ROOT_PC="${BROWSE_ROOT_PC:-./data/Data}"
export BROWSE_ROOT_ORANGEPI="${BROWSE_ROOT_ORANGEPI:-/media/${USER:-orangepi}}"
export EDGE_DATA_HOME_PC="${EDGE_DATA_HOME_PC:-./data/runtime}"
export EDGE_DATA_HOME_ORANGEPI="${EDGE_DATA_HOME_ORANGEPI:-/opt/ad-edge/runtime}"
export RENDER_4D_MODE_PC="${RENDER_4D_MODE_PC:-single_volume}"
export RENDER_4D_MODE_ORANGEPI="${RENDER_4D_MODE_ORANGEPI:-single_volume}"
export EDGE_PROBE="${EDGE_PROBE:-0}"
export EDGE_PROBE_DIR="${EDGE_PROBE_DIR:-./debug/probe}"
# User-provided .env values override the safe defaults above.
[ -f .env ] && source .env || true
export APP_HOST BACKEND_PORT FRONTEND_PORT CASE_ROOT OUTPUT_OVERWRITE DEPLOY_TARGET
export BROWSE_ROOT BROWSE_ROOT_PC BROWSE_ROOT_ORANGEPI EDGE_DATA_HOME EDGE_DATA_HOME_PC EDGE_DATA_HOME_ORANGEPI
export RENDER_4D_MODE RENDER_4D_MODE_PC RENDER_4D_MODE_ORANGEPI EDGE_PROBE EDGE_PROBE_DIR
case "$BACKEND_PORT" in
''|*[!0-9]*) die "BACKEND_PORT must be a number, got: $BACKEND_PORT" ;;
esac
case "$FRONTEND_PORT" in
''|*[!0-9]*) die "FRONTEND_PORT must be a number, got: $FRONTEND_PORT" ;;
esac
resolve_path() {
local input="$1"
if [[ "$input" = /* ]]; then
printf '%s\n' "$input"
else
printf '%s\n' "$PROJECT_ROOT/${input#./}"
fi
}
CASE_ROOT_ABS="$(resolve_path "$CASE_ROOT")"
[[ -d "$CASE_ROOT_ABS" ]] || die "CASE_ROOT does not exist: $CASE_ROOT_ABS"
mapfile -t CASE_DIRS < <(find "$CASE_ROOT_ABS" -mindepth 1 -maxdepth 1 -type d | sort)
if (( ${#CASE_DIRS[@]} == 0 )); then
die "No case package directories found under CASE_ROOT: $CASE_ROOT_ABS"
fi
[[ -x ".venv/bin/python" ]] || die "Missing .venv/bin/python. Create/install the backend virtual environment inside WSL first."
PYTHON_PATH="$(.venv/bin/python -c 'import sys; print(sys.executable)')"
case "$PYTHON_PATH" in
/mnt/*|*[Cc]:\\*) die "Backend Python resolved to a Windows path: $PYTHON_PATH" ;;
esac
if ! .venv/bin/python -c 'import uvicorn' >/dev/null 2>&1; then
die "Backend dependency uvicorn is not available in .venv. Install backend requirements inside WSL."
fi
[[ -d "frontend/node_modules" ]] || die "Missing frontend/node_modules. Install frontend dependencies inside WSL first."
prepend_nvm_node_if_needed
command -v node >/dev/null 2>&1 || die "Linux/WSL node was not found in PATH. Install Node.js inside WSL; do not use Windows Node."
command -v npm >/dev/null 2>&1 || die "Linux/WSL npm was not found in PATH. Install npm inside WSL; do not use Windows npm."
NODE_PATH="$(command -v node)"
NPM_PATH="$(command -v npm)"
case "$NODE_PATH" in
/mnt/*|*[Cc]:\\*) die "node resolves to a Windows path: $NODE_PATH. Install/use Linux Node inside WSL." ;;
esac
case "$NPM_PATH" in
/mnt/*|*[Cc]:\\*) die "npm resolves to a Windows path: $NPM_PATH. Install/use Linux npm inside WSL." ;;
esac
BACKEND_PID=""
FRONTEND_PID=""
LOG_DIR="$PROJECT_ROOT/debug/logs"
mkdir -p "$LOG_DIR"
BACKEND_LOG="$LOG_DIR/backend-${BACKEND_PORT}.log"
FRONTEND_LOG="$LOG_DIR/frontend-${FRONTEND_PORT}.log"
terminate_tree() {
local pid="$1"
local child
if [[ -z "$pid" ]] || ! kill -0 "$pid" >/dev/null 2>&1; then
return 0
fi
while IFS= read -r child; do
[[ -n "$child" ]] && terminate_tree "$child"
done < <(pgrep -P "$pid" 2>/dev/null || true)
kill "$pid" >/dev/null 2>&1 || true
}
cleanup() {
local code=$?
trap - INT TERM EXIT
info "Stopping demo services..."
terminate_tree "$FRONTEND_PID"
terminate_tree "$BACKEND_PID"
wait "$FRONTEND_PID" >/dev/null 2>&1 || true
wait "$BACKEND_PID" >/dev/null 2>&1 || true
exit "$code"
}
trap cleanup INT TERM EXIT
info "Project root: $PROJECT_ROOT"
info "CASE_ROOT: $CASE_ROOT_ABS"
info "Detected case packages: ${#CASE_DIRS[@]}"
info "OUTPUT_OVERWRITE: $OUTPUT_OVERWRITE"
info "DEPLOY_TARGET: $DEPLOY_TARGET"
info "BROWSE_ROOT_PC: $BROWSE_ROOT_PC"
info "EDGE_DATA_HOME_PC: $EDGE_DATA_HOME_PC"
info "RENDER_4D_MODE_PC: $RENDER_4D_MODE_PC"
info "Starting FastAPI backend with .venv/bin/python..."
.venv/bin/python -m uvicorn backend.main:app \
--host "$APP_HOST" \
--port "$BACKEND_PORT" \
>"$BACKEND_LOG" 2>&1 &
BACKEND_PID="$!"
if [[ "$APP_HOST" == "0.0.0.0" ]]; then
health_host="127.0.0.1"
else
health_host="$APP_HOST"
fi
health_url="http://${health_host}:${BACKEND_PORT}/api/health"
health_ok=0
for _ in {1..40}; do
if ! kill -0 "$BACKEND_PID" >/dev/null 2>&1; then
warn "Backend exited early. Log:"
sed -n '1,160p' "$BACKEND_LOG" >&2 || true
die "FastAPI backend failed to start."
fi
if .venv/bin/python - "$health_url" <<'PY' >/dev/null 2>&1
import json
import sys
import urllib.request
url = sys.argv[1]
opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
with opener.open(url, timeout=1.0) as response:
payload = json.load(response)
if payload.get("status") != "ok":
raise SystemExit(1)
PY
then
health_ok=1
break
fi
sleep 0.25
done
if [[ "$health_ok" != "1" ]]; then
warn "Backend health check failed at $health_url. Log:"
sed -n '1,160p' "$BACKEND_LOG" >&2 || true
die "FastAPI backend did not become healthy in time."
fi
info "Backend health check passed: $health_url"
info "Starting Vite frontend with Linux/WSL npm..."
(
cd frontend
npm run dev -- --host "$APP_HOST" --port "$FRONTEND_PORT"
) >"$FRONTEND_LOG" 2>&1 &
FRONTEND_PID="$!"
sleep 1
if ! kill -0 "$FRONTEND_PID" >/dev/null 2>&1; then
warn "Frontend exited early. Log:"
sed -n '1,160p' "$FRONTEND_LOG" >&2 || true
die "Vite frontend failed to start."
fi
cat <<EOF
AD dMRI edge demo is running.
Backend: http://${APP_HOST}:${BACKEND_PORT}
Frontend: http://${APP_HOST}:${FRONTEND_PORT}
Backend log: $BACKEND_LOG
Frontend log: $FRONTEND_LOG
This edge demo uses the selected runtime Profile for branding, browse roots,
runtime upload storage, and rendering defaults. RI metrics are computed from
prepared data/cases packages. It does not run full DisC-Diff, FreeSurfer, or
whole-brain registration on the edge device.
Press Ctrl+C to stop both services.
EOF
wait "$BACKEND_PID" "$FRONTEND_PID"