Skip to content

Update server from browser - #37

Merged
mathewdunne merged 15 commits into
mathewdunne:mainfrom
eprig:add-update-panel
Nov 29, 2026
Merged

mathewdunne merged 15 commits into
mathewdunne:mainfrom
eprig:add-update-panel

Conversation

@eprig

@eprig eprig commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds an Admin → Update tab that updates a self-hosted, Compose-deployed
CodeRunner from the browser. An admin picks a published release or a branch
from one searchable list and confirms. A release pulls the prebuilt GHCR
images; a branch is built on the server. The server finishes with the same
docker compose up -d an operator would run, waits for the control
container's healthcheck, and rolls back automatically if it fails.

The manual flow keeps working, and the GCE Deploy workflow is unchanged. The
design and its trade-offs are recorded in
docs/decisions/052-admin-server-updates.md.

How it works

  • Short-lived helper container. POST /admin/server-update starts a
    one-shot sibling container from the currently running control image,
    with the host checkout and data dir mounted. It outlives the control
    container it replaces, which is what makes the health wait and rollback
    possible. Job state lives in data/server-update.json, so the new control
    process can report the result after the restart.
  • :latest means "what this server runs". The helper pulls …:vX.Y.Z
    (or builds …:branch-<name>), re-tags those images as :latest, then runs
    docker compose up -d. A hand-run docker compose up -d therefore always
    redeploys the local images; docker compose pull still fetches the newest
    release. Nothing version-related is written to .env.
  • Safety:
    • rollback re-tags the previous images by ID;
    • old versioned tags are removed only after a healthy start;
    • a second update while one is running is refused (409);
    • a job whose helper disappeared (host reboot, docker kill) is marked
      failed instead of locking the tab.
  • Uses the stack you started. Compose calls use the control container's
    own com.docker.compose.* labels (project, config files, service), so
    stacks started with -f work.

UI

  • Running now: version (release, or the branch via a new
    coderunner.git.branch image label), commit title, author and age, update
    available or up to date, and the last update (result, when, how long, who).
  • Choose a version: one searchable dropdown with Releases and Branches
    sections, labelled Current and Latest. For the selection it shows the
    release notes, the commits it adds, or the commits it removes for a
    downgrade, plus a one-line plan. Active students and builds are shown only
    when someone would be interrupted.
  • While updating: an overall progress bar with an elapsed timer and a
    collapsible command log. A failed poll during the restart shows
    "Reconnecting…", and a Reload prompt appears once the new version is up.
  • GitHub API: responses are cached to stay under the anonymous limit of
    60 requests/hour; Check for updates forces a refresh. GITHUB_TOKEN is
    optional.

Build changes

  • No legacy builder. The control image now ships buildx. Without it,
    docker build in the helper fell back to the legacy builder, which leaves
    TARGETARCH empty and fails this Dockerfile.
  • Fewer base images to pull:
    • the Docker CLI, buildx and Compose are checksum-pinned static downloads,
      not copied out of docker:27.5.1-cli;
    • the PathPlanner dist is fetched on oven/bun instead of debian.
  • AdvantageScope reuse. scripts/image.ts skips the emsdk compile
    (~30 min) when an identical build exists: a local copy in .cache/, or a
    release's ascope-dist.tar.gz whose inputs match exactly (submodule
    commit, patches, both build scripts, the Dockerfile stage). Measured
    locally: a control build took 3m22s, versus about 33 minutes cold.
    CODERUNNER_ASCOPE_PREBUILT=0 always compiles.
  • Stabler cache key. The AdvantageScope build context excludes volatile
    submodule git files (reflogs, FETCH_HEAD, tool folders).

Compatibility

  • No new required .env settings. CODERUNNER_UPDATE_REPO_DIR (rarely
    needed) and GITHUB_TOKEN are optional.
  • Legacy CODERUNNER_TAG in .env still works. Compose runs that tag,
    and the updater keeps the line current. This covers GCE, whose
    render-env.sh writes it.
  • First upgrade is by hand. Upgrading to the first release that has this
    tab must be done with the existing CLI flow, which installs a control image
    that has the updater.
  • Where it works. The tab is read-only without a Compose-managed git
    checkout: plain docker run, the GCE VM (compose files are copied, not
    cloned), and from-source dev. Demo mode shows sample data.

Security

  • Both endpoints are behind requireAdmin. Starting an update is audited as
    server.update and records the admin's email.
  • Only refs returned by the GitHub API are accepted.
  • The helper is effectively root with the Docker socket, which admin access
    already implies. It gets the same SELinux label=disable exemption as the
    control service (decision 040).

Testing

  • Checks: bun run check and bun run typecheck are clean.
  • Web tests: 203/203 pass.
  • Bun tests: 645/646. The one failure moves between runs:
    idle-and-admin ("Cannot use a closed database" from a run-readiness
    timer firing after its test closed the app), or a 5 s timeout in
    imports. Both files pass on their own (3/3 runs each), and this branch
    doesn't touch that code. It showed up while the machine was heavily loaded.
  • Updater helper: run end to end against a fake docker and a throwaway
    git repo: release update, unhealthy start with rollback, legacy .env
    pin, failed pull, and a non-git checkout.
  • Against real Docker:
    • re-tagging :latest plus docker compose up -d recreates the container;
    • a full bun run docker:build:control reused upstream v0.6.10's
      AdvantageScope with no emsdk layers pulled;
    • the image contains Docker 27.5.1, buildx 0.20.1, Compose 2.39.2 and git.

Known issues / follow-ups

  • Test helper starts real containers. withApp in
    apps/control/src/__tests__/helpers.ts falls back to the real docker
    when a test passes no fake. Some test then starts real workspace
    containers that outlive the run and fill /tmp. This predates this PR; the
    helper should default to a fake runner.
  • Root-owned files. The helper runs as root, so files it checks out may
    be root-owned on the host.
  • Not yet trimmed. The workspace image still pulls both node:16 and
    node:22 for two small builder stages.

🤖 Generated with Claude Code

I'm open to any changes and suggestions. Consider squashing and merging this pr because of the large number of commits (I had to test on an external server)

eprig and others added 12 commits November 26, 2026 22:21
- Control builds reuse an identical AdvantageScope Lite build instead of
  pulling emsdk and compiling (~30 min): .cache/ascope-dist/<key>, else a
  release's ascope-dist.tar.gz (origin, then upstream) whose inputs match.
  Keyed on the inputs as they are on disk; CODERUNNER_ASCOPE_PREBUILT=0
  always compiles. build-ascope-lite.ts matches v0.6.10 again so releases
  can be reused.
- Install the Docker CLI and buildx as checksum-pinned static downloads
  instead of copying them out of docker:27.5.1-cli.
- Fetch the PathPlanner dist on the oven/bun image instead of debian.
- Render the version picker's dropdown in a portal so the card no longer
  clips it; it flips above near the window bottom and always scrolls.
- Drop the "same code as what's running" message and empty details box.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Replace the running step text with one overall progress bar, with the
  elapsed timer on its left. Progress is estimated from the step states
  (weighted per release or branch) plus the running step's own signals
  (BuildKit stage counter, which image is downloading, health wait) and
  never moves backwards; the raw step text is the bar's tooltip.
- Remove the Server checkout field from the Running now card.
- Last update is one line: result (incl. "rolled back"), age, duration,
  admin. The ref is only named when it differs from what is running.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Progress row is now timer, percentage, then a shorter bar, without the
  fixed-width gap after the timer.
- Last update uses real " · " separators in the text instead of CSS gaps,
  so it reads and copies correctly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@eprig
eprig marked this pull request as ready for review November 28, 2026 18:59
@mathewdunne mathewdunne self-assigned this Nov 29, 2026
@mathewdunne

Copy link
Copy Markdown
Owner

This is a cool idea! Claude flagged a few issues:

1. A running update can be marked failed — apps/control/src/server-update.ts (reconcileLostJob)
inspectContainer returns null on any non-zero docker exit and never throws, so the catch { // Docker didn't answer; don't guess } branch can't run. After the 30s startup grace period, one failed inspect during a long build marks the job failed and unlocks the Update tab while the helper is still working, so a second update can start.
Fix: use inspectContainerOrThrow and treat only /no such container/i as lost, the same pattern isContainerRunning uses. Any other error goes to the existing catch and the job is left alone. Please add a test where the runner fails with a non-"no such container" error and the job stays running.

2. PathPlanner network errors now fail local builds — scripts/docker-pathplanner-dist.ts
downloadAndExtract only returns false on an HTTP error status; fetch throws on DNS or connection errors. The catch calls process.exit(1) without checking required, so offline or firewalled builds with the default latest now fail. The old curl stage warned and continued, and this file's header says latest is best-effort.
Fix: in the catch, exit 1 only when required; otherwise print the existing WARNING and continue to the mkdir.

3. The helper's docker compose loses settings passed on the command line — scripts/update-server.ts / startServerUpdate
The helper only sees .env, but the README tells Linux/WSL users to run CODERUNNER_DOCKER_GID=$(stat -c '%g' /var/run/docker.sock) docker compose up. For them, compose up -d drops the docker group, the new control can't reach the socket, and the health check fails. The rollback runs compose up -d with the same missing settings, so it fails too and the server stays down. The same applies to CODERUNNER_UID/GID and CODERUNNER_HOST_DATA_DIR set on the command line.
Fix: forward what the control container is actually running with. Self-inspect already reads it: Config.User → CODERUNNER_UID/CODERUNNER_GID, HostConfig.GroupAdd → CODERUNNER_DOCKER_GID, config.hostDataDir → CODERUNNER_HOST_DATA_DIR. Shell values override .env in compose, so the helper recreates exactly what's running now.

4. The helper runs from the mutable image name, not the running image — apps/control/src/server-update.ts (docker run args in startServerUpdate)
The helper is started from imageInfo.image (e.g. …:latest). If :latest has moved since the control started (a manual docker compose pull without up -d), the helper runs a different update-server.ts from the deployed one.
Fix: imageInfo.imageId ?? imageInfo.image.

5. AGENTS.md wasn't updated
It still lists decisions as "011–051 active", and Current Status doesn't mention the updater. Please bump the range to 011–052 and add a short "Admin server updates (post-V2)" paragraph pointing at docs/decisions/052-admin-server-updates.md.

(also, we should probably change the cemo strings and tests to mathewdunne/CodeRunner and ghcr.io/mathewdunne)

I can fix these and merge or you can fix them if you want, let me know. Cool idea though and I appreciate the PR

@eprig

eprig commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

I can fix them with claude tomorrow. Glad you like it

eprig and others added 2 commits November 29, 2026 01:30
…wdunne

- reconcileLostJob: only "no such container" marks a job lost; other
  docker errors leave it running
- helper forwards running UID/GID, docker group, and host data dir to
  compose, and runs from the running image ID instead of its tag
- docker-pathplanner-dist: network errors only fail pinned builds
- AGENTS.md: decisions 011–052, admin server updates paragraph
- demo strings and tests use mathewdunne/CodeRunner and ghcr.io/mathewdunne

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@eprig

eprig commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

I had claude implement your changes, let me know if anything else needs to be done.

Compose resolves both images from CODERUNNER_IMAGE_NS, so a namespace
passed on the command line instead of .env was lost when the helper ran
`docker compose`, and a fork deployment would resolve upstream images.
Forward the running control image's registry prefix with the other
runtime settings; locally named images have none and leave .env in charge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mathewdunne
mathewdunne merged commit f5eb72e into mathewdunne:main Nov 29, 2026
1 check passed
@mathewdunne

Copy link
Copy Markdown
Owner

Thanks! Just made one small fix and then merged! You'll have to do the first update the old way, but then the updater should work. I appreciate the contribution!

@eprig
eprig deleted the add-update-panel branch November 29, 2026 23:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants