From c556bea0d70222412ac9792e3dfc12ceeb225ab6 Mon Sep 17 00:00:00 2001
From: Jesse Pretorius <jesse.pretorius@rackspace.co.uk>
Date: Thu, 23 Aug 2018 10:23:07 +0100
Subject: [PATCH] MNAIO: Clean up existing data when switching backing store

When switching from LVM to a file backing store, the existing pool
cannot be undefined until any existing VM's and LV's are removed.

This ensures that if this is the case, it will be cleaned up so
that the switch is effortless.

Change-Id: Ie1460b37593306044f0a63f445c3da1987362d34
---
 multi-node-aio/playbooks/setup-host.yml | 29 +++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/multi-node-aio/playbooks/setup-host.yml b/multi-node-aio/playbooks/setup-host.yml
index 6be317fc..b5c7309d 100644
--- a/multi-node-aio/playbooks/setup-host.yml
+++ b/multi-node-aio/playbooks/setup-host.yml
@@ -261,6 +261,35 @@
         - (default_vm_disk_mode == "file" and _virt_pools.pools.default.format is defined) or
           (default_vm_disk_mode == "lvm" and _virt_pools.pools.default.format is not defined)
       block:
+        - name: Stop running VMs
+          virt:
+            name: "{{ item }}"
+            command: destroy
+          failed_when: false
+          with_items: "{{ _virt_pools.pools.default.volumes }}"
+
+        - name: Delete VM LVs
+          lvol:
+            vg: "{{ default_vm_disk_vg }}"
+            lv: "{{ item }}"
+            state: absent
+            force: yes
+          failed_when: false
+          with_items: "{{ _virt_pools.pools.default.volumes }}"
+
+        - name: Delete VM Disk Images
+          file:
+            path: "{{ _virt_pools.pools.default.path | default('/data/images') }}/{{ item }}.img"
+            state: absent
+          with_items: "{{ _virt_pools.pools.default.volumes }}"
+
+        - name: Undefine the VMs
+          virt:
+            name: "{{ item }}"
+            command: undefine
+          failed_when: false
+          with_items: "{{ _virt_pools.pools.default.volumes }}"
+
         - name: Dismount the mount point if default_vm_disk_mode is 'lvm'
           mount:
             path: /data