]> git-server-git.apps.pok.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 08:49:07 +0000 (10:49 +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 3634a59ff69632d1d90d8cf834b82561ab44b2dd..078a887227b9e05792d884cdf98f1c15410aa0ce 100644 (file)
@@ -212,20 +212,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)
@@ -307,7 +308,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')
 
@@ -386,7 +387,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)
@@ -425,7 +426,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')
@@ -438,7 +439,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
@@ -451,12 +452,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
@@ -479,7 +498,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')
 
@@ -604,8 +623,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 917d94db7d626da1730719abd0abb6595f6a1a78..07e5466ee92a5b2d90136c93d10fec5e11626c25 100644 (file)
@@ -81,6 +81,8 @@ class TestCephVolumeModule(object):
                                  '-v', '/var/log/ceph/:/var/log/ceph/:z',
                                  '--entrypoint=ceph-volume',
                                  'docker.io/ceph/daemon:latest-luminous',
+                                 '--cluster',
+                                 'ceph',
                                  'lvm',
                                  'zap',
                                  '--destroy',
@@ -93,6 +95,8 @@ class TestCephVolumeModule(object):
         fake_module.params = {'data': '/dev/sda'}
         fake_container_image = None
         expected_command_list = ['ceph-volume',
+                                 '--cluster',
+                                 'ceph',
                                  'lvm',
                                  'zap',
                                  '--destroy',
@@ -105,6 +109,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',
@@ -115,6 +121,8 @@ class TestCephVolumeModule(object):
 
     def test_activate_osd(self):
         expected_command_list = ['ceph-volume',
+                                 '--cluster',
+                                 'ceph',
                                  'lvm',
                                  'activate',
                                  '--all']
@@ -164,6 +172,8 @@ class TestCephVolumeModule(object):
         fake_module = MagicMock()
         fake_container_image = None
         expected_command_list = ['ceph-volume',
+                                 '--cluster',
+                                 'ceph',
                                  'inventory',
                                  '--format=json',
                                  ]
@@ -183,6 +193,8 @@ class TestCephVolumeModule(object):
                                  '-v', '/var/log/ceph/:/var/log/ceph/:z',
                                  '--entrypoint=ceph-volume',
                                  'docker.io/ceph/daemon:latest-luminous',
+                                 '--cluster',
+                                 'ceph',
                                  'inventory',
                                  '--format=json',
                                  ]