-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·404 lines (353 loc) · 9.33 KB
/
Copy pathdev.sh
File metadata and controls
executable file
·404 lines (353 loc) · 9.33 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
#!/bin/bash
# Development environment setup and local metrics stack control for FlashRL
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
COMPOSE_FILE="$ROOT_DIR/metric/docker-compose.yml"
GRAFANA_URL="http://localhost:3000"
PROMETHEUS_URL="http://localhost:9090"
PUSHGATEWAY_URL="http://localhost:9091"
DEFAULT_VLLM_VENV="$HOME/.venv-vllm"
DEFAULT_VLLM_METAL_VENV="$HOME/.venv-vllm-metal"
METRICS_READY_TIMEOUT_SECONDS="${FLASHRL_METRICS_READY_TIMEOUT_SECONDS:-60}"
METRICS_READY_INTERVAL_SECONDS="${FLASHRL_METRICS_READY_INTERVAL_SECONDS:-1}"
is_sourced() {
[[ "${BASH_SOURCE[0]}" != "$0" ]]
}
finish() {
local exit_status="$1"
if is_sourced; then
return "$exit_status"
fi
exit "$exit_status"
}
activate_env() {
export PYTHONPYCACHEPREFIX="$ROOT_DIR/.cache/pycache"
export PYTHONPATH="$ROOT_DIR:${PYTHONPATH:-}"
auto_export_vllm_python
echo "✓ FlashRL dev environment activated"
echo " - Bytecode cache: .cache/pycache/"
echo " - PYTHONPATH set"
if [[ -n "${FLASHRL_VLLM_PYTHON:-}" ]]; then
echo " - FLASHRL_VLLM_PYTHON: $FLASHRL_VLLM_PYTHON"
fi
}
metrics_usage() {
echo "Usage: ./dev.sh metrics <up|down|reset|status>"
}
vllm_usage() {
echo "Usage: ./dev.sh vllm <setup|status>"
}
require_command() {
local command_name="$1"
if command -v "$command_name" >/dev/null 2>&1; then
return 0
fi
echo "Error: required command '$command_name' was not found in PATH." >&2
return 1
}
is_macos_arm64() {
[[ "$(uname -s)" == "Darwin" && "$(uname -m)" == "arm64" ]]
}
default_vllm_python() {
if is_macos_arm64 && [[ -x "$DEFAULT_VLLM_METAL_VENV/bin/python" ]]; then
printf '%s\n' "$DEFAULT_VLLM_METAL_VENV/bin/python"
return 0
fi
if [[ -x "$DEFAULT_VLLM_VENV/bin/python" ]]; then
printf '%s\n' "$DEFAULT_VLLM_VENV/bin/python"
return 0
fi
return 1
}
auto_export_vllm_python() {
if [[ -n "${FLASHRL_VLLM_PYTHON:-}" ]]; then
return 0
fi
local runtime_python=""
if runtime_python="$(default_vllm_python)"; then
export FLASHRL_VLLM_PYTHON="$runtime_python"
fi
}
print_vllm_export_hint() {
local python_path="$1"
echo "export FLASHRL_VLLM_PYTHON=\"$python_path\""
}
python_has_module() {
local python_path="$1"
local module_name="$2"
"$python_path" -c "import $module_name" >/dev/null 2>&1
}
linux_vllm_python_command() {
local candidate=""
for candidate in python3.13 python3.12 python3; do
if ! command -v "$candidate" >/dev/null 2>&1; then
continue
fi
if "$candidate" -c 'import sys; raise SystemExit(0 if (3, 12) <= sys.version_info[:2] < (3, 14) else 1)'; then
printf '%s\n' "$candidate"
return 0
fi
done
return 1
}
vllm_status() {
local runtime_python=""
local source_label="prepared runtime"
if [[ -n "${FLASHRL_VLLM_PYTHON:-}" ]]; then
runtime_python="$FLASHRL_VLLM_PYTHON"
source_label="FLASHRL_VLLM_PYTHON"
elif runtime_python="$(default_vllm_python)"; then
source_label="default prepared runtime"
else
echo "Prepared vLLM runtime: not found"
if is_macos_arm64; then
echo "Run: ./dev.sh vllm setup"
else
echo "Run: ./dev.sh vllm setup"
echo "Or install FlashRL with the optional vllm extra in your current environment."
fi
return 0
fi
echo "Prepared vLLM runtime: $runtime_python"
echo "Source: $source_label"
print_vllm_export_hint "$runtime_python"
}
vllm_setup_macos() {
require_command curl || return 1
require_command uv || return 1
if [[ -x "$DEFAULT_VLLM_METAL_VENV/bin/python" ]] && python_has_module "$DEFAULT_VLLM_METAL_VENV/bin/python" vllm_metal; then
echo "✓ Existing vllm-metal runtime found at $DEFAULT_VLLM_METAL_VENV"
else
echo "Installing vllm-metal into $DEFAULT_VLLM_METAL_VENV ..."
if ! curl -fsSL https://raw.githubusercontent.com/vllm-project/vllm-metal/main/install.sh | bash; then
echo "Installer did not complete cleanly; falling back to source install for vllm-metal ..."
fi
fi
if [[ ! -x "$DEFAULT_VLLM_METAL_VENV/bin/python" ]]; then
echo "Error: vllm-metal setup did not create $DEFAULT_VLLM_METAL_VENV/bin/python" >&2
return 1
fi
if ! python_has_module "$DEFAULT_VLLM_METAL_VENV/bin/python" vllm_metal; then
require_command git || return 1
echo "Installing vllm-metal from source fallback ..."
uv pip install --python "$DEFAULT_VLLM_METAL_VENV/bin/python" \
git+https://github.com/vllm-project/vllm-metal.git || return 1
fi
if ! python_has_module "$DEFAULT_VLLM_METAL_VENV/bin/python" vllm_metal; then
echo "Error: vllm-metal setup completed but the runtime still cannot import vllm_metal." >&2
return 1
fi
echo
echo "Prepared vLLM runtime:"
print_vllm_export_hint "$DEFAULT_VLLM_METAL_VENV/bin/python"
}
vllm_setup_linux() {
require_command uv || return 1
local python_cmd=""
if ! python_cmd="$(linux_vllm_python_command)"; then
echo "Error: Linux vLLM setup requires python3.12 or python3.13 in PATH." >&2
return 1
fi
echo "Preparing vLLM runtime with $python_cmd ..."
uv venv --python "$python_cmd" "$DEFAULT_VLLM_VENV" || return 1
uv pip install --python "$DEFAULT_VLLM_VENV/bin/python" "vllm>=0.16.0" || return 1
echo
echo "Prepared vLLM runtime:"
print_vllm_export_hint "$DEFAULT_VLLM_VENV/bin/python"
}
vllm_setup() {
if is_macos_arm64; then
vllm_setup_macos
return $?
fi
if [[ "$(uname -s)" == "Linux" ]]; then
vllm_setup_linux
return $?
fi
echo "Error: unsupported platform for automatic vLLM setup: $(uname -s) $(uname -m)" >&2
return 1
}
compose() {
docker compose -f "$COMPOSE_FILE" "$@"
}
service_health_url() {
case "$1" in
grafana)
echo "$GRAFANA_URL/api/health"
;;
prometheus)
echo "$PROMETHEUS_URL/-/ready"
;;
pushgateway)
echo "$PUSHGATEWAY_URL/-/ready"
;;
*)
return 1
;;
esac
}
service_label() {
case "$1" in
grafana)
echo "Grafana"
;;
prometheus)
echo "Prometheus"
;;
pushgateway)
echo "Pushgateway"
;;
*)
return 1
;;
esac
}
service_ready() {
local service="$1"
local url
url="$(service_health_url "$service")" || return 1
curl --fail --silent --show-error --output /dev/null "$url"
}
unready_services() {
local services=""
local service
for service in grafana prometheus pushgateway; do
if ! service_ready "$service"; then
if [[ -n "$services" ]]; then
services="$services $service"
else
services="$service"
fi
fi
done
printf '%s' "$services"
}
print_readiness_summary() {
local service
local readiness
local url
for service in grafana prometheus pushgateway; do
url="$(service_health_url "$service")" || continue
if service_ready "$service"; then
readiness="ready"
else
readiness="not_ready"
fi
echo " - $(service_label "$service"): $readiness ($url)"
done
}
wait_for_metrics_stack() {
local deadline=$((SECONDS + METRICS_READY_TIMEOUT_SECONDS))
local unready=""
while true; do
unready="$(unready_services)"
if [[ -z "$unready" ]]; then
return 0
fi
if (( SECONDS >= deadline )); then
echo "Error: metrics stack did not become ready within ${METRICS_READY_TIMEOUT_SECONDS}s." >&2
echo "Still not ready: $unready" >&2
return 1
fi
sleep "$METRICS_READY_INTERVAL_SECONDS"
done
}
metrics_up() {
require_command docker || return 1
require_command curl || return 1
compose up -d || return 1
echo "Waiting for metrics endpoints to become ready..."
if ! wait_for_metrics_stack; then
echo "Current container status:" >&2
compose ps >&2 || true
return 1
fi
echo "✓ FlashRL metrics stack is running"
echo " - Grafana: $GRAFANA_URL"
echo " - Troubleshooting:"
echo " Prometheus: $PROMETHEUS_URL"
echo " Pushgateway: $PUSHGATEWAY_URL"
}
metrics_down() {
require_command docker || return 1
compose down || return 1
echo "✓ FlashRL metrics stack stopped"
}
metrics_reset() {
require_command docker || return 1
compose down -v --remove-orphans || return 1
echo "✓ FlashRL metrics stack reset"
}
metrics_status() {
require_command docker || return 1
require_command curl || return 1
compose ps || return 1
echo
echo "Endpoint readiness:"
print_readiness_summary
}
main() {
if [[ $# -eq 0 ]]; then
activate_env
return 0
fi
case "$1" in
metrics)
local action="${2:-}"
case "$action" in
up)
metrics_up
;;
down)
metrics_down
;;
reset)
metrics_reset
;;
status)
metrics_status
;;
*)
metrics_usage
return 1
;;
esac
;;
vllm)
local action="${2:-}"
case "$action" in
setup)
vllm_setup
;;
status)
vllm_status
;;
*)
vllm_usage
return 1
;;
esac
;;
help|-h|--help)
echo "Usage:"
echo " source ./dev.sh"
echo " ./dev.sh metrics <up|down|reset|status>"
echo " ./dev.sh vllm <setup|status>"
;;
*)
echo "Unknown command: $1" >&2
echo
echo "Usage:"
echo " source ./dev.sh"
echo " ./dev.sh metrics <up|down|reset|status>"
echo " ./dev.sh vllm <setup|status>"
return 1
;;
esac
}
if is_sourced; then
main
finish $?
else
main "$@"
finish $?
fi