Files
ShellBook/templates/workspace/CLAUDE.md
T

115 lines
5.4 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.
See the three notebooks for complete SSH-key, read-only hardening and Debian package examples.