From 399628bf16c670af654c7a48248b2325d4121162 Mon Sep 17 00:00:00 2001 From: Florian Schmidt Date: Mon, 16 Nov 2026 10:57:42 +0100 Subject: [PATCH] feat(deploy): one-step install/update from the Proxmox host (#6) bootstrap.sh runs on the Proxmox host via bash -c "$(curl -fsSL .../deploy/proxmox/bootstrap.sh)" It unpacks the repository into a temporary directory, runs create-ct.sh and removes everything again; create-ct.sh also removes the installer files it pushed into the CT. Nothing stays on the host. install.sh now installs or updates: an existing checkout is pulled (fast-forward, SHELLBOOK_REF pins are kept), the service is restarted and health-checked, and the summary prints the address, the one-time setup token while setup is pending, the Claude login state and the SSH public key. It also installs a root-owned `shellbook-update` that downloads the installer from the repository and reruns it. Root never executes scripts from /opt/shellbook, which the service user and its terminal panel can modify, so deploy/update.sh is removed. Verified in a Debian 13 container: fresh install, then an update that pulls a new commit while a tampered checkout script is not executed; bootstrap.test.sh checks that host and CT keep no installer files. --- .agents/mind/index.md | 2 +- deploy/README.md | 37 ++++++++++++----- deploy/install.sh | 65 ++++++++++++++++++++++++------ deploy/proxmox/bootstrap.sh | 15 +++++++ deploy/proxmox/create-ct.sh | 3 +- deploy/test/bootstrap.test.sh | 40 ++++++++++++++++++ deploy/test/install-script.test.sh | 23 +++++++---- deploy/update.sh | 8 ---- 8 files changed, 152 insertions(+), 41 deletions(-) create mode 100755 deploy/proxmox/bootstrap.sh create mode 100755 deploy/test/bootstrap.test.sh delete mode 100755 deploy/update.sh diff --git a/.agents/mind/index.md b/.agents/mind/index.md index 579d3b9..1edfd34 100644 --- a/.agents/mind/index.md +++ b/.agents/mind/index.md @@ -3,7 +3,7 @@ ## Current Focus - [2026-11-16] M0–M3 (#1–#4) and the UI-first follow-ups (#7 hosts/credentials, #8 chat, #9 notebook management, #10 settings/setup) closed. -- [2026-11-16] Open: #5/#6 need a real CT 501 deploy (update.sh, setup token, real SSH run, tmux terminal). Not yet verified in a real browser or against real OpenSSH. +- [2026-11-16] Open: #5/#6 need a real CT 501 deploy (bootstrap.sh one-liner, setup token, real SSH run, tmux terminal). Not yet verified in a real browser or against real OpenSSH. ## Invariants - Displayed script == executed script (one substitution path). diff --git a/deploy/README.md b/deploy/README.md index 176e2af..2b46d44 100644 --- a/deploy/README.md +++ b/deploy/README.md @@ -4,8 +4,15 @@ Access is root-equivalent across managed hosts. Restrict CT port 8080 to the Traefik host with the Proxmox firewall; use HTTPS and limit the router to your LAN/Tailscale addresses. No password is set initially: API access fails closed. -1. Copy `deploy/` to the Proxmox VE 9 host. As root run - `bash deploy/proxmox/create-ct.sh`. Defaults: unprivileged Debian 13 CT 501, +1. On the Proxmox VE 9 host, as root, run (installs or updates; nothing stays on the host): + + ```bash + bash -c "$(curl -fsSL https://git.finalfactory.de/FinalFactory/ShellBook/raw/branch/main/deploy/proxmox/bootstrap.sh)" + ``` + + It unpacks the repository into a temporary directory, runs + `deploy/proxmox/create-ct.sh` from it, and removes it again; the installer files + pushed into the CT are removed too. Defaults: unprivileged Debian 13 CT 501, 2 cores, 2 GiB RAM, 16 GiB disk, DHCP on vmbr0, onboot, nesting and TUN. Overrides: `CTID`, `CT_STORAGE`, `TEMPLATE_STORAGE`, `CT_BRIDGE`, `CT_IP` (Proxmox ip syntax), `CT_HOSTNAME`, `CT_CORES`, `CT_MEMORY`, `CT_DISK_GB`. @@ -15,12 +22,13 @@ LAN/Tailscale addresses. No password is set initially: API access fails closed. `SHELLBOOK_PORT`, `SHELLBOOK_TRUSTED_PROXIES` (comma-separated proxy IPs), and `TS_AUTHKEY` in the host environment. Auth keys are transferred in a mode-0600 temporary file and removed, never stored in the service env. -3. Open ShellBook in your browser. In the CT run `cat /var/lib/shellbook/setup-token` - and enter the token with a new password (at least 12 characters) in the setup screen. - This token is private, survives restart, and is deleted after setup. It is never logged. +3. Open the address the installer prints and enter the one-time setup token it prints + (also `cat /var/lib/shellbook/setup-token` in the CT) with a new password (at least + 12 characters). The token survives restart, is deleted after setup, and the server + never logs it; only the installer shows it to the admin running it. Alternative password CLI: `SHELLBOOK_DATA_DIR=/var/lib/shellbook pnpm --filter @shellbook/server set-password` from the checkout (hidden prompt or one password line on stdin; never arguments). -4. Run `claude` as shellbook and use `/login`. Authorize +4. Unless a Claude login already exists: Settings > "Open terminal to log in", then `/login`. Authorize `/home/shellbook/.ssh/id_ed25519.pub` on managed hosts. The service user has no sudo access. Initialize notebooks manually if templates were absent. 5. Install the Traefik example in its file-provider directory and replace the @@ -30,10 +38,16 @@ LAN/Tailscale addresses. No password is set initially: API access fails closed. detection (and thus Secure cookies). TLS terminates at Traefik. Standalone CT installation: run `bash deploy/install.sh` as root on Debian 13. -The installer can be rerun; it preserves env, password, workspace, SSH key and -session signing secret. It builds the checkout but does not pull on rerun. -Updates: `bash /opt/shellbook/deploy/update.sh` (root). Failure before restart -leaves the running service in place. Check `journalctl -u shellbook`. + +Updates: `shellbook-update` as root in the CT (from the Proxmox host: +`pct exec 501 -- shellbook-update`), or rerun the bootstrap command. It downloads +the installer from the repository (`/etc/shellbook/installer-url`, override with +`SHELLBOOK_INSTALLER_URL`) and reruns it: pull (fast-forward; a `SHELLBOOK_REF` +pin is kept), install, build, restart, health check. Env, password, workspace, +SSH key, Claude login and session secret are preserved. Root never executes +scripts from `/opt/shellbook`, which the service user (and the terminal panel) +can modify. A failed build leaves the running service in place; check +`journalctl -u shellbook`. Configuration is `/etc/shellbook/env`; data is `/var/lib/shellbook` (back it up along with workspace and service-user home). `SHELLBOOK_SESSION_SECONDS` defaults @@ -66,7 +80,8 @@ Node 22 security releases; unattended upgrades cover Debian packages only. Build tools support node-pty compilation. Claude uses its native installer. Local deployment regressions: `bash deploy/test/template-picker.test.sh` and -`bash deploy/test/ct-config.test.sh`, `bash deploy/test/install-script.test.sh`. Existing CTs with a manual TUN mount entry +`bash deploy/test/ct-config.test.sh`, `bash deploy/test/install-script.test.sh`, +`bash deploy/test/bootstrap.test.sh` (needs root; run it in a Debian container). Existing CTs with a manual TUN mount entry (earlier Tailscale setups) keep it; no `dev0` is added on top. The scripts require bash, use LF endings, and avoid early-consuming pipelines under pipefail. Proxmox, systemd, Tailscale and SSH require deployment validation. diff --git a/deploy/install.sh b/deploy/install.sh index 35ae991..ad24171 100755 --- a/deploy/install.sh +++ b/deploy/install.sh @@ -1,4 +1,7 @@ #!/usr/bin/env bash +# Installs ShellBook on Debian 13, or updates an existing installation (rerunnable). +# Keeps env, password, workspace, SSH key, Claude login and session secret. +# Also installs `shellbook-update`, which reruns this installer fetched over HTTPS. set -euo pipefail [[ $EUID == 0 ]] || { echo 'Run as root inside Debian 13' >&2; exit 1; } # `pct exec` starts with PATH=/sbin:/bin:/usr/sbin:/usr/bin; Node and pnpm live in /usr/local/bin. @@ -44,7 +47,10 @@ if [[ ! -e /opt/shellbook ]]; then install -d -o shellbook -g shellbook /opt/shellbook runuser -u shellbook -- git clone "$repo" /opt/shellbook fi -if [[ -n ${SHELLBOOK_REF:-} ]]; then runuser -u shellbook -- git -C /opt/shellbook checkout "$SHELLBOOK_REF"; fi +git_app() { runuser -u shellbook -- git -C /opt/shellbook "$@"; } +if [[ -n ${SHELLBOOK_REF:-} ]]; then git_app fetch --tags origin; git_app checkout "$SHELLBOOK_REF"; fi +if git_app symbolic-ref -q HEAD >/dev/null; then git_app pull --ff-only +else echo "Not pulling: /opt/shellbook is pinned to $(git_app rev-parse --short HEAD) (SHELLBOOK_REF)"; fi runuser -u shellbook -- bash -c 'cd /opt/shellbook; pnpm install --frozen-lockfile; pnpm build' workspace=/home/shellbook/workspace install -d -o shellbook -g shellbook "$workspace" @@ -60,6 +66,27 @@ if [[ ! -x /home/shellbook/.local/bin/claude ]]; then runuser -u shellbook -- env HOME=/home/shellbook bash -o pipefail -c 'curl -fsSL https://claude.ai/install.sh | bash' fi install -d -m 0750 /etc/shellbook +# Where `shellbook-update` fetches the installer; remembered across updates. +installer_url=${SHELLBOOK_INSTALLER_URL:-$(cat /etc/shellbook/installer-url 2>/dev/null || echo https://git.finalfactory.de/FinalFactory/ShellBook/raw/branch/main/deploy/install.sh)} +[[ $installer_url =~ ^(https|file)://[^[:space:]]+$ ]] || { echo 'Invalid SHELLBOOK_INSTALLER_URL' >&2; exit 1; } +printf '%s\n' "$installer_url" > /etc/shellbook/installer-url +# Root must never execute code from /opt/shellbook: the service user (and its +# terminal panel) can write there. The updater runs the installer from the repository. +cat > /usr/local/sbin/shellbook-update <<'EOF' +#!/usr/bin/env bash +# Updates ShellBook: reruns the installer fetched from the repository. +set -euo pipefail +[[ $EUID == 0 ]] || { echo 'Run as root' >&2; exit 1; } +export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin +url=${SHELLBOOK_INSTALLER_URL:-$(cat /etc/shellbook/installer-url)} +installer=$(mktemp) +trap 'rm -f -- "$installer"' EXIT +curl -fsSL "$url" -o "$installer" +[[ $(head -n1 "$installer") == '#!/usr/bin/env bash' ]] || { echo "Not an installer: $url" >&2; exit 1; } +cd / +bash "$installer" +EOF +chmod 0755 /usr/local/sbin/shellbook-update if [[ ! -f /etc/shellbook/env ]]; then host=${SHELLBOOK_HOST:-0.0.0.0} port=${SHELLBOOK_PORT:-8080} @@ -120,16 +147,28 @@ fi systemctl daemon-reload systemctl enable shellbook systemctl restart shellbook -cat <<'EOF' -Next steps: -1. Open ShellBook in the browser and enter the setup token shown by: - cat /var/lib/shellbook/setup-token - Alternative password CLI (hidden prompt): - runuser -u shellbook -- env SHELLBOOK_DATA_DIR=/var/lib/shellbook node /opt/shellbook/apps/server/dist/cli/set-password.js -2. Log into Claude as the service user: - runuser -l shellbook -c '/home/shellbook/.local/bin/claude' - Then use /login. -3. Authorize this public SSH key on managed servers: -EOF +# Wait until the server answers before reporting success. +listen_host=$(sed -n 's/^SHELLBOOK_HOST=//p' /etc/shellbook/env) +listen_port=$(sed -n 's/^SHELLBOOK_PORT=//p' /etc/shellbook/env) +case ${listen_host:-0.0.0.0} in 0.0.0.0|::|'[::]') probe=127.0.0.1 ;; *:*) probe="[$listen_host]" ;; *) probe=$listen_host ;; esac +for ((attempt=0; attempt<60; attempt++)); do + if curl -fsS "http://$probe:${listen_port:-8080}/api/health" >/dev/null 2>&1; then break; fi + sleep 1 +done +curl -fsS "http://$probe:${listen_port:-8080}/api/health" >/dev/null 2>&1 || { + echo 'ShellBook did not start; see: journalctl -u shellbook -n 50' >&2; exit 1; +} +address=$(hostname -I 2>/dev/null | awk '{print $1}') +echo +echo "ShellBook $(git_app describe --always --dirty) is running: http://${address:-}:${listen_port:-8080}" +if [[ -f /var/lib/shellbook/setup-token ]]; then + echo "First-time setup token (one-time, deleted after setup): $(cat /var/lib/shellbook/setup-token)" + echo 'Enter it in the browser together with your new password.' +fi +if [[ ! -f /home/shellbook/.claude/.credentials.json ]]; then + echo 'Claude is not logged in yet: Settings > "Open terminal to log in", then /login.' +fi +echo "ShellBook's SSH public key (the Hosts page can install it on your servers):" cat /home/shellbook/.ssh/id_ed25519.pub -echo '4. Configure Traefik, trusted proxy IPs and the LAN/Tailscale firewall; see deploy/README.md.' +echo 'Update later with: shellbook-update (from the Proxmox host: pct exec -- shellbook-update)' +echo 'Behind Traefik: see deploy/README.md for the proxy and firewall settings.' diff --git a/deploy/proxmox/bootstrap.sh b/deploy/proxmox/bootstrap.sh new file mode 100755 index 0000000..ae16c3f --- /dev/null +++ b/deploy/proxmox/bootstrap.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# Install or update ShellBook in a Proxmox CT in one step, leaving nothing on the host. +# Run on the Proxmox host as root: +# bash -c "$(curl -fsSL https://git.finalfactory.de/FinalFactory/ShellBook/raw/branch/main/deploy/proxmox/bootstrap.sh)" +# Accepts the overrides of create-ct.sh (CTID, CT_*, SHELLBOOK_*, TS_AUTHKEY). +# The repository is unpacked into a temporary directory that is always removed. +set -euo pipefail +[[ $EUID == 0 ]] && command -v pct >/dev/null || { echo 'Run on the Proxmox host as root' >&2; exit 1; } +archive=${SHELLBOOK_ARCHIVE_URL:-https://git.finalfactory.de/FinalFactory/ShellBook/archive/${SHELLBOOK_REF:-main}.tar.gz} +work=$(mktemp -d) +trap 'rm -rf -- "$work"' EXIT +curl -fsSL "$archive" | tar -xz -C "$work" --strip-components=1 --no-same-owner +[[ -f $work/deploy/proxmox/create-ct.sh ]] || { echo "No ShellBook deploy scripts in $archive" >&2; exit 1; } +# stdin stays away from pct exec: callers may pipe this script into bash. +bash "$work/deploy/proxmox/create-ct.sh" > "$settings"; fi done pct push "$ctid" "$settings" /root/shellbook-install.env --perms 0600 -pct exec "$ctid" -- bash -c 'trap "rm -f /root/shellbook-install.env" EXIT; source /root/shellbook-install.env; bash /root/shellbook-install.sh' +# Leave no installer files behind in the CT either; later updates use `shellbook-update`. +pct exec "$ctid" -- bash -c 'trap "rm -f /root/shellbook-install.env /root/shellbook-install.sh" EXIT; source /root/shellbook-install.env; bash /root/shellbook-install.sh' diff --git a/deploy/test/bootstrap.test.sh b/deploy/test/bootstrap.test.sh new file mode 100755 index 0000000..017b2e4 --- /dev/null +++ b/deploy/test/bootstrap.test.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +set -euo pipefail +# Runs bootstrap.sh against a stub `pct` (existing CT 501) and checks that the +# host keeps no files and the CT keeps no installer files. Needs root (Linux/Docker). +[[ $EUID == 0 ]] || { echo 'bootstrap test skipped: needs root (run it in a Debian container)'; exit 0; } +repo=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd) +test_dir=$(mktemp -d) +trap 'rm -rf -- "$test_dir"' EXIT +mkdir "$test_dir/bin" "$test_dir/tmp" "$test_dir/ct" +# Archive of the working tree, shaped like Gitea's (one top-level folder). +tar -C "$repo/.." -czf "$test_dir/archive.tar.gz" --exclude=node_modules --exclude=.git "$(basename "$repo")/deploy" +cat > "$test_dir/bin/pct" <<'STUB' +#!/usr/bin/env bash +log=$STUB_DIR/pct.log; ct=$STUB_DIR/ct +echo "$*" >> "$log" +case $1 in + config) printf 'arch: amd64\nfeatures: nesting=1\nostype: debian\nunprivileged: 1\nlxc.mount.entry: /dev/net/tun dev/net/tun none bind,create=file\n' ;; + status) echo 'status: running' ;; + start|set|shutdown) ;; + push) cp "$3" "$ct/$(basename "$4")" ;; + exec) + shift 3 + if [[ $1 == dpkg ]]; then echo amd64 + elif [[ $1 == bash && $2 == -c ]]; then + # Run the real cleanup wrapper with /root mapped into the fake CT. + script=${3//\/root\//$ct/}; script=${script//bash $ct\/shellbook-install.sh/echo installer-ran} + bash -c "$script" + fi ;; +esac +STUB +printf '#!/usr/bin/env bash\necho amd64\n' > "$test_dir/bin/dpkg" +chmod +x "$test_dir/bin/pct" "$test_dir/bin/dpkg" +out=$(PATH="$test_dir/bin:$PATH" STUB_DIR="$test_dir" TMPDIR="$test_dir/tmp" CTID=501 SHELLBOOK_ARCHIVE_URL="file://$test_dir/archive.tar.gz" \ + bash "$repo/deploy/proxmox/bootstrap.sh") +[[ $out == *installer-ran* ]] || { echo "installer did not run: $out" >&2; exit 1; } +[[ -z $(ls -A "$test_dir/tmp") ]] || { echo "host temp files left: $(ls -A "$test_dir/tmp")" >&2; exit 1; } +[[ -z $(ls -A "$test_dir/ct") ]] || { echo "CT files left: $(ls -A "$test_dir/ct")" >&2; exit 1; } +grep -q '^push 501 .*/install.sh /root/shellbook-install.sh' "$test_dir/pct.log" || { echo 'installer not pushed' >&2; exit 1; } +! grep -q -- '--dev0' "$test_dir/pct.log" || { echo 'dev0 added despite TUN mount' >&2; exit 1; } +printf '5 bootstrap checks passed\n' diff --git a/deploy/test/install-script.test.sh b/deploy/test/install-script.test.sh index d6e28df..56a9050 100755 --- a/deploy/test/install-script.test.sh +++ b/deploy/test/install-script.test.sh @@ -3,12 +3,21 @@ set -euo pipefail # Static guards for regressions seen on a real Proxmox deploy (full run needs Debian 13 as root). dir="$(dirname "${BASH_SOURCE[0]}")/.." path_line='export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' -for script in install.sh update.sh; do - # pct exec omits /usr/local/bin: PATH must be set before the first command that needs node/pnpm. - first_path=$(grep -nxF "$path_line" "$dir/$script" | head -n1 | cut -d: -f1) - first_use=$(grep -nE '\b(node|pnpm|npm|corepack)\b' "$dir/$script" | grep -v '^[0-9]*:#' | head -n1 | cut -d: -f1) - [[ -n $first_path && $first_path -lt $first_use ]] || { echo "$script: PATH not set before node/pnpm use" >&2; exit 1; } -done +# pct exec omits /usr/local/bin: PATH must be set before the first command that needs node/pnpm. +first_path=$(grep -nxF "$path_line" "$dir/install.sh" | head -n1 | cut -d: -f1) +first_use=$(grep -nE '\b(node|pnpm|npm|corepack)\b' "$dir/install.sh" | grep -v '^[0-9]*:#' | head -n1 | cut -d: -f1) +[[ -n $first_path && $first_path -lt $first_use ]] || { echo 'install.sh: PATH not set before node/pnpm use' >&2; exit 1; } # Node tarball must not hand /usr/local to the archive's uid. grep -qE '^tar --no-same-owner .*node_file' "$dir/install.sh" || { echo 'install.sh: tar keeps archive owners' >&2; exit 1; } -printf '3 install script checks passed\n' +# The root-run updater must not execute anything from the service-user-writable checkout. +updater=$(sed -n "/^cat > \/usr\/local\/sbin\/shellbook-update <<'EOF'$/,/^EOF$/p" "$dir/install.sh" | sed '1d;$d') +[[ -n $updater ]] || { echo 'install.sh: shellbook-update not generated' >&2; exit 1; } +bash -n <<< "$updater" || { echo 'shellbook-update: syntax error' >&2; exit 1; } +[[ $updater != */opt/shellbook* ]] || { echo 'shellbook-update: references /opt/shellbook' >&2; exit 1; } +[[ $updater == *"$path_line"* ]] || { echo 'shellbook-update: PATH not set' >&2; exit 1; } +# An existing checkout must be updated, not just rebuilt. +grep -q 'git_app pull --ff-only' "$dir/install.sh" || { echo 'install.sh: existing checkout is not pulled' >&2; exit 1; } +# Nothing may remain in the CT's /root after the Proxmox install. +grep -q 'rm -f /root/shellbook-install.env /root/shellbook-install.sh' "$dir/proxmox/create-ct.sh" \ + || { echo 'create-ct.sh: installer files left in the CT' >&2; exit 1; } +printf '8 install script checks passed\n' diff --git a/deploy/update.sh b/deploy/update.sh deleted file mode 100755 index 55fa927..0000000 --- a/deploy/update.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail -[[ $EUID == 0 ]] || { echo 'Run as root' >&2; exit 1; } -# `pct exec` omits /usr/local/bin, where Node and pnpm live. -export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin -runuser -u shellbook -- bash -c 'cd /opt/shellbook; git pull --ff-only; pnpm install --frozen-lockfile; pnpm build' -systemctl restart shellbook -systemctl --no-pager status shellbook