From 799a377cc03a9c5d86c7212dcb082339022c7e05 Mon Sep 17 00:00:00 2001 From: Nageshwaran Manikumar Date: Thu, 26 Nov 2026 02:05:13 -0500 Subject: [PATCH] Prepare immutable release recovery automation and deployment safeguards --- .../ansible/recovery-production.yml | 283 ++++++++++++++++++ .../ansible/restore-production-v2.yml | 270 +++++++++++++++++ pricing-service/ansible/validate-recovery.yml | 58 ++++ .../production-lock-test-20261126-070255.log | 17 ++ .../recovery/hold-production-lock.sh | 35 +++ .../recovery/monitor-production.py | 53 ++++ pricing-service/recovery/releases/v1.yml | 28 ++ pricing-service/recovery/releases/v2.yml | 29 ++ 8 files changed, 773 insertions(+) create mode 100644 pricing-service/ansible/recovery-production.yml create mode 100644 pricing-service/ansible/restore-production-v2.yml create mode 100644 pricing-service/ansible/validate-recovery.yml create mode 100644 pricing-service/chapter6-evidence/06-rollback-recovery/production-lock-test-20261126-070255.log create mode 100755 pricing-service/recovery/hold-production-lock.sh create mode 100644 pricing-service/recovery/monitor-production.py create mode 100644 pricing-service/recovery/releases/v1.yml create mode 100644 pricing-service/recovery/releases/v2.yml diff --git a/pricing-service/ansible/recovery-production.yml b/pricing-service/ansible/recovery-production.yml new file mode 100644 index 0000000..0245547 --- /dev/null +++ b/pricing-service/ansible/recovery-production.yml @@ -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." diff --git a/pricing-service/ansible/restore-production-v2.yml b/pricing-service/ansible/restore-production-v2.yml new file mode 100644 index 0000000..6e1cb73 --- /dev/null +++ b/pricing-service/ansible/restore-production-v2.yml @@ -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." diff --git a/pricing-service/ansible/validate-recovery.yml b/pricing-service/ansible/validate-recovery.yml new file mode 100644 index 0000000..e6e87db --- /dev/null +++ b/pricing-service/ansible/validate-recovery.yml @@ -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" diff --git a/pricing-service/chapter6-evidence/06-rollback-recovery/production-lock-test-20261126-070255.log b/pricing-service/chapter6-evidence/06-rollback-recovery/production-lock-test-20261126-070255.log new file mode 100644 index 0000000..3e8d29f --- /dev/null +++ b/pricing-service/chapter6-evidence/06-rollback-recovery/production-lock-test-20261126-070255.log @@ -0,0 +1,17 @@ +=== PRODUCTION LOCK EXCLUSION TEST === +Thu Nov 26 07:02:55 UTC 2026 +SUCCESS: Primary production lock acquired +SUCCESS: Concurrent lock acquisition rejected +Concurrent acquisition exit code: 75 +SUCCESS: Primary lock released +SUCCESS: Lock available after release +Thu Nov 26 07:02:56 UTC 2026 +=== TEST SSH LOCK-HOLDER SCRIPT === +PRODUCTION_LOCK_ACQUIRED +Thu Nov 26 07:02:56 UTC 2026 +PRODUCTION_LOCK_RELEASED +=== VERIFY PRODUCTION HEALTH === +{"status":"healthy","version":"v2"} + +SUCCESS: Production lock tests completed +Thu Nov 26 07:02:57 UTC 2026 diff --git a/pricing-service/recovery/hold-production-lock.sh b/pricing-service/recovery/hold-production-lock.sh new file mode 100755 index 0000000..0774eb0 --- /dev/null +++ b/pricing-service/recovery/hold-production-lock.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +set -euo pipefail + +SSH_KEY="$HOME/.ssh/ssw590_digitalocean" +HOST="root@git.nageshwaran.dev" + +ssh -T \ + -o BatchMode=yes \ + -o StrictHostKeyChecking=yes \ + -o ConnectTimeout=10 \ + -o ServerAliveInterval=10 \ + -o ServerAliveCountMax=3 \ + -i "$SSH_KEY" \ + "$HOST" \ + 'bash -c " + set -eu + + exec 9>/run/lock/chapter6-production.lock + + if ! flock -n 9; then + echo LOCK_BUSY + exit 75 + fi + + echo PRODUCTION_LOCK_ACQUIRED + date -u + + while IFS= read -r command; do + if [ \"\$command\" = RELEASE ]; then + echo PRODUCTION_LOCK_RELEASED + exit 0 + fi + done + "' diff --git a/pricing-service/recovery/monitor-production.py b/pricing-service/recovery/monitor-production.py new file mode 100644 index 0000000..97fb0c0 --- /dev/null +++ b/pricing-service/recovery/monitor-production.py @@ -0,0 +1,53 @@ +import datetime +import json +import time +import urllib.request +import urllib.error + +URL = "https://pricing.nageshwaran.dev/health" + +print("timestamp_utc,http_status,healthy,version,latency_ms", + flush=True) + +try: + while True: + started = time.monotonic() + timestamp = datetime.datetime.now( + datetime.timezone.utc + ).isoformat(timespec="milliseconds") + + status = 0 + healthy = False + version = "unknown" + + try: + with urllib.request.urlopen(URL, timeout=3) as response: + status = response.status + data = json.load(response) + healthy = ( + status == 200 + and data.get("status") == "healthy" + ) + version = data.get("version", "not-reported") + + except urllib.error.HTTPError as error: + status = error.code + + except Exception: + pass + + latency = round( + (time.monotonic() - started) * 1000 + ) + + print( + f"{timestamp},{status},{str(healthy).lower()}," + f"{version},{latency}", + flush=True + ) + + elapsed = time.monotonic() - started + time.sleep(max(0, 1 - elapsed)) + +except KeyboardInterrupt: + pass diff --git a/pricing-service/recovery/releases/v1.yml b/pricing-service/recovery/releases/v1.yml new file mode 100644 index 0000000..fc30b04 --- /dev/null +++ b/pricing-service/recovery/releases/v1.yml @@ -0,0 +1,28 @@ +release: v1 +image: git.nageshwaran.dev/ssw590-startup/startup-pricing@sha256:d0d0a2c2052b01a33ce12fd2b278e906552ea9797b5b918f44e5065f444d3027 +environment: production +container_name: startup-pricing-production +network: gitea_gitea_internal +host_binding: "127.0.0.1:5061:5000" +application_port: 5000 +workers: 2 +user: appuser +memory: 64m +memory_swap: 128m +cpus: 0.5 +pids_limit: 64 +read_only: true +tmpfs: "/tmp:rw,nosuid,nodev,size=16m" +restart_policy: unless-stopped +no_new_privileges: true +cap_drop: ALL +health_url: https://pricing.nageshwaran.dev/health +pricing_url: https://pricing.nageshwaran.dev/pricing +expected_health: healthy +expected_pricing: + Basic: 10 + Pro: 25 + Enterprise: 50 +expected_total: 85 +source_commit: unverified +configuration_provenance: reconstructed_recovery_record diff --git a/pricing-service/recovery/releases/v2.yml b/pricing-service/recovery/releases/v2.yml new file mode 100644 index 0000000..e9a972b --- /dev/null +++ b/pricing-service/recovery/releases/v2.yml @@ -0,0 +1,29 @@ +release: v2 +image: git.nageshwaran.dev/ssw590-startup/startup-pricing@sha256:14d520f6fbe60c9cc5e661551a03e944de3a2ad777a64c316ec397ee765db22d +environment: production +container_name: startup-pricing-production +network: gitea_gitea_internal +host_binding: "127.0.0.1:5061:5000" +application_port: 5000 +workers: 2 +user: appuser +memory: 64m +memory_swap: 128m +cpus: 0.5 +pids_limit: 64 +read_only: true +tmpfs: "/tmp:rw,nosuid,nodev,size=16m" +restart_policy: unless-stopped +no_new_privileges: true +cap_drop: ALL +health_url: https://pricing.nageshwaran.dev/health +pricing_url: https://pricing.nageshwaran.dev/pricing +expected_health: healthy +expected_pricing: + Starter: 5 + Basic: 12 + Pro: 30 + Enterprise: 60 +expected_total: 107 +source_commit: unverified +configuration_provenance: reconstructed_recovery_record