From: Redouane Kachach Date: Wed, 16 Feb 2022 11:01:57 +0000 (+0100) Subject: mgr/cephadm: Delete ceph.target if last cluster X-Git-Tag: v16.2.8~129^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=caae97244eebb75624926396a865c5885fe4c907;p=ceph.git mgr/cephadm: Delete ceph.target if last cluster Fixes: https://tracker.ceph.com/issues/46655 Signed-off-by: Redouane Kachach --- diff --git a/src/cephadm/cephadm b/src/cephadm/cephadm index 3d0fdee450316..c1f474c29f96a 100755 --- a/src/cephadm/cephadm +++ b/src/cephadm/cephadm @@ -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']) diff --git a/src/cephadm/tests/test_cephadm.py b/src/cephadm/tests/test_cephadm.py index 13939318fc09a..4aaae01359cda 100644 --- a/src/cephadm/tests/test_cephadm.py +++ b/src/cephadm/tests/test_cephadm.py @@ -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