Files
openbot/scripts/hosting/openbot-container
T
NBandClaude Opus 5.5 1ed7b4eaf8 Use the memory guard on self-hosted and Docker servers (#2111)
* Use the memory guard on self-hosted and Docker servers

A self-hosted server and the Docker container now create the memory
guard of a hosted server. New turns wait when free memory is low, a
fixed number of turns run at the same time, and the browser gets the
low-memory signal. The desktop app does not change.

- Rename HostedServerMemory to ServerMemory.
- When no cgroup v2 memory limit can be read, use the machine memory
  and log one warning. Before, a missing memory.max stopped the guard.
- `openbot status` shows the memory limit, the free memory, the level
  and the turn limit.
- Document the guard for self-hosted servers and containers.

Closes #2093

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Fix the review findings of the server memory guard

- In the container, start Xvfb in a subshell, so tini adopts it. As a
  child of main, it got the OOM value of the agent processes.
- A self-hosted or container server has no plan, so its low-memory
  notice does not tell the user to buy a larger plan.
- Use the machine memory only when memory.max is missing. Another read
  error is a read error, as before.
- Read /proc with a limit of 16 at the same time.
- Correct the comments that say only a hosted server reads memory.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 18:48:44 +02:00

79 lines
4.1 KiB
Bash
Executable File

#!/bin/bash
# The entrypoint of the OpenBot Docker image (docker/Dockerfile). It runs OpenBot as a self-hosted
# server, as openbot-hosted-server does on a computer with systemd: a D-Bus session with an unlocked
# keyring, so that Electron `safeStorage` can keep the account session, a virtual display, and
# `OPENBOT_SERVER=1`. Its owner signs it in with `docker exec -it openbot openbot login`.
#
# It runs as the image user. The home folder of that user is the /data volume, so the database, the
# workspaces, the provider sign-ins and the keyring stay when the container is replaced. The keyring
# has the key of the encrypted data in that folder, so the two always go together. See docs/docker.md.
set -euo pipefail
umask 077
fail() {
echo "$*" >&2
exit 1
}
[ "$(id -u)" -ne 0 ] || fail "Run the OpenBot image as its own user, not as root."
[ -w "$HOME" ] || fail "User $(id -u) cannot write to $HOME. Give the data volume to that user: chown -R $(id -u):$(id -g) <folder>"
# The Electron sandbox stays on. It makes user namespaces, and the default seccomp profile of Docker
# refuses them to a container. `--no-sandbox` is never the answer.
unshare --user --map-root-user true 2>/dev/null ||
fail "OpenBot cannot make a user namespace for the Electron sandbox. Start the container with --security-opt seccomp=seccomp.json. See https://github.com/nightly-labs/openbot/blob/main/docs/docker.md"
# The control socket of `openbot`. Main refuses a directory that other users can enter. The socket of
# the last run stays in the container, and `openbot` must not take it for the new one.
mkdir -p "$XDG_RUNTIME_DIR"
chmod 0700 "$XDG_RUNTIME_DIR"
rm -f "$XDG_RUNTIME_DIR/control.sock"
# D-Bus and the virtual display run in the background, and OpenBot replaces this script. tini then
# gives the stop signal to OpenBot only and waits for it: OpenBot stops its agents and closes the
# database. A wrapper such as xvfb-run would stop at once, and Docker would kill OpenBot with it.
# An assignment first, so `set -e` stops the start when a daemon fails. `eval "$(…)"` ignores it.
session=$(dbus-launch --sh-syntax)
eval "$session"
# The lock of the last run stays in the container too.
rm -f /tmp/.X0-lock /tmp/.X11-unix/X0
# The subshell exits at once, so tini adopts Xvfb. As a child of OpenBot, Xvfb would get the high OOM
# value of the agent processes, and OpenBot stops when the kernel kills its display.
(Xvfb :0 -screen 0 1280x800x24 -nolisten tcp >/dev/null 2>&1 &)
export DISPLAY=:0
for _ in $(seq 1 100); do
xdpyinfo >/dev/null 2>&1 && break
sleep 0.1
done
xdpyinfo >/dev/null 2>&1 || fail "The virtual display did not start."
# Each data volume makes its own keyring password on its first start. The password only lets the
# daemon open the keyring without a person; the file is 0600. A keyring with no password file is not
# new: a new password cannot open it, so the start stops.
state="$HOME/.config/openbot-container"
keyrings="$HOME/.local/share/keyrings"
mkdir -p "$state" "$keyrings"
if [ ! -s "$state/keyring-password" ] && [ -e "$keyrings/login.keyring" ]; then
fail "The keyring exists, but its password file $state/keyring-password is missing."
fi
if [ ! -s "$state/keyring-password" ]; then
temporary=$(mktemp "$state/keyring-password.XXXXXX")
head -c 32 /dev/urandom | base64 | tr -d '\n' >"$temporary"
mv -f "$temporary" "$state/keyring-password"
fi
# libsecret stores in the `default` collection, so the alias points to the `login` keyring that the
# daemon unlocks.
printf login >"$keyrings/default"
unlocked=$(gnome-keyring-daemon --unlock --components=secrets <"$state/keyring-password")
eval "$unlocked"
started=$(gnome-keyring-daemon --start --components=secrets)
eval "$started"
export GNOME_KEYRING_CONTROL
# Chromium keeps its shared memory in /dev/shm. Some runtimes, such as Kubernetes, give only 64 MB
# and have no --shm-size. Below 1 GiB, Chromium keeps that memory in /tmp.
chromium=()
shm=$(df --output=size -k /dev/shm 2>/dev/null | tail -n 1 | tr -d ' ') || shm=0
[ "${shm:-0}" -ge 1048576 ] 2>/dev/null || chromium+=(--disable-dev-shm-usage)
export OPENBOT_SERVER=1
exec /opt/OpenBot/app/openbot --password-store=gnome-libsecret "${chromium[@]}"