From 1c9cfb19b7febb69683b9ea4f8d3a27b40d8a0c5 Mon Sep 17 00:00:00 2001 From: pavel Date: Tue, 20 Jan 2026 13:35:16 +0200 Subject: [PATCH] 1335 --- .../ptsd_reacquire_attempt.yml | 141 ++++++++++++++---- 1 file changed, 116 insertions(+), 25 deletions(-) diff --git a/files/ansible-playbooks/ptsd_reacquire_attempt.yml b/files/ansible-playbooks/ptsd_reacquire_attempt.yml index 32d32c8..2b97905 100644 --- a/files/ansible-playbooks/ptsd_reacquire_attempt.yml +++ b/files/ansible-playbooks/ptsd_reacquire_attempt.yml @@ -1,14 +1,18 @@ -# ptsd_reacquire_attempt.yml — DHCP reacquire verification (standalone) +# ptsd_reacquire_attempt.yml — DHCP reacquire verification + CASE A cleanup (standalone) # Purpose: -# - Re-run Phase 0 to reach DEV2 under uncertain connectivity -# - Verify "migrated to DHCP" state on DEV2: -# * ppp0 is absent -# * eth0.4000 exists AND has IPv4 +# 1) Re-run Phase 0 connectivity to DEV2 (same logic as main playbook) +# 2) Verify migrated DHCP state: +# - ppp0 is absent +# - eth0.4000 exists AND has IPv4 +# 3) If CASE A confirmed (DHCP ok, no PPP), perform minimal cleanup step: +# - detect pending reboot process; if found, kill it +# Maintain cumulative cleanup report and print at end. +# # Notes: -# - Intentionally ignores /tmp/ptsd.inprogress (it may be set during migration) -# - Intentionally does NO cleanup (to avoid impacting the main playbook) +# - Does NOT respect /tmp/ptsd.inprogress (verification runs while migration is in progress). +# - Does NOT do DEV1 teardown cleanup here (no tunnel/IP removal); this is intentional. -- name: "PTSD DHCP Reacquire Attempt | Phase 0 | Connectivity + wrapper + DHCP state check" +- name: "PTSD DHCP Reacquire Attempt | Phase 0 | Connectivity + wrapper + DHCP state check + CASE A cleanup" hosts: all gather_facts: no @@ -594,54 +598,54 @@ esac changed_when: false - # ============================ VERIFICATION: DHCP STATE ON DEV2 ============================ - - name: "VERIFY | DEV2 check: ppp0 exists?" + # ============================ VERIFY: DHCP MIGRATION STATE ============================ + - name: VERIFY | DEV2 check: ppp0 exists? delegate_to: localhost ansible.builtin.shell: | export DEV2_CMD="ip link show ppp0 >/dev/null 2>&1 && echo YES || echo NO" {{ dev2_exec_cmd }} args: { executable: /bin/bash } - register: dev2_ppp0_exists + register: v_ppp0_exists changed_when: false failed_when: false - - name: "VERIFY | DEV2 check: eth0.4000 exists?" + - name: VERIFY | DEV2 check: eth0.4000 exists? delegate_to: localhost ansible.builtin.shell: | export DEV2_CMD="ip link show eth0.4000 >/dev/null 2>&1 && echo YES || echo NO" {{ dev2_exec_cmd }} args: { executable: /bin/bash } - register: dev2_eth04000_exists + register: v_eth04000_exists changed_when: false failed_when: false - - name: "VERIFY | DEV2 check: eth0.4000 has IPv4?" + - name: VERIFY | DEV2 check: eth0.4000 has IPv4? delegate_to: localhost ansible.builtin.shell: | export DEV2_CMD="ip -4 addr show dev eth0.4000 2>/dev/null | grep -m1 'inet ' >/dev/null 2>&1 && echo YES || echo NO" {{ dev2_exec_cmd }} args: { executable: /bin/bash } - register: dev2_eth04000_has_ipv4 + register: v_eth04000_has_ipv4 changed_when: false failed_when: false - - name: "VERIFY | Summarize DHCP migration state" + - name: VERIFY | Summarize DHCP migration state delegate_to: localhost ansible.builtin.set_fact: - ppp0_exists: "{{ (dev2_ppp0_exists.stdout | default('') | trim) == 'YES' }}" - eth04000_exists: "{{ (dev2_eth04000_exists.stdout | default('') | trim) == 'YES' }}" - eth04000_has_ipv4: "{{ (dev2_eth04000_has_ipv4.stdout | default('') | trim) == 'YES' }}" + ppp0_exists: "{{ (v_ppp0_exists.stdout | default('') | trim) == 'YES' }}" + eth04000_exists: "{{ (v_eth04000_exists.stdout | default('') | trim) == 'YES' }}" + eth04000_has_ipv4: "{{ (v_eth04000_has_ipv4.stdout | default('') | trim) == 'YES' }}" dhcp_migrated_ok: >- {{ - ((dev2_ppp0_exists.stdout | default('') | trim) != 'YES') + ((v_ppp0_exists.stdout | default('') | trim) != 'YES') and - ((dev2_eth04000_exists.stdout | default('') | trim) == 'YES') + ((v_eth04000_exists.stdout | default('') | trim) == 'YES') and - ((dev2_eth04000_has_ipv4.stdout | default('') | trim) == 'YES') + ((v_eth04000_has_ipv4.stdout | default('') | trim) == 'YES') }} changed_when: false - - name: "VERIFY | Result: DHCP migrated (SUCCESS) or not (graceful stop)" + - name: VERIFY | Result: DHCP migrated (SUCCESS) or not (graceful stop) delegate_to: localhost ansible.builtin.debug: msg: @@ -652,8 +656,95 @@ - "dhcp_migrated_ok={{ dhcp_migrated_ok }}" changed_when: false - - name: "VERIFY | Graceful stop if DHCP migration state is not confirmed" + - name: VERIFY | Graceful stop if DHCP migration state is not confirmed + when: not (dhcp_migrated_ok | bool) + delegate_to: localhost + ansible.builtin.debug: + msg: + - "Graceful stop: DHCP migration state is not confirmed (not CASE A)." + - "ppp0_exists={{ ppp0_exists }}" + - "eth0.4000_exists={{ eth04000_exists }}" + - "eth0.4000_has_ipv4={{ eth04000_has_ipv4 }}" + - "dev2_conn_final={{ dev2_conn_final }}" + changed_when: false + + - name: VERIFY | End host if DHCP migration state is not confirmed when: not (dhcp_migrated_ok | bool) ansible.builtin.meta: end_host - # Intentionally no post_tasks cleanup in this playbook. + # ============================ CASE A CLEANUP: CANCEL PENDING REBOOT ============================ + - name: CASE A | Initialize cumulative cleanup report + when: dhcp_migrated_ok | bool + delegate_to: localhost + ansible.builtin.set_fact: + cleanup_report: "case A observed" + changed_when: false + + - name: "CASE A | DEV2 detect pending reboot processes (best-effort)" + when: dhcp_migrated_ok | bool + delegate_to: localhost + ansible.builtin.shell: | + # BusyBox-friendly: look for delayed reboot patterns. + export DEV2_CMD="ps w 2>/dev/null | grep -E '[r]eboot( |$)|[n]ohup reboot' || true" + {{ dev2_exec_cmd }} + args: { executable: /bin/bash } + register: reboot_ps + changed_when: false + failed_when: false + + - name: "CASE A | DEV2 cancel pending reboot if found (kill reboot PIDs)" + when: dhcp_migrated_ok | bool + delegate_to: localhost + ansible.builtin.shell: | + set -e + # Extract PID from BusyBox ps output: assume PID is the first column. + # Only target lines that clearly include "reboot" or "nohup reboot". + PIDS="$(printf '%s\n' "{{ reboot_ps.stdout | default('') | regex_replace('\r','') }}" \ + | grep -E '(^| )reboot( |$)|nohup reboot' \ + | awk '{print $1}' \ + | grep -E '^[0-9]+$' \ + | sort -u || true)" + + if [ -z "$PIDS" ]; then + echo "NONE" + exit 0 + fi + + # Try TERM then KILL. + for p in $PIDS; do + kill "$p" 2>/dev/null || true + done + sleep 1 + for p in $PIDS; do + kill -9 "$p" 2>/dev/null || true + done + + echo "$PIDS" + args: { executable: /bin/bash } + register: reboot_kill + changed_when: false + failed_when: false + + - name: CASE A | Append reboot cancellation result to cleanup report + when: dhcp_migrated_ok | bool + delegate_to: localhost + ansible.builtin.set_fact: + cleanup_report: >- + {{ + cleanup_report ~ + ( + ('; pending reboot found, cancelled (pids=' ~ (reboot_kill.stdout | trim) ~ ')') + if ((reboot_kill.stdout | default('') | trim) not in ['','NONE']) + else + '; no pending reboot found' + ) + }} + changed_when: false + + - name: CASE A | Final cumulative cleanup report + when: dhcp_migrated_ok | bool + delegate_to: localhost + ansible.builtin.debug: + msg: + - "{{ cleanup_report }}" + changed_when: false