Prepare immutable release recovery automation and deployment safeguards

This commit is contained in:
2026-11-26 02:05:13 -05:00
parent 151c59564d
commit 799a377cc0
8 changed files with 773 additions and 0 deletions
@@ -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."
@@ -0,0 +1,270 @@
---
- name: Restore preserved hardened production V2
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 restoration authorization
ansible.builtin.assert:
that:
- recovery_authorized | default(false) | bool
- not ansible_check_mode
- preserved_v2 is defined
- preserved_v2 is match('^startup-pricing-production-recovery-[0-9]{8}-[0-9]{6}$')
- name: Inspect preserved V2
ansible.builtin.command:
argv:
- docker
- inspect
- "{{ preserved_v2 }}"
register: saved
changed_when: false
no_log: true
- name: Inspect current V1
ansible.builtin.command:
argv:
- docker
- inspect
- "{{ container_name }}"
register: active
changed_when: false
no_log: true
- name: Validate release identities
ansible.builtin.assert:
that:
- (saved.stdout | from_json | first).Config.Image == v2_image
- not (saved.stdout | from_json | first).State.Running
- (active.stdout | from_json | first).Config.Image == v1_image
- (active.stdout | from_json | first).State.Running
- name: Generate restoration identifier
ansible.builtin.command: date -u +%Y%m%d-%H%M%S
register: restore_id
changed_when: false
- name: Record container identities
ansible.builtin.set_fact:
v1_id: "{{ (active.stdout | from_json | first).Id }}"
v2_id: "{{ (saved.stdout | from_json | first).Id }}"
preserved_v1: "{{ container_name }}-drill-v1-{{ restore_id.stdout }}"
- name: Check V1 preservation slot
ansible.builtin.command:
argv:
- docker
- inspect
- "{{ preserved_v1 }}"
register: slot
changed_when: false
failed_when: slot.rc != 1
- name: Restore hardened V2
block:
- name: Record restoration start
ansible.builtin.command: date -u
register: started
changed_when: false
- ansible.builtin.debug:
var: started.stdout
- name: Stop V1
ansible.builtin.command:
argv:
- docker
- stop
- "{{ v1_id }}"
- name: Preserve V1 drill container
ansible.builtin.command:
argv:
- docker
- rename
- "{{ v1_id }}"
- "{{ preserved_v1 }}"
- name: Restore original V2 container name
ansible.builtin.command:
argv:
- docker
- rename
- "{{ v2_id }}"
- "{{ container_name }}"
- name: Start original V2
ansible.builtin.command:
argv:
- docker
- start
- "{{ v2_id }}"
- name: Verify V2 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'
- health.json.version | default('') == 'v2'
- name: Verify restored V2 pricing
ansible.builtin.uri:
url: "{{ pricing_url }}"
return_content: true
timeout: 5
register: pricing
- name: Assert exact V2 behavior
ansible.builtin.assert:
that:
- pricing.json.version == 'v2'
- pricing.json.plans | length == v2_release.expected_pricing | length
- "dict(pricing.json.plans | map(attribute='name') | zip(pricing.json.plans | map(attribute='price'))) == v2_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}}' }}"
- "{{ v2_image }}"
register: expected_id
changed_when: false
- name: Verify immutable deployed release identity
ansible.builtin.assert:
that:
- deployed_reference.stdout == v2_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 restoration completion
ansible.builtin.command: date -u
register: finished
changed_when: false
- ansible.builtin.debug:
msg:
- "Restoration started: {{ started.stdout }}"
- "Restoration completed: {{ finished.stdout }}"
- "Preserved V1: {{ preserved_v1 }}"
- "Restored V2 ID: {{ v2_id }}"
- "Health: {{ health.json }}"
- "Pricing: {{ pricing.json }}"
rescue:
- name: Stop V2 if running
ansible.builtin.command:
argv:
- docker
- stop
- "{{ v2_id }}"
- name: Inspect preserved V2 current name
ansible.builtin.command:
argv:
- docker
- inspect
- --format
- "{{ '{{.Name}}' }}"
- "{{ v2_id }}"
register: v2_current_name
changed_when: false
- name: Return V2 to preservation name
ansible.builtin.command:
argv:
- docker
- rename
- "{{ v2_id }}"
- "{{ preserved_v2 }}"
when: v2_current_name.stdout != '/' + preserved_v2
- name: Inspect original V1 name
ansible.builtin.command:
argv:
- docker
- inspect
- --format
- "{{ '{{.Name}}' }}"
- "{{ v1_id }}"
register: v1_name
changed_when: false
- name: Restore V1 name
ansible.builtin.command:
argv:
- docker
- rename
- "{{ v1_id }}"
- "{{ container_name }}"
when: v1_name.stdout != '/' + container_name
- name: Restart V1
ansible.builtin.command:
argv:
- docker
- start
- "{{ v1_id }}"
- name: Verify recovered V1 health
ansible.builtin.uri:
url: "{{ health_url }}"
return_content: true
timeout: 5
register: recovered
retries: 15
delay: 2
until:
- recovered.status | default(0) == 200
- recovered.json.status | default('') == 'healthy'
- name: Report restoration failure
ansible.builtin.fail:
msg: "V2 restoration failed; V1 health recovered."
@@ -0,0 +1,58 @@
---
- name: Validate recovery inputs without deployment
hosts: localhost
connection: local
gather_facts: false
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 }}"
tasks:
- name: Validate immutable release records
ansible.builtin.assert:
that:
- v1_release.release == 'v1'
- v2_release.release == 'v2'
- v1_release.image is match('^.+@sha256:[a-f0-9]{64}$')
- v2_release.image is match('^.+@sha256:[a-f0-9]{64}$')
- v1_release.image != v2_release.image
- v1_release.container_name == v2_release.container_name
- v1_release.host_binding == v2_release.host_binding
- v1_release.expected_pricing.values() | sum == v1_release.expected_total
- v2_release.expected_pricing.values() | sum == v2_release.expected_total
- v1_release.read_only | bool
- v2_release.read_only | bool
- name: Verify external production HTTPS health
ansible.builtin.uri:
url: "{{ v2_release.health_url }}"
return_content: true
timeout: 5
register: health
- name: Verify external production pricing
ansible.builtin.uri:
url: "{{ v2_release.pricing_url }}"
return_content: true
timeout: 5
register: pricing
- name: Validate actual V2 behavior
ansible.builtin.assert:
that:
- health.json.status == 'healthy'
- health.json.version == 'v2'
- pricing.json.version == 'v2'
- pricing.json.plans | length == v2_release.expected_pricing | length
- "dict(pricing.json.plans | map(attribute='name') | zip(pricing.json.plans | map(attribute='price'))) == v2_release.expected_pricing"
- pricing.json.plans | map(attribute='price') | sum == v2_release.expected_total
- name: Report verified release inputs
ansible.builtin.debug:
msg:
- "V1 image: {{ v1_release.image }}"
- "V2 image: {{ v2_release.image }}"
- "Production health: {{ health.json }}"
- "Production pricing: {{ pricing.json }}"
- "SUCCESS: Recovery inputs validated"