Upgrading a server
How a running server moves to a new release, and back. docs/self-hosting.md, "Updates", is the
short form for deploy/compose.yml; this page adds what to read first, the rollback and why it
works. The commands use the dc alias from docs/self-hosting.md (a relay: the alias in
deploy/README.md), from the repository root.
A server release is the git tag v<version> and the images
ghcr.io/rim-platform/rim-server:<version> and ghcr.io/rim-platform/rim-imagecheck:<version>
its release workflow publishes; RIM_VERSION in deploy/.env names them. The web client is
rim-ui's own release, versioned on its own: RIM_WEB_VERSION names its image
ghcr.io/rim-platform/rim-web:<version> (unset, it is RIM_VERSION). A server upgrade leaves
the web client at its version, and the web client moves alone ("The web client" below).
CHANGELOG.md lists what each release changes, and a
line starting with Upgrade: is something the operator does before or after the upgrade.
Before #
- Note the version running now:
dc exec rim /app/rim-server --version. It is also in/healthzand in the first log line. - Read every
CHANGELOG.mdsection between that version and the new one, and do what its Upgrade: lines say. - Go one release at a time when you can. Skipping releases brings the schema to the newest one just the same, but the binary rollback below is only safe to the release right before the new one, and after a skip that is not the release you ran.
- Take a backup (
docs/self-hosting.md, "Backups"). It is the only way back further than the release before.
Upgrade #
deploy/upgrade.sh runs the steps of this section in one go. Put the new release's files in
place first, then run it with the version:
git fetch --tags && git checkout v<version> # or copy the tag's deploy/ over yours
deploy/upgrade.sh --dry-run <version> # checks the images, prints the rest
deploy/upgrade.sh <version>
In order, it:
- checks that the version's images are published, and
rim-webat the web client's version when the web client runs; when one is not, it says which, changes nothing and exits 2; - verifies the signatures against the release workflows, as
docs/self-hosting.md, "Verify the images", says (cosign must be installed):rim-webagainst rim-ui's of the tagv<web client version>. The digest each signature is over is kept: the images are pulled by it in step 5; - runs
deploy/backup.sh, or says there is nothing to back up when the deployment has no data volume yet (the first start); - writes
RIM_VERSION=<version>intodeploy/.env, keeping the file before asdeploy/.env.previous. When the web client runs andRIM_WEB_VERSIONis unset, it writesRIM_WEB_VERSIONtoo: the version that ran before, so the web client stays as it is, or on the first start<version>. ARIM_WEB_IMAGEofghcr.io/rim-platform/rim-web:<version>becomesRIM_WEB_VERSION=<version>in the same write. The verified digests go in too (RIM_SERVER_DIGEST,RIM_IMAGECHECK_DIGESTandRIM_WEB_DIGEST), so compose runs exactly the images whose signatures were checked, and a tag moved in the registry in between changes nothing. With a digest set, change a version with this script, not by editing.env; - pulls the images, runs the migrations (
dc run --rm migrate) anddc up -d; - waits up to 90 seconds (
RIM_UPGRADE_TIMEOUT) for/readyz, then as long forhttps://<RIM_DOMAIN>/.well-known/rim-serverfrom the host; - reloads Caddy, so a Caddyfile that changed with the release applies, and prints the version
/healthzreports.
When a step after the env file fails, it prints the tails of the migrate and rim logs, puts
deploy/.env.previous back, starts the version before as "Rolling back" below does, and exits
- On a first start there is no version before, and it stops the stack instead. A version older
than the running one is that binary rollback: no migrations, and
rimandwebstart without their dependencies.
It reads COMPOSE_FILE from the environment or from deploy/.env, as the backup scripts do, so
a relay sets it once in deploy/.env (COMPOSE_FILE=deploy/compose.yml:deploy/compose.relay.yml,
relative to the directory the scripts run from). It touches no git, and refuses to run while
RIM_SERVER_IMAGE or RIM_IMAGECHECK_IMAGE is set, since those override the version. Any
other RIM_WEB_IMAGE than a published rim-web is a custom web client: the script says so and
neither checks nor changes it. A server that runs published images only can leave all of this
to rim-deploy, which installs each release of rim-server and of rim-ui as it is published
(deploy/README.md, "Unattended upgrades from releases").
The same steps by hand:
deploy/backup.sh
git fetch --tags && git checkout v<version>
$EDITOR deploy/.env # RIM_VERSION=<version>
dc pull # pulls the new images; verify them next
dc run --rm migrate # applies the new migrations, then exits
dc up -d
dc logs -f rim
Between dc pull and the migrations, verify the new images' signatures as
docs/self-hosting.md, "Verify the images", says. With deploy/compose.dev.yml, which builds
the images from checkouts, check out the rim-ui release the web client runs next to this
repository and run dc build in place of dc pull.
dc run --rm migrate runs /app/migration up from the new image against the database while the
old server is still serving; that is safe because a release's migrations only add (below). dc up -d alone also runs it before it starts the new server; running it first shows the migrations
succeed before anything restarts.
Check the new server:
dc ps # rim: healthy
dc exec rim /app/rim-server --version
dc exec rim curl -fsS http://localhost:8080/readyz; echo
When /readyz fails, curl -s without -f shows the body; a migrations check with the
reason N pending means the migrations did not run.
Without compose the order is the same: run the new build's migrations while the old server
keeps serving, DATABASE_URL=… /app/migration up (or cargo run --release -p migration -- up
from a checkout), then restart rim-server on the new binary.
While a container restarts, Caddy cannot reach it and answers in its place
(handle_errors 502 503 504 in deploy/Caddyfile and deploy/Caddyfile.relay): 503 with
Retry-After: 30 and Cache-Control: no-store. Requests to /api, /s2s/ and /.well-known/
get JSON ({"error":"unavailable","message":"The server is being updated. …"}), which the
apps read as the server being unreachable and other servers retry; a browser gets a page saying
RIM is being updated, which reloads itself every 30 seconds. /healthz and /readyz stay
404 from outside. The same answer covers any other time rim-server or the web client is down.
Rolling back #
The binary, to the release before. Migrations stay applied; the previous release's server
runs on the newer schema. The previous image's migrate service does not: it refuses a database
holding migrations it does not know,
Custom Error: Migration file of version '…' is missing, this migration has been applied but its file is missing
and rim waits for migrate to succeed. So start the old server without its dependencies:
git checkout v<previous version>
$EDITOR deploy/.env # RIM_VERSION=<previous version>
dc pull rim imagecheck
dc up -d --no-deps rim imagecheck
The web client keeps its own version. From the previous release's
checkout, deploy/upgrade.sh <previous version> does the same after a backup. The old server's
/readyz passes: it counts only the migrations it knows as pending. Until the next upgrade, a plain
dc up -d stops at the failing migrate; use --no-deps as above. When you upgrade again there is
no migration left to run.
Bots. After bots exist, calls naming a bot's id fail with an internal error on the older
binary, because it can't read the new kind. Deactivate bots before rolling back.
Post as bot. Once a workspace stores a PostAsBot action, an older binary can't read it, and that
workspace's pipelines stop listing and firing until the action is removed. The older binary can't remove
it either, so remove these actions before rolling back. This lists them:
SELECT id, server_id FROM pipeline WHERE actions_data::text LIKE '%PostAsBot%';
Webhook formats. An older binary ignores a webhook's template, so that webhook acts as a plain one:
a body with a string text posts that text, and any other body answers 400 and posts nothing. It still
lists and can be revoked, and the next upgrade brings its format back. A webhook on the GitHub preset with
no format of its own keeps working.
Home layouts. The table stays, but an older binary has no homeLayout, saveHomeLayout or
resetHomeLayout and doesn't list home_layout. The app then shows its default Home with no way
to edit it, and a save it sends before it notices is refused as an unknown field and says the
layout wasn't saved. The stored layouts are untouched and come back with the next upgrade.
Further back. Restore the backup taken before the upgrade (docs/self-hosting.md,
"Restore"). Everything written since that backup is lost.
The web client goes back as it goes forward, alone (below).
Never migrate down a server with data. Each migration has a down, for development and
tests: on a real database it drops tables and columns with their rows, and some refuse to run
once there are rows to lose.
The web client #
The web client is static files: a rim-ui release moves it forward or back with no migration
and no backup, and the server keeps running. Verify the image as docs/self-hosting.md,
"Verify the images", says, with the rim-ui release in place of $v, then:
$EDITOR deploy/.env # RIM_WEB_VERSION=<rim-ui version>
dc pull web
dc up -d --no-deps web
dc ps web # web: healthy
Back is the same with the version before. On a host that rim-deploy runs,
rim-deploy web <version> does these steps, checks the signature, and puts the env file back
when the container does not become healthy. An Upgrade: line in the changelogs says when a
web client needs a newer server, which then goes first.
Why migrations only go forward #
A release's migrations expand the schema: new tables, new columns that are nullable or have a default, new indexes. The previous release's server ignores what it does not know, so it keeps working on the expanded schema, which is what makes the binary rollback and the upgrade with the old server still running safe. Removing something takes two releases: the first stops using the table or column, the next one's migration drops it (the contract). A rename is an expand (the new name, filled from the old), then a contract one release later.
So the release right before always runs on the current schema. One further back may not: it
may still use what a contract dropped. A release with a contract migration says so in
CHANGELOG.md with an Upgrade: line, which tells an operator who skipped the release before
it that the backup is their only way back.