celld/HOMEFLARE.md
| 1 | # celld for Homeflare |
| 2 | |
| 3 | A fork of [denoland/celld](https://github.com/denoland/celld) with the |
| 4 | patches Homeflare's runtimes need. Upstream stopped at v0.6.3 when celld began |
| 5 | merging into workerd, so fixes for gaps Homeflare apps hit land here. |
| 6 | |
| 7 | ## Branches |
| 8 | |
| 9 | `main` is upstream's v0.6.3 plus the patches below, one commit each, so a patch |
| 10 | can be dropped once the merged workerd covers it. Rebase onto upstream when it |
| 11 | moves: |
| 12 | |
| 13 | ```sh |
| 14 | git remote add upstream https://github.com/denoland/celld.git |
| 15 | git fetch upstream && git rebase upstream/main |
| 16 | ``` |
| 17 | |
| 18 | ## Patches |
| 19 | |
| 20 | | Patch | Why | |
| 21 | |---|---| |
| 22 | | `node:net` `isIP` / `isIPv4` / `isIPv6`; `node:dns` and `node:dns/promises` `lookup()` | Fedify checks a URL's host with both before fetching it, so palm's remote lookups failed with `node:net.isIP is not implemented in celld`. `lookup()` uses the node's resolver through a new `__dns_lookup` op, with `connect()`'s egress rules. | |
| 23 | | `Response.clone()` on a streaming body | celld threw `Cannot clone a streaming response before consumption`; workerd tees the body, as celld's `Request.clone()` already did. palm's media proxy returns a fetched image and caches `response.clone()`. | |
| 24 | | `node:url` (all but the legacy `parse` / `resolve`) | celld had no `node:url`; Fedify's WebFinger handler calls `domainToASCII` and `domainToUnicode`. Both run Node's own steps (the input set as a `ws://x` hostname) through new host ops on the `url` crate. | |
| 25 | | `node:process` as the global `process` | The module was the throwing stub while `globalThis.process` existed, so Fedify's User-Agent (`Node.js/${process.versions.node}`, taken when the global has a Node version) threw on every outgoing request. Found by listing palm's bundle imports. | |
| 26 | | `media` binding (Media Transformations) | palm makes video thumbnails with `env.MEDIA_TRANSFORMS.input(stream).transform(...).output({ mode: "frame" })`. The Wrangler `media` key becomes a binding (config → manifest → `WorkerConfig::with_media` → `__makeMediaBinding`) that reads the input and posts it, with both option objects in the `x-media-transform` header, to `CELLD_MEDIA_URL/transform`: Homeflare's `homeflare-media` (ffmpeg). Its answers and `{code, message}` errors (as `MediaError`) are the binding's. Without `CELLD_MEDIA_URL` a result rejects with code 9500. | |
| 27 | | `version_metadata` binding | Upstream celld had none, so Homeflare passed the version ID as a plain string. The binding is now Cloudflare's frozen `{ id, tag, timestamp }`. The config may set all three (Homeflare passes its own version ID, tag and time, so they match its Dashboard and `wrangler versions`); otherwise the id is the deployment's version, the tag is empty and the timestamp is the deploy time. | |
| 28 | | `ratelimits` binding | Diffinery limits sign-ins and API calls with `env.RL_*.limit({ key })`. Each entry becomes a binding counted on the node by a `__ratelimit` op: fixed windows of the period (10 or 60 s), keyed by namespace id and key and shared by the node's isolates, as each Cloudflare location counts on its own. | |
| 29 | | `send_email` binding (Email Sending) | Diffinery sends sign-up codes, invitations and notifications with `env.EMAIL.send({ from, to, subject, html, text })`. The binding checks the message and the binding's `allowed_sender_addresses` / `destination_address` / `allowed_destination_addresses`, then posts it in the REST API's shape (`{address, name}`, `reply_to`, base64 attachments) to `CELLD_EMAIL_URL/send` with `CELLD_EMAIL_TOKEN` as a bearer. Homeflare relays it to Cloudflare's Email Sending API. The result is `{ messageId, delivered, queued, permanent_bounces, suppressed_recipients }`; API errors become the binding's `E_*` codes. The legacy raw `EmailMessage` isn't supported (celld has no `cloudflare:email`). | |
| 30 | | Container images in parts | `celld deploy` read the whole `docker save` output into memory and wrote it in one `PUT`, which has to finish within the bucket client's 30 s; Diffinery's multi-GB runner image timed out against RustFS, failing every redeploy. The save now streams into a multipart upload of 16 MiB parts (`Bucket::put_large`), completed only once the save exits successfully. A node loading the image downloads it in 16 MiB ranges to a temporary file (`Bucket::get_large`) and streams that to the engine's `images/load` (`Docker::post_file`). Test: `CELLD_TEST_S3_ENDPOINT=... cargo test -p celld --lib large_object_round_trip -- --ignored` against any S3 (moto_server works). | |
| 31 | | Fence in another network namespace (`CELLD_FENCE_NETNS`) | Under rootless Podman the fence's `--network host` container can't load nftables rules (`netlink: Operation not permitted`), and the container bridges aren't in the host's namespace anyway: they're in Podman's rootless-netns, which the user owns. With `CELLD_FENCE_NETNS=<path>` the fence joins that namespace (`ns:<path>`) and loads `FENCE_RULES_NETNS`: as `FENCE_RULES`, plus `ct state established,related accept` in forward (the node is another container there, so its replies are forwarded) and port 53 to the bridge gateways (aardvark-dns). Unset keeps upstream's behaviour. | |
| 32 | | Fence exceptions (`CELLD_FENCE_DNAT`, `CELLD_FENCE_ALLOW`) | A container reaching one of the node's own Workers on Cloudflare goes over the Internet. On Homeflare the Worker's hostname resolves to the LAN, which the fence refuses (Diffinery's import agent couldn't report back). `CELLD_FENCE_DNAT=HOST:PORT=HOST:PORT[,...]` rewrites the bridges' new TCP connections to the first endpoint into the second (a nat prerouting chain at -110, ahead of the engine's port forwarding, since a connection is translated once). The rewrite also sets connection mark `0x1000`: Podman (netavark) isolates its networks from one another and drops forwarding from celld's bridge to the edge's network, but accepts connections with that mark ahead of its isolation, as it does for its own port forwarding; the mark stays with the connection, so replies pass. `CELLD_FENCE_ALLOW=HOST:PORT[,...]` accepts the bridges' connections to those endpoints inside the fence's input and forward chains, ahead of the rejects (an accept in a chain of its own wouldn't stop them). A host may be a name; it's resolved to IPv4 each time a container starts, and the fence is installed again when its rules change. Only addresses and numbers reach the rules. Test: `cargo test -p celld --lib fence_rule_tests`. | |
| 33 | | The node's own connections into its containers (`CELLD_FENCE_NODE`) | Podman isolates its networks from one another, so the node (on the engine's network) couldn't reach a port a container serves: Diffinery's sandbox server never answered `startAndWaitForPorts`. `CELLD_FENCE_NODE=HOST` (resolved each container start) marks the node's new connections into `celld0`/`celld1` with netavark's `0x1000`, which it accepts ahead of its isolation; replies keep the mark. The rule is in a table of the node's own (`inet celld_node_<host>`), since the main table is shared by every node on the engine and each replaces it. | |
| 34 | | Container memory budget (`CELLD_CONTAINER_MEMORY_MB`) | A node counted each running container's instance-type cap towards its memory pressure, so four of Diffinery's `standard-4` runners (12 GiB each) on a 60 GiB machine put it over its ceiling: it refused every new cell (`CapacityExhausted`, the forge's own `RepoDO`s too) and evicted live ones, which frees none of a container's memory and stops a container cell's runner. Containers no longer count towards pressure. Instead a start that would take the running containers' caps past the budget fails with `too many containers: ...`, as one over `max_instances` does, and Diffinery queues the job again. The budget is `CELLD_CONTAINER_MEMORY_MB` (`0` for none); unset, it's the node's memory ceiling, where containers used to stop the node. Caps are rarely all used, so a machine with headroom can set it past its memory. | |
| 35 | | Privileged containers (`CELLD_PRIVILEGED_CONTAINER_CLASSES`) | A Cloudflare Container runs with root's capabilities, so Diffinery's runner starts its own Docker daemon for a job's `docker` steps, service containers and builds. celld drops every capability, so the daemon couldn't start. The classes named in `CELLD_PRIVILEGED_CONTAINER_CLASSES=Class[,...]` run privileged instead, on a second engine, `CELLD_PRIVILEGED_CONTAINER_HOST=unix:///path`: another user's rootless Podman, so a container that escapes lands as that user, not as the node's. The daemon's storage (`/var/lib/docker`) is an anonymous volume, removed with the container (every delete passes `v=true`), and the process ceiling is 8192, since it holds the daemon's own containers. The engine's bridges get their own fence, in `CELLD_PRIVILEGED_FENCE_NETNS` (that user's rootless-netns), without the default engine's exceptions. Both engines' containers share the memory budget. | |
| 36 | | `homeflare:` line in `celld --version` | Lists what this build adds (`HOMEFLARE_FEATURES` in `lib.rs`), so Homeflare sends a config key only to a celld that accepts it. Add a feature there with each patch. | |
| 37 | |
| 38 | ## Building |
| 39 | |
| 40 | Homeflare pins a commit of this repository in its `deploy/runtime/Containerfile` |
| 41 | (`ghcr.io/jason-s13r/celld:<first 12 of the commit>`). Homeflare's |
| 42 | Release builds that image from this repository the first time it's pinned and |
| 43 | pushes it to this repository's package (which gives jason-s13r/homeflare write |
| 44 | access under Manage Actions access); |
| 45 | its `install.sh` pulls it, or builds it when it isn't published. |
| 46 | `HOMEFLARE_CELLD_IMAGE` in `homeflare.env` picks another image, and |
| 47 | `HOMEFLARE_CELLD_SOURCE` builds from a git URL or a local checkout (see |
| 48 | Homeflare's README). By hand, with Podman: |
| 49 | |
| 50 | ```sh |
| 51 | podman build -t localhost/homeflare-celld:dev \ |
| 52 | --build-arg CELLD_COMMIT="$(git rev-parse HEAD)" . |
| 53 | ``` |
| 54 | |
| 55 | The Dockerfile is upstream's: the image builds only if `cargo test` and |
| 56 | `cargo clippy -D warnings` pass. `--build-arg CELLD_PROFILE=lab` swaps fat LTO |
| 57 | for thin, which builds much faster. |
| 58 | |
| 59 | ## Smoke tests |
| 60 | |
| 61 | Each folder in `homeflare/smoke/` is a Worker whose `GET /` reports its checks |
| 62 | as JSON; a `paths.txt` beside it adds requests that must answer 200 with the given text. With a built `celld` and `esbuild` on `PATH`: |
| 63 | |
| 64 | ```sh |
| 65 | cargo build -p celld |
| 66 | homeflare/smoke/run.sh # target/debug/celld |
| 67 | ``` |