feat: self-update of master and agents (#15) (#32)

This commit was merged in pull request #32.
This commit is contained in:
2026-10-13 10:17:52 +00:00
parent ec28d1e027
commit 850a670446
39 changed files with 1421 additions and 130 deletions
@@ -229,13 +229,46 @@ Online/offline is held in memory only.
| Master restarts mid-job | Jobs in `running` are reconciled when the agent reconnects; jobs the agent does not know are marked `failed` ("lost"). |
| New trigger while a job for the same service is queued or running | No new job; only `available_*` is updated. Two jobs never race on one service. |
| Job exceeds `UPDATE_TIMEOUT` | The agent cancels it, as today. |
| Agent updates itself | The old agent runs `up -d` in a helper container and waits. If the helper exits while the old agent still runs, the old agent reports the outcome. If the helper stops the old agent, the old agent records nothing; the new agent finds the helper by its labels before it connects, so its hello lists the job, and it reports the helper's outcome. If no new agent resumes the job, it is marked lost when the agent reconnects without it. If `UPDATE_TIMEOUT` passes first, the old agent reports a timeout and leaves the helper running; a later success is then not recorded. |
| Polling fails for one image | Error stored in `last_poll_error` and shown; other images continue. |
### Self-protection
### Self-update
The agent reports its own container and the master knows its own service. Both are shown
as "updated manually outside the system", and the master refuses to create jobs for
them. An agent recreating itself would kill the job it is running.
The agent reports its own container with `self`; the overview shows "This agent". The
master's own Compose service and every Compose agent's own service are normal update
targets. A Swarm agent's own service is refused ("an agent's own Swarm service cannot
update itself").
- **Master.** No special execution. The agent on the master's host runs the job; the
recreate drops its connection, and it sends the result again after the new master is up
(unacknowledged results are resent).
- **Agent.** The old agent does the usual checks, writes the override and pulls. Only
`up -d <service>` runs in a helper container made from the agent's own image, with the
agent's own bind and volume mounts and no Compose labels. The helper's labels
(`se.shcizo.package-updater.self-update.` plus `job`, `ref`, `started`, `project`,
`service`, `output`) hold what the new agent needs: the job ID, the pinned ref, the
start time and the output so far (tail, 32 KiB). If the helper exits while the old agent
still runs, the old agent copies its logs into the job output, removes it and reports
the outcome itself.
- **Resume.** At startup, before the runner and the connection start, the agent lists
helpers whose project and service labels match its own container and registers each as
a job at the front of its queue. The job is then in `known_jobs` of the first hello.
Its result is a normal `job.result`: output, helper logs, pinned ref on exit 0,
otherwise an error. A helper with unusable labels fails the job with a message saying so.
- **Stopping.** When the agent is stopped (SIGTERM) while a job runs, the runner drops the
job and records no result, so a "context canceled" result cannot overtake the new
agent's.
- **Order.** `job.start` carries `self`; the agent runs a job for its own service only
when no other job is queued (the master's job on a shared host has to finish first).
The master starts such jobs after the other jobs of the same poll cycle or webhook
request; messages to one agent arrive in order, so the others are queued first. A job
that reaches an agent while it replaces itself is marked lost when the new agent
connects without it. Whether to use a helper is decided by the agent from its own
container ID, not from the flag.
- **Protocol 4.** The master routes a job for an agent's own service, and any job for a
service with the `AGENT_IMAGE` repo (an older agent does not report itself), only to an
agent speaking protocol 4 (`agent too old to update itself`). The helper runs on the
default network, so `DOCKER_HOST` must be a `docker.sock` bind mount.
## Registry polling
@@ -355,7 +388,7 @@ Each phase ends with something that works on its own.
snapshot and live events, Postgres storage, OIDC login, Overview and Agents pages.
The overview works; nothing can be updated yet.
2. **Jobs and manual update.** Job protocol, agent queue and executors (Compose override
file, Swarm with digest), job history, Service and Jobs pages, self-protection.
file, Swarm with digest), job history, Service and Jobs pages.
3. **Webhook and auto mode.** `POST /update` on the master, matching, waiting for
auto jobs, mode toggle.
4. **Polling and version history.** `image_versions`, registry polling, version picker
@@ -372,6 +405,5 @@ Each phase ends with something that works on its own.
- Export/import of a fleet configuration to restore a setup. The `services` table
already holds what an export needs.
- Writing version bumps back to git.
- Self-update of master and agents (`internal/selfupdate` is the starting point).
- Updating plain (non-Compose, non-Swarm) containers.
- Credential management UI for registries.