Prepare immutable release recovery automation and deployment safeguards
This commit is contained in:
@@ -0,0 +1,283 @@
|
||||
---
|
||||
- name: Controlled production rollback drill
|
||||
hosts: production
|
||||
gather_facts: false
|
||||
any_errors_fatal: true
|
||||
|
||||
vars:
|
||||
v1_release: "{{ lookup('ansible.builtin.file', playbook_dir + '/../recovery/releases/v1.yml') | from_yaml }}"
|
||||
v2_release: "{{ lookup('ansible.builtin.file', playbook_dir + '/../recovery/releases/v2.yml') | from_yaml }}"
|
||||
container_name: "{{ v2_release.container_name }}"
|
||||
v1_image: "{{ v1_release.image }}"
|
||||
v2_image: "{{ v2_release.image }}"
|
||||
health_url: "{{ v2_release.health_url }}"
|
||||
pricing_url: "{{ v2_release.pricing_url }}"
|
||||
|
||||
tasks:
|
||||
- name: Require explicit drill authorization
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- recovery_authorized | default(false) | bool
|
||||
- not ansible_check_mode
|
||||
|
||||
- name: Record rollback decision timestamp
|
||||
ansible.builtin.command: date -u
|
||||
register: decision_time
|
||||
changed_when: false
|
||||
|
||||
- ansible.builtin.debug:
|
||||
var: decision_time.stdout
|
||||
|
||||
- name: Inspect current production
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- docker
|
||||
- inspect
|
||||
- "{{ container_name }}"
|
||||
register: original
|
||||
changed_when: false
|
||||
no_log: true
|
||||
|
||||
- name: Verify original production is running V2
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- (original.stdout | from_json | first).State.Running
|
||||
- (original.stdout | from_json | first).Config.Image == v2_image
|
||||
|
||||
- name: Verify rollback image exists locally
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- docker
|
||||
- image
|
||||
- inspect
|
||||
- "{{ v1_image }}"
|
||||
changed_when: false
|
||||
no_log: true
|
||||
|
||||
- name: Verify current HTTPS health
|
||||
ansible.builtin.uri:
|
||||
url: "{{ health_url }}"
|
||||
return_content: true
|
||||
timeout: 5
|
||||
register: baseline
|
||||
|
||||
- name: Confirm healthy V2 baseline
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- baseline.json.status == 'healthy'
|
||||
- baseline.json.version == 'v2'
|
||||
|
||||
- name: Generate unique recovery identifier
|
||||
ansible.builtin.command: date -u +%Y%m%d-%H%M%S
|
||||
register: recovery_id
|
||||
changed_when: false
|
||||
|
||||
- name: Set preserved container name
|
||||
ansible.builtin.set_fact:
|
||||
preserved: "{{ container_name }}-recovery-{{ recovery_id.stdout }}"
|
||||
original_id: "{{ (original.stdout | from_json | first).Id }}"
|
||||
|
||||
- name: Ensure preservation name is unused
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- docker
|
||||
- inspect
|
||||
- "{{ preserved }}"
|
||||
register: preservation_slot
|
||||
changed_when: false
|
||||
failed_when: preservation_slot.rc != 1
|
||||
|
||||
- name: Execute controlled rollback
|
||||
block:
|
||||
- name: Record rollback start
|
||||
ansible.builtin.command: date -u
|
||||
register: rollback_start
|
||||
changed_when: false
|
||||
|
||||
- ansible.builtin.debug:
|
||||
var: rollback_start.stdout
|
||||
|
||||
- name: Stop original production
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- docker
|
||||
- stop
|
||||
- "{{ original_id }}"
|
||||
|
||||
- name: Preserve original production container
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- docker
|
||||
- rename
|
||||
- "{{ original_id }}"
|
||||
- "{{ preserved }}"
|
||||
|
||||
- name: Deploy immutable V1 release
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- docker
|
||||
- run
|
||||
- -d
|
||||
- --pull=never
|
||||
- --name
|
||||
- "{{ container_name }}"
|
||||
- --restart
|
||||
- "{{ v1_release.restart_policy }}"
|
||||
- --network
|
||||
- "{{ v1_release.network }}"
|
||||
- "--memory={{ v1_release.memory }}"
|
||||
- "--memory-swap={{ v1_release.memory_swap }}"
|
||||
- "--cpus={{ v1_release.cpus }}"
|
||||
- "--pids-limit={{ v1_release.pids_limit }}"
|
||||
- --read-only
|
||||
- --tmpfs
|
||||
- "{{ v1_release.tmpfs }}"
|
||||
- --security-opt
|
||||
- no-new-privileges:true
|
||||
- --cap-drop
|
||||
- ALL
|
||||
- -p
|
||||
- "{{ v1_release.host_binding }}"
|
||||
- "{{ v1_image }}"
|
||||
|
||||
- name: Wait for V1 HTTPS health
|
||||
ansible.builtin.uri:
|
||||
url: "{{ health_url }}"
|
||||
return_content: true
|
||||
timeout: 5
|
||||
register: health
|
||||
retries: 15
|
||||
delay: 2
|
||||
until:
|
||||
- health.status | default(0) == 200
|
||||
- health.json.status | default('') == 'healthy'
|
||||
|
||||
- name: Verify V1 pricing
|
||||
ansible.builtin.uri:
|
||||
url: "{{ pricing_url }}"
|
||||
return_content: true
|
||||
timeout: 5
|
||||
register: pricing
|
||||
|
||||
- name: Assert exact V1 behavior
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- pricing.json.version == 'v1'
|
||||
- pricing.json.plans | length == v1_release.expected_pricing | length
|
||||
- "dict(pricing.json.plans | map(attribute='name') | zip(pricing.json.plans | map(attribute='price'))) == v1_release.expected_pricing"
|
||||
|
||||
- name: Inspect active release reference
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- docker
|
||||
- inspect
|
||||
- --format
|
||||
- "{{ '{{.Config.Image}}' }}"
|
||||
- "{{ container_name }}"
|
||||
register: deployed_reference
|
||||
changed_when: false
|
||||
|
||||
- name: Inspect active image ID
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- docker
|
||||
- inspect
|
||||
- --format
|
||||
- "{{ '{{.Image}}' }}"
|
||||
- "{{ container_name }}"
|
||||
register: deployed_id
|
||||
changed_when: false
|
||||
|
||||
- name: Inspect expected immutable image ID
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- docker
|
||||
- image
|
||||
- inspect
|
||||
- --format
|
||||
- "{{ '{{.Id}}' }}"
|
||||
- "{{ v1_image }}"
|
||||
register: expected_id
|
||||
changed_when: false
|
||||
|
||||
- name: Verify immutable deployed release identity
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- deployed_reference.stdout == v1_image
|
||||
- deployed_id.stdout == expected_id.stdout
|
||||
|
||||
- name: Report immutable deployed release
|
||||
ansible.builtin.debug:
|
||||
msg:
|
||||
- "Registry reference: {{ deployed_reference.stdout }}"
|
||||
- "Image ID: {{ deployed_id.stdout }}"
|
||||
|
||||
- name: Record recovery timestamp
|
||||
ansible.builtin.command: date -u
|
||||
register: recovery_time
|
||||
changed_when: false
|
||||
|
||||
- ansible.builtin.debug:
|
||||
msg:
|
||||
- "Rollback start: {{ rollback_start.stdout }}"
|
||||
- "Recovery time: {{ recovery_time.stdout }}"
|
||||
- "Preserved V2: {{ preserved }}"
|
||||
- "Health: {{ health.json }}"
|
||||
- "Pricing: {{ pricing.json }}"
|
||||
|
||||
rescue:
|
||||
- name: Inspect current production identity
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- docker
|
||||
- inspect
|
||||
- --format
|
||||
- "{{ '{{.Id}}' }}"
|
||||
- "{{ container_name }}"
|
||||
register: current
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
|
||||
- name: Remove failed replacement only
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- docker
|
||||
- rm
|
||||
- -f
|
||||
- "{{ current.stdout }}"
|
||||
when:
|
||||
- current.rc == 0
|
||||
- current.stdout != original_id
|
||||
|
||||
- name: Restore original container name
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- docker
|
||||
- rename
|
||||
- "{{ original_id }}"
|
||||
- "{{ container_name }}"
|
||||
when: current.rc != 0 or current.stdout != original_id
|
||||
|
||||
- name: Restart original V2
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- docker
|
||||
- start
|
||||
- "{{ original_id }}"
|
||||
|
||||
- name: Verify restored V2 HTTPS health
|
||||
ansible.builtin.uri:
|
||||
url: "{{ health_url }}"
|
||||
return_content: true
|
||||
timeout: 5
|
||||
register: restored
|
||||
retries: 15
|
||||
delay: 2
|
||||
until:
|
||||
- restored.status | default(0) == 200
|
||||
- restored.json.status | default('') == 'healthy'
|
||||
- restored.json.version | default('') == 'v2'
|
||||
|
||||
- name: Report failed rollback
|
||||
ansible.builtin.fail:
|
||||
msg: "Rollback failed; original V2 health restored."
|
||||
Reference in New Issue
Block a user