diff --git a/.env.example b/.env.example index 6250871..214aba1 100644 --- a/.env.example +++ b/.env.example @@ -46,6 +46,19 @@ # ENVIRONMENT=production # STATE_STORE_PATH=/data/state.db +# Host path for tenants.json itself (the bind-mount source, not its path +# inside the container — that's TENANTS_CONFIG_PATH above). Leave unset for +# local dev (defaults to ./backend/tenants.json, next to this repo +# checkout). Production MUST override this to an absolute path inside +# $SIGHTBOX_DATA_DIR — confirmed the hard way: Dokploy deletes and +# re-clones the whole git checkout on every AutoDeploy, so a file left at +# the default relative path never survives to the next deploy, and Docker +# silently creates an empty directory there instead of failing, which then +# crashes backend-billing with a confusing IsADirectoryError. Putting it in +# $SIGHTBOX_DATA_DIR instead means it survives every redeploy AND is +# already covered by backup.sh's nightly snapshot for free. +# TENANTS_JSON_PATH=/opt/sightbox/data/tenants.json + # backend-billing's image — same build-locally-push-to-registry pattern as # CUSTOM_IMAGE above, see backend/build.sh. A bare name (no "/") like the # default below means "local build only, nothing to push" — build.sh diff --git a/backup/backup.sh b/backup/backup.sh index e4e08e6..05e4a83 100755 --- a/backup/backup.sh +++ b/backup/backup.sh @@ -7,12 +7,14 @@ # What this backs up, and why raw-copy instead of `bench backup`/`bench # restore` per site: every persistent volume this deployment uses is # bind-mounted under $SIGHTBOX_DATA_DIR (docker-compose.data-dir.yml) — -# briefly stopping the writer containers and tarring that whole tree, -# together with the app/ repo checkout itself (including backend/tenants.json, -# real secrets never committed to git — backend/.env was retired; those -# four values are now plain Dokploy/Compose environment variables, not a -# file, see docker-compose.yml's backend-billing service), gives one -# self-contained, site-count-agnostic archive. Restoring never needs to +# including tenants.json itself (real secrets, never committed to git; +# TENANTS_JSON_PATH points here specifically so it survives a Dokploy +# AutoDeploy's git-checkout wipe, see docker-compose.yml's backend-billing +# service) — briefly stopping the writer containers and tarring that whole +# tree, together with the app/ repo checkout itself (backend/.env was +# retired the same way; its four values are now plain Dokploy/Compose +# environment variables, not a file), gives one self-contained, +# site-count-agnostic archive. Restoring never needs to # know how many client sites exist or loop over them individually the way # `bench restore` would — see restore.sh, which really is just "untar, # docker compose up -d". diff --git a/backup/restore.sh b/backup/restore.sh index aa3116a..a7ab66f 100755 --- a/backup/restore.sh +++ b/backup/restore.sh @@ -122,7 +122,7 @@ fi # but on the off chance one's missing, create it rather than fail obscurely. mkdir -p "${DATA_DIR}"/{sites,db-data,redis-queue-data,billing-state} -# backend/tenants.json travels inside app/ already (see backup.sh's +# tenants.json travels inside $SIGHTBOX_DATA_DIR already (see backup.sh's # docstring) — nothing further to restore for it. backend/.env was retired # in favor of plain Compose/Dokploy environment variables, so there's no # file-based equivalent to restore for backend-billing's own config either. diff --git a/docker-compose.dokploy.yml b/docker-compose.dokploy.yml index 1dfa570..35f55b9 100644 --- a/docker-compose.dokploy.yml +++ b/docker-compose.dokploy.yml @@ -50,10 +50,8 @@ services: target: /data type: volume volume: {} - - bind: - create_host_path: true - read_only: true - source: ./backend/tenants.json + - read_only: true + source: ${TENANTS_JSON_PATH:-./backend/tenants.json} target: /app/tenants.json type: bind configurator: diff --git a/docker-compose.yml b/docker-compose.yml index f88763b..46f05da 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -95,7 +95,36 @@ services: # into the image (see backend/.dockerignore). Multi-tenant billing: # every client's Frappe API key/secret and Stripe webhook secret live # here, one JSON object per tenant slug (see backend/tenants.example.json). - - ./backend/tenants.json:/app/tenants.json:ro + # + # TENANTS_JSON_PATH, not a hardcoded ./backend/tenants.json — confirmed + # the hard way: Dokploy deletes and re-clones the whole git checkout + # (including backend/) on every AutoDeploy, so a file living inside it + # never survives to the next deploy. Worse, Docker silently creates an + # empty DIRECTORY at a bind-mount source that doesn't exist yet rather + # than failing, which then crashes the app with a confusing + # IsADirectoryError instead of a clear missing-file error. The default + # below (./backend/tenants.json) is correct for local dev, where the + # checkout is stable; production must override this in .env to an + # absolute path inside $SIGHTBOX_DATA_DIR (e.g. + # /opt/sightbox/data/tenants.json) — outside the wiped checkout, and + # already covered by the nightly backup since it's already inside the + # directory backup.sh tars. See .env.example and + # INSTALLATION_PLAYBOOK.md Stage 1. + # + # Explicit long-form (type: bind), not the short-form + # "source:target:ro" string — confirmed the hard way: + # generate-dokploy-compose.sh's --no-interpolate flag (needed so + # Dokploy's own Environment tab keeps control of these values, see + # that script's header) leaves this source as a literal, unresolved + # "${TENANTS_JSON_PATH:-...}" string. `docker compose config` can't + # tell that's a path when it can't interpolate it, and silently + # normalizes the short form to type: volume instead of type: bind — + # Dokploy then rejects it as "refers to undefined volume". Declaring + # type: bind explicitly here removes the guesswork entirely. + - type: bind + source: ${TENANTS_JSON_PATH:-./backend/tenants.json} + target: /app/tenants.json + read_only: true depends_on: - backend ports: diff --git a/playbook/BACKUP_RESTORE.md b/playbook/BACKUP_RESTORE.md index 65e44be..61ac37a 100644 --- a/playbook/BACKUP_RESTORE.md +++ b/playbook/BACKUP_RESTORE.md @@ -30,9 +30,10 @@ a completely standard, safe pattern — the case-insensitivity problem doesn't e `bench backup` is the right tool when you need a live, no-downtime, per-site backup — but a restore then has to loop over every client site individually, which is exactly the kind of per-site orchestration this design avoids. Instead: briefly stop every container that writes to -`$SIGHTBOX_DATA_DIR`, tar the whole tree (plus the repo checkout itself, `$SIGHTBOX_APP_DIR`, -including `backend/tenants.json` — a real secret, never committed to git) into -one archive, restart. New client sites are automatically included in the very next backup, with +`$SIGHTBOX_DATA_DIR` — which includes `tenants.json` itself (a real secret, never committed to +git; `TENANTS_JSON_PATH` points here specifically so it survives a Dokploy AutoDeploy's +git-checkout wipe) — tar that whole tree, plus the repo checkout itself (`$SIGHTBOX_APP_DIR`), +into one archive, restart. New client sites are automatically included in the very next backup, with zero script changes. Restore genuinely is "unpack, `docker compose up -d`" — no per-site knowledge required at all. @@ -93,7 +94,7 @@ Confirmed against a real R2 bucket while building this: Both scripts set these via `RCLONE_CONFIG_R2_*` environment variables inline — no `~/.config/rclone/rclone.conf` needed, keeping every credential inside this repo's own gitignored -`backup/.env`, the same pattern as `backend/tenants.json`. +`backup/.env`, the same never-committed-secret pattern as `tenants.json`. ## Restoring — including onto a brand-new VPS, different provider diff --git a/playbook/INSTALLATION_PLAYBOOK.md b/playbook/INSTALLATION_PLAYBOOK.md index e7f5f11..10b853d 100644 --- a/playbook/INSTALLATION_PLAYBOOK.md +++ b/playbook/INSTALLATION_PLAYBOOK.md @@ -122,10 +122,24 @@ work if this is done up front). Bind mounts don't auto-create their target direc named volume does, so create them first: ``` mkdir -p /opt/sightbox/data/{sites,db-data,redis-queue-data,billing-state} +[ -f /opt/sightbox/data/tenants.json ] || echo '{}' > /opt/sightbox/data/tenants.json echo "SIGHTBOX_DATA_DIR=/opt/sightbox/data" >> .env +echo "TENANTS_JSON_PATH=/opt/sightbox/data/tenants.json" >> .env docker compose -f docker-compose.yml -f docker-compose.data-dir.yml up -d ``` +⚠ **Confirmed the hard way:** `TENANTS_JSON_PATH` isn't optional here the way it might look — +`backend-billing`'s own `docker-compose.yml` mount defaults to a path *inside* the repo checkout +(`./backend/tenants.json`), which is correct for local dev but gets wiped on every Dokploy +AutoDeploy same as `backend/.env` used to. Worse than a clean failure: Docker silently creates an +empty **directory** at a bind-mount source that doesn't exist yet, which then crashes +`backend-billing` with a confusing `IsADirectoryError` instead of an obvious missing-file error. +The `echo '{}' >` above seeds a real, empty (zero-tenant) file at the durable path before the +first `docker compose up` — safe even before Stage 9 adds a real client, since an empty object is +a valid, zero-tenant config. Putting it inside `$SIGHTBOX_DATA_DIR` specifically (not some other +absolute path) means it also survives a VPS migration and is already covered by Stage 10's nightly +backup, with nothing extra to configure there. + **Local dev / testing (`.localhost`):** skip the `data-dir` file entirely — just ``` docker compose up -d @@ -409,8 +423,11 @@ both one-time-per-bench, from Stage 1 — not something this or any later client configure here either. 2. Generate its API key/secret: Settings → My Settings → API Access, or `bench --site execute frappe.core.doctype.user.user.generate_keys --args "['']"`. -3. Add one entry to `backend/tenants.json` (copy the shape from `tenants.example.json`), keyed by - a slug for this client: +3. Add one entry to `tenants.json` (copy the shape from `backend/tenants.example.json`), keyed by + a slug for this client — for a real Dokploy deployment this file now lives at + `$SIGHTBOX_DATA_DIR/tenants.json` (`/opt/sightbox/data/tenants.json`, seeded empty back in + Stage 1), **not** `backend/tenants.json` inside the repo checkout; edit it directly at that + path on the VPS. Local dev still uses `backend/tenants.json` next to the repo, unchanged: ```json { "acme": {