Files
Kamino/roles/_binary_update/README.md
T
Defame1297 234648919c
Build / build (pull_request) Successful in 27s
Molecule / molecule (pull_request) Successful in 4s
Lint / lint (pull_request) Successful in 1m21s
docs(roles): bring every role README to the fixed heading set
Every public role README has the headings Supported platforms, Requirements
and run order, Role variables, Entry points (when several), Variables set by
the role (when any), Tags, Example play, Tests and Notes, in that order, with
an "Idempotent" line in Notes. Role-specific sections move under Entry points
or Notes; nothing is removed. The internal _binary_update README keeps its own
headings and gains the platform and Idempotent lines.

Closes #136

Assisted-by: Claude Code:claude-opus-5-5
2026-11-25 17:24:27 +00:00

74 lines
3.5 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 on Ubuntu 24.04 (noble) 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.
Idempotent: yes.