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.
140 lines
6.6 KiB
Markdown
140 lines
6.6 KiB
Markdown
# Writing ShellBook notebooks
|
|
|
|
This repository is the user's workspace. Write runbooks as ordinary Markdown in
|
|
`notebooks/**/*.md`. ShellBook renders these files live as you edit them. This
|
|
workspace is available through ShellBook chat (the real `claude -p` CLI) or a real
|
|
shell in a persistent tmux session. Both use the user's CLI login. Follow this
|
|
format when creating or editing notebooks. Do not add API clients or subscription tokens.
|
|
|
|
## Style and safety
|
|
|
|
- Show literal shell commands, directly in `sh` or `bash` fences. The displayed
|
|
script is exactly what executes. No Python/JavaScript wrappers, generated command
|
|
strings, hidden scripts, or `eval`.
|
|
- One logical action per step. Explain the target, effect and prerequisites in prose.
|
|
- Put checks before runs. Checks are strictly side-effect free: no writes, installs,
|
|
service restarts, uploads or redirection to files. Exit 0 means passed; nonzero fails.
|
|
- Make runs idempotent; test existing state before appending or replacing things.
|
|
- Never put secrets in notebooks, defaults, prose, commands, captures, logs or artifact
|
|
metadata. Never use `set -x` with secrets or print them. A secret parameter is an
|
|
environment variable named exactly as declared; reference `$TOKEN`, never `{{TOKEN}}`.
|
|
- Scripts execute as `bash -euo pipefail -s` without a TTY. Interactive tools belong
|
|
in the terminal. Steps with `sudo` need passwordless `sudo -n`.
|
|
- Never disable SSH host-key checking. Inventory names come from `hosts.yaml`.
|
|
|
|
## Frontmatter and substitution
|
|
|
|
```yaml
|
|
---
|
|
title: Example runbook
|
|
params:
|
|
host: { type: host, required: true, label: Target host }
|
|
user: { type: name, default: admin, label: Login user }
|
|
keyname: { type: name, default: "id_ed25519_{{host}}" }
|
|
destination: { type: path, default: /etc/example.conf }
|
|
comment: { type: string, label: Description }
|
|
mode: { type: choice, options: [safe, full], default: safe }
|
|
TOKEN: { type: secret, required: true, label: Access token }
|
|
---
|
|
```
|
|
|
|
All parameter fields: `type`, `required`, `default`, `label`, `options`. Omitted
|
|
`required` is false. `options` is for `choice`; never set a default for a secret.
|
|
Types: `host` selects an inventory name; `name` allows `[A-Za-z0-9._-]+`;
|
|
`path` allows absolute/relative paths without NUL or newline; `string` is free text;
|
|
`choice` must match an option; `secret` is supplied only through the environment.
|
|
Non-secret defaults may reference other parameters.
|
|
|
|
Use `{{name}}` in prose, attributes and scripts. Script substitution is already
|
|
shell-quoted by ShellBook's core: write `id {{user}}`, not `id '{{user}}'`.
|
|
Do not invent escaping rules or assume templates expand like shell variables.
|
|
Missing required parameters disable execution. In shell strings use ordinary
|
|
shell variables where needed, e.g. `destination={{destination}}` then `"$destination"`.
|
|
|
|
## Step syntax and every attribute
|
|
|
|
````markdown
|
|
```sh {check host="{{host}}" id=user-exists title="User exists" timeout=10}
|
|
id {{user}}
|
|
```
|
|
|
|
```sh {check host="{{host}}" id=os title="Read operating system" capture=os_id}
|
|
. /etc/os-release
|
|
printf '%s\n' "$ID"
|
|
```
|
|
|
|
```sh {run host="{{host}}" id=reload title="Reload SSH configuration" sudo confirm timeout=60}
|
|
sshd -t
|
|
systemctl reload ssh
|
|
```
|
|
````
|
|
|
|
- `run` or `check`: required kind. `sh` and `bash` retain Markdown highlighting.
|
|
- `host="{{host}}"`: remote target. `on=local`: ShellBook service host instead;
|
|
use one target form per step. Local does **not** mean the browser's computer.
|
|
- `id=...`: unique, stable identifier; default is the title's slug. Use explicit
|
|
IDs for references. `title="..."`: human-readable heading.
|
|
- `sudo`: passwordless elevation, both kinds. `confirm`: run only, prompts first;
|
|
remote runs against hosts tagged `prod` already default to confirmation.
|
|
- `timeout=60`: seconds; defaults are 300 for run and 30 for check.
|
|
- `capture=var`: both kinds, stores trimmed stdout as a runtime value for subsequent
|
|
steps. Print only the desired value; use `{{var}}` later. Never capture secrets.
|
|
- `artifacts="a,b"`: run only, comma-separated files relative to that run's fresh
|
|
workdir. Offered after success; remote files are fetched using SFTP.
|
|
- `upload="src:dst"`: run only, copies a prior artifact to a remote destination
|
|
before executing. Use explicit, non-colliding temporary destinations and clean up.
|
|
|
|
## Artifacts and conditional follow-up
|
|
|
|
````markdown
|
|
```sh {run on=local id=keygen title="Generate key pair" artifacts="{{keyname}},{{keyname}}.pub"}
|
|
ssh-keygen -q -t ed25519 -N '' -C {{user}}@{{host}} -f {{keyname}}
|
|
```
|
|
|
|
```sh {run host="{{host}}" id=inspect-key title="Inspect uploaded public key" upload="{{keyname}}.pub:/tmp/shellbook-key.pub"}
|
|
ssh-keygen -lf /tmp/shellbook-key.pub
|
|
rm -f /tmp/shellbook-key.pub
|
|
```
|
|
|
|
:::after{step=keygen}
|
|
Download the private key and store it securely with mode 600.
|
|
:::
|
|
|
|
:::after{step=inspect-key failed}
|
|
Inspect the error before retrying; check that the earlier artifact is still available.
|
|
:::
|
|
````
|
|
|
|
`:::after{step=id}` appears after that step succeeds in the current browser session;
|
|
add `failed` for failure. Refer to a real step ID. Each local run gets a fresh
|
|
`data/runs/<run-id>/` workdir; do not assume files survive between steps. Use artifact
|
|
uploads to transfer earlier results. Artifacts expire after 24 hours by default;
|
|
private keys are sensitive, downloadable once, then removed. Do not commit them.
|
|
|
|
## 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.<id>`. `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.
|