pybot / world /entrypoint.sh
Agent Manager
Rename pybot world and task for legion
c786dae
Raw History Blame Contribute Delete
2.55 kB
#!/bin/bash
# pybot-entrypoint: check the env, then serve the pybot world on $PORT (`python pybot.py`).
#
# The world keeps its own state in PYBOT_BUCKET: an empty PYBOT_WS is restored from
# <bucket>/world.tar.gz before it serves (a failed restore parks the world, which retries with
# backoff; /healthz says "parked"), and it pushes every PYBOT_BUCKET_PUSH_S (300) and at exit
# (SIGTERM, or SIGUSR1, which the server turns into SIGTERM).
#
# Arguments are ignored, so a launcher's placeholder command (e.g. `sleep infinity`) is harmless.
#
# Required env: HF_TOKEN LEGION_WORLD_TOKEN PYBOT_REPO PYBOT_BUCKET
# (hf://buckets/<ns>/<bucket>/<prefix>, or "off" for no restore and no pushes: smoke tests only).
# Optional: PORT (7860), LEGION_WORLD_HOST_FILE (writes host:port there: the file legion's controller
# follows), WORLD_HOST ($HOSTNAME), PYBOT_WORK (/work), every PYBOT_* world setting, and
# PYBOT_SANDBOX_URL + PYBOT_SANDBOX_TOKEN (bots run in the sidecar, not under unshare here).
set -euo pipefail
die() { printf '%s pybot-entrypoint: FATAL: %s\n' "$(date -u +%FT%TZ)" "$*" >&2; exit 2; }
for v in HF_TOKEN LEGION_WORLD_TOKEN PYBOT_REPO PYBOT_BUCKET; do
[ -n "${!v:-}" ] || die "$v is not set"
done
[ -z "${PYBOT_SANDBOX_URL:-}" ] || [ -n "${PYBOT_SANDBOX_TOKEN:-}" ] \
|| die "PYBOT_SANDBOX_URL is set but PYBOT_SANDBOX_TOKEN is not (the sidecar would refuse every run)"
case ${PYBOT_BUCKET%/} in
off) unset PYBOT_BUCKET ;; # the world: no PYBOT_BUCKET = no restore and no pushes
hf://buckets/*/*) ;;
*) die "PYBOT_BUCKET must be hf://buckets/<ns>/<bucket>[/prefix] or off" ;;
esac
export PORT=${PORT:-7860}
case $PORT in ''|*[!0-9]*|0) die "PORT=$PORT must be a positive integer";; esac
WORLD=${WORLD_DIR:-/opt/world}
WORK=${PYBOT_WORK:-/work}
# Set here, not in the image: pyxis passes the host env (HOME, PYTHONPATH) into the container.
export PYTHONPATH=$WORLD/src HOME=$WORK/home HF_HOME=$WORK/hf TMPDIR=$WORK/tmp
export PYBOT_WS=$WORK/ws PYTHONUNBUFFERED=1
mkdir -p "$PYBOT_WS" "$HOME" "$HF_HOME" "$TMPDIR" || die "cannot create dirs under $WORK"
if [ -n "${LEGION_WORLD_HOST_FILE:-}" ]; then # atomic: legion's controller re-reads it every 15 s
{ echo "${WORLD_HOST:-$HOSTNAME}:$PORT" > "$LEGION_WORLD_HOST_FILE.tmp" && mv "$LEGION_WORLD_HOST_FILE.tmp" "$LEGION_WORLD_HOST_FILE"; } \
|| echo "pybot-entrypoint: cannot write $LEGION_WORLD_HOST_FILE" >&2
fi
cd "$WORLD" || die "cannot cd $WORLD"
exec python pybot.py # serve() on 0.0.0.0:$PORT; the token comes from LEGION_WORLD_TOKEN, never argv