From adcd2f9aecacf97c94dd20450809ba8c0bb92f9e Mon Sep 17 00:00:00 2001 From: David Young Date: Thu, 3 Dec 2026 20:02:03 -0700 Subject: [PATCH] Let an installed app drop a need Taking sso or email out of needs removes the client or mail account once the new version answers; postgres or mysql need --drop-database (coreapp deploy asks for the app's name). Their settings leave .env first, so a postgres -> mysql switch gets mysql's own values; a failed update rolls back with them intact. The route wait now compares traefik's answer with the app's own, so an app's 404 isn't taken for a missing route. Co-Authored-By: Claude Opus 5.5 --- PLAN.md | 12 +++++- README.md | 2 +- TEST.md | 23 +++++++++- bin/coreapp | 36 +++++++++++----- core/coreapps | 93 ++++++++++++++++++++++++++++++++++------- skills/coreapp/SKILL.md | 10 +++++ 6 files changed, 148 insertions(+), 28 deletions(-) diff --git a/PLAN.md b/PLAN.md index 7a3c969..7d91192 100644 --- a/PLAN.md +++ b/PLAN.md @@ -512,7 +512,17 @@ Later: - [ ] Very large databases: a dump on every update may still be too slow; a btrfs snapshot of the database server's data, or skipping the copy with the user's agreement, could replace it -- [ ] Uninstalling a need without removing the app (now refused) +- Done 2026-12-03: dropping a need from an installed app. `sso` and + `email` go freely; `postgres`/`mysql` (the app's data) need + `--drop-database` (coreapps install/plan; `coreapp deploy` asks for the + app's name). The dropped needs' settings leave `.env` before the update + (so a switch postgres -> mysql gets mysql's own `DB_*`), and their + teardown, from the service folder kept for the rollback, runs only once + the new version answers; a failed update rolls back with them intact. + Tested on dev.fedcom.net: sso and email dropped, a crashing version + rolled back with them kept, the refusals, postgres -> mysql. Found and + fixed with it: deploy's route wait took an app's own 404 for traefik's + (it now waits for traefik to answer as the app does) - [ ] `bin/start` / `bin/stop` don't know coreapps' apps (see "Tell Federated"); `restart: always` covers reboots - [ ] Trust: a list of reviewed apps, or signed releases, and a warning in diff --git a/README.md b/README.md index 5514cdd..3920707 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ each new app explains the files and the settings it gets. | `coreapp new --postgres --sso ` | Both: a database and sign-in (the example counts each person's visits) | | `coreapp new --mysql ` | (Instead of `--postgres`, with no example app.) Its own database on the Core's MariaDB | | `coreapp new --email ` | (With any of the above.) Sending mail through the Core's mail server, with the app's own account | -| `coreapp deploy [--recreate]` | Build its image, copy it to the Core over SSH, install it, or update it keeping its settings and data; a failed update rolls back to the version before (`--recreate`: a fresh container even if nothing changed) | +| `coreapp deploy [--recreate] [--drop-database]` | Build its image, copy it to the Core over SSH, install it, or update it keeping its settings and data; a failed update rolls back to the version before (`--recreate`: a fresh container even if nothing changed). Needs added to `coreapp.toml` are set up; needs taken out are removed once the new version works, and taking out `postgres` or `mysql` (deleting the database) needs `--drop-database`, which asks you to type the app's name | | `coreapp check` | It answers at `https://.` with a valid certificate | | `coreapp core` | What the Core in the VM offers apps: PostgreSQL (and which extensions an app can enable), MariaDB, Authelia, the mail server and Traefik with their versions, Docker, and the machine | | `coreapp mail` | What the Core's mail server did with the mail the app sent in the last day: accepted from its account, then delivered, deferred or bounced, with the reason (apps with `email`) | diff --git a/TEST.md b/TEST.md index 52ced11..c5c01ab 100644 --- a/TEST.md +++ b/TEST.md @@ -234,7 +234,28 @@ The Core's own scripts trace secrets, so their output goes to (change `image`, and skip coreapp's build by running `coreapp-vm ssh /opt/coreapps/coreapps install ` on a copy) - with the app installed with `needs = ["postgres"]`, deploying with - `needs = []` ("drops needs postgres") + `needs = []` without `--drop-database` ("drops postgres: that deletes + ...'s database ... add --drop-database"); with it, typing another name + at the question: "Not deployed; nothing changed." +8a. Dropping needs (an app from `coreapp new --postgres --sso --email`, + then given the template's static `Dockerfile` and `www/`, which need + nothing): + - `needs = ["postgres"]`, deploy: "no longer needs: sso email", then, + after the app answers, "Removing what it no longer needs from the + Core: sso, email..."; the Authelia block and the client's copy are + gone, Authelia healthy after its restart, the LDAP mail account gone, + no `OIDC_*` or `SMTP_*` in `.env`, the database kept. + - With the example's own code (which needs `OIDC_*`): the new version + doesn't start and the update rolls back; the needs, settings, client + and mail account are all still there. + - `needs = ["mysql"]`, `coreapp deploy --drop-database`, typing the name, + with a Dockerfile whose container exits: rolled back, the PostgreSQL + database still there. With the working Dockerfile: the PostgreSQL + database and user are gone, `.env` has `DB_HOST=pdnsmysql.` + and a new `DB_PASSWORD` that logs in to MariaDB. + - After a deploy whose health path answers 404 from the app itself, no + "traefik doesn't route" note (deploy compares traefik's answer with + the app's own). 9. `/data` is writable by the app: with an image that runs as `nobody` (the `--postgres`/`--sso` examples), `coreapp shell` then `touch /data/x` works; on the Core, `/federated/apps//data` belongs diff --git a/bin/coreapp b/bin/coreapp index 73f67d4..303c451 100755 --- a/bin/coreapp +++ b/bin/coreapp @@ -6,9 +6,12 @@ # coreapp new [--postgres | --mysql] [--sso] [--email] # make a new app in ./ (with its own database, # sign-in through the Core's Authelia, sending mail) -# coreapp deploy [--recreate] +# coreapp deploy [--recreate] [--drop-database] # build, copy to the Core, install or update -# (--recreate: a new container even if nothing changed) +# (--recreate: a new container even if nothing +# changed; --drop-database: allow dropping +# "postgres" or "mysql" from needs, which deletes +# the app's database; asks first) # coreapp check the app answers at https://./ # coreapp status its container, address and URL # coreapp logs [-f] its log @@ -239,12 +242,15 @@ cmd_new() { # --- deploy ----------------------------------------------------------------- cmd_deploy() { - local plan state tier answer local_id core_id addon="" recreate="" - case "${1:-}" in - --recreate) recreate=--recreate ;; - "") ;; - *) die "Usage: coreapp deploy [--recreate]" ;; - esac + local plan state tier answer local_id core_id dropped addon="" recreate="" dropdb="" + while [[ $# -gt 0 ]]; do + case "$1" in + --recreate) recreate=--recreate ;; + --drop-database) dropdb=--drop-database ;; + *) die "Usage: coreapp deploy [--recreate] [--drop-database]" ;; + esac + shift + done load_app # (The installer tells about an icon it can't use) [[ "$ICON" != ok || -z "$ICON_NOTE" ]] || echo "Note: $ICON_NOTE" @@ -252,9 +258,19 @@ cmd_deploy() { echo "Updating the installer on the Core ($REMOTE_INSTALLER)..." sync_installer # What installing would do (and whether the Core refuses it: name taken...) - plan="$(installer plan)" || exit 1 + plan="$(installer plan $dropdb)" || exit 1 state="$(sed -n 's/^state=//p' <<< "$plan")" tier="$(sed -n 's/^tier=//p' <<< "$plan")" + dropped="$(sed -n 's/^dropped_needs=//p' <<< "$plan")" + if [[ -n "$dropped" ]]; then + echo "coreapp.toml no longer needs: $dropped. Once the new version works, the Core removes" + echo "what they set up for $NAME (its database, sign-in client or mail account)." + if [[ " $dropped " == *" postgres "* || " $dropped " == *" mysql "* ]]; then + echo "That deletes $NAME's database, with everything in it." + read -r -p "Type the app's name ($NAME) to go on: " answer + [[ "$answer" == "$NAME" ]] || die "Not deployed; nothing changed." + fi + fi if grep -qx 'addon=yes' <<< "$plan"; then echo "This Core's tier is '$tier'. Installing records '$NAME' as an add-on app" echo "(ADDON_APPS in /etc/federated), which Federated may bill for." @@ -279,7 +295,7 @@ cmd_deploy() { # Install, or update (which recreates the container if the image changed, # even under the same version) - installer install $addon $recreate || exit 1 + installer install $addon $recreate $dropdb || exit 1 [[ "$state" == new ]] && echo "(coreapp check tests it at its public address, as people reach it.)" return 0 } diff --git a/core/coreapps b/core/coreapps index 0224936..a3af564 100755 --- a/core/coreapps +++ b/core/coreapps @@ -10,11 +10,14 @@ whoever wrote the app. coreapps manifest check /coreapp.toml, print its values coreapps plan [--downgrade] what install would do on this Core -coreapps install [--addon-ok] [--recreate] [--downgrade] +coreapps install [--addon-ok] [--recreate] [--downgrade] [--drop-database] install the app, or update it (--recreate: a new container even if nothing changed; --downgrade: allow an - older release than the installed one) + older release than the installed one; + --drop-database: allow dropping + "postgres" or "mysql" from needs, + deleting its database) coreapps update [--addon-ok] update an app installed from a repository to its newest release coreapps remove uninstall an app installed by coreapps @@ -537,7 +540,7 @@ def marker_value(name, key): return "" -def plan(m, origin=None, downgrade=False): +def plan(m, origin=None, downgrade=False, drop_database=False): """What installing m would do here, or Refused.""" name = m["name"] domain, tier = federated_settings() @@ -563,10 +566,14 @@ def plan(m, origin=None, downgrade=False): if installed: before = marker_needs(name) dropped = [n for n in before if n not in m["needs"]] - if dropped: - raise Refused(f"The new coreapp.toml drops needs {', '.join(dropped)}; " - "removing them would delete what the app keeps there. " - "Remove the app and install it again to do that.") + # A database is the app's data; a sign-in client or mail account + # can be made again + data = [n for n in dropped if n in DUMP] + if data and not drop_database: + raise Refused(f"The new coreapp.toml drops {', '.join(data)}: that deletes " + f"{name}'s database, with everything in it. To do that, add " + "--drop-database (it's dropped once the new version works).") + p["dropped_needs"] = dropped p["new_needs"] = [n for n in m["needs"] if n not in before] # Another repository's app of the same name would take over this # one's data, database and sign-in client @@ -588,6 +595,7 @@ def plan(m, origin=None, downgrade=False): p["downgrade_from"] = installed_tag else: p["new_needs"] = list(m["needs"]) + p["dropped_needs"] = [] for key in ("source", "tag", "commit"): if origin: p[key] = origin[key] @@ -843,12 +851,12 @@ done ''' -def install(arg, addon_ok, recreate=False, downgrade=False): +def install(arg, addon_ok, recreate=False, downgrade=False, drop_database=False): with tempfile.TemporaryDirectory(prefix="coreapps-") as staging: folder, origin = resolve(arg, staging) m = read_manifest(folder) check_release(m, origin) - p = plan(m, origin, downgrade) + p = plan(m, origin, downgrade, drop_database) return install_app(m, p, folder, origin, addon_ok, recreate) @@ -893,6 +901,9 @@ def install_app(m, p, folder, origin, addon_ok, recreate): keep_for_rollback(m, p, backup, log) try: make_service_folder(m, p, folder, origin) + # Settings of needs it drops go, so a need that gives the + # same names (postgres, mysql: DB_*) gets its own values + drop_env(name, p["dropped_needs"]) env = dict(os.environ, NAME_ID=name) with open(log, "a") as f: result = subprocess.run(["bash", "-c", UPDATE_SCRIPT, "update", name, @@ -906,6 +917,10 @@ def install_app(m, p, folder, origin, addon_ok, recreate): except (Exception, KeyboardInterrupt) as e: say(f"{e}" if isinstance(e, Refused) else f"Updating failed: {e!r}") raise Refused(roll_back(m, p, backup, log)) from e + # The new version works: undo what the dropped needs set up, + # with their teardown from before the update + if p["dropped_needs"]: + drop_needs(name, p["dropped_needs"], backup, log) check_needs(m) if p["state"] == "update": @@ -1077,6 +1092,46 @@ for need in "$@"; do done ''' +# Teardown of needs an update dropped, from the service folder kept before +# it. Arguments: the kept folder, needs. +DROP_SCRIPT = r''' +cd /federated +. /etc/federated +. lib/functions.sh +. lib/helpers.sh +kept=$1 +shift +for need in "$@"; do + if [ -e "$kept/needs/$need/teardown" ]; then . "$kept/needs/$need/teardown"; fi +done +''' + + +def drop_env(name, needs): + """Remove the settings these needs gave from the app's .env.""" + keys = {line.split("=", 1)[0] for n in needs + for line in read_text(NEEDS_DIR, n, "env.in").splitlines() + if re.match(r"[A-Z_]+=", line)} + if not keys: + return + path = f"{APPS}/{name}/.env" + with open(path) as f: + lines = [line for line in f.read().splitlines() + if line.split("=", 1)[0] not in keys] + write(path, "\n".join(lines) + "\n", 0o600) + + +def drop_needs(name, needs, backup, log): + say(f"Removing what it no longer needs from the Core: {', '.join(needs)}...") + with open(log, "a") as f: + result = subprocess.run(["bash", "-c", DROP_SCRIPT, "drop", + os.path.join(backup, "service"), *needs], + stdout=f, stderr=subprocess.STDOUT) + if result.returncode != 0: + say(f"Warning: removing {', '.join(needs)} didn't finish (log on the Core: {log}); " + "the app runs without them.") + + # After the old service folder is back: its needs' update steps, so what # they set up matches it again (e.g. an Authelia client the update changed) RESYNC_SCRIPT = r''' @@ -1229,10 +1284,17 @@ def wait_for_route(m, p): traefik = service_value(os.path.join(SERVICES, "traefik", "service"), "INTERNAL_IP") if not traefik: return + + def status(*args): + return run(["curl", "-sk", "-o", "/dev/null", "-w", "%{http_code}", "--max-time", "3", + *args]).stdout + # What the app itself answers there (a 404 can be the app's own, so + # wait for traefik to give the same, not for "not 404") + direct = status("-H", f"Host: {host}", "-H", "X-Forwarded-Proto: https", + f"http://{p['address']}:{m['port']}{m['health']}") for _ in range(30): - code = run(["curl", "-sk", "-o", "/dev/null", "-w", "%{http_code}", "--max-time", "3", - "--resolve", f"{host}:443:{traefik}", f"https://{host}{m['health']}"]).stdout - if code not in ("", "000", "404", "502", "503"): + code = status("--resolve", f"{host}:443:{traefik}", f"https://{host}{m['health']}") + if code == direct or code not in ("", "000", "404", "502", "503"): return time.sleep(1) say(f"Note: traefik doesn't route https://{host}/ to it yet; it may take a moment.") @@ -1498,13 +1560,14 @@ def main(args): if os.geteuid() != 0: raise Refused("Run coreapps as root on the Core.") downgrade = "--downgrade" in rest - rest = [a for a in rest if a != "--downgrade"] + drop_database = "--drop-database" in rest + rest = [a for a in rest if a not in ("--downgrade", "--drop-database")] if cmd == "plan" and len(rest) == 1: with tempfile.TemporaryDirectory(prefix="coreapps-") as staging: folder, origin = resolve(rest[0], staging) m = read_manifest(folder) check_release(m, origin) - p = plan(m, origin, downgrade) + p = plan(m, origin, downgrade, drop_database) for key in ("name", "title", "description", "version", "image"): print(f"{key}={m[key]}") print(f"needs={' '.join(m['needs'])}") @@ -1516,7 +1579,7 @@ def main(args): recreate = "--recreate" in rest folders = [a for a in rest if a not in ("--addon-ok", "--recreate")] if len(folders) == 1: - return install(folders[0], addon_ok, recreate, downgrade) + return install(folders[0], addon_ok, recreate, downgrade, drop_database) if cmd == "update" and rest: names = [a for a in rest if a != "--addon-ok"] if len(names) == 1: diff --git a/skills/coreapp/SKILL.md b/skills/coreapp/SKILL.md index 6a1a81a..e7eecb0 100644 --- a/skills/coreapp/SKILL.md +++ b/skills/coreapp/SKILL.md @@ -134,6 +134,16 @@ a Core (`CORE_DOMAIN` set), rather than using the single-user edition. ## Needs +Needs can be added to an installed app (deploy sets them up) and dropped +(deploy removes them once the new version answers; if it doesn't, the +update rolls back with them intact). Dropping `sso` or `email` removes the +sign-in client or mail account and their settings. Dropping `postgres` or +`mysql` deletes the app's database, so deploy refuses it unless the user +asks for that: `coreapp deploy --drop-database`, which asks them to type +the app's name. That's the user's decision; don't run it on your own. +Switching between `postgres` and `mysql` is dropping one and adding the +other: the data doesn't move. + ### `postgres`: its own database The Core makes a PostgreSQL user and database just for this app, and adds: