##################################
+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:
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],
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],
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'])
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