feat(scripts): deploy to Homeflare, skipping steps that need Cloudflare itself
Zone check, R2 lifecycle, Email Sending, Turnstile and Access; see HOMEFLARE.md.
Jason Schwarzenberger authored 1 parent b6dbbfdcommit ff5bbbcea5de9032c5c74fb1ed0b123740c260cbBrowse files
5 files changed+125−15
Files (5)
addedHOMEFLARE.md+65−0View file
| @@ -1,0 +1,65 @@ | ||
| 1 | +# Diffinery on Homeflare | |
| 2 | + | |
| 3 | +A fork of [codician-team/diffinery](https://github.com/codician-team/diffinery) | |
| 4 | +whose deploy scripts also work against Homeflare. `main` is upstream's `main` | |
| 5 | +plus a few commits, each one droppable or fit to send upstream on its own: | |
| 6 | + | |
| 7 | +- `CLOUDFLARE_API_BASE_URL` and `CLOUDFLARE_CONTAINER_REGISTRY` in the scripts, | |
| 8 | + with Cloudflare's API and registry as the defaults. | |
| 9 | +- The Homeflare deploy described below, skipping the steps that need Cloudflare | |
| 10 | + itself. | |
| 11 | + | |
| 12 | +Rebase onto upstream when it moves: | |
| 13 | + | |
| 14 | +```sh | |
| 15 | +git remote add upstream https://github.com/codician-team/diffinery.git | |
| 16 | +git fetch upstream && git rebase upstream/main | |
| 17 | +``` | |
| 18 | + | |
| 19 | +## How it works | |
| 20 | + | |
| 21 | +[Homeflare](https://github.com/jason-s13r/homeflare) runs Workers on your own machines (celld) behind a | |
| 22 | +Cloudflare-compatible API. These scripts deploy to it when `CLOUDFLARE_API_BASE_URL` points at that API, | |
| 23 | +which `homeflare-wrangler run` sets, along with `CLOUDFLARE_CONTAINER_REGISTRY` (Homeflare's registry) and a | |
| 24 | +`docker` command on your Podman. | |
| 25 | + | |
| 26 | +On another API than Cloudflare's, `scripts/setup.sh` and `scripts/bootstrap.sh` skip what needs Cloudflare | |
| 27 | +itself: | |
| 28 | + | |
| 29 | +| Skipped | What it means | | |
| 30 | +| --- | --- | | |
| 31 | +| Zone check | The hostname is a Homeflare custom domain, not a route in a Cloudflare zone. | | |
| 32 | +| R2 lifecycle rules | Logs, artifacts, archives and exports don't age out by themselves. | | |
| 33 | +| Email Sending setup and its event subscription | Onboard the sending domain in Cloudflare yourself (Homeflare relays email through it with `HOMEFLARE_EMAIL=cloudflare`). Bounces and complaints don't reach the suppression list. | | |
| 34 | +| Turnstile widget | Make one in the Cloudflare dashboard and pass its keys: `TURNSTILE_SITE_KEY` and `TURNSTILE_SECRET`. Without them open sign-up and anonymous security reports stay off. | | |
| 35 | +| Cloudflare Access | Email recovery and the `/auth/access` first sign-in stay off. Passkeys work. | | |
| 36 | + | |
| 37 | +## Deploy | |
| 38 | + | |
| 39 | +Put the settings in a file outside the repository (it holds the Turnstile secret), for example | |
| 40 | +`~/.config/homeflare/diffinery.env`, readable by you only: | |
| 41 | + | |
| 42 | +```sh | |
| 43 | +CLOUDFLARE_ACCOUNT_ID=<Homeflare's account id> | |
| 44 | +DIFFINERY_DOMAIN=git.homeflare.example.com | |
| 45 | +MAIL_DOMAIN=example.com | |
| 46 | +[email protected] | |
| 47 | +TURNSTILE_SITE_KEY=0x4AAA... | |
| 48 | +TURNSTILE_SECRET=0x4AAA... | |
| 49 | +``` | |
| 50 | + | |
| 51 | +`MAIL_DOMAIN` is a domain onboarded to Email Sending in your Cloudflare account. Homeflare's account id is | |
| 52 | +in the Dashboard, or from `GET /client/v4/accounts`. Then, from the repository: | |
| 53 | + | |
| 54 | +```sh | |
| 55 | +homeflare-wrangler --env-file ~/.config/homeflare/diffinery.env run scripts/setup.sh --yes | |
| 56 | +``` | |
| 57 | + | |
| 58 | +`homeflare-wrangler` reads the Homeflare API token from `~/.config/homeflare/wrangler.env` and sets | |
| 59 | +`CLOUDFLARE_API_BASE_URL` itself. Run it again to update. | |
| 60 | + | |
| 61 | +## The first administrator | |
| 62 | + | |
| 63 | +Without Access, the first account comes from open sign-up, which needs Turnstile and email. Deploy once with | |
| 64 | +`"SIGNUP": "open"` under `vars` in `apps/diffinery/wrangler.jsonc`, sign up at `https://<host>/signup` with an | |
| 65 | +address in `BOOTSTRAP_ADMINS`, add a passkey, then remove `SIGNUP` and deploy again. |
modifiedscripts/bootstrap.sh+28−8View file
| @@ -27,6 +27,9 @@ | ||
| 27 | 27 | WRANGLER=(pnpm --dir "$APP" exec wrangler) |
| 28 | 28 | CF_API="${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}" |
| 29 | 29 | API="$CF_API/accounts/$CLOUDFLARE_ACCOUNT_ID" |
| 30 | +# 0 on another Cloudflare-compatible API (Homeflare): the steps that need Cloudflare itself are skipped. | |
| 31 | +ON_CLOUDFLARE=1 | |
| 32 | +[[ "$CF_API" == https://api.cloudflare.com/* ]] || ON_CLOUDFLARE=0 | |
| 30 | 33 | |
| 31 | 34 | cf() { # cf METHOD PATH [JSON] |
| 32 | 35 | local method="$1" path="$2" body="${3:-}" |
| @@ -75,6 +78,9 @@ | ||
| 75 | 78 | {"id":"action-mirror-30d","enabled":true,"conditions":{"prefix":"action-mirror/"},"deleteObjectsTransition":{"condition":{"type":"Age","maxAge":2592000}}} |
| 76 | 79 | ]}' |
| 77 | 80 | # Applied on every deploy; a failure stops it, since a wrong rule can delete data. |
| 81 | +if ((ON_CLOUDFLARE == 0)); then | |
| 82 | + warn "R2 lifecycle rules are skipped on $CF_API: transient objects (logs, artifacts, archives, exports) don't age out by themselves." | |
| 83 | +else | |
| 78 | 84 | RESULT="$(cf PUT "/r2/buckets/diffinery-data/lifecycle" "$LIFECYCLE")" |
| 79 | 85 | if ! jq -e '.success' >/dev/null <<<"$RESULT"; then |
| 80 | 86 | echo "could not set the R2 lifecycle rules (token has R2: Edit?): $(jq -c '.errors // .' <<<"$RESULT" 2>/dev/null || echo "$RESULT")" >&2 |
| @@ -86,6 +92,7 @@ | ||
| 86 | 92 | exit 1 |
| 87 | 93 | fi |
| 88 | 94 | echo "R2 lifecycle rules set" |
| 95 | +fi | |
| 89 | 96 | endlog |
| 90 | 97 | |
| 91 | 98 | # ------------------------------------------------------------------ Queues |
| @@ -163,12 +170,16 @@ | ||
| 163 | 170 | warn "Could not subscribe diffinery-mail-events to Email Sending's bounce and complaint events for $SEND_DOMAIN. The token needs $SUB_PERMISSION (and $SEND_DOMAIN onboarded to Email Sending). Cloudflare said: $(api_errors "$res")" |
| 164 | 171 | fi |
| 165 | 172 | } |
| 166 | -subscribe_mail_events | |
| 173 | +if ((ON_CLOUDFLARE)); then subscribe_mail_events; else echo "skipped on $CF_API: bounces and complaints don't reach the suppression list by themselves"; fi | |
| 167 | 174 | endlog |
| 168 | 175 | |
| 169 | 176 | # ------------------------------------------------------------------ Email Sending |
| 170 | 177 | # notifications@<MAIL_DOMAIN or DIFFINERY_DOMAIN>; warns (and email is skipped) until the domain is onboarded. |
| 171 | -DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/email-sending.sh" || warn "Email Sending check failed; email may not be delivered." | |
| 178 | +if ((ON_CLOUDFLARE)); then | |
| 179 | + DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/email-sending.sh" || warn "Email Sending check failed; email may not be delivered." | |
| 180 | +else | |
| 181 | + echo "Email Sending setup skipped on $CF_API: the sending domain is set up wherever that API sends from." | |
| 182 | +fi | |
| 172 | 183 | |
| 173 | 184 | # ------------------------------------------------------------------ Turnstile |
| 174 | 185 | # The widget "diffinery <DIFFINERY_DOMAIN>", created when missing, so people who aren't signed in can |
| @@ -178,19 +189,28 @@ | ||
| 178 | 189 | # Account → Turnstile → Edit; without it turnstile.sh warns, naming it, and the deploy goes on with |
| 179 | 190 | # anonymous reports refused. Sign-up stays as SIGNUP says: keys alone don't open it. |
| 180 | 191 | log "Turnstile" |
| 181 | -TURNSTILE_SITE_KEY="$(DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/turnstile.sh")" || { warn "scripts/turnstile.sh failed; anonymous security reports stay refused."; TURNSTILE_SITE_KEY=""; } | |
| 192 | +if ((ON_CLOUDFLARE)); then | |
| 193 | + TURNSTILE_SITE_KEY="$(DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/turnstile.sh")" || { warn "scripts/turnstile.sh failed; anonymous security reports stay refused."; TURNSTILE_SITE_KEY=""; } | |
| 194 | +else | |
| 195 | + # No Turnstile API here: a widget made in the Cloudflare dashboard, its keys in the environment. | |
| 196 | + TURNSTILE_SITE_KEY="${TURNSTILE_SITE_KEY:-}" | |
| 197 | + [[ -n "$TURNSTILE_SITE_KEY" ]] || warn "No TURNSTILE_SITE_KEY: open sign-up and anonymous security reports stay off." | |
| 198 | +fi | |
| 182 | 199 | echo "site key: ${TURNSTILE_SITE_KEY:-none}" |
| 183 | 200 | endlog |
| 184 | 201 | |
| 185 | 202 | # ------------------------------------------------------------------ Access |
| 186 | 203 | log "Cloudflare Access" |
| 187 | 204 | TEAM="${ACCESS_TEAM_DOMAIN:-}" |
| 188 | -if [[ -z "$TEAM" ]]; then | |
| 205 | +if [[ -z "$TEAM" ]] && ((ON_CLOUDFLARE)); then | |
| 189 | 206 | TEAM="$(cf GET /access/organizations | jq -r '.result.auth_domain // empty' || true)" |
| 190 | 207 | fi |
| 191 | -APPS="$(cf GET "/access/apps?per_page=100" || echo '{}')" | |
| 192 | 208 | AUD="" |
| 193 | -if ! jq -e '.success' >/dev/null <<<"$APPS"; then | |
| 209 | +APPS='{}' | |
| 210 | +((ON_CLOUDFLARE == 0)) || APPS="$(cf GET "/access/apps?per_page=100" || echo '{}')" | |
| 211 | +if ((ON_CLOUDFLARE == 0)); then | |
| 212 | + echo "Access skipped on $CF_API: passkeys work; email recovery and the /auth/access first sign-in stay off." | |
| 213 | +elif ! jq -e '.success' >/dev/null <<<"$APPS"; then | |
| 194 | 214 | warn "Could not read Access applications (token needs Access: Apps and Policies Edit). Passkeys work; email recovery stays off." |
| 195 | 215 | else |
| 196 | 216 | # People sign in with passkeys. Access guards only /auth/access, the email-based |
| @@ -217,11 +237,11 @@ | ||
| 217 | 237 | cf DELETE "/access/apps/$BYPASS_ID" | jq -e '.success' >/dev/null && echo "removed the old Access bypass app" || warn "Could not remove the old Access bypass application." |
| 218 | 238 | fi |
| 219 | 239 | fi |
| 220 | -if [[ -z "$TEAM" ]]; then | |
| 240 | +if [[ -z "$TEAM" ]] && ((ON_CLOUDFLARE)); then | |
| 221 | 241 | # Fall back to the sign-in redirect, which names the team domain. |
| 222 | 242 | TEAM="$(curl -sS -o /dev/null -w '%{redirect_url}' "https://$DOMAIN/auth/access" | sed -nE 's#^https://([^/]+\.cloudflareaccess\.com)/.*#\1#p' || true)" |
| 223 | 243 | fi |
| 224 | -[[ -n "$TEAM" ]] || warn "Access team domain unknown; set the ACCESS_TEAM_DOMAIN repository variable (Zero Trust → Settings → Custom pages shows it)." | |
| 244 | +[[ -n "$TEAM" ]] || ((ON_CLOUDFLARE == 0)) || warn "Access team domain unknown; set the ACCESS_TEAM_DOMAIN repository variable (Zero Trust → Settings → Custom pages shows it)." | |
| 225 | 245 | echo "team=${TEAM:-?} aud=${AUD:+set}" |
| 226 | 246 | endlog |
| 227 | 247 |
modifiedscripts/ensure-secrets.sh+5−1View file
| @@ -36,7 +36,11 @@ | ||
| 36 | 36 | # Without the Turnstile permission this warns (scripts/turnstile.sh names it) and the deploy goes on: |
| 37 | 37 | # people who aren't signed in can't send security reports until it's set. |
| 38 | 38 | SITE_KEY="$(tail -n +2 "$APP/wrangler.deploy.jsonc" | jq -r '.vars.TURNSTILE_SITE_KEY // empty')" |
| 39 | -if [[ -n "$SITE_KEY" ]] && ! grep -qx TURNSTILE_SECRET <<<"$EXISTING"; then | |
| 39 | +if [[ -n "$SITE_KEY" && -n "${TURNSTILE_SECRET:-}" ]] && ! grep -qx TURNSTILE_SECRET <<<"$EXISTING"; then | |
| 40 | + # A widget's secret from the environment (an API without Turnstile, such as Homeflare's). | |
| 41 | + printf '%s' "$TURNSTILE_SECRET" | "${WRANGLER[@]}" secret put TURNSTILE_SECRET -c wrangler.deploy.jsonc >/dev/null | |
| 42 | + echo "set TURNSTILE_SECRET" | |
| 43 | +elif [[ -n "$SITE_KEY" ]] && ! grep -qx TURNSTILE_SECRET <<<"$EXISTING"; then | |
| 40 | 44 | if "$ROOT/scripts/turnstile.sh" secret "$SITE_KEY" -c wrangler.deploy.jsonc; then |
| 41 | 45 | echo "set TURNSTILE_SECRET" |
| 42 | 46 | else |
modifiedscripts/render-config.mjs+3−1View file
| @@ -34,7 +34,9 @@ | ||
| 34 | 34 | if (url.protocol !== "https:" || url.pathname !== "/") throw new Error("--public-url must be an https origin, like https://git.example.com"); |
| 35 | 35 | cfg.vars.PUBLIC_URL = url.origin; |
| 36 | 36 | const zone = values.zone ?? url.hostname.split(".").slice(-2).join("."); |
| 37 | - cfg.routes = [{ pattern: `${url.hostname}/*`, zone_name: zone }]; | |
| 37 | + // On another Cloudflare-compatible API (Homeflare), a custom domain: the zone isn't Cloudflare's. | |
| 38 | + const onCloudflare = (process.env.CLOUDFLARE_API_BASE_URL ?? "https://api.cloudflare.com/").startsWith("https://api.cloudflare.com/"); | |
| 39 | + cfg.routes = onCloudflare ? [{ pattern: `${url.hostname}/*`, zone_name: zone }] : [{ pattern: url.hostname, custom_domain: true }]; | |
| 38 | 40 | cfg.vars.MAIL_FROM = `notifications@${url.hostname}`; |
| 39 | 41 | } |
| 40 | 42 | if (values["mail-from"]) cfg.vars.MAIL_FROM = values["mail-from"]; |
modifiedscripts/setup.sh+24−5View file
| @@ -19,8 +19,12 @@ | ||
| 19 | 19 | # GITHUB_APP_ID, GITHUB_APP_SLUG, GITHUB_APP_CLIENT_ID, GITHUB_APP_URL |
| 20 | 20 | # the GitHub App of GitHub runners (docs/self-hosting/configuration.md#github-runners) |
| 21 | 21 | # ROLLOUT_WAIT_MINUTES how long to wait for the container rollout (default 10) |
| 22 | -# CLOUDFLARE_API_BASE_URL another Cloudflare-compatible API (default Cloudflare's) | |
| 22 | +# CLOUDFLARE_API_BASE_URL another Cloudflare-compatible API, such as Homeflare's (default Cloudflare's). | |
| 23 | +# Steps that need Cloudflare itself (the zone check, R2 lifecycle rules, Email | |
| 24 | +# Sending setup and events, Turnstile and Access) are skipped there; see | |
| 25 | +# HOMEFLARE.md. | |
| 23 | 26 | # CLOUDFLARE_CONTAINER_REGISTRY the registry runner images go to (default registry.cloudflare.com) |
| 27 | +# TURNSTILE_SITE_KEY, TURNSTILE_SECRET a Turnstile widget's keys, for an API without Turnstile | |
| 24 | 28 | # |
| 25 | 29 | # Every step is idempotent: it creates only what is missing and keeps your data and secrets, so run |
| 26 | 30 | # it again to update. Secrets are never printed. Needs Node.js 22+, pnpm, jq, curl, openssl and Docker. |
| @@ -30,6 +34,11 @@ | ||
| 30 | 34 | APP="$ROOT/apps/diffinery" |
| 31 | 35 | CF_API="${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}" |
| 32 | 36 | PLACEHOLDER_D1="00000000-0000-0000-0000-000000000000" |
| 37 | +# 1 on Cloudflare's own API; 0 on another Cloudflare-compatible one (Homeflare), which skips the | |
| 38 | +# steps that need Cloudflare itself. | |
| 39 | +ON_CLOUDFLARE=1 | |
| 40 | +[[ "$CF_API" == https://api.cloudflare.com/* ]] || ON_CLOUDFLARE=0 | |
| 41 | +export ON_CLOUDFLARE | |
| 33 | 42 | DRY_RUN=0 |
| 34 | 43 | ASK=1 |
| 35 | 44 | |
| @@ -152,12 +161,16 @@ | ||
| 152 | 161 | fi |
| 153 | 162 | jq -e '.success == true and .result.status == "active"' >/dev/null 2>&1 <<<"$verify" || die "Cloudflare doesn't accept the API token: $(errors_of "$verify")" |
| 154 | 163 | info "the token is valid and active" |
| 164 | + if ((ON_CLOUDFLARE == 0)); then | |
| 165 | + info "API: $CF_API (not Cloudflare's: the zone check, R2 lifecycle rules, Email Sending setup, Turnstile and Access are skipped)" | |
| 166 | + else | |
| 155 | 167 | zones="$(cf_get "/zones?name=$ZONE&account.id=$CLOUDFLARE_ACCOUNT_ID" || true)" |
| 156 | 168 | jq -e '.success == true' >/dev/null 2>&1 <<<"$zones" || die "couldn't look up the zone $ZONE: $(errors_of "$zones")" |
| 157 | 169 | zone_status="$(jq -r '.result[0].status // empty' <<<"$zones")" |
| 158 | 170 | [[ -n "$zone_status" ]] || die "the zone $ZONE isn't in account $CLOUDFLARE_ACCOUNT_ID, or the token can't see it. Add the domain to the account, give the token Zone → Zone → Read on it, or set DIFFINERY_ZONE if the zone that holds $DIFFINERY_DOMAIN is another one." |
| 159 | 171 | [[ "$zone_status" == active ]] || warn "the zone $ZONE is $zone_status, not active: the route serves nothing until its nameservers point at Cloudflare" |
| 160 | 172 | info "zone $ZONE is in the account ($zone_status)" |
| 173 | + fi | |
| 161 | 174 | fi |
| 162 | 175 | |
| 163 | 176 | cd "$ROOT" |
| @@ -223,7 +236,7 @@ | ||
| 223 | 236 | step "Worker secrets (generated on the first deploy; never printed)" |
| 224 | 237 | "$ROOT/scripts/ensure-secrets.sh" |
| 225 | 238 | |
| 226 | -if [[ -z "$ACCESS_TEAM" ]]; then | |
| 239 | +if [[ -z "$ACCESS_TEAM" ]] && ((ON_CLOUDFLARE)); then | |
| 227 | 240 | step "Finishing the Access setup (first deploy)" |
| 228 | 241 | : >"$OUTPUTS" |
| 229 | 242 | GITHUB_OUTPUT="$OUTPUTS" "$ROOT/scripts/bootstrap.sh" |
| @@ -239,12 +252,18 @@ | ||
| 239 | 252 | [[ "$code" == 200 ]] && break |
| 240 | 253 | sleep 10 |
| 241 | 254 | done |
| 242 | -[[ "$code" == 200 ]] || die "https://$DIFFINERY_DOMAIN/healthz did not return 200 (last: ${code:-no answer}). Check that the $DIFFINERY_DOMAIN DNS record is proxied (orange cloud); a new certificate can take a few minutes, so run this again." | |
| 255 | +if ((ON_CLOUDFLARE)); then | |
| 256 | + [[ "$code" == 200 ]] || die "https://$DIFFINERY_DOMAIN/healthz did not return 200 (last: ${code:-no answer}). Check that the $DIFFINERY_DOMAIN DNS record is proxied (orange cloud); a new certificate can take a few minutes, so run this again." | |
| 257 | +else | |
| 258 | + [[ "$code" == 200 ]] || die "https://$DIFFINERY_DOMAIN/healthz did not return 200 (last: ${code:-no answer}). Check that $DIFFINERY_DOMAIN resolves to the host serving $CF_API and that the Worker started." | |
| 259 | +fi | |
| 243 | 260 | info "/healthz -> 200" |
| 244 | 261 | curl -sS "https://$DIFFINERY_DOMAIN/login" | grep -q "Sign in with a passkey" || die "/login is not the Diffinery sign-in page: check DIFFINERY_DOMAIN, DIFFINERY_ZONE and the route in apps/diffinery/wrangler.deploy.jsonc" |
| 245 | 262 | info "/login serves the passkey sign-in page" |
| 246 | 263 | loc="$(curl -sS -o /dev/null -w '%{http_code} %{redirect_url}' "https://$DIFFINERY_DOMAIN/auth/access" || true)" |
| 247 | -if [[ "$loc" == 302\ https://*.cloudflareaccess.com/* ]]; then | |
| 264 | +if ((ON_CLOUDFLARE == 0)); then | |
| 265 | + info "no Cloudflare Access here: the first administrator signs up with SIGNUP=open (needs TURNSTILE_SITE_KEY and TURNSTILE_SECRET and email), see HOMEFLARE.md" | |
| 266 | +elif [[ "$loc" == 302\ https://*.cloudflareaccess.com/* ]]; then | |
| 248 | 267 | info "/auth/access is behind Cloudflare Access" |
| 249 | 268 | else |
| 250 | 269 | warn "/auth/access is not behind Cloudflare Access yet ($loc), so email recovery and the first admin sign-in won't work. Set ACCESS_TEAM_DOMAIN and run this again." |
| @@ -254,4 +273,4 @@ | ||
| 254 | 273 | echo "Diffinery is live at https://$DIFFINERY_DOMAIN." |
| 255 | 274 | echo "Sign in as the first administrator at https://$DIFFINERY_DOMAIN/auth/access with an email in BOOTSTRAP_ADMINS," |
| 256 | 275 | echo "then add a passkey. Run scripts/setup.sh again at any time to update." |
| 257 | -[[ -n "$ACCESS_TEAM" ]] || warn "the Access team domain is still unknown; set ACCESS_TEAM_DOMAIN (Zero Trust → Settings → Custom pages shows it) and run this again" | |
| 276 | +[[ -n "$ACCESS_TEAM" ]] || ((ON_CLOUDFLARE == 0)) || warn "the Access team domain is still unknown; set ACCESS_TEAM_DOMAIN (Zero Trust → Settings → Custom pages shows it) and run this again" |