feat(deploy): bring new notebook templates into existing workspaces (#11)

Each shipped template is offered once (recorded in
/etc/shellbook/templates-offered) and copied as the service user only
when the file does not exist, so updates add new templates without
overwriting files or restoring deleted ones.
This commit is contained in:
Florian Schmidt
2026-11-16 11:42:13 +01:00
parent 85afd105f7
commit 54115289cc
2 changed files with 34 additions and 1 deletions
+16
View File
@@ -59,6 +59,22 @@ if [[ -z $(find "$workspace" -mindepth 1 -maxdepth 1 -print -quit) ]]; then
chown -R shellbook:shellbook "$workspace" chown -R shellbook:shellbook "$workspace"
runuser -u shellbook -- git -C "$workspace" init runuser -u shellbook -- git -C "$workspace" init
fi 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() { # <templates dir> <workspace> <offered list>
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 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 runuser -u shellbook -- ssh-keygen -q -t ed25519 -N '' -f /home/shellbook/.ssh/id_ed25519 -C shellbook
fi fi
+18 -1
View File
@@ -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. # 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" \ 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; } || { 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'