prain stack: requires_NAME refuses to start without its services, merged --help

This commit is contained in:
2026-11-06 15:50:33 +01:00
parent 8c58c43b1b
commit dfc38db6f8
6 changed files with 80 additions and 29 deletions
+25 -7
View File
@@ -1,6 +1,6 @@
#!/bin/bash
# prain front door. up, down, restart, status and logs drive the stack's OpenRC user
# services (prain.NAME); run, need and wait are what those services call. Anything else
# services (prain.NAME); run, deps and wait are what those services call. Anything else
# goes to prain's own CLI in PRAIN_ROOT/.venv, with its scripts/env.sh sourced.
set -euo pipefail
@@ -50,9 +50,18 @@ run)
prain_env
eval "exec ${cmd//$'\n'/ }"
;;
need)
get need "$2"
echo
deps) # fail, without starting anything, unless the services NAME requires are up
for d in $(get requires "$2"); do
url=$(get ready "$d")
if [ -n "$url" ]; then
curl -sf -o /dev/null "$url" && continue
else
rc-service --user "prain.$d" status > /dev/null 2>&1 && continue
fi
echo "prain: $2 needs $d, which is not running${url:+ (no answer from $url)}." \
"Start it first: prain up $d" >&2
exit 1
done
;;
wait)
url=$(get ready "$2")
@@ -64,10 +73,19 @@ wait)
sleep 2
done
;;
'' | -h | --help)
echo "stack: prain up|down|restart|status|logs [NAME...] (services: ${services[*]})"
'' | -h | --help) # prain's own help, with the stack commands merged into its list
prain_env
exec "$PRAIN_ROOT/.venv/bin/prain" --help
"$PRAIN_ROOT/.venv/bin/prain" --help | awk -v stack="$(cat <<-EOF
up [NAME...] start services (all: ${services[*]}), each waits until ready
down [NAME...] stop services, in reverse order
restart [NAME...] restart services
status [NAME...] show whether services run
logs [NAME...] follow service logs ($logdir/NAME.log)
EOF
)" '
/^(usage: prain| )[^{]*\{/ { sub(/\{/, "{up,down,restart,status,logs,") }
{ print }
/^ \{/ { print stack }'
;;
*)
prain_env
+4 -3
View File
@@ -7,8 +7,9 @@ PRAIN_ROOT=/home/fabian/coding/misc/prain
PRAIN_SERVICES="vllm web" # `prain up` starts in this order, `prain down` stops in reverse
# cmd_NAME: what service prain.NAME runs. ready_NAME: URL that answers once it can serve
# (`prain up` waits for it, PRAIN_READY_TIMEOUT seconds, default 900). need_NAME: services
# it depends on (OpenRC starts them first and stops it with them).
# (`prain up` waits for it, PRAIN_READY_TIMEOUT seconds, default 900). requires_NAME:
# services that must already be up (their ready URL answers); if not, NAME refuses to start
# and says so. They are never started for it.
cmd_vllm='prain serve build/agent-int4.toml models/gemma-4-26B-A4B-it-Uncensored-MAX --
--served-model-name gemma4 --language-model-only --max-model-len 16384
--max-num-batched-tokens 1024 --enable-prefix-caching --port 8000
@@ -16,4 +17,4 @@ cmd_vllm='prain serve build/agent-int4.toml models/gemma-4-26B-A4B-it-Uncensored
ready_vllm=http://127.0.0.1:8000/health
cmd_web='prain web --host 0.0.0.0 --port 8080 --url http://127.0.0.1:8000 --config build/agent-int4.toml'
need_web=vllm
requires_web=vllm
+25 -4
View File
@@ -10,7 +10,15 @@ cat > "$t/root/scripts/env.sh" <<EOF
export FROM_ENV_SH=yes
PATH=$t/root/.venv/bin:\$PATH
EOF
printf '#!/bin/sh\necho "prain $* env=$FROM_ENV_SH cwd=$PWD"\n' > "$t/root/.venv/bin/prain"
cat > "$t/root/.venv/bin/prain" <<'EOF'
#!/bin/sh
if [ "$1" = --help ]; then
printf 'usage: prain [-h] {check,web} ...\n\npositional arguments:\n {check,web}\n'
printf ' check validate\n'
exit 0
fi
echo "prain $* env=$FROM_ENV_SH cwd=$PWD"
EOF
printf '#!/bin/sh\necho "$*"\n' > "$t/bin/rc-service"
chmod +x "$t/root/.venv/bin/prain" "$t/bin/rc-service"
cat > "$t/prain.conf" <<EOF
@@ -19,10 +27,12 @@ PRAIN_SERVICES="a b"
cmd_a='echo a: \$FROM_ENV_SH
in \$PWD'
cmd_b='prain web --port 1'
need_b=a
ready_b=http://127.0.0.1:9/never
requires_b=a
requires_x=b
EOF
export PRAIN_CONF=$t/prain.conf PRAIN_READY_TIMEOUT=1 PATH=$t/bin:$PATH
unset XDG_STATE_HOME
fail=0
check() { # name expected actual
if [ "$2" = "$3" ]; then echo "ok $1"; else echo "FAIL $1: expected [$2], got [$3]"; fail=1; fi
@@ -32,8 +42,6 @@ check passthrough "prain check x.toml env=yes cwd=/" "$("$p" check x.toml)"
check run-joins-lines-in-root "a: yes in $t/root" "$("$p" run a)"
check run-uses-venv-prain "prain web --port 1 env=yes cwd=$t/root" "$("$p" run b)"
check run-unknown-fails 1 "$("$p" run zzz 2>/dev/null; echo $?)"
check need "a" "$("$p" need b)"
check need-none "" "$("$p" need a)"
check wait-without-url 0 "$("$p" wait a; echo $?)"
check wait-times-out 1 "$("$p" wait b 2>/dev/null; echo $?)"
check up-in-order "--user prain.a start
@@ -41,4 +49,17 @@ check up-in-order "--user prain.a start
check down-in-reverse "--user prain.b stop
--user prain.a stop" "$("$p" down)"
check status-one "--user prain.b status" "$("$p" status b)"
check deps-ok-when-running 0 "$("$p" deps b; echo $?)"
check deps-fail-without-starting-them "prain: x needs b, which is not running (no answer from http://127.0.0.1:9/never). Start it first: prain up b
1" "$("$p" deps x 2>&1; echo $?)"
check help-merges-stack-commands "usage: prain [-h] {up,down,restart,status,logs,check,web} ...
positional arguments:
{up,down,restart,status,logs,check,web}
up [NAME...] start services (all: a b), each waits until ready
down [NAME...] stop services, in reverse order
restart [NAME...] restart services
status [NAME...] show whether services run
logs [NAME...] follow service logs ($HOME/.local/state/prain/NAME.log)
check validate" "$("$p" --help)"
exit $fail