diff --git a/deploy/install.sh b/deploy/install.sh index ad24171..4cc7a95 100755 --- a/deploy/install.sh +++ b/deploy/install.sh @@ -59,6 +59,22 @@ if [[ -z $(find "$workspace" -mindepth 1 -maxdepth 1 -print -quit) ]]; then chown -R shellbook:shellbook "$workspace" runuser -u shellbook -- git -C "$workspace" init fi +# Offer each shipped notebook template once: updates bring new templates into existing +# workspaces, but never overwrite files or bring back ones the user deleted. The copy runs +# as the service user, so links inside the workspace cannot redirect a root write. +offer_templates() { # + local file rel + touch "$3" + [[ -d $1/notebooks ]] || return 0 + while IFS= read -r -d '' file; do + rel=${file#"$1"/} + grep -qxF -- "$rel" "$3" && continue + runuser -u shellbook -- bash -c 'test -e "$2" || test -L "$2" || install -D -m 644 "$1" "$2"' _ "$file" "$2/$rel" + printf '%s\n' "$rel" >> "$3" + done < <(find "$1/notebooks" -type f -name '*.md' -print0 | sort -z) +} +install -d -m 0750 /etc/shellbook +offer_templates /opt/shellbook/templates/workspace "$workspace" /etc/shellbook/templates-offered 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 diff --git a/deploy/test/install-script.test.sh b/deploy/test/install-script.test.sh index 56a9050..f14087a 100755 --- a/deploy/test/install-script.test.sh +++ b/deploy/test/install-script.test.sh @@ -20,4 +20,21 @@ grep -q 'git_app pull --ff-only' "$dir/install.sh" || { echo 'install.sh: existi # 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' +# 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; } +) +printf '9 install script checks passed\n'