Files

271 lines
8.0 KiB
YAML

---
- 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."