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
This commit is contained in:
1 parent
d3cbcefd54
commit
f4965db6f3
22 files changed
+882
-3
No files matched your search
@@ -17,3 +17,8 @@ exclude_paths:
|
||||
# names (mortis.kamino.<role>, used by the scenarios) resolve the same way: scripts/ansible-lint.sh,
|
||||
# which the hook runs, links the checkout as ansible_collections/mortis/kamino. A bare `ansible-lint`
|
||||
# does not do that and reports the role names as not found; run it through the hook.
|
||||
|
||||
skip_list:
|
||||
# role-name requires ^[a-z][a-z0-9_]*$, which rejects the leading underscore that marks an
|
||||
# internal role (_binary_update), as in prometheus.prometheus (roles/_common).
|
||||
- role-name
|
||||
@@ -0,0 +1,11 @@
|
||||
# var-naming[no-role-prefix] cannot pass inside a role whose name starts with an underscore:
|
||||
# ansible-lint strips leading underscores from the variable name but not from the role name, so
|
||||
# `_binary_update_x` is compared with the prefix `_binary_update_` as `binary_update_x` and fails.
|
||||
# The variables do carry the prefix. Only the internal role's own files are listed; a caller's
|
||||
# include_role by FQCN is not checked against that prefix.
|
||||
roles/_binary_update/defaults/main.yml var-naming[no-role-prefix]
|
||||
roles/_binary_update/tasks/main.yml var-naming[no-role-prefix]
|
||||
roles/_binary_update/molecule/check_mode/verify.yml var-naming[no-role-prefix]
|
||||
roles/_binary_update/molecule/default/verify.yml var-naming[no-role-prefix]
|
||||
roles/_binary_update/molecule/default/tasks/assert_state.yml var-naming[no-role-prefix]
|
||||
roles/_binary_update/molecule/resources/roles/binary_update_caller/tasks/main.yml var-naming[no-role-prefix]
|
||||
@@ -40,9 +40,8 @@ the environment that uses it) are met.
|
||||
|
||||
Still to come:
|
||||
|
||||
- Wave 2: `_binary_update` (internal shared binary-update logic, called only by Collection
|
||||
roles; ships with `traefik`), `pve_vm`, `pve_lxc`, `traefik`, `gitea`, `gitea_runner`,
|
||||
`tailscale`, `cloudflared`, `pihole`.
|
||||
- Wave 2: `pve_vm`, `pve_lxc`, `traefik`, `gitea`, `gitea_runner`, `tailscale`,
|
||||
`cloudflared`, `pihole`.
|
||||
- Wave 3: `rke2`, `proxmox_host`.
|
||||
- Later, wave not yet assigned: `opnsense`, `proxmox_firewall`.
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
trivial:
|
||||
- >-
|
||||
_binary_update - new internal role that swaps a staged, verified binary into place for a systemd service and
|
||||
rolls back on failure. Called only by roles of this Collection; not part of the public API.
|
||||
@@ -42,6 +42,7 @@ build_ignore:
|
||||
- .yamllint
|
||||
- docs
|
||||
- .ansible-lint
|
||||
- .ansible-lint-ignore
|
||||
- CLAUDE.md
|
||||
- CONTRIBUTING.md
|
||||
- GLOSSARY.md
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
# _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.
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
# Internal role: see meta/argument_specs.yml. The caller sets the required inputs.
|
||||
|
||||
# One capture group: the version without a leading v. Its first match in the output of
|
||||
# _binary_update_version_command is compared with _binary_update_expected_version.
|
||||
_binary_update_version_regex: 'v?([0-9]+\.[0-9]+\.[0-9]+)'
|
||||
|
||||
# Seconds to wait for the service to stop, and to become active after a start.
|
||||
_binary_update_stop_timeout: 60
|
||||
_binary_update_start_timeout: 30
|
||||
@@ -0,0 +1,72 @@
|
||||
---
|
||||
argument_specs:
|
||||
main:
|
||||
short_description: "[INTERNAL] Swap a staged binary into place for a systemd service, with rollback"
|
||||
description:
|
||||
- >-
|
||||
[INTERNAL] Called only by roles of this Collection, by its fully qualified name; a direct call from a play
|
||||
fails. Not part of the Collection's public API, so its interface can change in any release.
|
||||
- >-
|
||||
The caller downloads and verifies the new binary and stages it at C(_binary_update_staged_path), on the same
|
||||
filesystem as C(_binary_update_binary_path), before it includes this role. The role refuses to stop the
|
||||
service when nothing executable is staged.
|
||||
- >-
|
||||
Then it reloads systemd, stops the service and waits for it to stop, backs up the live binary to
|
||||
C(<_binary_update_binary_path>.bak) (when there is one), moves the staged file over the live path, starts the
|
||||
service, waits for it to become active and compares the version it reports with
|
||||
C(_binary_update_expected_version). On success the backup is removed.
|
||||
- >-
|
||||
Any failure after the stop restores the backup taken by this run, starts the service again and fails with a
|
||||
message that says whether the restore and the restart worked. A failure before the stop leaves the service
|
||||
running and does not restart it. The staged file is removed in every case.
|
||||
- >-
|
||||
Under C(--check) the role skips the waits and the version check, so a preview against a stopped service does
|
||||
not report a rollback. The stop, backup and start report what they would change.
|
||||
options:
|
||||
_binary_update_service:
|
||||
type: str
|
||||
required: true
|
||||
description:
|
||||
- Name of the systemd unit that runs the binary.
|
||||
_binary_update_binary_path:
|
||||
type: path
|
||||
required: true
|
||||
description:
|
||||
- Path of the live binary that the unit runs.
|
||||
_binary_update_staged_path:
|
||||
type: path
|
||||
required: true
|
||||
description:
|
||||
- >-
|
||||
Downloaded and verified new binary, executable, on the same filesystem as C(_binary_update_binary_path) so
|
||||
that the swap is a rename. Removed by the role whether the update succeeds or fails.
|
||||
_binary_update_version_command:
|
||||
type: list
|
||||
elements: str
|
||||
required: true
|
||||
description:
|
||||
- >-
|
||||
Command, as an argument list, whose output holds the version of the binary at
|
||||
C(_binary_update_binary_path), for example C([/usr/local/bin/traefik, version]).
|
||||
_binary_update_expected_version:
|
||||
type: str
|
||||
required: true
|
||||
description:
|
||||
- Version the new binary must report. A leading C(v) is ignored.
|
||||
_binary_update_version_regex:
|
||||
type: str
|
||||
default: 'v?([0-9]+\.[0-9]+\.[0-9]+)'
|
||||
description:
|
||||
- >-
|
||||
Regular expression with one capture group, the version without a leading C(v). Its first match in the
|
||||
output of C(_binary_update_version_command) is compared with C(_binary_update_expected_version).
|
||||
_binary_update_stop_timeout:
|
||||
type: int
|
||||
default: 60
|
||||
description:
|
||||
- Seconds to wait for the service to stop. It is checked every 2 seconds.
|
||||
_binary_update_start_timeout:
|
||||
type: int
|
||||
default: 30
|
||||
description:
|
||||
- Seconds to wait for the service to become active after a start, also after a rollback. It is checked every 2 seconds.
|
||||
@@ -0,0 +1,17 @@
|
||||
---
|
||||
galaxy_info:
|
||||
namespace: mortis
|
||||
author: Defame1297
|
||||
license: GPL-3.0-or-later
|
||||
description: >-
|
||||
[INTERNAL] Swap a staged, verified binary into place for a systemd service, check its version and roll back
|
||||
on failure. Called only by roles of this Collection; not part of its public API.
|
||||
min_ansible_version: "2.19"
|
||||
platforms:
|
||||
- name: Ubuntu
|
||||
versions:
|
||||
- noble
|
||||
galaxy_tags:
|
||||
- systemd
|
||||
- update
|
||||
dependencies: []
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
# Run only by the check step, so under --check: a preview of the bump from 1.0.0 to 2.0.0
|
||||
# against a stopped service. It must pass without waiting for the service and without
|
||||
# entering the rollback, and change nothing.
|
||||
- name: Converge
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Preview the update of the stub to 2.0.0
|
||||
ansible.builtin.include_role:
|
||||
name: binary_update_caller
|
||||
vars:
|
||||
binary_update_caller_version: 2.0.0
|
||||
@@ -0,0 +1,43 @@
|
||||
---
|
||||
# Runs on the repository's own runner (label kamino-molecule), in the molecule-ubuntu2404 image
|
||||
# (MOLECULE_IMAGE, the pin in .gitea/workflows/molecule.yml). Needs no network: the "binaries"
|
||||
# are stub shell scripts written by ../resources/prepare.yml, run by a stub systemd unit. The
|
||||
# test caller role lives in ../resources/roles, because the role refuses a call from a play.
|
||||
driver:
|
||||
name: docker
|
||||
platforms:
|
||||
- name: binary-update-check-instance
|
||||
image: ${MOLECULE_IMAGE}
|
||||
pre_build_image: true
|
||||
pull: true
|
||||
command: /sbin/init
|
||||
cgroupns_mode: host
|
||||
volumes:
|
||||
- /sys/fs/cgroup:/sys/fs/cgroup:rw
|
||||
tmpfs:
|
||||
- /run
|
||||
- /run/lock
|
||||
security_opts:
|
||||
- apparmor=unconfined
|
||||
provisioner:
|
||||
name: ansible
|
||||
env:
|
||||
ANSIBLE_ROLES_PATH: "${MOLECULE_SCENARIO_DIRECTORY}/../resources/roles"
|
||||
verifier:
|
||||
name: ansible
|
||||
scenario:
|
||||
# molecule's default sequence minus its first destroy: the workflow removes what a killed run
|
||||
# left before any scenario starts, so that destroy only waits out the
|
||||
# driver's 5 s poll on a container that is not there. After an aborted local run, run
|
||||
# `molecule destroy` yourself.
|
||||
test_sequence:
|
||||
- dependency
|
||||
- cleanup
|
||||
- syntax
|
||||
- create
|
||||
- prepare
|
||||
# converge only under --check: the scenario tests the preview, nothing is applied
|
||||
- check
|
||||
- verify
|
||||
- cleanup
|
||||
- destroy
|
||||
@@ -0,0 +1,12 @@
|
||||
---
|
||||
- name: Prepare
|
||||
ansible.builtin.import_playbook: ../resources/prepare.yml
|
||||
|
||||
- name: Stop the stub service, as a preview against a service that is down would find it
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Stop the stub service
|
||||
ansible.builtin.systemd:
|
||||
name: stubd
|
||||
state: stopped
|
||||
@@ -0,0 +1,31 @@
|
||||
---
|
||||
- name: Verify
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Read the stub version
|
||||
ansible.builtin.command:
|
||||
argv: [/usr/local/bin/stubd, version]
|
||||
register: check_version
|
||||
changed_when: false
|
||||
|
||||
- name: Stat the staged file and the backup
|
||||
ansible.builtin.stat:
|
||||
path: "{{ check_path }}"
|
||||
loop: [/usr/local/bin/stubd.new, /usr/local/bin/stubd.bak]
|
||||
loop_control:
|
||||
loop_var: check_path
|
||||
register: check_leftovers
|
||||
|
||||
- name: Read the stub unit
|
||||
ansible.builtin.systemd:
|
||||
name: stubd
|
||||
register: check_unit
|
||||
|
||||
- name: The preview changed nothing and left the service down
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- check_version.stdout == 'stubd version v1.0.0'
|
||||
- check_leftovers.results | selectattr('stat.exists') | list | length == 0
|
||||
- check_unit.status.ActiveState in ['inactive', 'failed']
|
||||
quiet: true
|
||||
@@ -0,0 +1,12 @@
|
||||
---
|
||||
# A successful bump from stub 1.0.0 to 2.0.0. The second run of the idempotence step finds
|
||||
# 2.0.0 installed and stages nothing.
|
||||
- name: Converge
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Update the stub to 2.0.0
|
||||
ansible.builtin.include_role:
|
||||
name: binary_update_caller
|
||||
vars:
|
||||
binary_update_caller_version: 2.0.0
|
||||
@@ -0,0 +1,44 @@
|
||||
---
|
||||
# Runs on the repository's own runner (label kamino-molecule), in the molecule-ubuntu2404 image
|
||||
# (MOLECULE_IMAGE, the pin in .gitea/workflows/molecule.yml). Needs no network: the "binaries"
|
||||
# are stub shell scripts written by ../resources/prepare.yml, run by a stub systemd unit. The
|
||||
# test caller role lives in ../resources/roles, because the role refuses a call from a play.
|
||||
driver:
|
||||
name: docker
|
||||
platforms:
|
||||
- name: binary-update-instance
|
||||
image: ${MOLECULE_IMAGE}
|
||||
pre_build_image: true
|
||||
pull: true
|
||||
command: /sbin/init
|
||||
cgroupns_mode: host
|
||||
volumes:
|
||||
- /sys/fs/cgroup:/sys/fs/cgroup:rw
|
||||
tmpfs:
|
||||
- /run
|
||||
- /run/lock
|
||||
security_opts:
|
||||
- apparmor=unconfined
|
||||
provisioner:
|
||||
name: ansible
|
||||
env:
|
||||
ANSIBLE_ROLES_PATH: "${MOLECULE_SCENARIO_DIRECTORY}/../resources/roles"
|
||||
verifier:
|
||||
name: ansible
|
||||
scenario:
|
||||
# molecule's default sequence minus its first destroy: the workflow removes what a killed run
|
||||
# left before any scenario starts, so that destroy only waits out the
|
||||
# driver's 5 s poll on a container that is not there. After an aborted local run, run
|
||||
# `molecule destroy` yourself.
|
||||
test_sequence:
|
||||
- dependency
|
||||
- cleanup
|
||||
- syntax
|
||||
- create
|
||||
- prepare
|
||||
- converge
|
||||
- idempotence
|
||||
- side_effect
|
||||
- verify
|
||||
- cleanup
|
||||
- destroy
|
||||
@@ -0,0 +1,3 @@
|
||||
---
|
||||
- name: Prepare
|
||||
ansible.builtin.import_playbook: ../resources/prepare.yml
|
||||
@@ -0,0 +1,32 @@
|
||||
---
|
||||
# The state every successful run and every rollback must leave: the expected stub version,
|
||||
# the service active, no backup and no staged file.
|
||||
- name: Read the stub version ({{ verify_case }})
|
||||
ansible.builtin.command:
|
||||
argv: [/usr/local/bin/stubd, version]
|
||||
register: verify_version_out
|
||||
changed_when: false
|
||||
|
||||
- name: Stat the staged file and the backup ({{ verify_case }})
|
||||
ansible.builtin.stat:
|
||||
path: "{{ verify_path }}"
|
||||
loop: [/usr/local/bin/stubd.new, /usr/local/bin/stubd.bak]
|
||||
loop_control:
|
||||
loop_var: verify_path
|
||||
register: verify_leftovers
|
||||
|
||||
- name: Read the stub unit ({{ verify_case }})
|
||||
ansible.builtin.systemd:
|
||||
name: stubd
|
||||
register: verify_unit
|
||||
|
||||
- name: The expected stub runs and nothing is left behind ({{ verify_case }})
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- verify_version_out.stdout == 'stubd version v' ~ verify_version
|
||||
- verify_unit.status.ActiveState == 'active'
|
||||
- verify_leftovers.results | selectattr('stat.exists') | list | length == 0
|
||||
fail_msg: >-
|
||||
{{ verify_case }}: version output {{ verify_version_out.stdout }}, unit {{ verify_unit.status.ActiveState }},
|
||||
left behind {{ verify_leftovers.results | selectattr('stat.exists') | map(attribute='verify_path') | list }}
|
||||
quiet: true
|
||||
@@ -0,0 +1,169 @@
|
||||
---
|
||||
# Converge left stub 2.0.0 running. Each failure case below runs after the one before it and
|
||||
# expects to find 2.0.0 running again: a case that broke the rollback breaks the next one too.
|
||||
- name: Verify the successful bump
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Check the state after converge
|
||||
ansible.builtin.include_tasks: tasks/assert_state.yml
|
||||
vars:
|
||||
verify_case: converge
|
||||
verify_version: 2.0.0
|
||||
|
||||
- name: Verify that a binary that cannot start is rolled back
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Update to a stub whose unit cannot start
|
||||
block:
|
||||
- name: Run the update
|
||||
ansible.builtin.include_role:
|
||||
name: binary_update_caller
|
||||
vars:
|
||||
binary_update_caller_version: 3.0.0
|
||||
binary_update_caller_stage: 3.0.0-broken
|
||||
- name: The update should have failed
|
||||
ansible.builtin.fail:
|
||||
msg: The update to a stub that cannot start succeeded.
|
||||
rescue:
|
||||
- name: The failure says the backup was restored and the service is active
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- ansible_failed_result.msg is search('Restored the previous binary')
|
||||
- ansible_failed_result.msg is search('stubd was started again and is active')
|
||||
# the failing ExecStartPre fails the start job itself, so the restart task fails
|
||||
- ansible_failed_result.msg is search('failed at Start the service:')
|
||||
fail_msg: "{{ ansible_failed_result.msg }}"
|
||||
quiet: true
|
||||
|
||||
- name: Check the state after the rollback
|
||||
ansible.builtin.include_tasks: tasks/assert_state.yml
|
||||
vars:
|
||||
verify_case: broken binary
|
||||
verify_version: 2.0.0
|
||||
|
||||
- name: Verify that a binary reporting another version is rolled back
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Update to a stub that reports the wrong version
|
||||
block:
|
||||
- name: Run the update
|
||||
ansible.builtin.include_role:
|
||||
name: binary_update_caller
|
||||
vars:
|
||||
binary_update_caller_version: 3.0.0
|
||||
binary_update_caller_stage: 3.0.0-mislabelled
|
||||
- name: The update should have failed
|
||||
ansible.builtin.fail:
|
||||
msg: The update to a mislabelled stub succeeded.
|
||||
rescue:
|
||||
- name: The failure names the version mismatch and the restore
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- ansible_failed_result.msg is search('reports 9\.9\.9, expected 3\.0\.0')
|
||||
- ansible_failed_result.msg is search('Restored the previous binary')
|
||||
- ansible_failed_result.msg is search('is active')
|
||||
fail_msg: "{{ ansible_failed_result.msg }}"
|
||||
quiet: true
|
||||
|
||||
- name: Check the state after the rollback
|
||||
ansible.builtin.include_tasks: tasks/assert_state.yml
|
||||
vars:
|
||||
verify_case: wrong version
|
||||
verify_version: 2.0.0
|
||||
|
||||
- name: Verify that nothing is stopped when nothing was staged
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Read the unit before the attempt
|
||||
ansible.builtin.systemd:
|
||||
name: stubd
|
||||
register: verify_unit_before
|
||||
|
||||
- name: Update without staging a binary
|
||||
block:
|
||||
- name: Run the update
|
||||
ansible.builtin.include_role:
|
||||
name: binary_update_caller
|
||||
vars:
|
||||
binary_update_caller_version: 3.0.0
|
||||
binary_update_caller_stage: ""
|
||||
- name: The update should have failed
|
||||
ansible.builtin.fail:
|
||||
msg: The update without a staged binary succeeded.
|
||||
rescue:
|
||||
- name: The failure says the service was not stopped
|
||||
ansible.builtin.assert:
|
||||
that: ansible_failed_result.msg is search('was not stopped')
|
||||
fail_msg: "{{ ansible_failed_result.msg }}"
|
||||
quiet: true
|
||||
|
||||
- name: Read the unit after the attempt
|
||||
ansible.builtin.systemd:
|
||||
name: stubd
|
||||
register: verify_unit_after
|
||||
|
||||
- name: The service kept running the same process
|
||||
ansible.builtin.assert:
|
||||
that: verify_unit_after.status.MainPID == verify_unit_before.status.MainPID
|
||||
quiet: true
|
||||
|
||||
- name: Check the state after the refusal
|
||||
ansible.builtin.include_tasks: tasks/assert_state.yml
|
||||
vars:
|
||||
verify_case: nothing staged
|
||||
verify_version: 2.0.0
|
||||
|
||||
- name: Verify that a play cannot call the role directly
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Call the role from a play
|
||||
block:
|
||||
- name: Include the internal role without a parent role
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino._binary_update
|
||||
vars:
|
||||
_binary_update_service: stubd
|
||||
_binary_update_binary_path: /usr/local/bin/stubd
|
||||
_binary_update_staged_path: /usr/local/bin/stubd.new
|
||||
_binary_update_version_command: [/usr/local/bin/stubd, version]
|
||||
_binary_update_expected_version: 2.0.0
|
||||
- name: The direct call should have failed
|
||||
ansible.builtin.fail:
|
||||
msg: A play called the internal role.
|
||||
rescue:
|
||||
- name: The failure says the role is internal
|
||||
ansible.builtin.assert:
|
||||
that: ansible_failed_result.msg is search('is internal')
|
||||
fail_msg: "{{ ansible_failed_result.msg }}"
|
||||
quiet: true
|
||||
|
||||
- name: Verify a first install, with no live binary to back up
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Stop the stub service
|
||||
ansible.builtin.systemd:
|
||||
name: stubd
|
||||
state: stopped
|
||||
|
||||
- name: Remove the live binary
|
||||
ansible.builtin.file:
|
||||
path: /usr/local/bin/stubd
|
||||
state: absent
|
||||
|
||||
- name: Install stub 2.0.0
|
||||
ansible.builtin.include_role:
|
||||
name: binary_update_caller
|
||||
vars:
|
||||
binary_update_caller_version: 2.0.0
|
||||
|
||||
- name: Check the state after the first install
|
||||
ansible.builtin.include_tasks: tasks/assert_state.yml
|
||||
vars:
|
||||
verify_case: first install
|
||||
verify_version: 2.0.0
|
||||
@@ -0,0 +1,66 @@
|
||||
---
|
||||
# Shared by the scenarios. Stub "binaries" are shell scripts: `version` prints a version, `check`
|
||||
# is the unit's ExecStartPre, `serve` is the long-running process. The unit only becomes active
|
||||
# when `check` succeeds, so a binary that cannot start fails deterministically instead of being
|
||||
# active for a moment before it exits.
|
||||
- name: Prepare
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Create the stub directory
|
||||
ansible.builtin.file:
|
||||
path: /opt/stubs
|
||||
state: directory
|
||||
mode: "0755"
|
||||
|
||||
- name: Write the stub binaries
|
||||
ansible.builtin.copy:
|
||||
dest: "/opt/stubs/stubd-{{ stub.name }}"
|
||||
mode: "0755"
|
||||
content: |
|
||||
#!/bin/sh
|
||||
case "$1" in
|
||||
version) echo "stubd version v{{ stub.reports }}" ;;
|
||||
check) exit {{ stub.check_rc }} ;;
|
||||
serve) exec sleep infinity ;;
|
||||
*) exit 2 ;;
|
||||
esac
|
||||
loop:
|
||||
- {name: 1.0.0, reports: 1.0.0, check_rc: 0}
|
||||
- {name: 2.0.0, reports: 2.0.0, check_rc: 0}
|
||||
# cannot start: the rollback test
|
||||
- {name: 3.0.0-broken, reports: 3.0.0, check_rc: 1}
|
||||
# starts but reports another version: the version check test
|
||||
- {name: 3.0.0-mislabelled, reports: 9.9.9, check_rc: 0}
|
||||
loop_control:
|
||||
loop_var: stub
|
||||
label: "{{ stub.name }}"
|
||||
|
||||
- name: Install stub 1.0.0 as the live binary
|
||||
ansible.builtin.copy:
|
||||
src: /opt/stubs/stubd-1.0.0
|
||||
dest: /usr/local/bin/stubd
|
||||
remote_src: true
|
||||
mode: "0755"
|
||||
|
||||
- name: Write the stub unit
|
||||
ansible.builtin.copy:
|
||||
dest: /etc/systemd/system/stubd.service
|
||||
mode: "0644"
|
||||
content: |
|
||||
[Unit]
|
||||
Description=Stub service for the _binary_update scenarios
|
||||
|
||||
[Service]
|
||||
ExecStartPre=/usr/local/bin/stubd check
|
||||
ExecStart=/usr/local/bin/stubd serve
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
||||
- name: Start the stub service
|
||||
ansible.builtin.systemd:
|
||||
name: stubd
|
||||
state: started
|
||||
enabled: true
|
||||
daemon_reload: true
|
||||
@@ -0,0 +1,40 @@
|
||||
---
|
||||
# Test caller for the scenarios: does what a service role does around mortis.kamino._binary_update.
|
||||
# It reads the installed version, stages a stub binary from /opt/stubs only when the version
|
||||
# differs (so a second converge changes nothing), then includes the role. An empty
|
||||
# binary_update_caller_stage stages nothing, to test the role's refusal to stop.
|
||||
- name: Read the installed stub version
|
||||
ansible.builtin.command:
|
||||
argv: [/usr/local/bin/stubd, version]
|
||||
register: binary_update_caller_installed
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
check_mode: false
|
||||
|
||||
- name: Decide whether an update is needed
|
||||
ansible.builtin.set_fact:
|
||||
binary_update_caller_needed: >-
|
||||
{{ (binary_update_caller_installed.stdout | default('') | regex_findall('v?([0-9]+\.[0-9]+\.[0-9]+)') | first | default('0.0.0'))
|
||||
!= binary_update_caller_version }}
|
||||
|
||||
- name: Stage the stub binary
|
||||
ansible.builtin.copy:
|
||||
src: "/opt/stubs/stubd-{{ binary_update_caller_stage | default(binary_update_caller_version) }}"
|
||||
dest: /usr/local/bin/stubd.new
|
||||
remote_src: true
|
||||
mode: "0755"
|
||||
when:
|
||||
- binary_update_caller_needed | bool
|
||||
- binary_update_caller_stage | default(binary_update_caller_version) | length > 0
|
||||
|
||||
- name: Swap the stub binary in
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino._binary_update
|
||||
vars:
|
||||
_binary_update_service: stubd
|
||||
_binary_update_binary_path: /usr/local/bin/stubd
|
||||
_binary_update_staged_path: /usr/local/bin/stubd.new
|
||||
_binary_update_version_command: [/usr/local/bin/stubd, version]
|
||||
_binary_update_expected_version: "v{{ binary_update_caller_version }}"
|
||||
_binary_update_start_timeout: "{{ binary_update_caller_start_timeout | default(30) }}"
|
||||
when: binary_update_caller_needed | bool
|
||||
@@ -0,0 +1,222 @@
|
||||
---
|
||||
# Internal: called only by other roles of this Collection, by FQCN, never from a play. The
|
||||
# caller downloads and verifies the new binary and stages it next to the live one before it
|
||||
# includes this role, so a slow or failed download costs no outage. This role then stops the
|
||||
# service, keeps a backup, swaps the staged file in with one rename, starts the service and
|
||||
# checks the version it reports. Any failure after the stop restores the backup of this run
|
||||
# and starts the service again, and the role fails with a message saying what worked.
|
||||
- name: Refuse a direct call
|
||||
ansible.builtin.assert:
|
||||
that: ansible_parent_role_names | default([]) | length > 0
|
||||
fail_msg: >-
|
||||
mortis.kamino._binary_update is internal: only roles of the mortis.kamino Collection call it.
|
||||
It is not part of the Collection's public API.
|
||||
quiet: true
|
||||
|
||||
- name: Set the update facts
|
||||
ansible.builtin.set_fact:
|
||||
_binary_update_backup_path: "{{ _binary_update_binary_path }}.bak"
|
||||
_binary_update_expected: "{{ _binary_update_expected_version | regex_replace('^v', '') }}"
|
||||
# Reset per run: the rescue restores only a backup this run took, never a .bak an
|
||||
# earlier run left behind, and starts the service again only when this run stopped it.
|
||||
_binary_update_backup_taken: false
|
||||
_binary_update_stop_attempted: false
|
||||
|
||||
# Before the stop: a caller that staged nothing (a tag-skipped download, a wrong path) must not
|
||||
# stop the service for a swap with nothing to swap in. Under --check the caller stages nothing.
|
||||
- name: Check the staged binary
|
||||
become: true
|
||||
ansible.builtin.stat:
|
||||
path: "{{ _binary_update_staged_path }}"
|
||||
register: _binary_update_staged
|
||||
|
||||
- name: Require an executable staged binary
|
||||
ansible.builtin.assert:
|
||||
that: _binary_update_staged.stat.executable | default(false)
|
||||
fail_msg: >-
|
||||
No executable {{ _binary_update_staged_path }} was staged, so {{ _binary_update_service }}
|
||||
was not stopped and still runs its current binary.
|
||||
quiet: true
|
||||
when: not ansible_check_mode
|
||||
|
||||
- name: Update the binary
|
||||
block:
|
||||
- name: Reload systemd units
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
daemon_reload: true
|
||||
|
||||
- name: Record that the service is being stopped
|
||||
ansible.builtin.set_fact:
|
||||
_binary_update_stop_attempted: true
|
||||
|
||||
- name: Stop the service
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
name: "{{ _binary_update_service }}"
|
||||
state: stopped
|
||||
|
||||
- name: Wait until the service has stopped
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
name: "{{ _binary_update_service }}"
|
||||
register: _binary_update_stop_status
|
||||
# A unit that exits non-zero on SIGTERM ends up failed, not inactive; both mean stopped.
|
||||
until: _binary_update_stop_status.status.ActiveState in ["inactive", "failed"]
|
||||
retries: "{{ (_binary_update_stop_timeout | int / 2) | round(0, 'ceil') | int }}"
|
||||
delay: 2
|
||||
# The stop above is skipped under --check, so the unit would never get there.
|
||||
when: not ansible_check_mode
|
||||
|
||||
- name: Check the live binary
|
||||
become: true
|
||||
ansible.builtin.stat:
|
||||
path: "{{ _binary_update_binary_path }}"
|
||||
register: _binary_update_live
|
||||
|
||||
- name: Remove a backup left by an earlier run
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ _binary_update_backup_path }}"
|
||||
state: absent
|
||||
|
||||
# Nothing to back up on a first install, so nothing to restore either.
|
||||
- name: Back up the live binary
|
||||
become: true
|
||||
ansible.builtin.copy:
|
||||
src: "{{ _binary_update_binary_path }}"
|
||||
dest: "{{ _binary_update_backup_path }}"
|
||||
remote_src: true
|
||||
owner: "{{ _binary_update_live.stat.uid }}"
|
||||
group: "{{ _binary_update_live.stat.gid }}"
|
||||
mode: preserve
|
||||
when: _binary_update_live.stat.exists
|
||||
|
||||
- name: Record the backup of this run
|
||||
ansible.builtin.set_fact:
|
||||
_binary_update_backup_taken: true
|
||||
when: _binary_update_live.stat.exists
|
||||
|
||||
# A rename on one filesystem: the live path holds either the old or the new binary, never
|
||||
# a partial file. The command module skips it under --check.
|
||||
- name: Swap in the staged binary
|
||||
become: true
|
||||
ansible.builtin.command:
|
||||
argv: [mv, -f, "{{ _binary_update_staged_path }}", "{{ _binary_update_binary_path }}"]
|
||||
changed_when: true
|
||||
|
||||
- name: Start the service
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
name: "{{ _binary_update_service }}"
|
||||
state: restarted
|
||||
|
||||
# Skipped under --check: nothing was stopped or swapped, and a preview against a service
|
||||
# that is already down would otherwise wait out the timeout and report a rollback.
|
||||
- name: Wait for the service to become active
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
name: "{{ _binary_update_service }}"
|
||||
register: _binary_update_start_status
|
||||
until: _binary_update_start_status.status.ActiveState == "active"
|
||||
retries: "{{ (_binary_update_start_timeout | int / 2) | round(0, 'ceil') | int }}"
|
||||
delay: 2
|
||||
when: not ansible_check_mode
|
||||
|
||||
- name: Read the version of the new binary
|
||||
become: true
|
||||
ansible.builtin.command:
|
||||
argv: "{{ _binary_update_version_command }}"
|
||||
register: _binary_update_version_after
|
||||
changed_when: false
|
||||
|
||||
# Skipped with the command under --check, where stdout is empty.
|
||||
- name: Fail when the new binary reports another version
|
||||
ansible.builtin.fail:
|
||||
msg: >-
|
||||
{{ _binary_update_binary_path }} reports
|
||||
{{ _binary_update_version_after.stdout | regex_findall(_binary_update_version_regex) | first | default('no version') }},
|
||||
expected {{ _binary_update_expected }}.
|
||||
when:
|
||||
- not ansible_check_mode
|
||||
- (_binary_update_version_after.stdout | regex_findall(_binary_update_version_regex) | first | default('')) != _binary_update_expected
|
||||
|
||||
rescue:
|
||||
- name: Keep the cause of the failure
|
||||
ansible.builtin.set_fact:
|
||||
# A wait that ran out of retries has no msg, only the unit status it last read.
|
||||
_binary_update_cause: >-
|
||||
{{ ansible_failed_task.name | default('a task') }}:
|
||||
{{ ansible_failed_result.msg | default('timed out, the unit was '
|
||||
~ (ansible_failed_result.status.ActiveState | default('in an unknown state'))) }}
|
||||
|
||||
# A rename, so the .bak is gone once the previous binary is back in place.
|
||||
- name: Restore the binary backed up by this run
|
||||
become: true
|
||||
ansible.builtin.command:
|
||||
argv: [mv, -f, "{{ _binary_update_backup_path }}", "{{ _binary_update_binary_path }}"]
|
||||
changed_when: true
|
||||
when: _binary_update_backup_taken | bool
|
||||
register: _binary_update_restore
|
||||
# The rollback steps do not abort the rescue: the last task always runs and says which
|
||||
# of them did not work.
|
||||
ignore_errors: true # noqa: ignore-errors
|
||||
|
||||
# Not when the failure came before the stop: the service still runs its current binary.
|
||||
- name: Start the service after the rollback
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
name: "{{ _binary_update_service }}"
|
||||
state: restarted
|
||||
register: _binary_update_rollback_start
|
||||
when: _binary_update_stop_attempted | bool
|
||||
ignore_errors: true # noqa: ignore-errors
|
||||
|
||||
- name: Wait for the service to become active after the rollback
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
name: "{{ _binary_update_service }}"
|
||||
register: _binary_update_rollback_status
|
||||
until: _binary_update_rollback_status.status.ActiveState == "active"
|
||||
retries: "{{ (_binary_update_start_timeout | int / 2) | round(0, 'ceil') | int }}"
|
||||
delay: 2
|
||||
when:
|
||||
- _binary_update_stop_attempted | bool
|
||||
- _binary_update_rollback_start is succeeded
|
||||
- not ansible_check_mode
|
||||
ignore_errors: true # noqa: ignore-errors
|
||||
|
||||
- name: Fail the update
|
||||
ansible.builtin.fail:
|
||||
msg: >-
|
||||
Updating {{ _binary_update_service }} to {{ _binary_update_expected }} failed at {{ _binary_update_cause }}.
|
||||
{% if not _binary_update_backup_taken | bool %}
|
||||
No backup from this run to restore (no live binary, or the run failed before the backup),
|
||||
so {{ _binary_update_binary_path }} was left as it was.
|
||||
{% elif _binary_update_restore is failed %}
|
||||
Restoring {{ _binary_update_backup_path }} FAILED: restore it by hand.
|
||||
{% else %}
|
||||
Restored the previous binary.
|
||||
{% endif %}
|
||||
{% if not _binary_update_stop_attempted | bool %}
|
||||
{{ _binary_update_service }} was not stopped and still runs its current binary.
|
||||
{% elif _binary_update_rollback_start is failed or _binary_update_rollback_status is failed %}
|
||||
{{ _binary_update_service }} did NOT come back up: check systemctl status {{ _binary_update_service }}.
|
||||
{% else %}
|
||||
{{ _binary_update_service }} was started again and is active.
|
||||
{% endif %}
|
||||
|
||||
always:
|
||||
- name: Remove the staged binary
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ _binary_update_staged_path }}"
|
||||
state: absent
|
||||
|
||||
# After the block: the update has succeeded by now, and a failure to remove the backup must
|
||||
# not roll it back. A .bak left here is replaced by the next run.
|
||||
- name: Remove the backup after a successful update
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ _binary_update_backup_path }}"
|
||||
state: absent
|
||||
@@ -71,6 +71,7 @@ IGNORED = (
|
||||
".gitignore",
|
||||
".yamllint",
|
||||
".ansible-lint",
|
||||
".ansible-lint-ignore",
|
||||
".pre-commit-config.yaml",
|
||||
"requirements-dev.txt",
|
||||
"requirements-dev.lock",
|
||||
|
||||
Reference in new issue
Block a user