scripts/ops/install-leleka-service.sh (owner runs it with sudo; nothing here was run by an agent):
--install [--no-start] [--force] copies scripts/ops/leleka.service to /etc/systemd/system, daemon-reload, enable, start; refuses when the
state is not prepared, a binary is missing, the user para does not exist, or the lab KAR holder runs
--remove stop, disable, remove the unit (state, logs, models untouched)
--status unprivileged: systemd state, lab mode, the supervisor's status.json (phase, uptime, restarts and reason, health), log tail
--lab on|off STATE/lab-mode: the unit (ConditionPathExists=!) stays down while the GPU belongs to the lab queue
docs/unattended.md: what runs, the exact owner steps (prepare with `leleka up --mode none`, install, use, look, update), lab mode, the fast cold
load (problem, change, measurements), the supervision table (what happens for every failure), logs and disk, security defaults, the soak test
(harness, criteria, the multi-day command), open items. docs/deploy.md section 6 follows the new restart, log and status behaviour.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
126 lines
5.7 KiB
Bash
Executable File
126 lines
5.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Owner-run (needs sudo for --install and --remove): put the Leleka runtime under systemd on the 5090 server, so that it
|
|
# starts at boot and is restarted when it fails. Nothing here is run by an agent. Read docs/unattended.md first.
|
|
#
|
|
# sudo scripts/ops/install-leleka-service.sh --install [--no-start] [--force]
|
|
# sudo scripts/ops/install-leleka-service.sh --remove
|
|
# scripts/ops/install-leleka-service.sh --status
|
|
# scripts/ops/install-leleka-service.sh --lab on|off (no sudo: the flag file only; see "Lab mode" in the doc)
|
|
#
|
|
# --install copies scripts/ops/leleka.service to /etc/systemd/system, reloads systemd, enables the unit (start at boot) and
|
|
# starts it unless --no-start is given. It refuses when the service state is not prepared, when the lab KAR
|
|
# holder is still running (it holds 51 GB of RAM and the GPU queue; --force overrides), or when the lab flag is set.
|
|
# --remove stops and disables the unit and removes the unit file. State, logs and models are left alone.
|
|
# --status unprivileged summary: systemd state, the supervisor's status.json, the last log lines.
|
|
# --lab on: create STATE/lab-mode (the unit will not start while it exists); off: remove it. The running service is
|
|
# not touched by this flag; stop or start it with systemctl.
|
|
set -euo pipefail
|
|
|
|
SERVICE_DIR=${LELEKA_SERVICE_DIR:-/data/leleka-e2e/service}
|
|
STATE=$SERVICE_DIR/state
|
|
PREFIX=$SERVICE_DIR/prefix
|
|
UNIT=leleka.service
|
|
HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
|
SRC=$HERE/$UNIT
|
|
DST=/etc/systemd/system/$UNIT
|
|
|
|
die() { echo "install-leleka-service: $*" >&2; exit 1; }
|
|
need_root() { [ "$(id -u)" = 0 ] || die "this needs root: sudo $0 $*"; }
|
|
|
|
lab_holder_running() { pgrep -f 'kar_holder' >/dev/null 2>&1; }
|
|
|
|
checks() {
|
|
[ -f "$SRC" ] || die "missing $SRC"
|
|
[ -f "$STATE/leleka.json" ] && [ -f "$STATE/runtime.env" ] || die "the service state is not prepared ($STATE/leleka.json, runtime.env): run the prepare step of docs/unattended.md first"
|
|
[ -x "$PREFIX/bin/leleka" ] || die "missing $PREFIX/bin/leleka"
|
|
for b in leleka-inference leleka-daemon leleka-cli strata-next; do
|
|
[ -x "$PREFIX/bin/$b" ] || die "missing $PREFIX/bin/$b"
|
|
done
|
|
id para >/dev/null 2>&1 || die "the unit runs as user para, which does not exist"
|
|
if [ -e "$STATE/lab-mode" ]; then
|
|
echo "note: $STATE/lab-mode exists, so the unit will be installed and enabled but will not start until it is removed ($0 --lab off)" >&2
|
|
fi
|
|
if lab_holder_running && [ "${FORCE:-0}" != 1 ]; then
|
|
die "the lab KAR holder (kar_holder*.py) is running: it holds 51 GB of RAM and the GPU queue. Stop it first (touch the STOP file in its jobs directory, wait for it to exit) or pass --force"
|
|
fi
|
|
if command -v systemd-analyze >/dev/null 2>&1; then
|
|
systemd-analyze verify "$SRC" 2>&1 | sed 's/^/systemd-analyze: /' || true
|
|
fi
|
|
}
|
|
|
|
install_unit() {
|
|
need_root "$@"
|
|
checks
|
|
install -m 0644 -o root -g root "$SRC" "$DST"
|
|
systemctl daemon-reload
|
|
systemctl enable "$UNIT"
|
|
if [ "${NO_START:-0}" = 1 ] || [ -e "$STATE/lab-mode" ]; then
|
|
echo "installed and enabled; not started. Start it with: sudo systemctl start $UNIT"
|
|
else
|
|
systemctl start "$UNIT"
|
|
echo "installed, enabled and started. Follow it with: journalctl -u $UNIT -f (first start after a boot: a few minutes)"
|
|
fi
|
|
echo "check: $0 --status"
|
|
}
|
|
|
|
remove_unit() {
|
|
need_root "$@"
|
|
if systemctl list-unit-files "$UNIT" >/dev/null 2>&1; then
|
|
systemctl disable --now "$UNIT" || true
|
|
fi
|
|
rm -f "$DST"
|
|
systemctl daemon-reload
|
|
echo "removed $DST (state, logs and models are untouched)"
|
|
}
|
|
|
|
status() {
|
|
echo "unit: $(systemctl is-enabled "$UNIT" 2>&1 || true) / $(systemctl is-active "$UNIT" 2>&1 || true)"
|
|
[ -e "$STATE/lab-mode" ] && echo "lab mode: ON ($STATE/lab-mode exists; the unit does not start)" || echo "lab mode: off"
|
|
if lab_holder_running; then echo "lab: the KAR holder is running"; fi
|
|
if [ -f "$STATE/run/status.json" ]; then
|
|
python3 - "$STATE/run/status.json" <<'PY' 2>/dev/null || cat "$STATE/run/status.json"
|
|
import json, sys, time
|
|
d = json.load(open(sys.argv[1]))
|
|
age = int(time.time()) - d.get("updated_unix", 0)
|
|
up = int(time.time()) - d.get("started_unix", d.get("updated_unix", 0))
|
|
print(f"supervisor: phase={d['phase']} ready={d['ready']} up={up//3600}h{up%3600//60:02d}m status age={age}s")
|
|
print(f"engine: pid={d.get('engine_pid')} restarts_total={d.get('restarts_total')} in_a_row={d.get('engine_restarts')} last={d.get('last_restart_reason')}")
|
|
print(f"health: {d.get('health')} endpoint {d.get('endpoint')} logs {d.get('log_bytes', 0)//(1<<20)} MiB")
|
|
print(f"message: {d.get('message')}")
|
|
PY
|
|
else
|
|
echo "status: no $STATE/run/status.json (the service has not run)"
|
|
fi
|
|
if [ -f "$STATE/logs/leleka.log" ]; then echo "--- last lines of $STATE/logs/leleka.log"; tail -n 8 "$STATE/logs/leleka.log"; fi
|
|
}
|
|
|
|
lab() {
|
|
case "${1:-}" in
|
|
on) : > "$STATE/lab-mode"; echo "lab mode ON: the unit will not start. If it is running: sudo systemctl stop $UNIT (then start the lab holder)";;
|
|
off) rm -f "$STATE/lab-mode"; echo "lab mode off. Stop the lab holder, then: sudo systemctl start $UNIT";;
|
|
*) die "--lab needs on or off";;
|
|
esac
|
|
}
|
|
|
|
ACTION=
|
|
FORCE=0
|
|
NO_START=0
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--install|--remove|--status) ACTION=${1#--}; shift;;
|
|
--lab) ACTION=lab; shift; LABARG=${1:-}; [ $# -gt 0 ] && shift;;
|
|
--no-start) NO_START=1; shift;;
|
|
--force) FORCE=1; shift;;
|
|
-h|--help) sed -n '2,20p' "$0"; exit 0;;
|
|
*) die "unknown argument $1 (see --help)";;
|
|
esac
|
|
done
|
|
export FORCE NO_START
|
|
case "$ACTION" in
|
|
install) install_unit "$@";;
|
|
remove) remove_unit "$@";;
|
|
status) status;;
|
|
lab) lab "${LABARG:-}";;
|
|
*) sed -n '2,20p' "$0"; exit 2;;
|
|
esac
|