From 6a032b33b78427df3c01615ae50317050f7542a3 Mon Sep 17 00:00:00 2001 From: Redouane Kachach Date: Wed, 16 Feb 2022 12:01:57 +0100 Subject: [PATCH] mgr/cephadm: Delete ceph.target if last cluster Fixes: https://tracker.ceph.com/issues/46655 Signed-off-by: Redouane Kachach (cherry picked from commit f2a916b985ce3fef103fb1385159d57f3788c888) --- src/cephadm/cephadm | 30 ++++++++++++++++-------------- src/cephadm/tests/test_cephadm.py | 10 ++++++++++ 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/cephadm/cephadm b/src/cephadm/cephadm index f2b320bd5f22c..77f8074429b6f 100755 --- a/src/cephadm/cephadm +++ b/src/cephadm/cephadm @@ -6462,6 +6462,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: @@ -6471,13 +6475,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], @@ -6485,14 +6483,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], @@ -6522,7 +6523,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 8ee1ddb556236..0426fb25a7e62 100644 --- a/src/cephadm/tests/test_cephadm.py +++ b/src/cephadm/tests/test_cephadm.py @@ -550,6 +550,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 -- 2.39.5