Repository navigation
Expand file tree
/
Copy pathopenprocessor
More file actions
executable file
·469 lines (422 loc) · 14.9 KB
/
Copy pathopenprocessor
File metadata and controls
executable file
·469 lines (422 loc) · 14.9 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
#!/bin/bash
# =============================================================================
# OpenProcessor Management CLI
# =============================================================================
# Unified command-line interface for managing OpenProcessor deployments.
# Ships at the root of every install dir (installer plan section 5.5); a
# checkout keeps a 3-line exec shim at scripts/openprocessor.sh so existing
# muscle memory / docs keep working.
#
# Usage: ./openprocessor <command> [options]
# =============================================================================
set -eo pipefail
# =============================================================================
# Configuration
# =============================================================================
# Physical path (-P): compose records the resolved working_dir, and the
# installer compares against the same form.
PROJECT_DIR="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT_DIR="${PROJECT_DIR}/scripts"
export PROJECT_DIR
# Source library functions
source "${SCRIPT_DIR}/lib/colors.sh"
source "${SCRIPT_DIR}/lib/gpu.sh"
source "${SCRIPT_DIR}/lib/ports.sh"
source "${SCRIPT_DIR}/lib/bind_dirs.sh"
source "${SCRIPT_DIR}/lib/vlm_catalog.sh"
source "${SCRIPT_DIR}/lib/vlm_switch.sh"
source "${SCRIPT_DIR}/lib/retired_env.sh"
# Installer plan section 5.1: every compose call goes through dc(), which
# always passes -p, --env-file and --project-directory. Section 5.5:
# PROJECT_DIR is the directory holding docker-compose.yml, whether that's a
# checkout or an install dir; in a checkout (src/main.py exists next to the
# compose file) it adds the dev overlay for hot-reload.
DC_FILES=(-f "$PROJECT_DIR/docker-compose.yml")
if [[ -f "$PROJECT_DIR/src/main.py" ]]; then
DC_FILES+=(-f "$PROJECT_DIR/docker-compose.dev.yml")
IS_CHECKOUT=1
else
IS_CHECKOUT=0
fi
if [[ -f "$PROJECT_DIR/docker-compose.cpu.yml" ]]; then
DC_FILES+=(-f "$PROJECT_DIR/docker-compose.cpu.yml")
fi
# The user's own overrides (extra bind mounts, a second source root), last so
# they win. docker-compose.override.yml is not read: the file name is yours.
if [[ -f "$PROJECT_DIR/docker-compose.local.yml" ]]; then
DC_FILES+=(-f "$PROJECT_DIR/docker-compose.local.yml")
fi
_env_value() {
local key="$1" file="${2:-$PROJECT_DIR/.env}"
[[ -f "$file" ]] || return 0
awk -v k="$key" '{ i = index($0, "=") } i > 1 && substr($0, 1, i - 1) == k { v = substr($0, i + 1) } END { print v }' "$file"
}
# B3: the project name comes from this directory's own .env (and must match
# .install/state.json), never from a default. With nothing resolvable the
# CLI refuses: a default name could be another stack's.
_PROJECT_RESOLVED=""
resolve_project() {
[[ -n "$_PROJECT_RESOLVED" ]] && return 0
local from_env from_state=""
from_env="$(_env_value COMPOSE_PROJECT_NAME)"
if [[ "$IS_CHECKOUT" != 1 ]]; then
[[ -f "$PROJECT_DIR/.env" ]] || { log_error "no .env in ${PROJECT_DIR}: not an OpenProcessor install dir"; exit 3; }
[[ -n "$from_env" ]] || { log_error "${PROJECT_DIR}/.env has no COMPOSE_PROJECT_NAME; refusing to guess a compose project"; exit 3; }
# The install's own state.json is the proof this directory (and so
# this project) was created by setup-openprocessor.sh.
local state="$PROJECT_DIR/.install/state.json" from_dir
[[ -f "$state" ]] || { log_error "${PROJECT_DIR} has no .install/state.json: not an install made by setup-openprocessor.sh"; exit 3; }
from_state="$(sed -n -E 's/^ "project": "([^"]*)",?$/\1/p' "$state" | head -n1)"
from_dir="$(sed -n -E 's/^ "install_dir": "([^"]*)",?$/\1/p' "$state" | head -n1)"
if [[ "$from_state" != "$from_env" ]]; then
log_error "project mismatch: .env says '${from_env}', .install/state.json says '${from_state}'"
exit 3
fi
if [[ "$from_dir" != "$PROJECT_DIR" ]]; then
log_error ".install/state.json belongs to ${from_dir:-another dir}, not ${PROJECT_DIR}"
exit 3
fi
fi
if [[ -n "$from_env" ]]; then
if [[ -n "${OP_PROJECT:-}" && "$OP_PROJECT" != "$from_env" ]]; then
log_error "OP_PROJECT=${OP_PROJECT} conflicts with this directory's project '${from_env}'"
exit 3
fi
OP_PROJECT="$from_env"
elif [[ -z "${OP_PROJECT:-}" ]]; then
# Only a checkout gets here: an install dir exited above. docker-compose.yml
# names the project this way too; assert_project_owned still refuses a
# project whose containers came from another directory.
log_warn "no COMPOSE_PROJECT_NAME in ${PROJECT_DIR}/.env: using the default project 'openprocessor' (set COMPOSE_PROJECT_NAME or OP_PROJECT to choose another)"
OP_PROJECT="openprocessor"
fi
if [[ ! "$OP_PROJECT" =~ ^[a-z0-9][a-z0-9_-]*$ ]]; then
log_error "invalid compose project name '${OP_PROJECT}'"
exit 3
fi
_PROJECT_RESOLVED=1
}
_dc_is_readonly() {
while (( $# > 0 )); do
case "$1" in
--profile) shift 2 ;;
-*) shift ;;
config|ps|version|images|ls|logs|port|top) return 0 ;;
*) return 1 ;;
esac
done
return 1
}
# assert_project_owned PROJECT DIR -- every container labelled with PROJECT
# was created from DIR. Fails closed when Docker cannot be queried.
declare -A _OWNED_OK=()
assert_project_owned() {
local project="$1" dir="$2" out wd
[[ -n "${_OWNED_OK[$project]:-}" ]] && return 0
if ! out="$(docker ps -a --filter "label=com.docker.compose.project=${project}" \
--format '{{.Names}}|{{.Label "com.docker.compose.project.working_dir"}}' 2>&1)"; then
log_error "cannot query Docker (${out}); refusing to change project '${project}'"
exit 5
fi
[[ -z "$out" ]] && { _OWNED_OK[$project]=1; return 0; }
while IFS= read -r wd; do
wd="${wd#*|}"
if [[ -z "$wd" ]]; then
log_error "a container of project '${project}' has no working_dir label; cannot prove ownership, refusing"
exit 3
fi
if [[ "$wd" != "$dir" ]]; then
log_error "compose project '${project}' belongs to ${wd}, not ${dir}; refusing"
exit 3
fi
done <<< "$out"
_OWNED_OK[$project]=1
}
# m12: a non-loopback bind must be the one recorded (consented) in .env.
assert_bind_consented() {
local recorded shell_value="${OP_BIND_ADDRESS:-}"
recorded="$(_env_value OP_BIND_ADDRESS)"
[[ -z "$shell_value" || "$shell_value" == "$recorded" || "$shell_value" == 127.* ]] && return 0
log_error "OP_BIND_ADDRESS=${shell_value} in your shell differs from this install's .env (${recorded:-127.0.0.1});"
log_error "a public bind needs consent: re-run ./setup-openprocessor.sh --bind ${shell_value}"
exit 9
}
dc() {
resolve_project
local -a cmd=(docker compose -p "$OP_PROJECT")
[[ -f "$PROJECT_DIR/.env" ]] && cmd+=(--env-file "$PROJECT_DIR/.env")
cmd+=(--project-directory "$PROJECT_DIR" "${DC_FILES[@]}" "$@")
if ! _dc_is_readonly "$@"; then
assert_project_owned "$OP_PROJECT" "$PROJECT_DIR"
assert_bind_consented
fi
if [[ "$IS_CHECKOUT" == 1 ]]; then
COMPOSE_PROJECT_NAME="$OP_PROJECT" "${cmd[@]}"
else
env -u COMPOSE_PROFILES -u COMPOSE_FILE -u COMPOSE_ENV_FILES \
COMPOSE_PROJECT_NAME="$OP_PROJECT" "${cmd[@]}"
fi
}
require_checkout() {
if [[ "$IS_CHECKOUT" != "1" ]]; then
log_error "'$1' requires a source checkout"
return 1
fi
}
# resolve every port from THIS deployment's .env (falling back to the
# stock defaults) instead of hardcoding localhost:4603/4600/4607/4605. On a
# shared host running a second isolated stack (remapped ports via .env),
# the hardcoded literals used to report ANOTHER stack's /health as this
# one's.
API_PORT="$(env_port API_PORT 4603)"
TRITON_HTTP_PORT="$(env_port TRITON_HTTP_PORT 4600)"
OPENSEARCH_PORT="$(env_port OPENSEARCH_PORT 4607)"
GRAFANA_PORT="$(env_port GRAFANA_PORT 4605)"
# Source optional libraries only when needed
load_download_lib() {
source "${SCRIPT_DIR}/lib/download.sh"
}
load_export_lib() {
source "${SCRIPT_DIR}/lib/export.sh"
}
load_config_lib() {
source "${SCRIPT_DIR}/lib/config.sh"
}
# Command groups, loaded by main() only for the command that needs them.
load_cli_core_lib() {
source "${SCRIPT_DIR}/lib/cli_core.sh"
}
load_cli_dev_lib() {
source "${SCRIPT_DIR}/lib/cli_dev.sh"
}
load_cli_gpu_lib() {
source "${SCRIPT_DIR}/lib/cli_gpu.sh"
}
# =============================================================================
# Service Management
# =============================================================================
cmd_start() {
log_step "Starting OpenProcessor services..."
cd "$PROJECT_DIR"
ensure_bind_mount_dirs "$PROJECT_DIR"
dc up -d --remove-orphans
log_success "Services started"
echo ""
cmd_status
}
cmd_stop() {
log_step "Stopping OpenProcessor services..."
cd "$PROJECT_DIR"
dc down
log_success "Services stopped"
}
cmd_restart() {
local service="${1:-}"
if [[ -n "$service" ]]; then
log_step "Restarting $service..."
cd "$PROJECT_DIR"
dc restart "$service"
log_success "$service restarted"
else
log_step "Restarting all services..."
cd "$PROJECT_DIR"
dc restart
log_success "All services restarted"
fi
}
cmd_logs() {
local service="${1:-}"
local follow="${2:-}"
cd "$PROJECT_DIR"
if [[ -n "$service" ]]; then
if [[ "$follow" == "-f" ]] || [[ "$follow" == "--follow" ]]; then
dc logs -f "$service"
else
dc logs --tail=100 "$service"
fi
else
if [[ "$1" == "-f" ]] || [[ "$1" == "--follow" ]]; then
dc logs -f
else
dc logs --tail=50
fi
fi
}
cmd_status() {
print_header "OpenProcessor Status"
cd "$PROJECT_DIR"
# Container status
echo -e "${BOLD}Containers:${NC}"
dc ps --format "table {{.Name}}\t{{.Status}}\t{{.Ports}}" 2>/dev/null || \
dc ps
echo ""
# Health checks
echo -e "${BOLD}Health Checks:${NC}"
# API health
local api_status
api_status=$(curl -s -o /dev/null -w "%{http_code}" "localhost:${API_PORT}/health" 2>/dev/null || echo "000")
if [[ "$api_status" == "200" ]]; then
echo -e " API (${API_PORT}): ${GREEN}healthy${NC}"
else
echo -e " API (${API_PORT}): ${RED}unavailable${NC} (HTTP $api_status)"
fi
# Triton health
local triton_status
triton_status=$(curl -s -o /dev/null -w "%{http_code}" "localhost:${TRITON_HTTP_PORT}/v2/health/ready" 2>/dev/null || echo "000")
if [[ "$triton_status" == "200" ]]; then
echo -e " Triton (${TRITON_HTTP_PORT}): ${GREEN}healthy${NC}"
else
echo -e " Triton (${TRITON_HTTP_PORT}): ${RED}unavailable${NC} (HTTP $triton_status)"
fi
# OpenSearch health
local opensearch_status
opensearch_status=$(curl -s -o /dev/null -w "%{http_code}" "localhost:${OPENSEARCH_PORT}" 2>/dev/null || echo "000")
if [[ "$opensearch_status" == "200" ]]; then
echo -e " OpenSearch (${OPENSEARCH_PORT}): ${GREEN}healthy${NC}"
else
echo -e " OpenSearch (${OPENSEARCH_PORT}): ${RED}unavailable${NC} (HTTP $opensearch_status)"
fi
# Grafana health
local grafana_status
grafana_status=$(curl -s -o /dev/null -w "%{http_code}" "localhost:${GRAFANA_PORT}/api/health" 2>/dev/null || echo "000")
if [[ "$grafana_status" == "200" ]]; then
echo -e " Grafana (${GRAFANA_PORT}): ${GREEN}healthy${NC}"
else
echo -e " Grafana (${GRAFANA_PORT}): ${YELLOW}unavailable${NC}"
fi
echo ""
# GPU status
if command -v nvidia-smi &> /dev/null; then
echo -e "${BOLD}GPU:${NC}"
check_gpu_memory_usage 0
fi
}
cmd_health() {
log_info "Checking API health..."
local response
response=$(curl -s "localhost:${API_PORT}/health" 2>/dev/null)
if [[ -n "$response" ]]; then
echo "$response" | python3 -m json.tool 2>/dev/null || echo "$response"
else
log_error "API not responding"
return 1
fi
}
# =============================================================================
# Main
# =============================================================================
main() {
local command="${1:-help}"
shift 2>/dev/null || true
# Only here: dc() often runs inside a command substitution, where an exit is swallowed.
case "$command" in
help|--help|-h|version) ;;
*) refuse_retired_env "$PROJECT_DIR/.env" ;;
esac
case "$command" in
start)
cmd_start "$@"
;;
stop)
cmd_stop "$@"
;;
restart)
cmd_restart "$@"
;;
logs)
cmd_logs "$@"
;;
status)
cmd_status "$@"
;;
health)
cmd_health "$@"
;;
models)
load_cli_core_lib
cmd_models "$@"
;;
export)
load_cli_core_lib
cmd_export "$@"
;;
download)
load_cli_core_lib
cmd_download "$@"
;;
profile)
load_cli_dev_lib
require_checkout "profile" && cmd_profile "$@"
;;
test)
load_cli_dev_lib
require_checkout "test" && cmd_test "$@"
;;
curation)
load_cli_core_lib
cmd_curation "$@"
;;
bench)
load_cli_dev_lib
require_checkout "bench" && cmd_bench "$@"
;;
clean)
load_cli_dev_lib
cmd_clean "$@"
;;
update)
load_cli_core_lib
cmd_update "$@"
;;
setup)
require_checkout "setup" && exec "${SCRIPT_DIR}/setup.sh" "$@"
;;
sample)
load_cli_core_lib
cmd_sample "$@"
;;
config)
load_cli_core_lib
cmd_config "$@"
;;
gpu)
load_cli_gpu_lib
cmd_gpu "$@"
;;
vlm)
load_cli_gpu_lib
cmd_vlm "$@"
;;
train-mode)
load_cli_gpu_lib
cmd_train_mode "$@"
;;
upgrade)
load_cli_core_lib
cmd_upgrade "$@"
;;
repair)
load_cli_core_lib
cmd_repair "$@"
;;
uninstall)
load_cli_core_lib
cmd_uninstall "$@"
;;
version)
echo "openprocessor CLI ${OP_CLI_VERSION:-0.6.0}"
;;
help|--help|-h)
load_cli_core_lib
show_help
;;
*)
log_error "Unknown command: $command"
load_cli_core_lib
echo ""
show_help
exit 1
;;
esac
}
main "$@"