From 85afd105f7b3edc6ba91d0f2bc375a698488a7bd Mon Sep 17 00:00:00 2001 From: Florian Schmidt Date: Mon, 16 Nov 2026 11:42:12 +0100 Subject: [PATCH] feat(templates): LXC container and chained web server notebooks (#11) infra/lxc-container.md creates an unprivileged container on a Proxmox host with ShellBook's key, waits for its address and adds it to Hosts. services/web-server.md installs nginx on an existing host or on a new container through ::use. Document chaining in the spec (3.6) and in the workspace format guide. --- docs/spec.md | 39 ++++++++ templates/workspace/CLAUDE.md | 27 +++++- .../notebooks/infra/lxc-container.md | 97 +++++++++++++++++++ .../notebooks/services/web-server.md | 50 ++++++++++ 4 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 templates/workspace/notebooks/infra/lxc-container.md create mode 100644 templates/workspace/notebooks/services/web-server.md diff --git a/docs/spec.md b/docs/spec.md index eecf992..26d3b4e 100644 --- a/docs/spec.md +++ b/docs/spec.md @@ -118,12 +118,51 @@ Info string: `sh { key=value key="value with spaces" flag}`. Language `sh` `:::after{step=}` … `:::` — rendered only after that step succeeded in the current session. `:::after{step= failed}` for the failure case. (remark-directive syntax.) +`:::when{param=a,b}` … `:::` — shown, and its steps runnable, only while the `choice` (or +other non-secret) parameter has one of the listed values. The server refuses to run a +step inside an inactive section. Values must be options of a `choice` parameter. + ### 3.5 Artifacts Each `on=local` run gets a fresh workdir under `data/runs//`. Artifacts are kept for 24 h (configurable), then deleted. Private keys are flagged sensitive: one download only, then removed. +### 3.6 Chained notebooks + +A notebook can include another one and continue with what it produced, e.g. a service +notebook that first creates an LXC container: + +```markdown +:::when{target=lxc} +::use{notebook="infra/lxc-container.md" as=ct with="hostname={{server_name}}"} +::: +``` + +- `::use{notebook="dir/name.md" as=alias with="p={{x}}, q=1"}` — path relative to + `notebooks/`, no `..`. The server composes the notebook (core `composeNotebook`): + the included params, step ids, captures and templates are namespaced by the alias + (`ct.hostname`, `ct.create`, `{{ct.address}}`) and shown inline as a collapsible + section with its own parameter group. Nested includes chain aliases (`svc.ct.x`); at + most 4 levels. Missing files, cycles and errors inside the included notebook are + errors on the `::use` line. The UI reloads when an included file changes. +- `with` sets defaults of the included params, written in the including notebook's + namespace. Secrets cannot be bound; they are entered in the form and passed only to + steps of their own notebook, under their local name (`$ROOTPW`). +- Required params gate only steps of their own notebook (scope); a step also needs every + value it references. +- Non-secret defaults may reference captures and builtins (`default: "{{ct.host}}"`). Until + the value exists the field shows "Available after the step that sets ct.host". A + required param that waits for a capture of its own notebook would block the step + producing it; make such params optional. +- `::host{name="{{hostname}}" address="{{address}}" user=root port=22 capture=host}` adds + the machine to Hosts (same validation as the Hosts page; never overwrites a different + host with that name). `capture` stores the host name as a runtime value so later + steps can target it. The first SSH connection still requires host key confirmation. +- Builtins are server-provided runtime values, never namespaced: + `{{shellbook.public_key}}` is ShellBook's SSH public key. The server sets them on every + run; client-sent values are ignored. + ## 4. Execution - Scripts run as `bash -euo pipefail -s` with the script on stdin; no TTY. diff --git a/templates/workspace/CLAUDE.md b/templates/workspace/CLAUDE.md index b5c20c9..8b5d3f4 100644 --- a/templates/workspace/CLAUDE.md +++ b/templates/workspace/CLAUDE.md @@ -111,4 +111,29 @@ add `failed` for failure. Refer to a real step ID. Each local run gets a fresh uploads to transfer earlier results. Artifacts expire after 24 hours by default; private keys are sensitive, downloadable once, then removed. Do not commit them. -See the three notebooks for complete SSH-key, read-only hardening and Debian package examples. +## Choices, chaining and new hosts + +````markdown +:::when{target=lxc} +Shown, and runnable, only while the choice parameter `target` is `lxc` (or `a,b` for several). +::use{notebook="infra/lxc-container.md" as=ct with="hostname={{server_name}}"} +::: + +```sh {run host="{{host}}" id=install} +apt-get install -y nginx +``` +```` + +- `::use` includes another notebook (path relative to `notebooks/`). Its parameters + appear in their own form group as `alias.name`; refer to its captures as + `{{ct.address}}` and to its steps as `ct.`. `with` sets defaults for its + parameters using this notebook's names. Never bind secrets. +- Defaults may reference captures: `host: { type: host, required: true, default: "{{ct.host}}" }`. + Only reference captures of *another* notebook from required parameters; within one + notebook make such parameters optional (e.g. `ctid: { type: name, default: "{{nextid}}" }`). +- `::host{name="{{hostname}}" address="{{address}}" user=root port=22 capture=host}` + offers to add a created machine to Hosts and stores its name in `{{host}}`. +- `{{shellbook.public_key}}` is ShellBook's SSH public key, e.g. for `pct create --ssh-public-keys`. + +See the notebooks for complete SSH-key, read-only hardening, Debian package, LXC +container (`infra/lxc-container.md`) and chained service (`services/web-server.md`) examples. diff --git a/templates/workspace/notebooks/infra/lxc-container.md b/templates/workspace/notebooks/infra/lxc-container.md new file mode 100644 index 0000000..ce0b540 --- /dev/null +++ b/templates/workspace/notebooks/infra/lxc-container.md @@ -0,0 +1,97 @@ +--- +title: Create an LXC container on Proxmox +params: + proxmox: { type: host, required: true, label: Proxmox host } + hostname: { type: name, required: true, label: Container hostname } + ctid: { type: name, default: "{{nextid}}", label: Container ID (empty = next free) } + os: { type: choice, options: [debian-12-standard, debian-13-standard, ubuntu-24.04-standard], default: debian-12-standard, label: Template } + cores: { type: name, default: "2", label: CPU cores } + memory: { type: name, default: "2048", label: Memory (MiB) } + swap: { type: name, default: "512", label: Swap (MiB) } + disk: { type: name, default: "8", label: Disk (GiB) } + storage: { type: name, default: local-lvm, label: Disk storage } + template_storage: { type: name, default: local, label: Template storage } + bridge: { type: name, default: vmbr0, label: Network bridge } + net: { type: string, default: "ip=dhcp", label: "IP config (ip=dhcp or ip=10.0.0.5/24,gw=10.0.0.1)" } +--- + +# Create LXC container {{hostname}} + +Creates an unprivileged container on **{{proxmox}}** (connects as a user that may run +`pct`, usually root), starts it with ShellBook's SSH key for root, and adds it to +Hosts. Other notebooks can include this one with +`::use{notebook="infra/lxc-container.md" as=ct}` and target the new machine as +`{{ct.host}}`. + +```sh {check host="{{proxmox}}" id=pve title="Proxmox tools are available"} +command -v pct +command -v pveam +command -v pvesh +``` + +```sh {check host="{{proxmox}}" id=next-id title="Next free container ID" capture=nextid} +pvesh get /cluster/nextid +``` + +```sh {check host="{{proxmox}}" id=id-free title="Container ID is free"} +if pct status {{ctid}} > /dev/null 2>&1; then + echo "Container {{ctid}} already exists" >&2 + exit 1 +fi +``` + +The template step refreshes the template index and downloads the newest +**{{os}}** template if it is not on **{{template_storage}}** yet. + +```sh {run host="{{proxmox}}" id=template title="Download template" capture=template timeout=900} +pveam update > /dev/null +name=$(pveam available --section system | awk -v prefix={{os}} 'index($2, prefix) == 1 { print $2 }' | sort -V | tail -n 1) +if [ -z "$name" ]; then + echo "No template matching {{os}} is available" >&2 + exit 1 +fi +if ! pveam list {{template_storage}} | grep -qF "$name"; then + pveam download {{template_storage}} "$name" >&2 +fi +printf '%s:vztmpl/%s\n' {{template_storage}} "$name" +``` + +```sh {run host="{{proxmox}}" id=create title="Create and start container" confirm timeout=600} +keys=$(mktemp) +trap 'rm -f "$keys"' EXIT +printf '%s\n' {{shellbook.public_key}} > "$keys" +pct create {{ctid}} {{template}} \ + --hostname {{hostname}} \ + --cores {{cores}} --memory {{memory}} --swap {{swap}} \ + --rootfs {{storage}}:{{disk}} \ + --net0 name=eth0,bridge={{bridge}},{{net}} \ + --unprivileged 1 --features nesting=1 --onboot 1 \ + --ssh-public-keys "$keys" +pct start {{ctid}} +``` + +The next check waits for the container's address and prints its SSH host key +fingerprints (stderr). Compare them with the fingerprint ShellBook shows on the first +connection. + +```sh {check host="{{proxmox}}" id=address title="Wait for the container address" capture=address timeout=120} +ip="" +for _ in $(seq 1 45); do + ip=$(pct exec {{ctid}} -- hostname -I 2> /dev/null | awk '{ print $1 }' || true) + [ -n "$ip" ] && break + sleep 2 +done +if [ -z "$ip" ]; then + echo "Container {{ctid}} has no IP address yet" >&2 + exit 1 +fi +pct exec {{ctid}} -- sh -c 'for key in /etc/ssh/ssh_host_*_key.pub; do ssh-keygen -lf "$key"; done' >&2 || true +printf '%s\n' "$ip" +``` + +::host{name="{{hostname}}" address="{{address}}" user=root port=22 capture=host} + +```sh {check host="{{host}}" id=login title="ShellBook can log in"} +uname -a +head -n 2 /etc/os-release +``` diff --git a/templates/workspace/notebooks/services/web-server.md b/templates/workspace/notebooks/services/web-server.md new file mode 100644 index 0000000..8aa0b26 --- /dev/null +++ b/templates/workspace/notebooks/services/web-server.md @@ -0,0 +1,50 @@ +--- +title: Install a web server (nginx) +params: + target: { type: choice, options: [lxc, existing], default: lxc, label: Install on } + server_name: { type: name, required: true, default: web, label: Server name } + host: { type: host, required: true, default: "{{ct.host}}", label: Target server } +--- + +# Install nginx on {{host}} + +Example of a chained notebook. Choose **Install on: lxc** to create a fresh Proxmox +container first (its form appears below), or **existing** to pick a server from +Hosts. The install steps then target **Target server**, which defaults to the new +container once it is in Hosts. + +:::when{target=lxc} +## New container + +::use{notebook="infra/lxc-container.md" as=ct with="hostname={{server_name}}"} +::: + +## Install + +Runs as root, or through passwordless `sudo -n` for other users. + +```sh {check host="{{host}}" id=apt title="Debian/Ubuntu with apt and systemd"} +command -v apt-get +command -v systemctl +``` + +```sh {check host="{{host}}" id=installed title="nginx is installed"} +dpkg-query -W -f='${Status}\n' nginx | grep -qx 'install ok installed' +``` + +```sh {run host="{{host}}" id=install title="Install and start nginx" confirm timeout=600} +if [ "$(id -u)" -eq 0 ]; then as_root=""; else as_root="sudo -n"; fi +$as_root apt-get update +$as_root env DEBIAN_FRONTEND=noninteractive apt-get install -y nginx +$as_root systemctl enable --now nginx +``` + +```sh {check host="{{host}}" id=active title="nginx is running and answers"} +systemctl is-active nginx +timeout 5 bash -c 'exec 3<>/dev/tcp/127.0.0.1/80' +``` + +:::after{step=install} +nginx is installed on **{{host}}**. Put your site into `/etc/nginx/sites-available/` +and review the firewall before exposing it. +:::