#!/usr/bin/env bash 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' # 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; } # 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; } # Templates: new ones reach existing workspaces once; existing and deleted files stay as they are. offer=$(sed -n '/^offer_templates() {/,/^}$/p' "$dir/install.sh") [[ -n $offer ]] || { echo 'install.sh: offer_templates missing' >&2; exit 1; } tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT ( runuser() { shift 3; "$@"; } # stub: run as the current user eval "$offer" mkdir -p "$tmp/t/notebooks/infra" "$tmp/w/notebooks" printf 'new\n' > "$tmp/t/notebooks/infra/lxc.md"; printf 'shipped\n' > "$tmp/t/notebooks/a.md" printf 'mine\n' > "$tmp/w/notebooks/a.md" offer_templates "$tmp/t" "$tmp/w" "$tmp/offered" [[ $(cat "$tmp/w/notebooks/a.md") == mine && $(cat "$tmp/w/notebooks/infra/lxc.md") == new ]] || { echo 'offer_templates: wrong copy' >&2; exit 1; } rm "$tmp/w/notebooks/infra/lxc.md" offer_templates "$tmp/t" "$tmp/w" "$tmp/offered" [[ ! -e $tmp/w/notebooks/infra/lxc.md ]] || { echo 'offer_templates: deleted template came back' >&2; exit 1; } [[ $(sort "$tmp/offered" | tr '\n' ' ') == 'notebooks/a.md notebooks/infra/lxc.md ' ]] || { echo 'offer_templates: wrong list' >&2; exit 1; } ) # Updates from the web UI: the service only touches a request file; root runs the fixed updater. unit_body() { sed -n "/^cat > \/etc\/systemd\/system\/$1 <<'\{0,1\}EOF'\{0,1\}$/,/^EOF$/p" "$dir/install.sh" | sed '1d;$d'; } update_service=$(unit_body shellbook-update.service) update_path=$(unit_body shellbook-update.path) [[ -n $update_service && -n $update_path ]] || { echo 'install.sh: update units not generated' >&2; exit 1; } grep -qxF 'ExecStart=/usr/local/sbin/shellbook-update' <<< "$update_service" || { echo 'update unit: wrong ExecStart' >&2; exit 1; } grep -qxF 'Type=oneshot' <<< "$update_service" || { echo 'update unit: not oneshot' >&2; exit 1; } [[ $update_service$update_path != */opt/shellbook* ]] || { echo 'update units: reference /opt/shellbook' >&2; exit 1; } # Root writes its output only below root-owned /var/log, never into the service user's data dir. grep -qxF 'StandardOutput=truncate:/var/log/shellbook-update.log' <<< "$update_service" || { echo 'update unit: log not in /var/log' >&2; exit 1; } grep -qxF 'PathChanged=$data_dir/update-request' <<< "$update_path" || { echo 'update path unit: wrong request file' >&2; exit 1; } grep -qxF 'Unit=shellbook-update.service' <<< "$update_path" || { echo 'update path unit: wrong unit' >&2; exit 1; } grep -qxF 'systemctl enable --now shellbook-update.path' "$dir/install.sh" || { echo 'install.sh: update trigger not enabled' >&2; exit 1; } printf '17 install script checks passed\n'