# esh-pve-nas cutover, step 5a of 5 — restore esh-docker-vm's NFS mounts.
#
# Run:  scripts/elway infra-ops@10.0.50.45 --playbook playbooks/esh-cutover-5-restore-docker-vm.yaml
#
# Reverses playbooks/esh-cutover-1-quiesce-docker-vm.yaml. Mount first, THEN start
# the container: calibre opens its SQLite library on startup, and starting it
# against an unmounted /mnt/books would have it create a fresh empty library on
# the local disk underneath the mountpoint — which then gets shadowed the moment
# the real mount lands, and looks exactly like data loss.

steps:
  - name: Mount /mnt/books
    shell: sudo -n mount /mnt/books
    when: "! mountpoint -q /mnt/books"

  - name: Mount /mnt/backup
    shell: sudo -n mount /mnt/backup
    when: "! mountpoint -q /mnt/backup"

  - name: Confirm the library is actually there before starting calibre
    shell: |
      test -f /mnt/books/calibre/calibre_library/metadata.db || {
        echo "calibre library NOT visible — refusing to start the container"; exit 1; }
      echo "metadata.db present: $(stat -c %s /mnt/books/calibre/calibre_library/metadata.db) bytes"
    changed_when: "false"

  - name: Start calibre-web-automated
    shell: sudo -n docker start calibre-web-automated
    when: "test -z \"$(sudo -n docker ps -q --filter name=^calibre-web-automated$)\""

verify:
  - name: Both NFS mounts are back
    shell: |
      mountpoint -q /mnt/books && mountpoint -q /mnt/backup
      findmnt -no SOURCE,OPTIONS /mnt/books | grep -q hard
    changed_when: "false"

  - name: calibre-web-automated is running
    shell: test -n "$(sudo -n docker ps -q --filter name=^calibre-web-automated$)"
    changed_when: "false"

  - name: Full container count is back
    shell: |
      n=$(sudo -n docker ps -q | wc -l); echo "$n containers running"; test "$n" -ge 17
    changed_when: "false"
