From 3c2ff029646f0bf4a8f529d873c6441c9b56571a Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Sat, 10 Oct 2026 20:02:57 +0000 Subject: [PATCH 1/2] feat(updates): upgrade page with live steps, and roll back when the new version doesn't start MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Settings → Updates page and banner show each step of an upgrade as the machine goes: back up, download, build, restart, check. If install.sh fails for the new version, the agent checks out the code that ran before and starts it again (LENS_KEEP_CODE=1), and leaves inbox/restore; the previous version restores the backup taken first as it starts, only when the new version had changed the database. Adds the "Email admins about new releases" switch, shown once the server has updates.email. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01FZXPL8F3sAdGJKYYgrVHxb --- docs/updates.md | 20 +- fastapi_backend/app/core/runtime.py | 2 + fastapi_backend/app/domain/migrations.py | 56 ++++ fastapi_backend/app/domain/updates.py | 49 ++- fastapi_backend/app/schemas/updates.py | 8 +- .../tests/api/test_upgrade_rollback.py | 81 +++++ .../tests/domain/test_upgrade_agent.py | 101 ++++++ install.sh | 5 +- .../__tests__/settings-updates.test.tsx | 134 ++++++++ .../app/openapi-client/types.gen.ts | 16 +- .../components/app-shell/app-shell.tsx | 2 + .../components/app-shell/update-banner.tsx | 53 +++ .../components/settings/bodies.tsx | 15 + nextjs-frontend/components/settings/model.ts | 38 +++ .../components/settings/settings-shell.tsx | 2 + .../components/settings/updates-panel.tsx | 304 ++++++++++++++++++ nextjs-frontend/openapi.json | 37 ++- packaging/host/lens-upgrade.sh | 68 +++- 18 files changed, 970 insertions(+), 21 deletions(-) create mode 100644 fastapi_backend/tests/api/test_upgrade_rollback.py create mode 100644 fastapi_backend/tests/domain/test_upgrade_agent.py create mode 100644 nextjs-frontend/__tests__/settings-updates.test.tsx create mode 100644 nextjs-frontend/components/app-shell/update-banner.tsx create mode 100644 nextjs-frontend/components/settings/updates-panel.tsx diff --git a/docs/updates.md b/docs/updates.md index 8c7cc937a..b8da267a9 100644 --- a/docs/updates.md +++ b/docs/updates.md @@ -1,8 +1,8 @@ # Updates and backups Lens tells admins when a new release is out, backs the database up, and installs the release from the web app, so -upgrading never needs a terminal. Status: **built** (the check, backups and upgrades from the API and the agent); -the web app's banner and Settings page come in their own change. +upgrading never needs a terminal. Status: **built**: the check, backups, one-click upgrades with their progress and a +rollback when the new version doesn't start, in Settings → Updates and a banner for admins. ## The check @@ -31,7 +31,21 @@ The containers see the control folder as `/control` (`LENS_CONTROL_DIR`). `POST 3. the agent (`packaging/host/lens-upgrade.sh`) runs install.sh again for that release, exactly as running the install line by hand does: the secrets, the database, the archive, the port and the address are kept; 4. it writes `upgrade-status` and `upgrade.log` next to the inbox, which the API reads back while Lens restarts on the - new version. The database upgrades itself on start ([Database](database.md#upgrades)). + new version, with the step it is on (download, build, start, check), which Settings → Updates shows. The database + upgrades itself on start ([Database](database.md#upgrades)). + +### When the new version doesn't start + +If install.sh fails for the new version (it didn't build, or didn't answer in time), the agent goes back by itself: + +1. it checks out the code that ran before and runs install.sh on it again (`LENS_KEEP_CODE=1`: no download), so the + previous version runs again (`rolling-back`, then `rolled-back`); +2. it leaves `inbox/restore` naming the backup taken first. As the previous version starts, it looks at the database: + if the new version had changed its shape (ran upgrade steps the previous version doesn't know), the backup is put + back; otherwise the database is kept as it is, so nothing written since is lost. What it did is shown with the run. + +Only the API restores the backup; a worker running meanwhile may write to the database as it is put back, so its work +from those seconds can be lost. If going back fails too, the run ends `failed` and says to run the install line. The agent installs only a release tag (`vX.Y.Z`) or the branch the install already follows, whoever writes the request, and the API refuses a release older than the one running (restore a backup instead). diff --git a/fastapi_backend/app/core/runtime.py b/fastapi_backend/app/core/runtime.py index 2e61798c8..1228415fb 100644 --- a/fastapi_backend/app/core/runtime.py +++ b/fastapi_backend/app/core/runtime.py @@ -58,6 +58,8 @@ def _index_text(self) -> None: log.warning("couldn't bring the full-text index up to date", exc_info=True) def prepare(self) -> None: + # first: after a rollback to this version, the database as it was before the upgrade (docs/updates.md) + updates.restore_on_start(self.db, self.base["data_dir"]) templates.seed(self.db) content_types.seed(self.db) routines.seed(self.db) diff --git a/fastapi_backend/app/domain/migrations.py b/fastapi_backend/app/domain/migrations.py index 4a5135b8c..9f6da21eb 100644 --- a/fastapi_backend/app/domain/migrations.py +++ b/fastapi_backend/app/domain/migrations.py @@ -156,6 +156,62 @@ def backup(db, label="manual"): return dst +def restore(db, src): + """Put a backup taken by backup() back: the database becomes what it was then, and what was written since is lost. + Used to go back to the previous version after a failed upgrade (upgrade.restore_if_needed).""" + src = pathlib.Path(src) + scheme, _, rest = db.url.partition("://") + if scheme in ("mem", "memory"): + raise ValueError("an in-memory database has no backups to restore") + if db.embedded: + with db.closed(): + shutil.rmtree(rest, ignore_errors=True) + shutil.copytree(src, rest) + return + # what the backup doesn't hold mustn't survive it: every table goes first (their indexes and events with them) + for table in (db.one("INFO FOR DB") or {}).get("tables") or {}: + db.q(f"REMOVE TABLE IF EXISTS `{table}`") + _import(db, src) + + +def _import(db, src): + """Run a server export (from _export) on this database, as `surreal import` does.""" + import tempfile + import urllib.error + import urllib.request + + base = db.url.split("://", 1) + scheme = {"ws": "http", "wss": "https"}.get(base[0], base[0]) + host = base[1].split("/", 1)[0] + ns, name = db._target + auth = base64.b64encode(f"{db._creds['username']}:{db._creds['password']}".encode()).decode() + with tempfile.TemporaryFile() as sql: + with gzip.open(src, "rb") as f: + shutil.copyfileobj(f, sql, 1 << 20) + size = sql.tell() + sql.seek(0) + req = urllib.request.Request( + f"{scheme}://{host}/import", + data=sql, + method="POST", + headers={ + "Surreal-NS": ns, + "Surreal-DB": name, + "Authorization": f"Basic {auth}", + "Accept": "application/json", + "Content-Length": str(size), + }, + ) + opener = urllib.request.build_opener(urllib.request.ProxyHandler({})) + try: + with opener.open(req, timeout=3600) as r: + body = r.read().decode(errors="replace") + except urllib.error.HTTPError as e: + raise RuntimeError(f"the database refused the backup: {e.read().decode(errors='replace')[:500]}") from e + if '"ERR"' in body: + raise RuntimeError(f"the database refused part of the backup: {body[:500]}") + + def _prune(folder, label): for old in sorted(folder.glob(f"{label}-*"), reverse=True)[KEEP:]: with contextlib.suppress(OSError): diff --git a/fastapi_backend/app/domain/updates.py b/fastapi_backend/app/domain/updates.py index 731b8eae2..c4a4aa62e 100644 --- a/fastapi_backend/app/domain/updates.py +++ b/fastapi_backend/app/domain/updates.py @@ -40,7 +40,8 @@ TICK_SECONDS = 600 NOTES_MAX = 20_000 LOG_LINES = 60 -ACTIVE = ("backing-up", "queued", "running") +ACTIVE = ("backing-up", "queued", "running", "rolling-back") +STEPS = ("download", "build", "start", "check") # what the agent reports it is on (after the backup) # How long a request may sit in the inbox before the web app says nobody picked it up PICKUP_SECONDS = 120 @@ -190,7 +191,9 @@ def run_state(db): for k in ("started_at", "finished_at", "version"): if s.get(k): run[k] = s[k] - if s.get("state") == "failed": + if s.get("step") in STEPS: + run["step"] = s["step"] + if s.get("state") in ("failed", "rolled-back"): run["error"] = s.get("error") or f"install.sh stopped (exit {s.get('code', '?')}); see the log" if run.get("state") in ACTIVE or run.get("id") == s.get("id"): run["log"] = _log_tail(d) @@ -256,7 +259,10 @@ def _hand_over(db, run, backup): run["backup"] = dst.name inbox = control_dir() / "inbox" part = inbox / f".upgrade.{run['id']}" - part.write_text(f"id={run['id']}\nref={run['ref']}\nby={run.get('by') or ''}\nat={_ts()}\n", encoding="utf-8") + part.write_text( + f"id={run['id']}\nref={run['ref']}\nbackup={run.get('backup') or ''}\nby={run.get('by') or ''}\nat={_ts()}\n", + encoding="utf-8", + ) part.replace(inbox / "upgrade") run.update(state="queued", queued_at=_ts()) except Exception as e: # noqa: BLE001 - the admin reads why @@ -267,6 +273,43 @@ def _hand_over(db, run, backup): _put(db, upgrade=run) +# ---------- after a rollback ---------- +def restore_on_start(db, data_dir): + """Called as Lens starts. When the agent went back to this version after a new one didn't start, it leaves + inbox/restore naming the backup taken before the upgrade. If the new version had changed the database (ran steps + this version doesn't know, migrations.status "unknown"), that backup is put back; otherwise the database is kept as + it is, so nothing written since is lost. One process does it: the first to take the file.""" + d = control_dir() + if not d: + return None + req, mine = d / "inbox" / "restore", d / "inbox" / f".restore.{os.getpid()}" + try: + req.replace(mine) + except OSError: + return None # none waiting, or another process took it + want = _kv(mine) + mine.unlink(missing_ok=True) + name = want.get("backup") or "" + newer_steps = [m["name"] for m in migrations.status(db) if m["state"] == "unknown"] + if not newer_steps: + said = "the new version hadn't changed the database, so it was kept as it is" + elif not re.match(r"^[A-Za-z0-9._-]+$", name) or not (pathlib.Path(data_dir) / "backups" / name).exists(): + said = f"the new version had changed the database ({', '.join(newer_steps)}), but its backup {name!r} is missing" + log.error("couldn't restore after the rollback: %s", said) + else: + try: + migrations.restore(db, pathlib.Path(data_dir) / "backups" / name) + said = f"the database was put back as it was before the upgrade ({name})" + log.warning("rolled back: %s; the new version had run %s", said, ", ".join(newer_steps)) + except Exception as e: # noqa: BLE001 - the admin reads why + said = f"restoring {name} failed: {e}"[:500] + log.exception("couldn't restore after the rollback") + run = dict(_row(db).get("upgrade") or {}) + if run.get("id") == want.get("id"): + _put(db, upgrade={**run, "restore": said}) + return said + + # ---------- the background check ---------- def tick(db, cfg, log_fn=None): """Check when due; with auto_install, start the upgrade to a new release once.""" diff --git a/fastapi_backend/app/schemas/updates.py b/fastapi_backend/app/schemas/updates.py index 01c782df2..2034b4e8d 100644 --- a/fastapi_backend/app/schemas/updates.py +++ b/fastapi_backend/app/schemas/updates.py @@ -20,7 +20,13 @@ class Release(ResponseModel): class UpgradeRun(ResponseModel): id: str ref: str = Field(description="the release tag (or branch) being installed") - state: Literal["backing-up", "queued", "running", "done", "failed"] + state: Literal["backing-up", "queued", "running", "rolling-back", "done", "failed", "rolled-back"] = Field( + description="rolling-back / rolled-back: the new version didn't start, so the agent went back to the one before" + ) + step: Literal["download", "build", "start", "check"] | None = Field( + None, description="while running: what the agent is on (the backup comes before)" + ) + restore: str | None = Field(None, description="after a rollback: whether the backup was put back, and why") by: str | None = None asked_at: str | None = None queued_at: str | None = None diff --git a/fastapi_backend/tests/api/test_upgrade_rollback.py b/fastapi_backend/tests/api/test_upgrade_rollback.py new file mode 100644 index 000000000..c29d70a76 --- /dev/null +++ b/fastapi_backend/tests/api/test_upgrade_rollback.py @@ -0,0 +1,81 @@ +"""Going back after a failed upgrade (docs/updates.md): the agent reports each step and the rollback, and the version +it went back to restores the backup taken first only when the new version had changed the database.""" + +from __future__ import annotations + +import pytest + +from app.domain import store, updates +from app.domain.store import R + + +@pytest.fixture +def control(tmp_path, monkeypatch): + d = tmp_path / "control" + (d / "inbox").mkdir(parents=True) + (d / "agent").write_text("kind=systemd\n") + monkeypatch.setenv("LENS_CONTROL_DIR", str(d)) + return d + + +@pytest.fixture +def kept(folder, db): + """A database that can be backed up: the test server when there is one, else an embedded surrealkv one.""" + if not db.embedded: + yield db + return + from tests.conftest import make_cfg + + conn = store.connect(make_cfg(folder, url="surrealkv://" + str(folder / "data" / "surrealdb"))) + yield conn + conn.close() + + +def _upgraded_then_rolled_back(kept, cfg, control, newer_steps): + """An upgrade whose backup was taken; the new version then wrote data (and ran `newer_steps`) and didn't start, so + the agent went back and left the restore request.""" + kept.q("CREATE $r SET title = 'before'", r=R("recording", 1)) + run = updates.upgrade(kept, cfg, "v99.0.0", by="t", wait=True) + assert run["state"] == "queued" and run["backup"].startswith("before-upgrade-") + req = dict(line.split("=", 1) for line in (control / "inbox" / "upgrade").read_text().splitlines()) + assert req["backup"] == run["backup"] # the agent is told which backup to go back to + kept.q("UPDATE $r SET title = 'after'", r=R("recording", 1)) + kept.q("CREATE recording SET title = 'new'") + for name in newer_steps: + kept.q("CREATE migration CONTENT $d", d={"name": name, "done_at": store.now()}) + (control / "inbox" / "restore").write_text(f"id={run['id']}\nbackup={run['backup']}\n") + (control / "upgrade-status").write_text( + f"id={run['id']}\nref=v99.0.0\nstate=rolled-back\nstep=check\ncode=1\nerror=the new version didn't start\n" + ) + return run + + +def test_the_backup_comes_back_when_the_new_version_changed_the_database(kept, cfg, control): + _upgraded_then_rolled_back(kept, cfg, control, ["a-step-from-the-future"]) + said = updates.restore_on_start(kept, cfg["data_dir"]) + assert said.startswith("the database was put back"), said + assert kept.values("SELECT VALUE title FROM recording") == ["before"] + assert "a-step-from-the-future" not in kept.values("SELECT VALUE name FROM migration") + assert not (control / "inbox" / "restore").exists() + run = updates.run_state(kept) + assert run["state"] == "rolled-back" and run["step"] == "check" and run["restore"] == said + assert updates.restore_on_start(kept, cfg["data_dir"]) is None # once + + +def test_the_data_is_kept_when_the_new_version_left_the_database_alone(kept, cfg, control): + _upgraded_then_rolled_back(kept, cfg, control, []) + said = updates.restore_on_start(kept, cfg["data_dir"]) + assert "kept as it is" in said + assert sorted(kept.values("SELECT VALUE title FROM recording")) == ["after", "new"] + + +def test_a_missing_backup_is_said(kept, cfg, control): + run = _upgraded_then_rolled_back(kept, cfg, control, ["a-step-from-the-future"]) + (control / "inbox" / "restore").write_text(f"id={run['id']}\nbackup=../../etc/passwd\n") + assert "missing" in updates.restore_on_start(kept, cfg["data_dir"]) + assert sorted(kept.values("SELECT VALUE title FROM recording")) == ["after", "new"] + + +def test_no_control_folder_nothing_to_do(db, cfg, monkeypatch): + monkeypatch.delenv("LENS_CONTROL_DIR", raising=False) + assert updates.restore_on_start(db, cfg["data_dir"]) is None diff --git a/fastapi_backend/tests/domain/test_upgrade_agent.py b/fastapi_backend/tests/domain/test_upgrade_agent.py new file mode 100644 index 000000000..e73a6f57e --- /dev/null +++ b/fastapi_backend/tests/domain/test_upgrade_agent.py @@ -0,0 +1,101 @@ +"""The machine's upgrade agent (packaging/host/lens-upgrade.sh), run for real: it takes the request from the inbox, +gets the release, runs install.sh and reports each step; when the new version doesn't start it goes back to the code +that ran before, starts it, and leaves the restore request for it (docs/updates.md). + +install.sh is a stand-in that gets the code as the real one does and prints its lines, failing for a broken release; +the repository is a local one with three releases.""" + +from __future__ import annotations + +import pathlib +import shutil +import subprocess + +import pytest + +ROOT = pathlib.Path(__file__).resolve().parents[3] + +FAKE_INSTALL = """#!/bin/sh +set -eu +cd "$LENS_DIR" +if [ "${LENS_KEEP_CODE:-0}" != 1 ]; then + echo "Updating Lens in $LENS_DIR..." + git fetch -q --depth 1 origin "$LENS_REF" && git checkout -q -f -B "$LENS_REF" FETCH_HEAD +fi +echo "Building and starting Lens (the first build takes a few minutes)..." +echo "Waiting for the web app..." +echo "$(cat VERSION) $LENS_KEEP_CODE_SEEN" >>"$LENS_DIR/../started" +if grep -q broken VERSION; then echo "Lens didn't answer within 10 minutes." && exit 1; fi +echo "Lens is running: http://localhost:3000/setup?code=x" +""".replace("$LENS_KEEP_CODE_SEEN", "${LENS_KEEP_CODE:-0}") + +pytestmark = pytest.mark.skipif(not shutil.which("git"), reason="needs git") + + +def git(cwd, *args): + subprocess.run( + ["git", "-c", "user.name=t", "-c", "user.email=t@t", "-c", "init.defaultBranch=main", *args], + cwd=cwd, + check=True, + capture_output=True, + ) + + +@pytest.fixture +def lens(tmp_path): + """An install on v0.6.0 whose origin also has v0.7.0 (works) and v0.8.0 (doesn't start).""" + origin = tmp_path / "origin" + (origin / "packaging" / "host").mkdir(parents=True) + git(origin, "init", "-q") + shutil.copy(ROOT / "packaging" / "host" / "lens-upgrade.sh", origin / "packaging" / "host") + (origin / "install.sh").write_text(FAKE_INSTALL) + (origin / "fastapi_backend").mkdir() + (origin / ".gitignore").write_text(".lens-control/\n.env\n") + for v in ("0.6.0", "0.7.0", "0.8.0-broken"): + (origin / "VERSION").write_text(v) + (origin / "fastapi_backend" / "pyproject.toml").write_text(f'[project]\nversion = "{v.split("-")[0]}"\n') + git(origin, "add", "-A") + git(origin, "commit", "-q", "-m", v) + git(origin, "tag", "v" + v.split("-")[0]) + lens = tmp_path / "lens" + git(tmp_path, "clone", "-q", "--branch", "v0.6.0", str(origin), str(lens)) + git(lens, "checkout", "-q", "-B", "v0.6.0") # as install.sh leaves a release + (lens / ".lens-control" / "inbox").mkdir(parents=True) + return lens + + +def upgrade(lens, ref, backup="before-upgrade-20261010-120000.surql.gz"): + ctl = lens / ".lens-control" + (ctl / "inbox" / "upgrade").write_text(f"id=r1\nref={ref}\nbackup={backup}\nby=t\n") + subprocess.run(["sh", str(lens / "packaging" / "host" / "lens-upgrade.sh"), str(lens)], check=True, timeout=60) + status = dict(line.split("=", 1) for line in (ctl / "upgrade-status").read_text().splitlines()) + return status, (ctl / "upgrade.log").read_text() + + +def test_a_release_is_installed(lens): + status, log = upgrade(lens, "v0.7.0") + assert status["state"] == "done" and status["version"] == "0.7.0" + assert (lens / "VERSION").read_text() == "0.7.0" + assert "Lens is running" in log + assert not (lens / ".lens-control" / "inbox" / "restore").exists() + assert not (lens / ".lens-control" / "inbox" / "upgrade").exists() + + +def test_a_release_that_doesnt_start_is_rolled_back(lens): + status, log = upgrade(lens, "v0.8.0") + assert status["state"] == "rolled-back", log + assert status["version"] == "0.6.0" and "didn't start" in status["error"] + assert status["backup"] == "before-upgrade-20261010-120000.surql.gz" + assert (lens / "VERSION").read_text() == "0.6.0" + # the broken one was started, then the previous one again from the code already there + assert (lens.parent / "started").read_text().splitlines() == ["0.8.0-broken 0", "0.6.0 1"] + restore = (lens / ".lens-control" / "inbox" / "restore").read_text() + assert restore == "id=r1\nbackup=before-upgrade-20261010-120000.surql.gz\n" + # and a later upgrade from there works + assert upgrade(lens, "v0.7.0")[0]["state"] == "done" + + +def test_only_releases_and_this_installs_branch(lens): + status, log = upgrade(lens, "evil-branch") + assert status["state"] == "failed" and "only a release tag" in status["error"] + assert (lens / "VERSION").read_text() == "0.6.0" diff --git a/install.sh b/install.sh index f1425683f..092f2c2f8 100755 --- a/install.sh +++ b/install.sh @@ -22,6 +22,7 @@ # LENS_UPGRADE_AGENT 0: don't set up the agent that lets the web app install new releases (docs/updates.md) # LENS_WIZARD 1: answer everything in the web installer, in the browser (the default for a first install at a # terminal, where Python 3 is there); 0: here in the terminal +# LENS_KEEP_CODE 1: build and start the code already in LENS_DIR, without getting it (the upgrade agent's rollback) set -eu # All in a function, so nothing runs until the whole script has arrived (curl | sh streams it). @@ -256,7 +257,9 @@ fi # --- The code ----------------------------------------------------------------------------------------------------- auth_url="https://github.com/$REPO.git" [ -n "${GITHUB_TOKEN:-}" ] && auth_url="https://x-access-token:$GITHUB_TOKEN@github.com/$REPO.git" -if [ -d "$DIR/.git" ]; then +if [ "${LENS_KEEP_CODE:-0}" = 1 ]; then + [ -f "$DIR/install.sh" ] || fail "LENS_KEEP_CODE=1, but there is no Lens in $DIR." +elif [ -d "$DIR/.git" ]; then say "Updating Lens in $DIR..." git -C "$DIR" fetch -q --depth 1 "$auth_url" "$REF" && git -C "$DIR" checkout -q -B "$REF" FETCH_HEAD elif have git; then diff --git a/nextjs-frontend/__tests__/settings-updates.test.tsx b/nextjs-frontend/__tests__/settings-updates.test.tsx new file mode 100644 index 000000000..e5bcad6f8 --- /dev/null +++ b/nextjs-frontend/__tests__/settings-updates.test.tsx @@ -0,0 +1,134 @@ +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; +import "@testing-library/jest-dom"; + +import { Updates } from "@/app/openapi-client"; +import { UpdatesPanel, steps } from "@/components/settings/updates-panel"; + +jest.mock("@/app/openapi-client", () => ({ + Updates: { + getUpdates: jest.fn(), + checkUpdates: jest.fn(), + startUpgrade: jest.fn(), + listBackups: jest.fn(), + takeBackup: jest.fn(), + deleteBackup: jest.fn(), + downloadBackup: jest.fn(), + }, +})); +jest.mock("next-auth/react", () => ({ useSession: () => ({ data: { accessToken: "t" } }) })); + +const ok = (data: unknown) => Promise.resolve({ data, response: { ok: true, status: 200 } }); +const LATEST = { tag: "v0.7.0", name: "Lens v0.7.0", url: "https://x/v0.7.0", notes: "## Features\n- more" }; +const STATUS = { + current: "0.6.0", + latest: LATEST, + available: true, + checked_at: "2026-10-10T12:00:00Z", + error: null, + check: true, + auto_install: false, + agent: "systemd", + manual: "curl … | LENS_REF=v0.7.0 sh", + upgrade: null, +}; + +function show() { + const qc = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } }); + return render( + + + , + ); +} + +describe("updates", () => { + beforeEach(() => { + (Updates.getUpdates as jest.Mock).mockReset().mockImplementation(() => ok(STATUS)); + (Updates.listBackups as jest.Mock).mockReset().mockImplementation(() => + ok({ + folder: "/data/backups", + backups: [ + { + name: "manual-20261001-101010.surql.gz", + label: "manual", + kind: "export", + bytes: 2048, + taken_at: "2026-10-01T10:10:10+00:00", + }, + ], + }), + ); + (Updates.startUpgrade as jest.Mock) + .mockReset() + .mockImplementation(() => + ok({ ...STATUS, upgrade: { id: "a", ref: "v0.7.0", state: "backing-up", by: "me@x.io", log: [] } }), + ); + }); + + it("offers the new release and installs it after a backup", async () => { + show(); + expect(await screen.findByText("Lens v0.7.0 is out. This is 0.6.0.")).toBeInTheDocument(); + expect(await screen.findByText(/taken by hand · 2.0 KB/)).toBeInTheDocument(); + fireEvent.click(screen.getByRole("button", { name: "Back up and install v0.7.0" })); + await waitFor(() => + expect(Updates.startUpgrade).toHaveBeenCalledWith( + expect.objectContaining({ body: { ref: "v0.7.0", backup: true } }), + ), + ); + expect(await screen.findByText("Backing up")).toBeInTheDocument(); + }); + + it("shows the line to run when the machine has no agent", async () => { + (Updates.getUpdates as jest.Mock).mockImplementation(() => ok({ ...STATUS, agent: null })); + show(); + expect(await screen.findByText("curl … | LENS_REF=v0.7.0 sh")).toBeInTheDocument(); + expect(screen.queryByRole("button", { name: /Back up and install/ })).not.toBeInTheDocument(); + }); + + it("says when it is up to date", async () => { + (Updates.getUpdates as jest.Mock).mockImplementation(() => + ok({ ...STATUS, available: false, latest: { ...LATEST, tag: "v0.6.0" } }), + ); + show(); + expect(await screen.findByText("Lens 0.6.0 is the latest.")).toBeInTheDocument(); + }); + + it("shows each step of the upgrade as the machine goes", async () => { + const run = { id: "a", ref: "v0.7.0", state: "running", step: "build", by: "me@x.io", log: [] }; + (Updates.getUpdates as jest.Mock).mockImplementation(() => ok({ ...STATUS, upgrade: run })); + show(); + const list = await screen.findByRole("list", { name: "Upgrade steps" }); + const states = Array.from(list.querySelectorAll("li")).map((li) => li.getAttribute("data-state")); + expect(states).toEqual(["done", "done", "running", "pending", "pending"]); + }); + + it("says when it went back to the previous version, and what became of the database", async () => { + const run = { + id: "a", + ref: "v0.7.0", + state: "rolled-back", + step: "check", + error: "the new version didn't start, so Lens went back to the one that ran before (0.6.0)", + restore: "the new version hadn't changed the database, so it was kept as it is", + log: [], + }; + (Updates.getUpdates as jest.Mock).mockImplementation(() => ok({ ...STATUS, upgrade: run })); + show(); + expect(await screen.findByText("Went back")).toBeInTheDocument(); + expect(screen.getByText(/so it was kept as it is/)).toBeInTheDocument(); + expect(screen.getByText(/went back to the one that ran before/)).toBeInTheDocument(); + }); +}); + +describe("upgrade steps", () => { + const at = (state: string, step?: string) => + steps({ id: "a", ref: "v1.0.0", state, step, log: [] } as Parameters[0]); + it("follow the run", () => { + expect(at("backing-up")).toEqual(["running", "pending", "pending", "pending", "pending"]); + expect(at("queued")).toEqual(["done", "pending", "pending", "pending", "pending"]); + expect(at("running")).toEqual(["done", "running", "pending", "pending", "pending"]); + expect(at("rolling-back", "start")).toEqual(["done", "done", "done", "failed", "pending"]); + expect(at("done")).toEqual(["done", "done", "done", "done", "done"]); + }); +}); diff --git a/nextjs-frontend/app/openapi-client/types.gen.ts b/nextjs-frontend/app/openapi-client/types.gen.ts index fd83bbb05..33f4163d6 100644 --- a/nextjs-frontend/app/openapi-client/types.gen.ts +++ b/nextjs-frontend/app/openapi-client/types.gen.ts @@ -16007,8 +16007,22 @@ export type UpgradeRun = { ref: string; /** * State + * + * rolling-back / rolled-back: the new version didn't start, so the agent went back to the one before + */ + state: "backing-up" | "queued" | "running" | "rolling-back" | "done" | "failed" | "rolled-back"; + /** + * Step + * + * while running: what the agent is on (the backup comes before) + */ + step?: "download" | "build" | "start" | "check" | null; + /** + * Restore + * + * after a rollback: whether the backup was put back, and why */ - state: "backing-up" | "queued" | "running" | "done" | "failed"; + restore?: string | null; /** * By */ diff --git a/nextjs-frontend/components/app-shell/app-shell.tsx b/nextjs-frontend/components/app-shell/app-shell.tsx index 2ff978bfc..8983abca1 100644 --- a/nextjs-frontend/components/app-shell/app-shell.tsx +++ b/nextjs-frontend/components/app-shell/app-shell.tsx @@ -13,6 +13,7 @@ import { CommandPalette } from "@/components/app-shell/command-palette"; import { NamespaceSwitcher } from "@/components/app-shell/namespace-switcher"; import { Nav } from "@/components/app-shell/nav"; import { Shortcuts } from "@/components/app-shell/shortcuts"; +import { UpdateBanner } from "@/components/app-shell/update-banner"; import { WelcomeTour } from "@/components/home/welcome-tour"; import { PageChat } from "@/components/page-chat/page-chat"; import { Button, IconButton } from "@/components/ui/button"; @@ -177,6 +178,7 @@ export function AppShell({ user, children }: { user: SessionUser; children: Reac
+ {waiting ? (
diff --git a/nextjs-frontend/components/app-shell/update-banner.tsx b/nextjs-frontend/components/app-shell/update-banner.tsx new file mode 100644 index 000000000..b10aca643 --- /dev/null +++ b/nextjs-frontend/components/app-shell/update-banner.tsx @@ -0,0 +1,53 @@ +"use client"; + +import Link from "next/link"; +import { useEffect, useState } from "react"; + +import { useUpdates } from "@/components/settings/updates-panel"; +import { Banner } from "@/components/ui/banner"; + +const KEY = "lens-update-dismissed"; + +/** For admins: a new release is out (until dismissed for that release), or one is being installed. */ +export function UpdateBanner({ admin }: { admin: boolean }) { + const { data: s } = useUpdates(admin); + const [dismissed, setDismissed] = useState(null); + useEffect(() => { + try { + setDismissed(localStorage.getItem(KEY)); + } catch { + // private window: shown again next time + } + }, []); + if (!admin || !s) return null; + const run = s.upgrade; + if (run && ["backing-up", "queued", "running"].includes(run.state) && !run.error) + return ( + + Lens restarts on its own when it’s done.{" "} + + Follow along + + + ); + const tag = s.latest?.tag; + if (!s.available || !tag || dismissed === tag) return null; + return ( + { + setDismissed(tag); + try { + localStorage.setItem(KEY, tag); + } catch { + // fine: dismissed for this visit + } + }} + > + + {s.agent ? "See what’s new and install it" : "See what’s new"} + + + ); +} diff --git a/nextjs-frontend/components/settings/bodies.tsx b/nextjs-frontend/components/settings/bodies.tsx index d7d16bf49..47b6d3ecb 100644 --- a/nextjs-frontend/components/settings/bodies.tsx +++ b/nextjs-frontend/components/settings/bodies.tsx @@ -14,6 +14,7 @@ import { RIGHTS } from "@/components/iiif/rights"; import { hubText } from "@/components/sensors/sensor-model"; import { SecretSetting, SettingField, ZoneBar, type FieldState } from "@/components/settings/fields"; import { SignInProviders } from "@/components/settings/sign-in-providers"; +import { UpdatesPanel } from "@/components/settings/updates-panel"; import { AddConnection } from "@/components/storage/add-connection"; import { NOT_STORAGE, fileStoreTry } from "@/components/storage/file-store"; import { @@ -379,6 +380,20 @@ export function SectionBody({ ctx }: { ctx: BodyCtx }) { return ; case "telemetry": return ; + case "updates": + return ( + <> + + Checking +
+ + {/* shown once the server has the setting (updates.email, from the release notices) */} + {ctx.view.updates?.values && "email" in ctx.view.updates.values && } + +
+ + + ); case "fedora": return ; case "sensors": diff --git a/nextjs-frontend/components/settings/model.ts b/nextjs-frontend/components/settings/model.ts index 6c59496a1..458ee3d45 100644 --- a/nextjs-frontend/components/settings/model.ts +++ b/nextjs-frontend/components/settings/model.ts @@ -74,6 +74,7 @@ export type SectionId = | "mail" | "bridge" | "telemetry" + | "updates" | "fedora" | "sensors" | "uploads" @@ -239,6 +240,13 @@ export const SECTIONS: SectionSpec[] = [ description: "Off unless you turn it on. Traces and metrics from the server and its workers, sent only to an OpenTelemetry collector you run or choose; nothing goes anywhere else.", }, + { + id: "updates", + label: "Updates and backups", + backend: ["updates"], + description: + "New Lens releases: Lens checks for one once a day, backs up and installs it when you say so. And the database backups.", + }, { id: "fedora", label: "Fedora repository", @@ -1866,6 +1874,36 @@ export const FIELDS: FieldSpec[] = [ max: 20, hint: "A failed send waits 30 seconds, then four times longer each time, up to 6 hours", }, + // Updates (docs/updates.md) + { + section: "updates", + key: "check", + label: "Check for new releases", + kind: "switch", + hint: "Asks GitHub for the latest release once a day; nothing about this install is sent", + }, + { + section: "updates", + key: "email", + label: "Email admins about new releases", + kind: "switch", + hint: "Once per release, to every admin; needs an email server (Settings → Email)", + }, + { + section: "updates", + key: "auto_install", + label: "Install new releases by themselves", + kind: "switch", + hint: "Backs up, then installs a new release as soon as it is seen. Off: you choose when", + }, + { + section: "updates", + key: "check_hours", + label: "Check every (hours)", + kind: "int", + min: 1, + max: 720, + }, // Telemetry (opt-in) { section: "telemetry", diff --git a/nextjs-frontend/components/settings/settings-shell.tsx b/nextjs-frontend/components/settings/settings-shell.tsx index 7918f3209..85a77ab47 100644 --- a/nextjs-frontend/components/settings/settings-shell.tsx +++ b/nextjs-frontend/components/settings/settings-shell.tsx @@ -34,6 +34,7 @@ import { MessagesSquare, PackageCheck, RadioTower, + RefreshCw, } from "lucide-react"; import Link from "next/link"; import { useCallback, useState } from "react"; @@ -80,6 +81,7 @@ const ICON: Record = { mail: Mail, bridge: MessagesSquare, telemetry: Activity, + updates: RefreshCw, fedora: Archive, sensors: RadioTower, uploads: Upload, diff --git a/nextjs-frontend/components/settings/updates-panel.tsx b/nextjs-frontend/components/settings/updates-panel.tsx new file mode 100644 index 000000000..2baeccd6e --- /dev/null +++ b/nextjs-frontend/components/settings/updates-panel.tsx @@ -0,0 +1,304 @@ +"use client"; + +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { Check, Circle, Copy, Download, LoaderCircle, RefreshCw, Trash2, X } from "lucide-react"; +import { useState } from "react"; + +import { Updates } from "@/app/openapi-client"; +import type { Backup, UpdateStatus, UpgradeRun } from "@/app/openapi-client/types.gen"; +import { saveBlob } from "@/components/recording/hooks"; +import { Badge, type Tone } from "@/components/ui/badge"; +import { Banner } from "@/components/ui/banner"; +import { Button } from "@/components/ui/button"; +import { Skeleton } from "@/components/ui/states"; +import { ApiError, data, useApiClient } from "@/lib/api/browser"; + +const ACTIVE: UpgradeRun["state"][] = ["backing-up", "queued", "running", "rolling-back"]; + +const RUN: Record = { + "backing-up": { label: "Backing up", tone: "intent" }, + queued: { label: "Waiting for the machine", tone: "intent" }, + running: { label: "Installing", tone: "intent" }, + "rolling-back": { label: "Going back", tone: "gate" }, + done: { label: "Installed", tone: "green" }, + failed: { label: "Failed", tone: "red" }, + "rolled-back": { label: "Went back", tone: "gate" }, +}; + +type StepState = "pending" | "running" | "done" | "failed"; +const STEPS = [ + { id: "backup", label: "Back up the database" }, + { id: "download", label: "Download the new version" }, + { id: "build", label: "Build it" }, + { id: "start", label: "Restart Lens" }, + { id: "check", label: "Check that it answers" }, +] as const; + +/** Where each step is, from the run's state and the step the machine says it is on. */ +export function steps(run: UpgradeRun): StepState[] { + const at = + run.state === "backing-up" + ? 0 + : run.state === "queued" + ? 1 + : run.state === "done" + ? STEPS.length + : Math.max( + 1, + STEPS.findIndex((s) => s.id === (run.step ?? "download")), + ); + const ended = run.state === "failed" || run.state === "rolled-back" || run.state === "rolling-back"; + return STEPS.map((_, i) => { + if (i < at) return "done"; + if (i > at) return "pending"; + if (ended || run.error) return "failed"; + return run.state === "queued" ? "pending" : "running"; + }); +} + +export const UPDATES_KEY = ["admin-updates"]; + +/** The update status, kept fresh while an upgrade runs (the API restarts in between: a failed poll just waits). */ +export function useUpdates(enabled = true) { + const client = useApiClient(); + return useQuery({ + queryKey: UPDATES_KEY, + queryFn: () => data(Updates.getUpdates({ client })), + enabled, + retry: false, + refetchInterval: (q) => { + const s = q.state.data as UpdateStatus | undefined; + return s?.upgrade && ACTIVE.includes(s.upgrade.state) && !s.upgrade.error ? 3_000 : 10 * 60_000; + }, + }); +} + +export function bytes(n: number): string { + if (n < 1024) return `${n} B`; + const u = ["KB", "MB", "GB", "TB"]; + let v = n / 1024; + let i = 0; + while (v >= 1024 && i < u.length - 1) { + v /= 1024; + i++; + } + return `${v.toFixed(v < 10 ? 1 : 0)} ${u[i]}`; +} + +function when(iso?: string | null): string { + return iso ? new Date(iso).toLocaleString() : "never"; +} + +function errText(e: unknown): string { + return e instanceof ApiError ? e.message : "Please try again."; +} + +/** Settings → Updates: the version running, the latest release, one-click upgrade, and the backups. */ +export function UpdatesPanel() { + const client = useApiClient(); + const qc = useQueryClient(); + const status = useUpdates(); + const s = status.data; + const [copied, setCopied] = useState(false); + const refresh = (d: UpdateStatus) => qc.setQueryData(UPDATES_KEY, d); + const check = useMutation({ mutationFn: () => data(Updates.checkUpdates({ client })), onSuccess: refresh }); + const upgrade = useMutation({ + mutationFn: () => data(Updates.startUpgrade({ client, body: { ref: s?.latest?.tag ?? null, backup: true } })), + onSuccess: refresh, + }); + const run = s?.upgrade; + const busy = Boolean(run && ACTIVE.includes(run.state) && !run.error); + + if (status.isPending) return ; + const restarting = status.isError && busy; + if (!s) + return ( + + If it’s installing an update, it comes back on its own in a minute or two. + + ); + + return ( +
+ {s.available && s.latest ? ( + + + Release notes on GitHub + + + ) : ( + + {s.error ? s.error : `Last checked ${when(s.checked_at)}.`} + + )} +
+ {s.available && s.agent && ( + + )} + +
+ {upgrade.error &&

{errText(upgrade.error)}

} + {check.error &&

{errText(check.error)}

} + {s.available && !s.agent && ( +
+

+ This machine can’t install it from here (it wasn’t installed with the install line). Run this on it once, + and updates work from here after that: +

+
+ + {s.manual} + + +
+
+ )} + {restarting && ( + + This page follows along on its own: there is nothing to do. + + )} + {run && } + {s.available && s.latest?.notes && ( +
+ What’s new in {s.latest.tag} +
{s.latest.notes}
+
+ )} + +
+ ); +} + +function RunView({ run }: { run: UpgradeRun }) { + const st = RUN[run.state]; + return ( +
+
+ Upgrade to {run.ref} + {st.label} + + {run.by === "auto_install" ? "by itself" : run.by ? `by ${run.by}` : ""} · {when(run.asked_at)} + +
+
    + {steps(run).map((st, i) => ( +
  1. + {st === "done" ? ( + + ) : st === "running" ? ( + + ) : st === "failed" ? ( + + ) : ( + + )} + {STEPS[i].label} +
  2. + ))} +
+ {run.state === "rolling-back" && ( +

+ The new version didn’t start, so Lens is going back to the version it ran before. +

+ )} + {run.error &&

{run.error}

} + {run.restore &&

After going back, {run.restore}.

} + {run.backup &&

Backed up first: {run.backup}

} + {run.log && run.log.length > 0 && ( +
+          {run.log.join("\n")}
+        
+ )} +
+ ); +} + +function Backups() { + const client = useApiClient(); + const qc = useQueryClient(); + const list = useQuery({ queryKey: ["admin-backups"], queryFn: () => data(Updates.listBackups({ client })) }); + const reload = () => qc.invalidateQueries({ queryKey: ["admin-backups"] }); + const take = useMutation({ mutationFn: () => data(Updates.takeBackup({ client })), onSuccess: reload }); + const remove = useMutation({ + mutationFn: (name: string) => data(Updates.deleteBackup({ client, path: { name } })), + onSuccess: reload, + }); + const download = useMutation({ + mutationFn: async (b: Backup) => { + const blob = (await data( + Updates.downloadBackup({ client, path: { name: b.name }, parseAs: "blob" }), + )) as unknown as Blob; + saveBlob(blob, b.name); + }, + }); + const err = take.error ?? remove.error ?? download.error; + return ( +
+
+

Backups

+ +
+

+ The database, kept in {list.data?.folder ?? "the data folder"}. Lens also backs up before each upgrade and keeps + the last three of those. +

+ {err &&

{errText(err)}

} + {list.isPending ? ( + + ) : !list.data?.backups.length ? ( +

No backups yet.

+ ) : ( +
    + {list.data.backups.map((b) => ( +
  • + + {when(b.taken_at)} + + {b.label === "before-upgrade" ? "before an upgrade" : "taken by hand"} · {bytes(b.bytes)} + + + {b.kind === "export" && ( + + )} + +
  • + ))} +
+ )} +
+ ); +} diff --git a/nextjs-frontend/openapi.json b/nextjs-frontend/openapi.json index cfe128052..7d057c069 100644 --- a/nextjs-frontend/openapi.json +++ b/nextjs-frontend/openapi.json @@ -51993,10 +51993,43 @@ "backing-up", "queued", "running", + "rolling-back", "done", - "failed" + "failed", + "rolled-back" ], - "title": "State" + "title": "State", + "description": "rolling-back / rolled-back: the new version didn't start, so the agent went back to the one before" + }, + "step": { + "anyOf": [ + { + "type": "string", + "enum": [ + "download", + "build", + "start", + "check" + ] + }, + { + "type": "null" + } + ], + "title": "Step", + "description": "while running: what the agent is on (the backup comes before)" + }, + "restore": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "title": "Restore", + "description": "after a rollback: whether the backup was put back, and why" }, "by": { "anyOf": [ diff --git a/packaging/host/lens-upgrade.sh b/packaging/host/lens-upgrade.sh index 29c132abb..8d6e625d4 100755 --- a/packaging/host/lens-upgrade.sh +++ b/packaging/host/lens-upgrade.sh @@ -7,7 +7,8 @@ # how it went into .lens-control/upgrade-status and upgrade.log, which the web app shows. # # It takes a release tag (vX.Y.Z), or the branch this install already follows; nothing else, whoever wrote the -# request. Run by hand: sh packaging/host/lens-upgrade.sh +# request. If the new version doesn't start, it goes back to the code that ran before and starts that (rolled-back). +# Run by hand: sh packaging/host/lens-upgrade.sh set -eu umask 022 # the containers read the status and the log @@ -26,6 +27,7 @@ trap 'rmdir "$CTL/.running" 2>/dev/null || true' EXIT val() { sed -n "s/^$1=//p" "$2" | head -1 | tr -cd 'A-Za-z0-9._/-'; } id="$(val id "$REQ")" ref="$(val ref "$REQ")" +backup="$(val backup "$REQ")" rm -f "$REQ" now() { date -u +%Y-%m-%dT%H:%M:%SZ; } status() { # state [extra key=value lines] @@ -36,32 +38,72 @@ status() { # state [extra key=value lines] echo "started_at=$started" shift for kv in "$@"; do echo "$kv"; done + if [ -n "$backup" ]; then echo "backup=$backup"; fi } >"$CTL/upgrade-status.tmp" && mv "$CTL/upgrade-status.tmp" "$CTL/upgrade-status" } started="$(now)" : >"$CTL/upgrade.log" -branch="$(git -C "$DIR" rev-parse --abbrev-ref HEAD 2>/dev/null || true)" +# (not rev-parse --abbrev-ref: a branch named like a tag, as install.sh makes for a release, comes out heads/vX.Y.Z) +branch="$(git -C "$DIR" symbolic-ref -q HEAD 2>/dev/null | sed 's|^refs/heads/||' || true)" if ! printf '%s' "$ref" | grep -Eq '^v[0-9]+\.[0-9]+\.[0-9]+$' && [ "$ref" != "$branch" ]; then echo "Not upgrading to '$ref': only a release tag (vX.Y.Z) or this install's branch ($branch)." >>"$CTL/upgrade.log" status failed "finished_at=$(now)" "code=2" "error=only a release tag (vX.Y.Z) or this install's branch can be installed" exit 0 fi -status running +status running "step=download" # The same settings this install was made with (install.sh keeps the port and the address itself) env_of() { [ -f "$DIR/.env" ] && sed -n "s/^$1=//p" "$DIR/.env" | head -1; } +# install.sh, with its lines in the log; the step it is on goes into the status as it goes. Its exit code, after. +install() { # extra environment, as NAME=value words + rm -f "$CTL/.code" + { + ( + cd "$DIR" && + env LENS_DIR="$DIR" LENS_REF="$ref" LENS_NO_OPEN=1 LENS_YES=1 LENS_WIZARD=0 LENS_AGENT_RUN=1 \ + LENS_REPO="$(env_of LENS_REPO)" LENS_TARGET="$(env_of LENS_TARGET)" LENS_BACKEND="$(env_of LENS_BACKEND)" \ + GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=safe.directory GIT_CONFIG_VALUE_0="$DIR" "$@" \ + sh "$DIR/install.sh" + ) &1 || echo "$?" >"$CTL/.code" + } | while IFS= read -r line; do + printf '%s\n' "$line" >>"$CTL/upgrade.log" + case "$line" in + *"Building and starting"* | *"Getting the base image"*) status "$phase" "step=build" ;; + *"Starting Lens in a microsandbox"* | *"Waiting for the web app"*) status "$phase" "step=start" ;; + *"Lens is running"*) status "$phase" "step=check" ;; + esac + done + rc="$(cat "$CTL/.code" 2>/dev/null || echo 0)" + rm -f "$CTL/.code" + return "$rc" +} + +# the code that runs now, to come back to if the new version doesn't start +prev="$(git -C "$DIR" rev-parse HEAD 2>/dev/null || true)" +phase=running set +e -( - cd "$DIR" && - LENS_DIR="$DIR" LENS_REF="$ref" LENS_NO_OPEN=1 LENS_YES=1 LENS_WIZARD=0 LENS_AGENT_RUN=1 \ - LENS_REPO="$(env_of LENS_REPO)" LENS_TARGET="$(env_of LENS_TARGET)" LENS_BACKEND="$(env_of LENS_BACKEND)" \ - GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=safe.directory GIT_CONFIG_VALUE_0="$DIR" \ - sh "$DIR/install.sh" -) >>"$CTL/upgrade.log" 2>&1 /dev/null)" != "$prev" ]; then + # The new version didn't start: the previous code comes back. When the new one had already changed the database + # (steps the previous version doesn't know), the previous version restores the backup taken first as it starts + # (updates.restore_on_start); otherwise the database is kept as it is, with nothing lost. + phase=rolling-back + status rolling-back "step=check" "code=$code" + echo "--- The new version didn't start (exit $code): going back to the version that ran before ($prev)" >>"$CTL/upgrade.log" + if [ -n "$backup" ]; then printf 'id=%s\nbackup=%s\n' "$id" "$backup" >"$CTL/inbox/restore"; fi + set +e + git -C "$DIR" -c safe.directory="$DIR" checkout -q -f -B "${branch:-lens}" "$prev" >>"$CTL/upgrade.log" 2>&1 && + install LENS_KEEP_CODE=1 + back=$? + set -e + rolled=$([ "$back" = 0 ] && echo yes || echo no) +fi + # run as root for a Docker only root can use: what it wrote stays the install's owner's if [ "$(id -u)" = 0 ]; then owner="$(stat -c %u:%g "$DIR" 2>/dev/null || stat -f %u:%g "$DIR")" @@ -71,6 +113,12 @@ fi version="$(sed -n 's/^version = "\(.*\)"/\1/p' "$DIR/fastapi_backend/pyproject.toml" 2>/dev/null | head -1)" if [ "$code" = 0 ]; then status "done" "finished_at=$(now)" "code=0" "version=$version" +elif [ "$rolled" = yes ]; then + status rolled-back "finished_at=$(now)" "code=$code" "version=$version" \ + "error=the new version didn't start, so Lens went back to the one that ran before ($version)" +elif [ "$rolled" = no ]; then + status failed "finished_at=$(now)" "code=$code" \ + "error=the new version didn't start, and going back to the previous one failed too: run the install line on the machine" else status failed "finished_at=$(now)" "code=$code" fi From 9bc77d4966a6edd3c822b8ad06cbbaa05ea76f78 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Sat, 10 Oct 2026 20:22:55 +0000 Subject: [PATCH 2/2] fix(tunnel): write the tunnel's status in the order it was read tick() snapshots the runner's state as cloudflared starts and writes it after; the log reader can snapshot and write the newer state (the error cloudflared printed) in between, and then tick's older write lands last, so the row said no error. Snapshot and write now happen under one lock. Seen as a flaky test_tunnel on the SurrealDB server CI job. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01FZXPL8F3sAdGJKYYgrVHxb --- fastapi_backend/app/domain/tunnel.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/fastapi_backend/app/domain/tunnel.py b/fastapi_backend/app/domain/tunnel.py index f5bbdf1da..6888aff9c 100644 --- a/fastapi_backend/app/domain/tunnel.py +++ b/fastapi_backend/app/domain/tunnel.py @@ -268,6 +268,9 @@ def __init__(self, db, cfg_fn, name, log=None): self.fails = 0 self.next_try = 0.0 self.lock = threading.Lock() + # snapshot and write together: otherwise an older snapshot (tick's, taken as cloudflared starts) can reach the + # database after a newer one (the log reader's), and the row keeps saying there is no error + self.saving = threading.Lock() def _lease(self): now = _now() @@ -282,16 +285,17 @@ def _lease(self): ) def _save(self, **extra): - with self.lock: - d = { - "running": self.proc is not None and self.proc.poll() is None, - "connected": self.connected, - "url": self.url, - "error": self.error, - "log": json.dumps(self.lines[-LOG_LINES:]), - **extra, - } - self.db.q("UPDATE $r MERGE $d WHERE owner = $me", r=ROW, d=d, me=self.name) + with self.saving: + with self.lock: + d = { + "running": self.proc is not None and self.proc.poll() is None, + "connected": self.connected, + "url": self.url, + "error": self.error, + "log": json.dumps(self.lines[-LOG_LINES:]), + **extra, + } + self.db.q("UPDATE $r MERGE $d WHERE owner = $me", r=ROW, d=d, me=self.name) def _signature(self, cfg): t = conf(cfg)