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
223 lines
9.3 KiB
YAML
223 lines
9.3 KiB
YAML
---
|
|
# 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
|