From 182d926a4b8a2d1c5c66809dbc89e3521b24d941 Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Thu, 4 Jun 2020 11:58:53 +0200 Subject: [PATCH] cephadm: More robust way to deduce the systemd unit Generate a better error message when using wrong args. Signed-off-by: Sebastian Wagner --- src/cephadm/cephadm | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/cephadm/cephadm b/src/cephadm/cephadm index 3e51e19f03c57..31f85b0df5bfe 100755 --- a/src/cephadm/cephadm +++ b/src/cephadm/cephadm @@ -1357,6 +1357,13 @@ def get_unit_name(fsid, daemon_type, daemon_id=None): else: return 'ceph-%s@%s' % (fsid, daemon_type) +def get_unit_name_by_daemon_name(fsid, name): + daemon = get_daemon_description(fsid, name) + try: + return daemon['systemd_unit'] + except KeyError: + raise Error('Failed to get unit name for {}'.format(daemon)) + def check_unit(unit_name): # type: (str) -> Tuple[bool, str, bool] # NOTE: we ignore the exit code here because systemctl outputs @@ -2975,8 +2982,9 @@ def command_unit(): # type: () -> None if not args.fsid: raise Error('must pass --fsid to specify cluster') - (daemon_type, daemon_id) = args.name.split('.', 1) - unit_name = get_unit_name(args.fsid, daemon_type, daemon_id) + + unit_name = get_unit_name_by_daemon_name(args.fsid, args.name) + call_throws([ 'systemctl', args.command, @@ -2990,8 +2998,7 @@ def command_logs(): if not args.fsid: raise Error('must pass --fsid to specify cluster') - (daemon_type, daemon_id) = args.name.split('.', 1) - unit_name = get_unit_name(args.fsid, daemon_type, daemon_id) + unit_name = get_unit_name_by_daemon_name(args.fsid, args.name) cmd = [find_program('journalctl')] cmd.extend(['-u', unit_name]) @@ -3101,6 +3108,7 @@ def list_daemons(detail=True, legacy_dir=None): 'style': 'cephadm:v1', 'name': name, 'fsid': fsid, + 'systemd_unit': unit_name, } if detail: # get container id @@ -3184,15 +3192,24 @@ def list_daemons(detail=True, legacy_dir=None): os.path.join(data_dir, fsid, j, 'unit.image')) i['configured'] = get_file_timestamp( os.path.join(data_dir, fsid, j, 'unit.configured')) - i['systemd_unit'] = unit_name ls.append(i) - # /var/lib/rook - # WRITE ME return ls +def get_daemon_description(fsid, name, detail=False, legacy_dir=None): + # type: (str, str, bool, Optional[str]) -> Dict[str, str] + + for d in list_daemons(detail=detail, legacy_dir=legacy_dir): + if d['fsid'] != fsid: + continue + if d['name'] != name: + continue + return d + raise Error('Daemon not found: {}. See `cephadm ls`'.format(name)) + + ################################## @default_image @@ -3591,11 +3608,13 @@ def command_rm_daemon(): l = FileLock(args.fsid) l.acquire() + unit_name = get_unit_name_by_daemon_name(args.fsid, args.name) + (daemon_type, daemon_id) = args.name.split('.', 1) if daemon_type in ['mon', 'osd'] and not args.force: raise Error('must pass --force to proceed: ' 'this command may destroy precious data!') - unit_name = get_unit_name(args.fsid, daemon_type, daemon_id) + call(['systemctl', 'stop', unit_name], verbose_on_failure=False) call(['systemctl', 'reset-failed', unit_name], -- 2.39.5