]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/cephadm: Delete ceph.target if last cluster 45228/head
authorRedouane Kachach <rkachach@redhat.com>
Wed, 16 Feb 2022 11:01:57 +0000 (12:01 +0100)
committerRedouane Kachach <rkachach@redhat.com>
Wed, 2 Mar 2022 18:56:02 +0000 (19:56 +0100)
Fixes: https://tracker.ceph.com/issues/46655
Signed-off-by: Redouane Kachach <rkachach@redhat.com>
src/cephadm/cephadm
src/cephadm/tests/test_cephadm.py

index 3d0fdee45031694f63db433dc3daf018d1a87e3b..c1f474c29f96a33b35254b0feb3909891b95acc9 100755 (executable)
@@ -5988,6 +5988,10 @@ def command_zap_osds(ctx: CephadmContext) -> None:
 ##################################
 
 
+def get_ceph_cluster_count(ctx: CephadmContext) -> int:
+    return len([c for c in os.listdir(ctx.data_dir) if is_fsid(c)])
+
+
 def command_rm_cluster(ctx):
     # type: (CephadmContext) -> None
     if not ctx.force:
@@ -5997,13 +6001,7 @@ def command_rm_cluster(ctx):
     lock = FileLock(ctx, ctx.fsid)
     lock.acquire()
 
-    # stop + disable individual daemon units
-    for d in list_daemons(ctx, detail=False):
-        if d['fsid'] != ctx.fsid:
-            continue
-        if d['style'] != 'cephadm:v1':
-            continue
-        unit_name = get_unit_name(ctx.fsid, d['name'])
+    def disable_systemd_service(unit_name: str) -> None:
         call(ctx, ['systemctl', 'stop', unit_name],
              verbosity=CallVerbosity.DEBUG)
         call(ctx, ['systemctl', 'reset-failed', unit_name],
@@ -6011,14 +6009,17 @@ def command_rm_cluster(ctx):
         call(ctx, ['systemctl', 'disable', unit_name],
              verbosity=CallVerbosity.DEBUG)
 
+    # stop + disable individual daemon units
+    for d in list_daemons(ctx, detail=False):
+        if d['fsid'] != ctx.fsid:
+            continue
+        if d['style'] != 'cephadm:v1':
+            continue
+        disable_systemd_service(get_unit_name(ctx.fsid, d['name']))
+
     # cluster units
     for unit_name in ['ceph-%s.target' % ctx.fsid]:
-        call(ctx, ['systemctl', 'stop', unit_name],
-             verbosity=CallVerbosity.DEBUG)
-        call(ctx, ['systemctl', 'reset-failed', unit_name],
-             verbosity=CallVerbosity.DEBUG)
-        call(ctx, ['systemctl', 'disable', unit_name],
-             verbosity=CallVerbosity.DEBUG)
+        disable_systemd_service(unit_name)
 
     slice_name = 'system-ceph\\x2d{}.slice'.format(ctx.fsid.replace('-', '\\x2d'))
     call(ctx, ['systemctl', 'stop', slice_name],
@@ -6048,7 +6049,8 @@ def command_rm_cluster(ctx):
     call_throws(ctx, ['rm', '-f', ctx.logrotate_dir + '/ceph-%s' % ctx.fsid])
 
     # if last cluster on host remove shared files
-    if not os.listdir(ctx.data_dir):
+    if get_ceph_cluster_count(ctx) == 0:
+        disable_systemd_service('ceph.target')
 
         # rm shared ceph target files
         call_throws(ctx, ['rm', '-f', ctx.unit_dir + '/multi-user.target.wants/ceph.target'])
index 13939318fc09ad4255f2a3f86de05a83c13b4c9b..4aaae01359cdabde1d92ff839d33b57fb847543c 100644 (file)
@@ -534,6 +534,16 @@ ff792c06d8544b983.scope not found.: OCI runtime error"""
         with pytest.raises(cd.Error, match='OCI'):
             cd.extract_uid_gid(ctx)
 
+    @pytest.mark.parametrize('test_input, expected', [
+        ([cd.make_fsid(), cd.make_fsid(), cd.make_fsid()], 3),
+        ([cd.make_fsid(), 'invalid-fsid', cd.make_fsid(), '0b87e50c-8e77-11ec-b890-'], 2),
+        (['f6860ec2-8e76-11ec-', '0b87e50c-8e77-11ec-b890-', ''], 0),
+        ([], 0),
+    ])
+    def test_get_ceph_cluster_count(self, test_input, expected):
+        ctx = cd.CephadmContext()
+        with mock.patch('os.listdir', return_value=test_input):
+            assert cd.get_ceph_cluster_count(ctx) == expected
 
 class TestCustomContainer(unittest.TestCase):
     cc: cd.CustomContainer