~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

The host and its console build as one image, run by a compose file

Merged chadtmiller.com asked to merge into main from chadtmiller.com/group-host:deploy/image
crates/group-host/src/lib.rs+12−2
@@ -559,9 +559,13 @@ fn token_routes() -> Router<Arc<Host>> {
.layer(axum::middleware::from_fn(oauth::cors))
}
/// The commit the host was built from: `GROUP_HOST_COMMIT` as it was
/// built, which the Dockerfile sets; none in a build of a checkout.
const COMMIT: Option<&str> = option_env!("GROUP_HOST_COMMIT");
/// That the host is up, and which build it is.
async fn health() -> Json<Value> {
Json(json!({ "version": env!("CARGO_PKG_VERSION") }))
Json(json!({ "version": env!("CARGO_PKG_VERSION"), "commit": COMMIT }))
}
async fn not_implemented(Path(nsid): Path<String>) -> XrpcError {
@@ -605,7 +609,13 @@ pub async fn run(args: &[String], config: Config) -> anyhow::Result<()> {
pub async fn serve(config: Config) -> anyhow::Result<()> {
let listener = TcpListener::bind(("0.0.0.0", config.port)).await?;
println!("listening on {}", listener.local_addr()?);
tracing::info!(public_url = %config.public_url, did = %config.service_did, "serving");
tracing::info!(
public_url = %config.public_url,
did = %config.service_did,
version = env!("CARGO_PKG_VERSION"),
commit = COMMIT,
"serving"
);
let host = Arc::new(Host::open(config)?);
// Uploads no record took, moves kept a day, and invites lapsed, swept
// now and each hour after.
crates/group-host/tests/serve.rs+5−0
@@ -48,6 +48,11 @@ async fn answers_health_and_refuses_unknown_methods() {
.await
.unwrap();
assert_eq!(health["version"], env!("CARGO_PKG_VERSION"));
// The commit the image was built from (Dockerfile); none otherwise.
assert_eq!(
health["commit"],
serde_json::json!(option_env!("GROUP_HOST_COMMIT"))
);
for req in [
http.get(format!("{}/xrpc/fyi.opensocial.noSuchQuery", host.1)),
deploy/docker-compose.ymlnew+104−0
@@ -0,0 +1,104 @@
# The host and its console, from one image (../Dockerfile), beside the
# .env it reads: docs/deploying.md says what goes in it.
#
# TLS is left to a reverse proxy in front: it joins the network named by
# GROUP_HOST_PROXY_NETWORK and sends the host's paths to group-host:2600
# and the rest to group-host-console:2601.
#
# Neither sets a container_name, so a rollout can start a new console
# beside the old. The host never runs twice over one DATA_DIR: its
# database is the groups' own, so it is stopped, then started.
name: group-host
# Each container's output, the host's lines JSON, kept in the machine's
# journal, which outlives the container.
x-logging: &logging
driver: journald
options:
labels: com.docker.compose.project,com.docker.compose.service
tag: "{{.Name}}"
services:
host:
image: group-host:current
restart: always
logging: *logging
command: ["group-host"]
environment:
PORT: "2600"
PUBLIC_URL: https://${GROUP_HOST_DOMAIN:?set GROUP_HOST_DOMAIN in .env}
# Groups' handles are made under it, with its leading dot: the
# default, .test, is not for a deployed host.
HANDLE_DOMAIN: ${GROUP_HOST_HANDLE_DOMAIN:?set GROUP_HOST_HANDLE_DOMAIN in .env}
DATA_DIR: /data
# Empty, each takes the host's default (docs/reference/config.md).
PLC_URL: ${GROUP_HOST_PLC_URL:-https://plc.directory}
GROUP_FOUNDERS: ${GROUP_HOST_FOUNDERS:-}
PROVISIONING_CLIENTS: ${GROUP_HOST_PROVISIONING_CLIENTS:-}
PDS_CRAWLERS: ${GROUP_HOST_CRAWLERS:-}
BLOB_UPLOAD_LIMIT: ${GROUP_HOST_BLOB_UPLOAD_LIMIT:-52428800}
RUST_LOG: ${GROUP_HOST_LOG:-info}
volumes:
# The database, blobs and keys: back it up (docs/deploying.md).
# Owned by uid 1000, the node user the image runs as.
- ${GROUP_HOST_DATA:-/var/lib/group-host}:/data
networks:
default: {}
proxy:
aliases: [group-host]
healthcheck:
test: ["CMD", "node", "-e", "fetch('http://127.0.0.1:2600/xrpc/_health').then(r => process.exit(r.ok ? 0 : 1), () => process.exit(1))"]
interval: 30s
timeout: 5s
retries: 3
start_period: 60s
start_interval: 1s
# Stopped, it answers the requests under way first.
stop_grace_period: 30s
deploy:
resources:
limits:
cpus: "2"
memory: 2g
console:
image: group-host:current
restart: always
logging: *logging
command: ["node", "/app/build"]
environment:
HOST: 0.0.0.0
PORT: "2601"
GROUP_HOST: http://host:2600
ORIGIN: https://${GROUP_HOST_DOMAIN:?set GROUP_HOST_DOMAIN in .env}
# At least the host's BLOB_UPLOAD_LIMIT, or a larger upload is
# refused before the host sees it.
BODY_SIZE_LIMIT: ${GROUP_HOST_BLOB_UPLOAD_LIMIT:-52428800}
networks:
default: {}
proxy:
aliases: [group-host-console]
depends_on:
- host
# A file the build writes, answered without asking the host.
healthcheck:
test: ["CMD", "node", "-e", "fetch('http://127.0.0.1:2601/_app/version.json').then(r => process.exit(r.ok ? 0 : 1), () => process.exit(1))"]
interval: 30s
timeout: 5s
retries: 3
start_period: 60s
start_interval: 1s
# Stopped, it finishes the requests it has, for up to adapter-node's
# SHUTDOWN_TIMEOUT (30s).
stop_grace_period: 35s
deploy:
resources:
limits:
cpus: "1"
memory: 512m
networks:
proxy:
name: ${GROUP_HOST_PROXY_NETWORK:-group-host}
external: true
docs/design/README.md+1−0
@@ -47,6 +47,7 @@ accounts on their own PDSes, anywhere on the network.
| `crates/group-host-mcp` | the MCP tools, each the twin of an `opensocial` command, and what each call comes to, carried out by the host as the member who connected an MCP client and by the command as the person running it |
| `crates/opensocial` | a client for group hosts, and the `opensocial` command ([commands](../reference/cli.md)) |
| `web/` | the console, a SvelteKit app where people start and run groups and approve apps |
| `Dockerfile`, `deploy/` | one image of the host, the console and the command, and the compose file that runs it ([deploying](../deploying.md)) |
| `lexicons/` | the schemas of the records and methods: every `fyi.opensocial.*` method's parameters and input are checked against its own before its handler runs, and a record of our own collections an app writes as a group before it is kept |
## The pages
docs/reference/api.md+1−1
@@ -252,7 +252,7 @@ repos as any PDS does:
| `GET com.atproto.identity.resolveHandle` | a handle here, to its DID; for a caller signed in, any handle, through `HANDLE_RESOLVERS` |
| `GET /.well-known/did.json` | the host's own DID document, when `SERVICE_DID` is a `did:web`: its `#atproto_pds` and `#opensocial` services at `PUBLIC_URL` |
| `GET /.well-known/atproto-did` | the DID of the group whose address the request's host names |
| `GET /xrpc/_health` | that the host is up, and its version |
| `GET /xrpc/_health` | that the host is up: its `version`, and the `commit` its image was built from (`null` in a build of a checkout) |
A switched-off group's public repo answers `RepoDeactivated`, and
`listRepos` shows it `active: false`.
docs/deploying.mdnew+175−0
@@ -0,0 +1,175 @@
# Deploying the host
A group host is a server that other servers on the network must reach. Members' PDSes, relays and apps fetch from it at its own domain, and at every group's handle, over https. It holds keys that sign each group's identity, and those keys cannot be replaced once lost. So running it takes more than starting a container: a domain and a wildcard name, a proxy that routes by path, an upgrade that never runs two hosts at once, and backups that keep the keys safe.
This page walks through each of these on one machine with Docker and a reverse proxy. Every variable named here is described in [the configuration reference](reference/config.md). What the host is, and what is still to come, is in [the design overview](design/README.md).
## What runs
A group is an atproto account of its own, with no password and no one person behind it. This host is each group's PDS, the authority for its private spaces, and its OAuth server. Members keep their own accounts on their own PDSes.
One image, `group-host:current`, carries three programs: the host, `group-host`, a Rust server over SQLite; the console, a SvelteKit app where people start and run groups, built into `/app/build`; and the command, `opensocial`. The host and the console run as two containers from that image, behind one domain.
## Building the image
Build from the repository root, naming the commit being built:
```sh
docker build --build-arg GROUP_HOST_COMMIT=$(git rev-parse HEAD) -t group-host:current .
```
The commit is compiled into the host, so a running host can say which build it is. Without the argument, the host reports its commit as `null`. The console is built from `web/` with `lexicons/` beside it, since its types are made from the lexicons.
Details:
- The Rust stage uses `rust:1.98-bookworm`, the toolchain CI tests with, and builds with `--locked` in the `deploy` profile: `release` without LTO and with 16 codegen units, so a rebuild is quick and uses every core. The cargo registry and `target/` are cache mounts, so a second build on the same machine rebuilds only the crates that changed.
- The console stage and the final image use `node:24.22.1-bookworm-slim`, the version `.node-version` names. The console stage runs `npm ci` and `npm run build`. adapter-node bundles the console's dependencies, so no `node_modules` is copied.
- The final image adds `ca-certificates` and `tini`, which runs as PID 1. It runs as uid 1000, with `DATA_DIR=/data`, `PORT=2600`, and `group-host` as its default command.
## Running it with compose
`deploy/docker-compose.yml` runs the host and the console. Copy it onto the machine, and put a `.env` beside it. The compose project is named `group-host`.
The two services are `host`, running `group-host` on port 2600, and `console`, running `node /app/build` on port 2601. Neither publishes a port. A reverse proxy in front terminates TLS and reaches them over a docker network it shares with them. That network is external to the compose file, so create it first, and attach the proxy to it:
```sh
docker network create group-host
```
On that network the host answers as `group-host` and the console as `group-host-console`. The console reaches the host at `http://host:2600`, over the project's own network.
A `.env` for a host at `group.example.com`, whose groups take handles under `groups.example.com`:
```sh
GROUP_HOST_DOMAIN=group.example.com
GROUP_HOST_HANDLE_DOMAIN=.groups.example.com
GROUP_HOST_CRAWLERS=https://bsky.network
GROUP_HOST_DATA=/var/lib/group-host
```
Compose reads these and passes each to the host under the name the host knows:
| `.env` | Sets | Unset |
| --- | --- | --- |
| `GROUP_HOST_DOMAIN` | host `PUBLIC_URL` as `https://<domain>`, console `ORIGIN` the same | required |
| `GROUP_HOST_HANDLE_DOMAIN` | `HANDLE_DOMAIN`, with its leading dot | required |
| `GROUP_HOST_PLC_URL` | `PLC_URL` | `https://plc.directory` |
| `GROUP_HOST_FOUNDERS` | `GROUP_FOUNDERS` | anyone may start a group |
| `GROUP_HOST_PROVISIONING_CLIENTS` | `PROVISIONING_CLIENTS` | none |
| `GROUP_HOST_CRAWLERS` | `PDS_CRAWLERS` | none |
| `GROUP_HOST_BLOB_UPLOAD_LIMIT` | host `BLOB_UPLOAD_LIMIT` and console `BODY_SIZE_LIMIT` | `52428800` |
| `GROUP_HOST_LOG` | `RUST_LOG` | `info` |
| `GROUP_HOST_DATA` | the directory mounted at `/data` | `/var/lib/group-host` |
| `GROUP_HOST_PROXY_NETWORK` | the proxy's docker network | `group-host` |
The host's own default handle domain, `.test`, is not for a deployed host, so compose refuses to start without `GROUP_HOST_HANDLE_DOMAIN`. One variable sets both upload limits, so an upload the host would take is never refused by the console first. An empty list is the same as an unset one.
The data directory must exist and be owned by uid 1000 before the first start. Then:
```sh
sudo install -d -o 1000 -g 1000 /var/lib/group-host
docker compose up -d
```
## Routing through the proxy
The console passes the host's paths on to it: `/console/`, `/xrpc/`, `/oauth/`, `/.well-known/` and `/img/`. It does so with `fetch`, which carries no websocket. Relays read a PDS's repos through `com.atproto.sync.subscribeRepos`, a websocket, so it must reach the host directly. The proxy therefore splits by path. It sends `/xrpc/*`, `/oauth/*`, `/.well-known/*` and `/img/*` to `group-host:2600`, and everything else to `group-host-console:2601`. The console's pages and `/console/*` work through the console.
Both containers sit behind the one domain. The host takes the console's address to be its own (`CONSOLE_URL` defaults to `PUBLIC_URL`), and this layout makes that true. PDSes and relays fetch the host's DID document at `/.well-known/did.json` and its OAuth metadata from that origin. The host's own DID is `did:web:<domain>` unless `SERVICE_DID` says otherwise.
## Giving groups their handles
A group's handle is its name followed by the handle domain, such as `hikers.groups.example.com`. Anyone resolving the handle asks `https://hikers.groups.example.com/.well-known/atproto-did`. The host answers with the DID of the group that the request's `Host` header names. So every handle must reach the host over https, including those of groups made after the host was set up.
That takes a wildcard DNS record for the handle domain, a wildcard certificate (which needs a DNS challenge) or on-demand TLS, and a proxy rule sending those names to the host. A Caddyfile for both domains:
```
group.example.com {
request_body {
max_size 60MB
}
@host path /xrpc/* /oauth/* /.well-known/* /img/*
reverse_proxy @host group-host:2600 {
lb_try_duration 20s
lb_try_interval 250ms
}
reverse_proxy group-host-console:2601 {
lb_try_duration 20s
lb_try_interval 250ms
}
}
*.groups.example.com {
tls {
dns <provider> ...
}
reverse_proxy group-host:2600
}
```
`lb_try_duration` holds each request through the second or two the host is down while it restarts. The body limit sits above the upload limit. How groups are named and made is in [Groups](design/groups.md).
## Checking it is up
`GET /xrpc/_health` answers with the host's version and the commit it was built from:
```sh
curl https://group.example.com/xrpc/_health
{"version":"0.1.0","commit":"dc64028..."}
```
As it starts, the host logs a `serving` line with its `public_url`, `did`, `version` and `commit`. The host's lines are JSON. Both containers log through docker's `journald` driver, so their lines stay in the machine's journal after a container is removed:
```sh
docker compose logs -f host
journalctl CONTAINER_NAME=group-host-host-1 --since today
```
Compose checks the host at `/xrpc/_health` and the console at `/_app/version.json`, a file the build writes, which the console answers without the host. Each is checked every second for up to 60 seconds while starting, then every 30 seconds.
## Upgrading
The host's SQLite database is the groups' own: their repos, their spaces, their members. The host makes its writes take turns within its own process, so two hosts over one data directory would interleave commits to the same repos. So the host is never run twice over one data directory. To upgrade, stop the old host, then start the new one:
```sh
docker build --build-arg GROUP_HOST_COMMIT=$(git rev-parse HEAD) -t group-host:current .
docker compose stop host
docker compose up -d host
docker compose up -d console
```
On SIGTERM the host answers the requests already under way, within its 30-second grace period, then exits. The proxy holds new requests until the new host answers. Compose replaces the console the same way. It keeps no state, so a rollout could start a new console beside the old one instead; neither service sets `container_name`, which leaves that possible. Stopped, the console finishes its requests within a 35-second grace period. Scripts to build on the machine and roll out, and a workflow that deploys, are not in the repository yet.
## Backing up
The data directory holds everything the host knows: `host.sqlite`, the database of accounts, repos, spaces, members, tokens and the index; the blobs; and `host-secrets.json`, the host's keys.
The host writes `host-secrets.json` the first time it starts; it is not set in config. Among its keys is the rotation key the host signs every group's PLC operations with. If it is lost, a group whose stewards hold no recovery key of their own can never change its identity again. [Recovery](design/recovery.md) explains what stewards can hold.
The database runs in WAL mode, so copying the file while the host runs can give a broken copy. Take a snapshot with SQLite instead:
```sh
sqlite3 /var/lib/group-host/host.sqlite "VACUUM INTO '/backup/host-$(date +%F).sqlite'"
```
Copy the snapshot and the blobs off the machine on a schedule. Keep `host-secrets.json` apart from them, somewhere fewer people and systems can read, since anyone holding it can change every group's identity. Then restore a backup onto a spare machine to know it works. Scheduled backups are not in the repository yet.
## Running the command
The image carries `opensocial`, so the command runs in the host's container, or in one of its own:
```sh
docker compose exec host opensocial --help
docker run --rm group-host:current opensocial --help
```
When the host's address changes, `group-host move-host` points every group's DID document at the new one. It runs once, over the data directory, with the host stopped and `.env` already naming the new domain. Run it first with `--dry-run` to see what it would change:
```sh
docker compose stop host
docker compose run --rm host group-host move-host --from https://old.example.com --dry-run
docker compose run --rm host group-host move-host --from https://old.example.com
docker compose up -d host
```
[Moving](design/moving.md) explains what a move changes. Every command is listed in [the command reference](reference/cli.md), and every method in [the API reference](reference/api.md). Building and testing the host are in [Development](development.md).
docs/README.md+2−0
@@ -13,6 +13,8 @@ atproto. It implements the opensocial proposal
- Reference: [the HTTP and XRPC API](reference/api.md), [the MCP
tools](reference/mcp.md), [the commands and recipes](reference/cli.md)
and [configuration](reference/config.md).
- [Deploying](deploying.md): the image, running it with its console
behind a proxy, and what to back up.
- [Development](development.md): building, testing, the checks a pull
runs, and the docs site.
docs-site/astro.config.mjs+2−1
@@ -7,7 +7,7 @@ import { remarkReference } from './reference.mjs'
/** Where the site is served: a private repository's site is at its own host's root. */
const base = '/'
// The sidebar follows docs/README.md: design, then reference, then development.
// The sidebar follows docs/README.md: design, then reference, then deploying and development.
const page = (label, slug) => ({ label, slug })
export default defineConfig({
@@ -62,6 +62,7 @@ export default defineConfig({
page('Configuration', 'reference/config'),
],
},
{ label: 'Running it', items: [page('Deploying', 'deploying')] },
{ label: 'Contributing', items: [page('Development', 'development')] },
],
}),
.dockerignorenew+13−0
@@ -0,0 +1,13 @@
.git
target
harness
data
*.profraw
lcov.info
web/node_modules
web/build
web/.svelte-kit
web/src/lib/lexicons
docs-site/node_modules
docs-site/dist
docs-site/.astro
Cargo.toml+7−0
@@ -50,3 +50,10 @@ debug = false
lto = "thin"
codegen-units = 1
strip = true
# The image's (Dockerfile): release's, built for the build's time, in 16
# codegen units without LTO, so a deploy rebuilds our crates on every core.
[profile.deploy]
inherits = "release"
lto = false
codegen-units = 16
Dockerfilenew+54−0