diff --git a/.claude/skills/commit/SKILL.md b/.claude/skills/commit/SKILL.md index c4a74cf..202b755 100644 --- a/.claude/skills/commit/SKILL.md +++ b/.claude/skills/commit/SKILL.md @@ -54,9 +54,9 @@ to the fctl session that made the change is welcome. 1. Branch from an up-to-date `main`: `/` (`feat/postgres`), or `fctl/-` when an fctl session makes the branch. 2. Commit as you go; the commits are squashed, so the title is what lasts. -3. Run `shellcheck provision.sh check.sh check-fctl.sh kernel/build.sh - kernel/check.sh` and `actionlint` if you have them; the Lint workflow - runs both anyway. +3. Run `shellcheck provision.sh check.sh check-fctl.sh examples/check.sh + kernel/build.sh kernel/check.sh` and `actionlint` if you have them; the + Lint workflow runs both anyway. 4. Open the pull request with a conventional title and a description. One change per pull request. 5. Wait for the checks in the same turn and fix failures (below). @@ -88,6 +88,9 @@ gh pr checks N --watch --fail-fast and arm64 and boots it with `kernel/check.sh` where the runner has KVM (amd64's). It runs when `kernel/` or its workflow change, and takes about ten minutes. +- **Examples** builds the images in `examples/` for amd64 and arm64 and + runs `examples/check.sh` in each. It runs when `examples/` or its + workflow change, and takes a few minutes. - **Lint** runs ShellCheck and actionlint. - **Skills** validates every skill in `.claude/skills` against the [Agent Skills specification](https://agentskills.io/specification). It diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml new file mode 100644 index 0000000..faa8711 --- /dev/null +++ b/.github/workflows/examples.yml @@ -0,0 +1,55 @@ +# Builds the images in examples/, which fctl's docs show (Sandbox images, An +# image from another base), for amd64 and arm64, and checks each with +# examples/check.sh, as the user fctl sessions run as. Nothing is published. +name: Examples + +on: + pull_request: + paths: + - examples/** + - .github/workflows/examples.yml + push: + branches: [main] + paths: + - examples/** + - .github/workflows/examples.yml + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: examples-${{ github.ref }} + cancel-in-progress: true + +jobs: + check: + name: ${{ matrix.example }} (${{ matrix.arch }}) + strategy: + fail-fast: false + matrix: + example: [ubuntu] + arch: [amd64, arm64] + include: + - arch: amd64 + runner: ubuntu-24.04 + - arch: arm64 + runner: ubuntu-24.04-arm + runs-on: ${{ matrix.runner }} + timeout-minutes: 20 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Build + env: + EXAMPLE: ${{ matrix.example }} + run: docker build --pull -t "example:$EXAMPLE" "examples/$EXAMPLE" + - name: Check it + env: + EXAMPLE: ${{ matrix.example }} + run: | + docker run --rm -i --entrypoint sh "example:$EXAMPLE" -s < examples/check.sh + # A new volume at /var/cache/fctl, as fctl mounts package caches, + # is the user's without a chown. + cache="fctl-example-cache-$EXAMPLE-$GITHUB_RUN_ID" + docker run --rm --mount "type=volume,source=$cache,target=/var/cache/fctl" "example:$EXAMPLE" touch /var/cache/fctl/written + docker volume rm "$cache" diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 1211bc1..cbf6e29 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -24,7 +24,7 @@ jobs: steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: ShellCheck - run: shellcheck --version && shellcheck provision.sh check.sh check-fctl.sh kernel/build.sh kernel/check.sh + run: shellcheck --version && shellcheck provision.sh check.sh check-fctl.sh examples/check.sh kernel/build.sh kernel/check.sh - name: actionlint run: | archive="actionlint_${ACTIONLINT_VERSION}_linux_amd64.tar.gz" diff --git a/AGENTS.md b/AGENTS.md index 2e976af..670ed46 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -16,8 +16,10 @@ to its code in hyperstructure/fctl. | `provision.sh` | Installs everything, as root. The versions are at the top. | | `check.sh` | Checks a built image for the layout fctl and derived images rely on. | | `check-fctl.sh` | Develops fctl in a built image, as sessions working on hyperstructure/fctl do. | +| `examples/` | Images that don't start from this one, which fctl's docs show (`ubuntu/Dockerfile`), and `check.sh`, which checks any image for what fctl needs of it. | | `.github/workflows/image.yml` | Builds both architectures, checks them and publishes them. | | `.github/workflows/fctl.yml` | On pull requests, runs `check-fctl.sh` in both architectures' builds. | +| `.github/workflows/examples.yml` | Builds the examples for both architectures and checks them with `examples/check.sh`, when they change. | | `kernel/` | The kernel of fctl's Firecracker sandboxes: `build.sh` builds the Linux release in `VERSION` with Firecracker's guest configuration, `check.sh` boots it in Firecracker. | | `.github/workflows/kernel.yml` | Builds the kernel for both architectures, boots it where there is KVM, and publishes it as `ghcr.io/hyperstructure/sandbox-kernel`. | | `.github/workflows/lint.yml`, `commits.yml`, `skills.yml` | ShellCheck and actionlint; pull request titles; the skills, against the Agent Skills specification. | @@ -31,7 +33,7 @@ docker build -t sandbox:dev --build-arg FCTL_VERSION=0.3.0 . # with another fc docker run --rm -i sandbox:dev sh -s < check.sh # check the layout docker run --rm -i --init sandbox:dev bash -s < check-fctl.sh # develop fctl in it kernel/build.sh && kernel/check.sh out/vmlinux # the kernel (Linux; booting needs /dev/kvm) -shellcheck provision.sh check.sh check-fctl.sh kernel/build.sh kernel/check.sh +shellcheck provision.sh check.sh check-fctl.sh examples/check.sh kernel/build.sh kernel/check.sh actionlint # the workflows ``` diff --git a/README.md b/README.md index 62d40ab..7dbb550 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,27 @@ To run it in fctl, name it in a project's Settings → Sandbox → Image, or as ([Sandbox images](https://fctl.sh/docs/sandbox-images/) in fctl's docs). Apple silicon hosts need arm64 images. +## Another base + +An image that doesn't start from this one, such as a company's own on +Ubuntu, runs fctl sessions once it has fctl, Claude Code, git, gh, bash and +a user other than root. It needs no fctl-agent: the host copies its own +into each sandbox when a session starts there. +[`examples/ubuntu/Dockerfile`](examples/ubuntu/Dockerfile) makes Ubuntu +24.04 into such an image, to add your own packages and steps to, and +[`examples/check.sh`](examples/check.sh) checks an image for what fctl +needs, as the user sessions run as: + +```sh +docker build -t my-sandbox examples/ubuntu +docker run --rm -i --entrypoint sh my-sandbox -s < examples/check.sh +``` + +The Examples workflow builds and checks the example for both +architectures when it changes. fctl's docs explain each step, and what to +do on other distributions: +[An image from another base](https://fctl.sh/docs/sandbox-images/#an-image-from-another-base). + ## Building it ```sh diff --git a/examples/check.sh b/examples/check.sh new file mode 100644 index 0000000..3d771e7 --- /dev/null +++ b/examples/check.sh @@ -0,0 +1,91 @@ +#!/bin/sh +# Checks an image that doesn't start from this one for what fctl needs of +# it (fctl's docs: https://fctl.sh/docs/sandbox-images/#what-an-image-needs). +# Runs inside the image as its user, in place of its ENTRYPOINT, as sessions +# do: +# +# docker run --rm -i --entrypoint sh IMAGE -s < examples/check.sh +# +# It fails on what sessions can't do without, warns about the rest, and needs +# the network, to reach GitHub as git does. +set -u + +failed=0 claude='' fctl_version='' +fail() { + echo "check.sh: $*" >&2 + failed=1 +} +warn() { echo "check.sh: warning: $*" >&2; } + +# Whether version $1 (2.1.101) is older than $2. +older() { + # shellcheck disable=SC2046 # split at the dots + set -- $(echo "$1 $2" | tr . ' ') + [ "$1" -lt "$4" ] || { [ "$1" -eq "$4" ] && { [ "$2" -lt "$5" ] || { [ "$2" -eq "$5" ] && [ "$3" -lt "$6" ]; }; }; } +} + +# What the host runs in the sandbox itself, by the system's own directories. +[ -x /bin/sh ] || fail "/bin/sh is missing" +for tool in tail id cat mkdir chown chmod mv rm stat; do + found= + for dir in /usr/sbin /usr/bin /sbin /bin; do + [ ! -x "$dir/$tool" ] || found=yes + done + [ -n "$found" ] || fail "$tool is not in /usr/bin or /bin" +done + +# The user Claude and its commands run as, and its home, from /etc/passwd as +# fctl-agent reads it. +uid=$(id -u) +[ "$uid" != 0 ] || + warn "the image runs as root, so Claude does, and its commands can stop fctl-agent: end the Dockerfile with USER and a user of its own" +home= +while IFS=: read -r _ _ id _ _ dir _; do + if [ "$id" = "$uid" ]; then + home=$dir + break + fi +done /dev/null 2>&1 || fail "$tool is not on the PATH ($PATH)" +done +command -v gh >/dev/null 2>&1 || warn "gh is not on the PATH, so sessions show no pull request" +if command -v bash >/dev/null 2>&1; then + for tool in git claude fctl; do + if command -v "$tool" >/dev/null 2>&1 && ! bash -lc "command -v $tool" >/dev/null 2>&1; then + fail "$tool is not on a login shell's PATH ($(bash -lc 'printf %s "$PATH"'))" + fi + done +fi + +if command -v claude >/dev/null 2>&1; then + # `2.1.302 (Claude Code)` + claude=$(claude --version 2>/dev/null) + claude=${claude%% *} + case $claude in + [0-9]*.[0-9]*.[0-9]*) + ! older "$claude" 2.1.101 || fail "Claude Code is $claude: fctl needs 2.1.101 or later" + ;; + *) fail "claude --version says '$claude'" ;; + esac +fi +if command -v fctl >/dev/null 2>&1; then + fctl_version=$(fctl --version 2>/dev/null) || fail "fctl --version fails: is it the build for this architecture?" +fi + +[ -w /var/cache/fctl ] || + warn "the user can't write to /var/cache/fctl, where fctl mounts the project's package caches, so on hosts that run nothing as root its package managers keep their own" +if command -v git >/dev/null 2>&1 && ! git ls-remote https://github.com/hyperstructure/sandbox HEAD >/dev/null 2>&1; then + fail "git can't reach GitHub over HTTPS: are CA certificates installed?" +fi + +[ "$failed" = 0 ] || exit 1 +echo "The image has what fctl needs, with ${fctl_version:-fctl} and Claude Code $claude, as uid $uid in $home." diff --git a/examples/ubuntu/Dockerfile b/examples/ubuntu/Dockerfile new file mode 100644 index 0000000..a783d2a --- /dev/null +++ b/examples/ubuntu/Dockerfile @@ -0,0 +1,50 @@ +# An image for fctl sessions that doesn't start from fctl's own: Ubuntu with +# what fctl needs, to which you add what your sessions need. fctl's docs +# explain each step: https://fctl.sh/docs/sandbox-images/#an-image-from-another-base +# +# docker build -t my-sandbox examples/ubuntu +# docker run --rm -i --entrypoint sh my-sandbox -s < examples/check.sh +FROM ubuntu:24.04 + +# A download piped into a shell fails the step when it fails. +SHELL ["/bin/bash", "-o", "pipefail", "-c"] +ENV LANG=C.UTF-8 LC_ALL=C.UTF-8 + +# What fctl needs: bash, git, curl and CA certificates (for the installers, +# and git over HTTPS), and the GitHub CLI from GitHub's repository. sudo lets +# the agent install what a task needs: the sandbox is the boundary. Add your +# own packages to the list. +RUN apt-get update \ + && apt-get install -y --no-install-recommends ca-certificates curl git sudo \ + && curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg \ + -o /usr/share/keyrings/githubcli-archive-keyring.gpg \ + && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" \ + > /etc/apt/sources.list.d/github-cli.list \ + && apt-get update \ + && apt-get install -y --no-install-recommends gh \ + && rm -rf /var/lib/apt/lists/* + +# fctl, for every user: a static build, which runs on any Linux. An empty +# FCTL_VERSION installs the latest release. The host puts its own fctl-agent +# into the sandbox each time a session starts, so the archive's goes. +ARG FCTL_VERSION= +RUN curl -fsSL https://fctl.sh/install \ + | env FCTL_VERSION="$FCTL_VERSION" FCTL_INSTALL_DIR=/usr/local/bin FCTL_NO_COMPLETIONS=1 sh \ + && rm -f /usr/local/bin/fctl-agent-*-linux + +# The user sessions run as, with sudo and the package caches' directory. +# Ubuntu's images come with the user ubuntu at uid 1000: agent takes its +# place. +RUN if id ubuntu >/dev/null 2>&1; then userdel --remove ubuntu; fi \ + && useradd --create-home --shell /bin/bash --uid 1000 agent \ + && echo 'agent ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/agent \ + && chmod 0440 /etc/sudoers.d/agent \ + && install -d -o agent -g agent /var/cache/fctl + +# Claude Code, the native build, in ~/.local/bin, as the user. Claude and its +# commands find their tools through the image's PATH, and so do setup +# scripts: Ubuntu's login shells keep it. +USER agent +WORKDIR /home/agent +RUN curl -fsSL https://claude.ai/install.sh | bash +ENV PATH=/home/agent/.local/bin:$PATH