prain front door and OpenRC user service for the prain stack
This commit is contained in:
Executable
+76
@@ -0,0 +1,76 @@
|
||||
#!/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
|
||||
# goes to prain's own CLI in PRAIN_ROOT/.venv, with its scripts/env.sh sourced.
|
||||
set -euo pipefail
|
||||
|
||||
conf=${PRAIN_CONF:-${XDG_CONFIG_HOME:-$HOME/.config}/prain/prain.conf}
|
||||
if [ -r "$conf" ]; then . "$conf"; fi
|
||||
: "${PRAIN_ROOT:?set PRAIN_ROOT in $conf}"
|
||||
read -ra services <<< "${PRAIN_SERVICES:-}"
|
||||
logdir=${XDG_STATE_HOME:-$HOME/.local/state}/prain
|
||||
|
||||
get() { # get cmd vllm -> $cmd_vllm from the config
|
||||
local v="$1_$2"
|
||||
printf '%s' "${!v:-}"
|
||||
}
|
||||
|
||||
prain_env() {
|
||||
set +eu # env.sh is not written for -e/-u
|
||||
. "$PRAIN_ROOT/scripts/env.sh"
|
||||
set -eu
|
||||
}
|
||||
|
||||
names() { # the services named on the command line, else all of them
|
||||
if [ $# -gt 0 ]; then printf '%s\n' "$@"; else printf '%s\n' "${services[@]}"; fi
|
||||
}
|
||||
|
||||
case ${1:-} in
|
||||
up | down | restart | status)
|
||||
action=${1/up/start}
|
||||
action=${action/down/stop}
|
||||
shift
|
||||
mapfile -t list < <(names "$@")
|
||||
if [ "$action" = stop ]; then mapfile -t list < <(printf '%s\n' "${list[@]}" | tac); fi
|
||||
rc=0
|
||||
for n in "${list[@]}"; do rc-service --user "prain.$n" "$action" || rc=1; done
|
||||
exit $rc
|
||||
;;
|
||||
logs)
|
||||
shift
|
||||
mapfile -t list < <(names "$@")
|
||||
files=()
|
||||
for n in "${list[@]}"; do files+=("$logdir/$n.log"); done
|
||||
exec tail -n "${PRAIN_LOG_LINES:-40}" -F "${files[@]}"
|
||||
;;
|
||||
run)
|
||||
cmd=$(get cmd "$2")
|
||||
[ -n "$cmd" ] || { echo "prain: no cmd_$2 in $conf" >&2; exit 1; }
|
||||
cd "$PRAIN_ROOT"
|
||||
prain_env
|
||||
eval "exec ${cmd//$'\n'/ }"
|
||||
;;
|
||||
need)
|
||||
get need "$2"
|
||||
echo
|
||||
;;
|
||||
wait)
|
||||
url=$(get ready "$2")
|
||||
[ -n "$url" ] || exit 0
|
||||
timeout=${PRAIN_READY_TIMEOUT:-900}
|
||||
end=$((SECONDS + timeout))
|
||||
until curl -sf -o /dev/null "$url"; do
|
||||
[ $SECONDS -lt $end ] || { echo "prain: $2 not ready at $url after $timeout s" >&2; exit 1; }
|
||||
sleep 2
|
||||
done
|
||||
;;
|
||||
'' | -h | --help)
|
||||
echo "stack: prain up|down|restart|status|logs [NAME...] (services: ${services[*]})"
|
||||
prain_env
|
||||
exec "$PRAIN_ROOT/.venv/bin/prain" --help
|
||||
;;
|
||||
*)
|
||||
prain_env
|
||||
exec "$PRAIN_ROOT/.venv/bin/prain" "$@"
|
||||
;;
|
||||
esac
|
||||
@@ -0,0 +1,19 @@
|
||||
# prain stack config, read by the `prain` front door (bash).
|
||||
# Install: cp scripts/prain.conf.example ~/.config/prain/prain.conf
|
||||
# Commands run in PRAIN_ROOT with its scripts/env.sh sourced, so `prain` in them is the
|
||||
# venv's CLI. Service names must be shell identifiers.
|
||||
|
||||
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).
|
||||
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
|
||||
--attention-backend TRITON_ATTN --enable-auto-tool-choice --tool-call-parser gemma4'
|
||||
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
|
||||
Executable
+44
@@ -0,0 +1,44 @@
|
||||
#!/bin/bash
|
||||
# Checks the prain front door against a fake prain checkout and a fake rc-service.
|
||||
# Run: bash scripts/test_prain.sh
|
||||
set -u
|
||||
p=$(cd "$(dirname "$0")" && pwd)/prain
|
||||
t=$(mktemp -d)
|
||||
trap 'rm -rf "$t"' EXIT
|
||||
mkdir -p "$t/root/scripts" "$t/root/.venv/bin" "$t/bin"
|
||||
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"
|
||||
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
|
||||
PRAIN_ROOT=$t/root
|
||||
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
|
||||
EOF
|
||||
export PRAIN_CONF=$t/prain.conf PRAIN_READY_TIMEOUT=1 PATH=$t/bin:$PATH
|
||||
fail=0
|
||||
check() { # name expected actual
|
||||
if [ "$2" = "$3" ]; then echo "ok $1"; else echo "FAIL $1: expected [$2], got [$3]"; fail=1; fi
|
||||
}
|
||||
cd /
|
||||
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
|
||||
--user prain.b start" "$("$p" up)"
|
||||
check down-in-reverse "--user prain.b stop
|
||||
--user prain.a stop" "$("$p" down)"
|
||||
check status-one "--user prain.b status" "$("$p" status b)"
|
||||
exit $fail
|
||||
Reference in New Issue
Block a user