#!/usr/bin/env bash 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. export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin # shellcheck source=/dev/null source /etc/os-release [[ $ID == debian && $VERSION_ID == 13 ]] || { echo 'Debian 13 required' >&2; exit 1; } export DEBIAN_FRONTEND=noninteractive apt-get update apt-get install -y ca-certificates git curl jq tmux build-essential python3 unattended-upgrades openssh-client xz-utils # Official Node binaries, verified against the SHA-256 manifest over HTTPS. case $(dpkg --print-architecture) in amd64) node_arch=x64 ;; arm64) node_arch=arm64 ;; *) echo 'Unsupported Node binary architecture' >&2; exit 1 ;; esac node_temp=$(mktemp -d) trap 'rm -rf -- "$node_temp"' EXIT curl -fsSL https://nodejs.org/dist/latest-v22.x/SHASUMS256.txt -o "$node_temp/SHASUMS256.txt" node_file=$(awk -v arch="$node_arch" '$2 ~ ("^node-v22\\.[0-9]+\\.[0-9]+-linux-" arch "\\.tar\\.xz$") {print $2}' "$node_temp/SHASUMS256.txt") [[ -n $node_file && $node_file != *$'\n'* ]] || { echo 'No unique Node 22 binary found' >&2; exit 1; } curl -fsSL "https://nodejs.org/dist/latest-v22.x/$node_file" -o "$node_temp/$node_file" awk -v file="$node_file" '$2 == file' "$node_temp/SHASUMS256.txt" > "$node_temp/checksum" (cd "$node_temp"; sha256sum -c checksum) mkdir "$node_temp/unpacked" # Root-owned: tar as root would keep the archive's uid, and cp -a would carry it into /usr/local. tar --no-same-owner -xJf "$node_temp/$node_file" -C "$node_temp/unpacked" --strip-components=1 # Replace the executable rather than truncating one a running service uses. install -m 0755 "$node_temp/unpacked/bin/node" /usr/local/bin/node rm "$node_temp/unpacked/bin/node" cp -a "$node_temp/unpacked/." /usr/local/ rm -rf -- "$node_temp" trap - EXIT [[ $(node --version) == v22.* ]] || { echo 'Node 22 required' >&2; exit 1; } # Pin a current Corepack rather than relying on the binary bundled with Node. npm install -g corepack@0.34.5 corepack enable corepack prepare pnpm@9.15.0 --activate if ! id shellbook >/dev/null 2>&1; then useradd --create-home --shell /bin/bash shellbook; fi install -d -m 0700 -o shellbook -g shellbook /var/lib/shellbook /home/shellbook/.ssh repo=${SHELLBOOK_REPO:-https://git.finalfactory.de/FinalFactory/ShellBook.git} 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 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" if [[ -z $(find "$workspace" -mindepth 1 -maxdepth 1 -print -quit) ]]; then if [[ -d /opt/shellbook/templates/workspace ]]; then cp -a /opt/shellbook/templates/workspace/. "$workspace/"; fi chown -R shellbook:shellbook "$workspace" runuser -u shellbook -- git -C "$workspace" init fi if [[ ! -f /home/shellbook/.ssh/id_ed25519 ]]; then runuser -u shellbook -- ssh-keygen -q -t ed25519 -N '' -f /home/shellbook/.ssh/id_ed25519 -C shellbook fi 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 if [[ ! -f /etc/shellbook/env ]]; then host=${SHELLBOOK_HOST:-0.0.0.0} port=${SHELLBOOK_PORT:-8080} proxies=${SHELLBOOK_TRUSTED_PROXIES:-} [[ $host =~ ^[a-zA-Z0-9.:_-]+$ && $port =~ ^[0-9]+$ && $proxies =~ ^[a-zA-Z0-9.,:/_-]*$ ]] || { echo 'Invalid listen/proxy settings' >&2; exit 1; } cat > /etc/shellbook/env < /etc/systemd/system/shellbook.service <<'EOF' [Unit] Description=ShellBook runbook server After=network-online.target Wants=network-online.target [Service] Type=simple User=shellbook Group=shellbook WorkingDirectory=/opt/shellbook EnvironmentFile=/etc/shellbook/env ExecStart=/usr/local/bin/node /opt/shellbook/apps/server/dist/main.js Restart=on-failure # tmux servers started by the terminal panel must survive service restarts (spec ยง6). KillMode=process RestartSec=5 UMask=0077 NoNewPrivileges=true PrivateTmp=true ProtectSystem=full # Home and /var/lib remain writable for workspace, SSH and Claude/tmux state. # Avoid namespace/device hardening that breaks unprivileged LXC or PTYs. [Install] WantedBy=multi-user.target EOF cat > /etc/apt/apt.conf.d/20auto-upgrades <<'EOF' APT::Periodic::Update-Package-Lists "1"; APT::Periodic::Unattended-Upgrade "1"; EOF if [[ -n ${TS_AUTHKEY:-} ]]; then curl -fsSL https://tailscale.com/install.sh | sh key_file=$(mktemp) trap 'rm -f "$key_file"' EXIT chmod 600 "$key_file" printf '%s' "$TS_AUTHKEY" > "$key_file" tailscale up --auth-key="file:$key_file" rm -f "$key_file" fi systemctl daemon-reload systemctl enable shellbook systemctl restart shellbook cat <<'EOF' Next steps: 1. Set the password (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 cat /home/shellbook/.ssh/id_ed25519.pub echo '4. Configure Traefik, trusted proxy IPs and the LAN/Tailscale firewall; see deploy/README.md.'