Files
Defame1297andClaude Opus 5.5 f4965db6f3
Build / build (pull_request) Successful in 12s
Lint / lint (pull_request) Successful in 1m17s
Molecule / molecule (pull_request) Successful in 11m34s
feat(_binary_update): add the internal binary update role
Swaps a staged, verified binary into place for a systemd service: refuse when nothing
executable is staged, then reload, stop and wait, back up the live binary, rename the
staged file over it, start and wait for active, and compare the reported version. A
failure after the stop restores this run's backup, starts the service again and fails
with a message that says whether the restore and the restart worked. Under --check the
waits and the version check are skipped, so a preview against a stopped service does
not fall into the rollback. The staged file is removed in every case.

The role is internal (ADR 0004 in the environment repository): called only by roles of
this Collection by FQCN, outside the public API, marked by the underscore, [INTERNAL]
in its meta and argument_specs, and a first task that refuses a call without a parent
role. traefik is its first caller and arrives in the same release.

Two molecule scenarios use stub binaries under a stub unit, without network: default
(successful bump and idempotence, a binary that cannot start, a binary that reports the
wrong version, nothing staged, a direct call, a first install) and check_mode (the bump
under molecule check against a stopped service changes nothing).

Lint: role-name is skipped (its regex rejects the leading underscore, as for
prometheus.prometheus roles/_common), meta has no role_name (schema[meta] uses the same
regex and cannot be skipped), and .ansible-lint-ignore lists the role's own files for
var-naming[no-role-prefix], which strips the underscore from the variable but not from
the role name and so cannot pass. The ignore file is kept out of the Collection build
and is on the scenario picker's IGNORED list. The README Roadmap no longer lists the role.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
2026-11-20 19:00:19 +00:00

72 lines
3.4 KiB
Markdown

# _binary_update
**Internal role.** It is called only by other roles of this Collection and is not part of the
Collection's public API: its interface can change in any release, without a major version.
Do not call it from a play; it refuses to run without a parent role.
It swaps a new binary into place for a systemd service and rolls back when the update fails. The
calling role does the download and the verification (a checksum, a signature, a checksum file:
each upstream differs) and stages the verified file next to the live binary before it includes
this role, so a slow or failed download never stops the service.
Variables and their types are defined in [`meta/argument_specs.yml`](meta/argument_specs.yml),
which is the source of truth.
## What it does
1. Refuses to continue, with the service untouched, when nothing executable is staged.
2. Reloads systemd, stops the service and waits until it has stopped.
3. Copies the live binary to `<binary>.bak`, when there is one.
4. Moves the staged file over the live binary: one rename on one filesystem, so the live path
never holds a partial file.
5. Starts the service, waits until it is active and compares the version the new binary
reports with the expected version.
6. Removes the backup.
Any failure after the stop restores the backup of this run, starts the service again and fails
with a message that says whether the restore and the restart worked. The staged file is removed
in every case. A `.bak` left by an earlier run is never restored, only replaced.
Under `--check` the waits and the version check are skipped, so a preview against a stopped
service does not report a rollback; the stop, the backup and the start report what they would
change (for a running service with a live binary: all three).
Limits:
- The backup keeps the owner, group and mode of the live binary, but nothing else (no extended
attributes or file capabilities).
- Only one backup is kept. When a restore fails, restore `<binary>.bak` by hand before the next
run: that run replaces it.
- "Active" is checked once the unit reports it. A `Type=simple` service that crashes a moment
after it started passes the wait and is caught only by the version check, if at all.
## Calling it
```yaml
- name: Swap in the new binary
ansible.builtin.include_role:
name: mortis.kamino._binary_update
apply:
tags: [myservice, update]
vars:
_binary_update_service: myservice
_binary_update_binary_path: /usr/local/bin/myservice
_binary_update_staged_path: /usr/local/bin/myservice.new
_binary_update_version_command: [/usr/local/bin/myservice, --version]
_binary_update_expected_version: "{{ myservice_version }}"
tags: [myservice, update]
```
An include does not pass its tags on to the included tasks, so pass them with `apply` as well.
Compare the installed version first and stage and include only when it differs; the role
itself always stops and starts the service.
## Testing
Two molecule scenarios run against stub binaries (shell scripts) under a stub unit, with no
network: `default` covers a successful bump, its idempotence, a binary that cannot start, a
binary that reports another version (both rolled back), a run with nothing staged (the service
is not stopped), a direct call from a play (refused) and a first install with nothing to back
up. `check_mode` runs the bump under `--check` against a stopped service and checks that
nothing changed. The calling roles' own scenarios cover a real upstream binary.