File size: 2,545 Bytes
adf68aa
 
 
 
 
 
 
 
 
 
c786dae
adf68aa
c786dae
adf68aa
 
 
 
 
c786dae
adf68aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c786dae
 
 
adf68aa
 
 
c786dae
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
#!/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