]> git.apps.os.sepia.ceph.com Git - ceph-ansible.git/commitdiff
ceph_volume: make zap function idempotent
authorGuillaume Abrioux <gabrioux@redhat.com>
Fri, 19 Jun 2020 13:09:04 +0000 (15:09 +0200)
committerGuillaume Abrioux <gabrioux@redhat.com>
Tue, 23 Jun 2020 07:50:01 +0000 (09:50 +0200)
This commit makes the zap function idempotent, especially when using
lvm_volumes variable.

Closes: https://bugzilla.redhat.com/show_bug.cgi?id=1845668
Signed-off-by: Guillaume Abrioux <gabrioux@redhat.com>
(cherry picked from commit 3f47236470e1571963850e8bed68fa2d26f05b66)

library/ceph_volume.py
tests/library/test_ceph_volume.py

index d062d913aa389877da9162a05bad30c782ceeaeb..7e9b916af7e471951928c691e275f675d635c3cb 100644 (file)
@@ -217,20 +217,21 @@ def container_exec(binary, container_image):
     return command_exec
 
 
-def build_ceph_volume_cmd(action, container_image, cluster=None):
+def build_cmd(action, container_image, cluster='ceph', binary='ceph-volume'):
     '''
     Build the ceph-volume command
     '''
 
+    _binary = binary
+
     if container_image:
-        binary = 'ceph-volume'
         cmd = container_exec(
             binary, container_image)
     else:
-        binary = ['ceph-volume']
+        binary = [binary]
         cmd = binary
 
-    if cluster:
+    if _binary == 'ceph-volume':
         cmd.extend(['--cluster', cluster])
 
     cmd.extend(action)
@@ -313,7 +314,7 @@ def batch(module, container_image):
 
     # Build the CLI
     action = ['lvm', 'batch']
-    cmd = build_ceph_volume_cmd(action, container_image, cluster)
+    cmd = build_cmd(action, container_image, cluster)
     cmd.extend(['--%s' % objectstore])
     cmd.append('--yes')
 
@@ -396,7 +397,7 @@ def prepare_or_create_osd(module, action, container_image):
 
     # Build the CLI
     action = ['lvm', action]
-    cmd = build_ceph_volume_cmd(action, container_image, cluster)
+    cmd = build_cmd(action, container_image, cluster)
     cmd.extend(['--%s' % objectstore])
     cmd.append('--data')
     cmd.append(data)
@@ -435,7 +436,7 @@ def list_osd(module, container_image):
 
     # Build the CLI
     action = ['lvm', 'list']
-    cmd = build_ceph_volume_cmd(action, container_image, cluster)
+    cmd = build_cmd(action, container_image, cluster)
     if data:
         cmd.append(data)
     cmd.append('--format=json')
@@ -448,7 +449,7 @@ def list_storage_inventory(module, container_image):
     '''
 
     action = ['inventory']
-    cmd = build_ceph_volume_cmd(action, container_image)
+    cmd = build_cmd(action, container_image)
     cmd.append('--format=json')
 
     return cmd
@@ -461,12 +462,30 @@ def activate_osd():
     # build the CLI
     action = ['lvm', 'activate']
     container_image = None
-    cmd = build_ceph_volume_cmd(action, container_image)
+    cmd = build_cmd(action, container_image)
     cmd.append('--all')
 
     return cmd
 
 
+def is_lv(module, vg, lv, container_image):
+    '''
+    Check if an LV exists
+    '''
+
+    args = [ '--noheadings', '--reportformat', 'json', '--select', 'lv_name={},vg_name={}'.format(lv, vg) ]
+
+    cmd = build_cmd(args, container_image, binary='lvs')
+
+    rc, cmd, out, err = exec_command(module, cmd)
+
+    result = json.loads(out)['report'][0]['lv']
+    if rc == 0 and len(result) > 0:
+        return True
+    else:
+        return False
+
+
 def zap_devices(module, container_image):
     '''
     Will run 'ceph-volume lvm zap' on all devices, lvs and partitions
@@ -489,7 +508,7 @@ def zap_devices(module, container_image):
 
     # build the CLI
     action = ['lvm', 'zap']
-    cmd = build_ceph_volume_cmd(action, container_image)
+    cmd = build_cmd(action, container_image)
     if destroy:
         cmd.append('--destroy')
 
@@ -615,8 +634,27 @@ def run_module():
 
     elif action == 'zap':
         # Zap the OSD
-        rc, cmd, out, err = exec_command(
-            module, zap_devices(module, container_image))
+        skip = []
+        for device_type in ['journal','data', 'db', 'wal']:
+            if module.params.get('{}_vg'.format(device_type), None) and module.params.get(device_type, None):
+                ret = is_lv(module, module.params['{}_vg'.format(device_type)], module.params[device_type], container_image)
+                skip.append(ret)
+                if not ret:
+                    module.params['{}_vg'.format(device_type)] = False
+                    module.params[device_type] = False
+            elif not module.params.get('{}_vg'.format(device_type), None) and module.params.get(device_type, None):
+                skip.append(True)
+
+        cmd = zap_devices(module, container_image)
+
+        if any(skip):
+            rc, cmd, out, err = exec_command(
+                module, cmd)
+        else:
+            out = 'Skipped, nothing to zap'
+            err = ''
+            changed = False
+            rc = 0
 
     elif action == 'list':
         # List Ceph LVM Metadata on a device
index 53a6d929b6c38eb8e93b956f7f8cce407ab203a7..54b9842b8af0e416b05a8a55ba07b02b88b90d77 100644 (file)
@@ -75,6 +75,8 @@ class TestCephVolumeModule(object):
         fake_module.params = {'data': '/dev/sda'}
         fake_container_image = "docker.io/ceph/daemon:latest"
         expected_command_list = container_cmd + [fake_container_image,
+                                                 '--cluster',
+                                                 'ceph',
                                                  'lvm',
                                                  'zap',
                                                  '--destroy',
@@ -87,6 +89,8 @@ class TestCephVolumeModule(object):
         fake_module.params = {'data': '/dev/sda'}
         fake_container_image = None
         expected_command_list = ['ceph-volume',
+                                 '--cluster',
+                                 'ceph',
                                  'lvm',
                                  'zap',
                                  '--destroy',
@@ -99,6 +103,8 @@ class TestCephVolumeModule(object):
         fake_module.params = {'osd_fsid': 'a_uuid'}
         fake_container_image = None
         expected_command_list = ['ceph-volume',
+                                 '--cluster',
+                                 'ceph',
                                  'lvm',
                                  'zap',
                                  '--destroy',
@@ -109,6 +115,8 @@ class TestCephVolumeModule(object):
 
     def test_activate_osd(self):
         expected_command_list = ['ceph-volume',
+                                 '--cluster',
+                                 'ceph',
                                  'lvm',
                                  'activate',
                                  '--all']
@@ -147,6 +155,8 @@ class TestCephVolumeModule(object):
         fake_module = MagicMock()
         fake_container_image = None
         expected_command_list = ['ceph-volume',
+                                 '--cluster',
+                                 'ceph',
                                  'inventory',
                                  '--format=json',
                                  ]
@@ -157,6 +167,8 @@ class TestCephVolumeModule(object):
         fake_module = MagicMock()
         fake_container_image = "docker.io/ceph/daemon:latest"
         expected_command_list = container_cmd + [fake_container_image,
+                                                 '--cluster',
+                                                 'ceph',
                                                  'inventory',
                                                  '--format=json']
         result = ceph_volume.list_storage_inventory(fake_module, fake_container_image)