diff --git a/scripts/smoke.sh b/.github/platform-check.sh similarity index 95% rename from scripts/smoke.sh rename to .github/platform-check.sh index 4094cec..1017122 100755 --- a/scripts/smoke.sh +++ b/.github/platform-check.sh @@ -2,7 +2,7 @@ # Smoke test one recipe against a harbord: upload → deploy → wait for running → # HTTP check of health_path through the node's router (real runtime only) → delete. # -# HARBOR_URL=http://127.0.0.1:7700 HARBOR_TOKEN=hbr_... scripts/smoke.sh apps/vaultwarden +# HARBOR_URL=http://127.0.0.1:7700 HARBOR_TOKEN=hbr_... .github/platform-check.sh apps/vaultwarden # # HEALTH_BASE (optional) is how this machine reaches pod hostnames, e.g. https://%s # where %s is replaced with the pod's domain. Without it the HTTP check is skipped, diff --git a/harbor.ref b/.github/platform.ref similarity index 100% rename from harbor.ref rename to .github/platform.ref diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index 4c0ac50..dfcbaaa 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -1,42 +1,52 @@ -name: validate +name: Check recipes on: [push, pull_request] permissions: { contents: read } jobs: - fake-runtime: + # The same test contributors run locally: every recipe with Docker Compose, until its + # health path answers 200. Runs on every pull request, forks included. + compose: + name: Docker Compose test runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Test every recipe + run: | + for app in apps/*/; do scripts/local-test.sh "$app"; done + + # Cottage's own checks: the recipe is accepted by the platform and runs its full lifecycle. + # The platform isn't public, so this needs the PLATFORM_KEY secret (a read-only deploy key) + # and is skipped for pull requests from forks; a maintainer runs it before merging. + platform: + name: Cottage checks + runs-on: ubuntu-latest + if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: { go-version: "1.26" } - # Harbor is private: a read-only deploy key is needed (secret HARBOR_DEPLOY_KEY). - - name: Build harbord at the pinned commit - env: { HARBOR_DEPLOY_KEY: "${{ secrets.HARBOR_DEPLOY_KEY }}" } + - name: Build the platform at the pinned commit + env: { PLATFORM_KEY: "${{ secrets.HARBOR_DEPLOY_KEY }}" } run: | - if [ -z "$HARBOR_DEPLOY_KEY" ]; then - if [ "${{ github.event.pull_request.head.repo.full_name || github.repository }}" != "${{ github.repository }}" ]; then - echo "::notice::Pull requests from forks can't build private Harbor, so the recipe tests are skipped. A maintainer runs them before merging." - echo "skip=true" >> "$GITHUB_ENV" - exit 0 - fi - echo "::error::Add the HARBOR_DEPLOY_KEY secret (a read-only deploy key on cottage-host/harbor) to run the recipe smoke tests." + if [ -z "$PLATFORM_KEY" ]; then + echo "::error::The HARBOR_DEPLOY_KEY secret is missing." exit 1 fi - mkdir -p ~/.ssh && printf '%s\n' "$HARBOR_DEPLOY_KEY" > ~/.ssh/id_ed25519 && chmod 600 ~/.ssh/id_ed25519 - ssh-keyscan github.com >> ~/.ssh/known_hosts - git clone -q git@github.com:cottage-host/harbor.git /tmp/harbor - git -C /tmp/harbor checkout -q "$(cat harbor.ref)" - (cd /tmp/harbor && go build -o /tmp/harbord ./cmd/harbord) - # harbord runs as root, as on a node: it hands volume directories to the containers' - # non-root users (x-harbor.volumes uid/gid), which needs root or CAP_CHOWN. - # Capacity is fixed so the runner's disk size can't refuse a pod. - - name: Smoke test every recipe - if: env.skip != 'true' + mkdir -p ~/.ssh && printf '%s\n' "$PLATFORM_KEY" > ~/.ssh/id_ed25519 && chmod 600 ~/.ssh/id_ed25519 + ssh-keyscan github.com >> ~/.ssh/known_hosts 2>/dev/null + git clone -q git@github.com:cottage-host/harbor.git /tmp/platform + git -C /tmp/platform checkout -q "$(cat .github/platform.ref)" + (cd /tmp/platform && go build -o /tmp/harbord ./cmd/harbord) + rm -f ~/.ssh/id_ed25519 + # Runs as root, as on a server: it hands volume directories to the containers' + # non-root users, which needs root or CAP_CHOWN. Capacity is fixed so the runner's + # disk size can't refuse a recipe. + - name: Check every recipe run: | export HARBOR_DATA_DIR=$RUNNER_TEMP/h HARBOR_RUNTIME=fake HARBOR_BASE_DOMAIN=pods.test export HARBOR_CAPACITY_CPU_MILLIS=8000 HARBOR_CAPACITY_MEMORY_MB=8192 HARBOR_CAPACITY_STORAGE_MB=65536 - # The runner's sudo ignores -E, so pass Harbor's settings explicitly. + # the runner's sudo ignores -E, so pass the settings explicitly H="HARBOR_DATA_DIR=$HARBOR_DATA_DIR HARBOR_RUNTIME=$HARBOR_RUNTIME HARBOR_BASE_DOMAIN=$HARBOR_BASE_DOMAIN HARBOR_CAPACITY_CPU_MILLIS=$HARBOR_CAPACITY_CPU_MILLIS HARBOR_CAPACITY_MEMORY_MB=$HARBOR_CAPACITY_MEMORY_MB HARBOR_CAPACITY_STORAGE_MB=$HARBOR_CAPACITY_STORAGE_MB" export HARBOR_TOKEN=$(sudo env $H /tmp/harbord token create ci | grep -o 'hbr_[A-Za-z0-9_-]*') sudo env $H /tmp/harbord serve & sleep 2 export HARBOR_URL=http://127.0.0.1:7700 - for t in apps/*/tests/smoke.sh; do sh "$t"; done + for app in apps/*/; do sh .github/platform-check.sh "$app"; done diff --git a/README.md b/README.md index fe5b96d..1e72f68 100644 --- a/README.md +++ b/README.md @@ -1,57 +1,77 @@ # Cottage catalog The recipes behind every app on [Cottage](https://cottage.host), managed hosting for open-source -apps. Each recipe is a Harbor template (Docker Compose plus a few Cottage settings) and a listing. -Cottage's control plane syncs this repo and pushes the templates to the servers that run your apps. +apps. A recipe is a Docker Compose file with a few Cottage settings, plus the listing people see +when they choose an app. Maintainers are welcome here: if you make one of these apps, you can check how we run it, suggest -changes, or co-maintain its recipe. See Cottage's maintainer program for how revenue is shared. +changes, or co-maintain its recipe. Cottage shares the revenue from each app with the people who +make it; see the maintainer program on cottage.host. ``` apps// - template.yml Harbor template: Compose + x-harbor (resources, parameters, health path) - cottage.yml listing: name, tagline, category, upstream, license, image digests - README.md first-run notes shown on the app's Overview tab + template.yml Docker Compose file, with Cottage's settings in the x-harbor block + cottage.yml listing: name, tagline, category, upstream, licence, image digests + README.md first-run notes shown on the app's page in the dashboard icon.svg 64×64 icon, no scripts or event handlers - tests/smoke.sh deploy → running → HTTP 200 on health_path → delete schema/cottage.schema.json +scripts/local-test.sh ``` -## Rules +## Writing a recipe -- **Slug** = directory name = `x-harbor.slug`, lowercase with hyphens, matching the upstream project. -- **Version** = `-c`, e.g. `1.34.3-c1`. Bump `-c` when only the - recipe changes. A published version is never edited: Harbor and Hearth both refuse a changed - document under an existing version. -- Images come from the official upstream, pinned by tag in `template.yml`, with the multi-arch - digest recorded in `cottage.yml`. -- Containers run as a non-root user; `x-harbor.volumes` hands the volume to that uid/gid. +`template.yml` is an ordinary Compose file. Its `x-harbor` block (Compose ignores `x-` keys) tells +Cottage how to run it: + +| Key | Meaning | +| --- | --- | +| `slug`, `name`, `description` | `slug` matches the directory name and the upstream project, lowercase with hyphens | +| `version` | `-c`, e.g. `1.34.3-c1` | +| `primary` | the service and port that serve the app over HTTPS, e.g. `{ service: app, port: 8080 }` | +| `health_path` | a path that answers 200 once the app is ready | +| `default_resources`, `min_resources` | `cpu_millis`, `memory_mb`, `storage_mb` | +| `volumes` | named volumes to hand to a non-root user: `data: { uid: 1000, gid: 1000, mode: "0750" }` | +| `parameters` | settings people can see or change; secrets are generated (`generate: random32`, `random16` or `hex32`) | + +Cottage provides `${HARBOR_PUBLIC_URL}` (the app's https:// address), `${HARBOR_HOSTNAME}`, +`${HARBOR_SLUG}` and `${HARBOR_POD_ID}`, and every parameter as a variable of the same name. + +Rules (CI checks them): + +- Images from the official upstream, pinned by tag in `template.yml`, with the multi-arch digest + recorded in `cottage.yml`. +- Named volumes only. No host paths, published `ports`, `privileged`, `network_mode`, `cap_add`, + or `deploy`/CPU/memory keys: Cottage sets the limits, the routing and the security options. +- Containers run as a non-root user; list their volumes under `x-harbor.volumes` with that uid/gid. - First-run admin credentials are generated secret parameters, never defaults. If the app can only - create its admin on first visit, set `first_visit_claims_admin: true` so the dashboard tells the - owner to finish setup straight away. -- Mail ports (25/465/587) are blocked by Harbor's egress firewall. Don't rely on SMTP. + create its admin on first visit, set `first_visit_claims_admin: true` in `cottage.yml`, so the + dashboard tells the owner to finish setup straight away. +- Outgoing mail ports (25, 465, 587) are blocked. Don't rely on SMTP. +- A published version is never edited, not even a comment. Change anything in a recipe and bump + the `-c` revision. - Copy follows the Cottage voice: plain, warm, specific, sentence case, no exclamation marks. ## Testing a recipe -Each recipe has a smoke test (`apps//tests/smoke.sh`): deploy, wait for it to run, check the -health path, delete. CI runs every test against Harbor's fake runtime on each push and pull request. - -Harbor isn't public yet, so CI needs a key to build it. Pull requests from forks can't use that -key: their recipe tests are skipped, and we run them before merging. +Run it locally with Docker Compose before opening a pull request: ```sh -# With a Harbor node to test against (URL and API token): -HARBOR_URL=http://127.0.0.1:7700 HARBOR_TOKEN=... apps/vaultwarden/tests/smoke.sh - -# Against a real Docker node, also check the health path over HTTPS: -HEALTH_BASE='https://%s' HARBOR_URL=... HARBOR_TOKEN=... apps/vaultwarden/tests/smoke.sh +scripts/local-test.sh apps/vaultwarden +KEEP=1 scripts/local-test.sh apps/vaultwarden # leave it running so you can try it in a browser ``` +The script starts the recipe close to the way Cottage runs it: parameters filled in, volumes owned +by the right user, the default memory and CPU limits, no-new-privileges, and the app on +`127.0.0.1` only. It passes when `health_path` answers 200, then removes everything it created. It +needs Docker with Compose v2, `python3` and `curl`. + +Every pull request runs the same test, plus Cottage's own checks of the recipe. Make sure both +pass; we can't merge a recipe until they do. Pull requests from forks get the Compose test, and we +run Cottage's checks before merging. + ## Contributing -Open an issue or a pull request. Keep a recipe's published version unchanged: bump the `-c` -revision instead. Please don't include secrets, real credentials or personal data. +Open an issue or a pull request. Please don't include secrets, real credentials or personal data. ## Licence diff --git a/apps/actual-budget/tests/smoke.sh b/apps/actual-budget/tests/smoke.sh deleted file mode 100755 index 1855d7b..0000000 --- a/apps/actual-budget/tests/smoke.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -# Deploy, wait for running, check the health path, delete. See scripts/smoke.sh. -exec "$(dirname "$0")/../../../scripts/smoke.sh" "$(dirname "$0")/.." diff --git a/apps/uptime-kuma/tests/smoke.sh b/apps/uptime-kuma/tests/smoke.sh deleted file mode 100755 index 1855d7b..0000000 --- a/apps/uptime-kuma/tests/smoke.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -# Deploy, wait for running, check the health path, delete. See scripts/smoke.sh. -exec "$(dirname "$0")/../../../scripts/smoke.sh" "$(dirname "$0")/.." diff --git a/apps/vaultwarden/tests/smoke.sh b/apps/vaultwarden/tests/smoke.sh deleted file mode 100755 index 1855d7b..0000000 --- a/apps/vaultwarden/tests/smoke.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -# Deploy, wait for running, check the health path, delete. See scripts/smoke.sh. -exec "$(dirname "$0")/../../../scripts/smoke.sh" "$(dirname "$0")/.." diff --git a/scripts/local-test-plan.py b/scripts/local-test-plan.py new file mode 100644 index 0000000..ea6cd36 --- /dev/null +++ b/scripts/local-test-plan.py @@ -0,0 +1,71 @@ +"""Helper for local-test.sh: reads the recipe's Compose config (as JSON) and writes +an env file, a Compose override and plan.sh. Standard library only.""" + +import json +import os +import secrets +import shlex +import socket +import sys + +slug, work = sys.argv[1], sys.argv[2] +with open(os.path.join(work, "config.json")) as f: + cfg = json.load(f) +x = cfg.get("x-harbor") or sys.exit(f"{slug}: template.yml has no x-harbor block") + +primary = x["primary"] +health = x.get("health_path", "/") +res = x.get("default_resources", {}) +owned = x.get("volumes") or {} + +with socket.socket() as s: # a free port on this machine + s.bind(("127.0.0.1", 0)) + port = s.getsockname()[1] + +GEN = { + "random32": lambda: secrets.token_urlsafe(24)[:32], + "random16": lambda: secrets.token_urlsafe(12)[:16], + "hex32": lambda: secrets.token_hex(16), +} +env = { + # what Cottage's servers provide to every app + "HARBOR_POD_ID": "local-test", + "HARBOR_SLUG": slug, + "HARBOR_HOSTNAME": "localhost", + "HARBOR_PUBLIC_URL": f"http://127.0.0.1:{port}", +} +for p in x.get("parameters") or []: + if p.get("generate"): + env[p["key"]] = GEN[p["generate"]]() + else: + env[p["key"]] = str(p.get("default", "")) +with open(os.path.join(work, "env"), "w") as f: + for k, v in env.items(): + f.write(f"{k}={json.dumps(v)}\n") + +override = {"services": {}} +for name, svc in cfg["services"].items(): + o = {"security_opt": ["no-new-privileges:true"]} + if name == primary["service"]: + o["ports"] = [f"127.0.0.1:{port}:{primary['port']}"] + if res.get("memory_mb"): + o["mem_limit"] = f"{res['memory_mb']}m" + if res.get("cpu_millis"): + o["cpus"] = res["cpu_millis"] / 1000 + mounts = [] + for m in svc.get("volumes") or []: + if m.get("type") == "volume" and m.get("source") in owned: + # same target, so Compose replaces the recipe's mount with this one + mounts.append({"type": "volume", "source": m["source"], "target": m["target"], + "read_only": m.get("read_only", False), "volume": {"nocopy": True}}) + if mounts: + o["volumes"] = mounts + override["services"][name] = o +with open(os.path.join(work, "override.json"), "w") as f: + json.dump(override, f) + +parts = [] +for name, o in owned.items(): + parts.append(f"{name}:{o.get('uid', 0)}:{o.get('gid', 0)}:{o.get('mode', '0755')}") +with open(os.path.join(work, "plan.sh"), "w") as f: + f.write(f"port={port}\nhealth={shlex.quote(health)}\nowned={shlex.quote(' '.join(parts))}\n") diff --git a/scripts/local-test.sh b/scripts/local-test.sh new file mode 100755 index 0000000..4eab930 --- /dev/null +++ b/scripts/local-test.sh @@ -0,0 +1,59 @@ +#!/bin/sh +# Test a recipe locally with Docker Compose, close to the way Cottage runs it: +# - parameters are filled in (generated secrets, defaults) +# - volumes listed under x-harbor.volumes belong to the given uid/gid, with Docker's +# copy-up turned off, as on Cottage's servers +# - the default memory and CPU limits apply, with no-new-privileges +# - the app is published on 127.0.0.1 only +# It waits for health_path to answer 200, then removes the containers and volumes. +# +# scripts/local-test.sh apps/vaultwarden +# KEEP=1 scripts/local-test.sh apps/vaultwarden # leave it running to try it in a browser +# +# Needs Docker with Compose v2, python3 and curl. +set -eu +dir=$(cd "${1:?usage: scripts/local-test.sh apps/}" && pwd) +slug=$(basename "$dir") +here=$(cd "$(dirname "$0")" && pwd) +work=$(mktemp -d) +project="cottage-test-$slug" +compose() { docker compose -p "$project" -f "$dir/template.yml" -f "$work/override.json" --env-file "$work/env" "$@"; } + +cleanup() { + if [ "${KEEP:-}" = 1 ] && [ "${passed:-}" = 1 ]; then + echo "left running: docker compose -p $project down -v # when you're done" + else + compose down -v --remove-orphans >/dev/null 2>&1 || true + fi + rm -rf "$work" +} +trap cleanup EXIT INT TERM + +docker compose -f "$dir/template.yml" config --format json --no-interpolate > "$work/config.json" +python3 -I "$here/local-test-plan.py" "$slug" "$work" +# plan.sh sets: port, health, owned (space-separated "volume:uid:gid:mode") +. "$work/plan.sh" + +echo "$slug: starting on http://127.0.0.1:$port" +compose create --quiet-pull >/dev/null +for v in $owned; do + name=${v%%:*}; rest=${v#*:}; uid=${rest%%:*}; rest=${rest#*:}; gid=${rest%%:*}; mode=${rest#*:} + docker run --rm -v "${project}_$name:/v" busybox:1.37 sh -c "chown $uid:$gid /v && chmod $mode /v" +done +compose start >/dev/null + +url="http://127.0.0.1:$port$health" +code=000 +for _ in $(seq 1 90); do + code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 "$url" || true) + [ "$code" = 200 ] && break + if compose ps --status exited --status dead -q | grep -q .; then break; fi + sleep 2 +done +if [ "$code" != 200 ]; then + echo "$slug: $url answered $code, expected 200. Recent logs:" + compose logs --tail 40 || true + exit 1 +fi +passed=1 +echo "$slug: ok ($url answered 200)"