diff --git a/PLAN.md b/PLAN.md index 4a9bb4e..3d47fb0 100644 --- a/PLAN.md +++ b/PLAN.md @@ -587,6 +587,31 @@ Steps: - [ ] David: sign in to listmonk-app as admin@dev.fedcom.net (Super Admin, Settings), and send a campaign from `listmonk-app@dev.fedcom.net`; remove it when done + - Rehearsed 2026-12-04, updating an app in use: a copy of the + listmonk port as `listmonk-upd` on 5.1.0 with real data (lists, + subscribers, a draft campaign, changed settings, an upload, a + sign-in), updated to 6.2.0 (three upstream migrations, one that + hashes API tokens for good): data, settings, sessions and sign-in + came through, and an update made to fail after migrating rolled + back to 5.1.0 exactly. Deployed at + `https://listmonk-upd.dev.fedcom.net/`. Fixed from it: `coreapp + deploy` from a folder refuses a lower `version` without + `--downgrade` (as `coreapp install` does); versions with `-N` + (`6.2.0-1`, the Coreapp's own fix) order after the upstream one, + for releases too; a failed start keeps the container's whole log + (`-failed-start.log`; the printed end missed the first + start, where the migrations ran); `coreapp status` shows the + installed version beside the folder's. Skill: references/ + updating.md (versions, what to check between versions, testing an + update over real data and its rollback, no copy after a + successful update), reading the installer's `bash -x` log + - [ ] Decide: keep the last pre-update database copy (and `/data` + snapshot) after a successful update, until the next one, so a + Core owner can go back days later (`coreapps restore `)? + Today it's deleted once the new version answers; it costs the + database's size on disk per app + - [ ] David: sign in to listmonk-upd as admin@dev.fedcom.net + (settings page, visual editor on 6.2.0); remove it when done - Rehearsed 2026-12-04 with a new app from scratch, Lending Shelf (`coreapp new --postgres --sso --email`; Python, postgres, sso, email): deployed at `https://lending-shelf.dev.fedcom.net/`, Core diff --git a/README.md b/README.md index 1a62c16..c634b8f 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,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] [--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 deploy [--recreate] [--drop-database] [--downgrade]` | 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. A lower `version` than the installed one is refused without `--downgrade`, since an older version may not understand the newer one's data | | `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 b419dac..407f4d0 100644 --- a/TEST.md +++ b/TEST.md @@ -258,6 +258,16 @@ The Core's own scripts trace secrets, so their output goes to deploys; breaking it again in an update gives the same message with the wget error, rolls back, and right after "Rolled back" the public address answers 200 (the rollback waits for traefik). +5i. Versions from a folder: with `6.2.0` installed, deploying `6.1.9` is + refused before building ("has version 6.2.0 installed; 6.1.9 is + older ... add --downgrade"); `coreapp deploy --downgrade` deploys it; + `6.2.0-1` and the same `6.2.0` deploy without asking. Releases order + `v1.2.0` < `v1.2.0-2` < `v1.10.0`. `coreapp status` shows "This + folder: ..." and "Installed version: 6.2.0" from the Core. +5j. A container that fails to start (prints a line and exits): deploy + prints the end of its log and "Its whole log, from its first start: + /federated/logs/coreapps/-failed-start.log", which has every + restart's lines with timestamps; the same after an unhealthy check. 5g. A slow start (`coreapp new slowtest`, with `CMD` sleeping 40 s, logging "migrating", before the server): deploy prints "still starting (31 s); its log: migrating", then "Installed". A container that exits diff --git a/bin/coreapp b/bin/coreapp index 51f119a..7fd18ec 100755 --- a/bin/coreapp +++ b/bin/coreapp @@ -6,7 +6,7 @@ # 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] [--drop-database] +# coreapp deploy [--recreate] [--drop-database] [--downgrade] # build, copy to the Core, install or update # (--recreate: a new container even if nothing # changed; --drop-database: allow dropping @@ -248,12 +248,13 @@ cmd_new() { # --- deploy ----------------------------------------------------------------- cmd_deploy() { - local plan state tier answer local_id core_id dropped addon="" recreate="" dropdb="" + local plan state tier answer local_id core_id dropped addon="" recreate="" dropdb="" downgrade="" while [[ $# -gt 0 ]]; do case "$1" in --recreate) recreate=--recreate ;; --drop-database) dropdb=--drop-database ;; - *) die "Usage: coreapp deploy [--recreate] [--drop-database]" ;; + --downgrade) downgrade=--downgrade ;; + *) die "Usage: coreapp deploy [--recreate] [--drop-database] [--downgrade]" ;; esac shift done @@ -264,7 +265,7 @@ 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 $dropdb)" || exit 1 + plan="$(installer plan $dropdb $downgrade)" || exit 1 state="$(sed -n 's/^state=//p' <<< "$plan")" tier="$(sed -n 's/^tier=//p' <<< "$plan")" dropped="$(sed -n 's/^dropped_needs=//p' <<< "$plan")" @@ -301,7 +302,7 @@ cmd_deploy() { # Install, or update (which recreates the container if the image changed, # even under the same version) - installer install $addon $recreate $dropdb || exit 1 + installer install $addon $recreate $dropdb $downgrade || exit 1 [[ "$state" == new ]] && echo "(coreapp check tests it at its public address, as people reach it.)" return 0 } @@ -343,7 +344,8 @@ cmd_status() { local address core "docker ps -a --filter name='^$NAME\$' --format 'Container: {{.Names}} {{.Status}} image {{.Image}}' [ -n \"\$(docker ps -aq --filter name='^$NAME\$')\" ] || echo 'Not installed on the Core.'" - echo "App: $TITLE $(image)" + echo "This folder: $TITLE $(image)" + echo "Installed version: $(core "sed -n 's/^IMAGE_VERSION=//p' /federated/apps/$NAME/.env 2> /dev/null || true")" address="$(core "sed -n 's/^INTERNAL_IP=//p' /federated/services/$NAME/service 2> /dev/null || true")" [[ -z "$address" ]] || echo "Address on the Core: $address, port $PORT" echo "Needs: ${NEEDS:-nothing}" diff --git a/core/coreapps b/core/coreapps index 7c5c4fe..94172c4 100755 --- a/core/coreapps +++ b/core/coreapps @@ -504,12 +504,25 @@ def normalize_url(url): return f"{scheme}://{host.lower()}/{path}" +# A release's version: numbers with dots, and a -N for the Coreapp's own +# fixes on the same upstream version (8.8.6-2 comes after 8.8.6) +VERSION_ORDER = r"([0-9]+(?:\.[0-9]+)*)(?:-([0-9]+))?" + + def is_release(tag): - return bool(re.fullmatch(r"v[0-9]+(?:\.[0-9]+)*", tag or "")) + return bool(re.fullmatch("v" + VERSION_ORDER, tag or "")) + + +def version_order(version): + """Comparable form of a version like 6.2.0 or 6.2.0-2, else None.""" + found = re.fullmatch(VERSION_ORDER, version or "") + if not found: + return None + return [int(part) for part in found.group(1).split(".")], int(found.group(2) or 0) def version_key(tag): - return [int(part) for part in tag[1:].split(".")] + return version_order(tag[1:]) def git(args, cwd=None, auth=None): @@ -696,6 +709,17 @@ def plan(m, origin=None, downgrade=False, drop_database=False): "To install it anyway, add --downgrade (if it fails to start, the update " "rolls back).") p["downgrade_from"] = installed_tag + # The same from a folder (coreapp deploy), by coreapp.toml's version + installed_version = service_value(f"{APPS}/{name}/.env", "IMAGE_VERSION") + old, new = version_order(installed_version), version_order(m["version"]) + if not origin and old and new and new < old: + if not downgrade: + raise Refused( + f"'{name}' has version {installed_version} installed; {m['version']} is " + "older, and may not understand the data the newer version left (its " + "database and /data). To deploy it anyway, add --downgrade (if it fails " + "to start, the update rolls back).") + p["downgrade_from"] = installed_version else: p["new_needs"] = list(m["needs"]) p["dropped_needs"] = [] @@ -1485,7 +1509,17 @@ def wait_until_answering(m, p): sys.stderr.write(logs.stdout + logs.stderr) raise Refused(f"{name} doesn't answer at {url} after " f"{time.monotonic() - started:.0f} s (state: {state or 'missing'}; " - "its log is above).") + f"the end of its log is above). {keep_failed_log(name)}") + + +def keep_failed_log(name): + """The failed container's whole log, every restart (its first start, + which ran the migrations, too), before a rollback removes it.""" + path = log_path(name, "failed-start") + with open(path, "w") as out: + subprocess.run(["docker", "logs", "--timestamps", name], stdout=out, + stderr=subprocess.STDOUT) + return f"Its whole log, from its first start: {path} on the Core." def wait_for_route(m, p): @@ -1513,7 +1547,7 @@ def wait_for_route(m, p): return health = container_health(m["name"]) if health == "unhealthy": - raise Refused(unhealthy_message(m, host)) + raise Refused(f"{unhealthy_message(m, host)} {keep_failed_log(m['name'])}") if health != "starting" and time.monotonic() - started > 30: break time.sleep(2) diff --git a/skills/coreapp/SKILL.md b/skills/coreapp/SKILL.md index f069bab..fa6e720 100644 --- a/skills/coreapp/SKILL.md +++ b/skills/coreapp/SKILL.md @@ -73,7 +73,7 @@ title = "My App" # shown in the Core's dashboard description = "What it does, in a sentence" docs = "https://example.com/docs" # optional; installed from a repository, defaults to it image = "localhost/coreapp/my-app" # image name; the Core runs image:version -version = "0.1.0" # the image tag; raise it for each release +version = "0.1.0" # the image tag; raise it for each release (6.2.0, then 6.2.0-1) port = 8080 # the port the app serves HTTP on (any; 8080 is usual) health = "/health" # a path that answers once it's up needs = ["postgres", "sso"] # what it needs from the Core, or [] ("mysql", "email" too) @@ -365,6 +365,13 @@ ends by running the upstream one (and written at every start, for settings the app keeps in its database); waiting for the database and Authelia at startup; a non-root user; its own `icon.png`; one container. +### 2c. Updating an app (a new upstream version or release) + +Read [references/updating.md](references/updating.md) first: versions +(`-N` for the Coreapp's own fixes), what to check between versions, +testing an update over real data and its rollback, and what the Core's +database copy covers. + ### 3. Deploy and test ```bash @@ -407,7 +414,7 @@ parts were tested that way rather than through a real sign-in. | Command | Shows | |---|---| -| `coreapp status` | Its container, address, port, needs, URL, and the Core's PostgreSQL or MariaDB version if it needs one | +| `coreapp status` | Its container, the version installed and the folder's, address, port, needs, URL, and the Core's PostgreSQL or MariaDB version if it needs one | | `coreapp mail` | What the Core's mail server did with the mail it sent (apps with `email`) | | `coreapp inbox [--clear]` | The mail the test account got: the newest 10, with their text (`--clear` empties it) | | `coreapp logs` / `coreapp logs -f` | The app's own log | @@ -425,9 +432,11 @@ Common causes: ran before, with its settings, its database and `/data` as they were before the update (`/data` only where the Core's filesystem can copy it instantly; the message says), and undid needs the update added. Whatever - the failed version wrote is gone. Its log is printed above that line - (`coreapp logs` now shows the old version's). When the version wasn't - raised, the image that failed is the one running again. + the failed version wrote is gone. The end of its log is printed above + that line, and its whole log, from its first start, is kept on the Core + (deploy says where; `coreapp logs` now shows the old version's). When + the version wasn't raised, the image that failed is the one running + again. - "keeps a copy of its database ... Nothing was changed. Free some space first.": before an update the Core copies the app's database, to roll back to, and the apps' disk hasn't room for it (it keeps 1 GB free for @@ -455,7 +464,8 @@ Common causes: The installer's own log (the Core's install scripts) is on the Core in `/federated/logs/coreapps/`. It contains secrets, so don't print it in full; look at the end of it with `coreapp-vm ssh tail -n 40 /federated/logs/coreapps/-install.log` -only when a deploy fails in the installer itself. +only when a deploy fails in the installer itself. It's a shell trace +(`bash -x`): look for the last command before the error, not for words. ### 5. Publishing (only when the user asks for a release) diff --git a/skills/coreapp/references/publishing.md b/skills/coreapp/references/publishing.md index f2d50b0..8de7a3e 100644 --- a/skills/coreapp/references/publishing.md +++ b/skills/coreapp/references/publishing.md @@ -52,4 +52,4 @@ To check a release the way a Core's owner will get it: publish a new version. - Going back to an older release needs `coreapp install --downgrade @v`; the user decides that, since the older version may not - understand the newer one's data. + understand the newer one's data (see references/updating.md). diff --git a/skills/coreapp/references/updating.md b/skills/coreapp/references/updating.md new file mode 100644 index 0000000..f7023bb --- /dev/null +++ b/skills/coreapp/references/updating.md @@ -0,0 +1,69 @@ +# Updating an app: a new upstream version, or a new release of your own + +Read this before moving an installed app to a new version (a newer +upstream image, or your own app's next release). SKILL.md has the deploy +and publish commands around it. + +## Versions + +`version` in `coreapp.toml` is the image's tag and the release's +(`v`). For a port, follow upstream's version (`6.2.0`); for a +release that changes only the Coreapp on the same upstream version (the +entrypoint, a setting), add `-N` (`6.2.0-1`, `6.2.0-2`): the Core orders +`6.2.0` < `6.2.0-1` < `6.2.1`. Raise it for every release; while you're +trying things on your own Core, you don't need to. + +A Core refuses an older version than the one it has, unless asked: +`coreapp deploy --downgrade` from a folder, `coreapp install --downgrade` +from a repository. That's for the user to decide: an older version may not +understand what the newer one left in its database and `/data`. + +## Before: what changes + +- Read upstream's release notes for every version in between (not only + the newest), their upgrade steps and their migrations (in the source: + `migrations/`). Note migrations that can't be undone (data rewritten, + tokens hashed) and slow ones (rebuilt indexes or views on big tables). +- Check what your Coreapp writes still fits: the settings your entrypoint + maps or writes into the database (renamed or moved keys), the upstream + entrypoint and `CMD` your entrypoint ends with, the image's user, ports, + `VOLUME`s and `HEALTHCHECK` (`docker inspect` both images and compare), + and a new minimum database version (`min_version`). +- "Back up the database first", in upstream's steps, is done by the Core: + before every update it copies the app's database (and `/data`, where + its filesystem can do that instantly) and rolls back to that copy if the + new version doesn't start. Upstream's own upgrade command (`--upgrade`, + `migrate`) belongs in the entrypoint, so it runs at every start. + +## Testing an update + +Test it over an install that has real data, not a fresh one: + +1. Deploy the version people run now (a copy under another `name` is + fine), and use it the way people do: content, a changed setting, an + upload, a `coreapp signin`. Commit that as the released version. +2. Record what's there (through the app's API or SQL as its database + user), so you can compare after. +3. Deploy the new version over it and compare: the data, the settings + the Core writes (sign-in and mail still work), sessions, uploads. Sign + in again, and as a brand-new account (`coreapp signin --remove-account` + first): new versions change first sign-in and roles. +4. `coreapp deploy --recreate` once more on the new version. + +To see a failed update roll back, deploy a throwaway version (say +`6.2.0-failtest`, not committed) whose entrypoint runs the migrations and +then exits: deploy says "Rolled back", and the old version runs with its +database as before. A throwaway version is needed: when the version isn't +raised, the image that failed is the one running again. The failed +container's whole log, from its first start (where the migrations ran), +is kept on the Core; deploy prints where. + +## After: there's no copy to go back to + +The Core's copy of the database is only for the update itself: once the +new version answers, it's deleted. If a problem shows up later, the way +forward is a fixed release (`-N`); going back to the older version means +the older version on the newer data (refused without `--downgrade`, and +often broken by migrations it doesn't know), or the Core's own backups, +which are the user's. Say this in your report when an update has +migrations that can't be undone.