# esh-pve-nas cutover, step 2 of 5 — quiesce esh-pve's hard NFS storages.
#
# Run:  scripts/elway root@10.0.250.35 --playbook playbooks/esh-cutover-2-quiesce-esh-pve.yaml
#
# esh-pve mounts two `hard` NFS storages from CT 103 on esh-pve-nas:
#   esh-nas    -> 10.0.50.50:/mnt/pvestore   at /mnt/pve/esh-nas
#   tank-vmbu  -> 10.0.50.50:/mnt/tank-vmbu  at /mnt/pve/tank-vmbu
#
# Disabling the storage first matters: if the storage stays enabled, pvestatd
# keeps stat()ing the path and will re-trigger the mount (and then block on it)
# the moment the server disappears. Disable, THEN unmount.
#
# Measured 2026-08-18: esh-nas holds 2.9 MB of 96 TB and no running guest has a
# disk on either storage — all three (100 esh-vm-docker, 101 esh-vm-db,
# 102 esh-vm-workstation) live on local-lvm. So this quiesce costs backup targets
# for the duration, not guest availability. Guests are deliberately left running.
#
# Reversed by playbooks/esh-cutover-5-restore.yaml.

steps:
  - name: Disable the esh-nas storage so pvestatd stops touching it
    shell: pvesm set esh-nas --disable 1
    when: "pvesm status 2>/dev/null | awk '$1==\"esh-nas\"{print $3}' | grep -q active"

  - name: Disable the tank-vmbu storage
    shell: pvesm set tank-vmbu --disable 1
    when: "grep -q '^nfs: tank-vmbu' /etc/pve/storage.cfg && ! grep -A8 '^nfs: tank-vmbu' /etc/pve/storage.cfg | grep -q 'disable'"

  - name: Give pvestatd a moment to let go before unmounting
    shell: sleep 5
    changed_when: "false"

  - name: Unmount /mnt/pve/esh-nas
    shell: umount /mnt/pve/esh-nas || umount -l /mnt/pve/esh-nas
    when: "mountpoint -q /mnt/pve/esh-nas"

  - name: Unmount /mnt/pve/tank-vmbu
    shell: umount /mnt/pve/tank-vmbu || umount -l /mnt/pve/tank-vmbu
    when: "mountpoint -q /mnt/pve/tank-vmbu"

verify:
  - name: Neither esh-nas-backed NFS mount remains
    shell: "! findmnt -t nfs,nfs4 -o SOURCE | grep -q '10\\.0\\.50\\.50'"
    changed_when: "false"

  - name: All three guests are still running
    shell: |
      n=$(qm list | awk 'NR>1 && $3=="running"' | wc -l)
      echo "$n VMs running"
      test "$n" -eq 3
    changed_when: "false"
