Files
openbot/scripts/hosting/openbot-hosted-server
T
NBandClaude Opus 5.5 4fd4637b1e Install OpenBot as a server from a terminal (#1297)
* Install OpenBot as a server from a terminal

Add a self-hosted server mode for a Linux computer with no screen.
install-server.sh downloads the latest release, checks it against the
release manifest, and runs the setup file of that release. The setup
uses the install steps of a hosted server, with the mode "self".

Main opens a control socket in the runtime directory of the service
user (0700, socket 0600). The `openbot` command uses it to sign in with
an email code, set the server name, show the status and sign out.
Main checks the server name before it stores it, and the status shows
why a publish failed.

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

* Keep the sign-in code out of curl arguments, and keep install values

- The openbot command sends the form body to curl on standard input.
  Other local users can read process arguments in /proc.
- A reinstall keeps the installed service user and account server when
  the options are not given, and refuses another --user. The keyring
  and the sign-in belong to them.
- Setup starts openbot-update.timer, so update checks start before the
  first reboot.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 17:37:34 +02:00

111 lines
5.3 KiB
Bash
Executable File

#!/bin/bash
# openbot.service runs this as the sandbox user. It waits for the env file that openbot-hosted-env
# writes, starts a D-Bus session with an unlocked keyring so that Electron `safeStorage` can keep
# the account session, and runs OpenBot on the boat desktop.
#
# On a self-hosted server (`mode` is `self`, which openbot-server-setup writes) there is no claim and
# no boat desktop: OpenBot starts with `OPENBOT_SERVER=1` on a virtual display, and its owner signs
# it in with `openbot login`.
set -euo pipefail
umask 077
state="$HOME/.config/openbot-hosted"
mode=hosted
if [ -r /opt/OpenBot/hosted/mode ] && [ "$(</opt/OpenBot/hosted/mode)" = self ]; then mode=self; fi
if [ "${1:-}" != --in-session ] && [ "$mode" = self ]; then
mkdir -p "$state"
chmod 0700 "$state"
exec dbus-run-session -- "$0" --in-session
fi
if [ "${1:-}" != --in-session ]; then
# The setup script starts after the sandbox is ready, so on the first boot this runs first.
for _ in $(seq 1 300); do
[ -s "$state/env" ] && break
sleep 2
done
[ -s "$state/env" ] || { echo "The hosted server env file is missing." >&2; exit 1; }
exec dbus-run-session -- "$0" --in-session
fi
# Read only the expected keys. The file is data, not a script.
if [ "$mode" = hosted ]; then
while IFS='=' read -r key value; do
case $key in
OPENBOT_HOSTED_HOST_ID | OPENBOT_HOSTED_CLAIM) export "$key=$value" ;;
esac
done <"$state/env"
export OPENBOT_HOSTED_SERVER=1
else
# The control socket of `openbot`. It goes in the runtime directory that openbot.service makes.
export OPENBOT_SERVER=1
fi
# A self-hosted server has this file only when its install named another account server.
if [ -r /opt/OpenBot/hosted/openbot.env ]; then
while IFS='=' read -r key value; do
if [ "$key" = OPENBOT_AUTH_API_URL ]; then export "$key=$value"; fi
done </opt/OpenBot/hosted/openbot.env
fi
# Each server makes its own keyring password on its first start, so no two servers share one.
# 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 service stops
# and systemd tries again after the home folder is back.
if [ ! -s "$state/keyring-password" ] && [ -e /srv/openbot-hosted/keyrings/login.keyring ]; then
echo "The keyring exists, but its password file is missing." >&2
exit 1
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
# The keyring files stay in /srv/openbot-hosted, which provision.sh makes for this user. They are
# not in ~/.local/share/keyrings: the boat image has a `default` keyring there with a password that
# this server does not know, and a snapshot restore resets that folder after this service starts.
# They are also not in /home: after a resume, boat serves /home from a slow FUSE mount until the
# disk is restored. There gnome-keyring does not see the new link count after its backup link, so
# each keyring write added links until the mount went away, 16,000 to 31,000 `*.temp-*` files. The
# next resume read that folder through the mount, and the start took 25 s longer. boat keeps
# changes in /srv and restores them before this service starts. libsecret stores in the `default`
# collection, so the alias points to the `login` keyring that the daemon unlocks.
export XDG_DATA_HOME=/srv/openbot-hosted
keyrings="$XDG_DATA_HOME/keyrings"
mkdir -p "$keyrings"
printf login >"$keyrings/default"
# An assignment first, so `set -e` stops the start when the daemon fails. `eval "$(…)"` ignores it.
unlocked=$(gnome-keyring-daemon --unlock --components=secrets <"$state/keyring-password")
eval "$unlocked"
started=$(gnome-keyring-daemon --start --components=secrets)
eval "$started"
unset XDG_DATA_HOME
export GNOME_KEYRING_CONTROL
# If boat ever serves /srv from the FUSE mount too, remove those links when the disk is back. A
# link that is not to the current keyring file is not the backup of a write that is in progress.
(
for _ in $(seq 1 300); do
case $(findmnt -n -o FSTYPE -T "$keyrings" || true) in fuse*) sleep 2 ;; *) break ;; esac
done
sleep 10
for keyring in "$keyrings"/*.keyring; do
[ -e "$keyring" ] || continue
find "$keyrings" -maxdepth 1 -name "${keyring##*/}.temp-*" ! -samefile "$keyring" -delete
done
) &
# The boat desktop is a lightdm session of this user on :0, with a window manager. OpenBot shows its
# windows there, so boat's desktop viewer and OpenBot remote desktop show the same screen, and a click
# moves the keyboard focus to another window. After a resume, boat can start that session after this
# service. A sandbox that has no desktop session, and a self-hosted server, use a virtual display.
for _ in $(seq 1 60); do
[ "$mode" = self ] && break
if DISPLAY=:0 XAUTHORITY="$HOME/.Xauthority" xdpyinfo >/dev/null 2>&1; then
export DISPLAY=:0 XAUTHORITY="$HOME/.Xauthority"
exec /opt/OpenBot/app/openbot --password-store=gnome-libsecret
fi
sleep 2
done
if [ "$mode" = hosted ]; then echo "The boat desktop session did not start. OpenBot uses a virtual display." >&2; fi
exec xvfb-run -a -s "-screen 0 1280x800x24 -nolisten tcp" \
/opt/OpenBot/app/openbot --password-store=gnome-libsecret