From: liuchang0812 Date: Wed, 24 May 2017 13:39:23 +0000 (+0800) Subject: tool/ceph: validate target in tell subcommand X-Git-Tag: v12.1.0~57^2~24^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=9753a0065db8bfb03d86a7185bc636c7aa4c7af7;p=ceph.git tool/ceph: validate target in tell subcommand Signed-off-by: liuchang0812 --- diff --git a/src/ceph.in b/src/ceph.in index ea4d5dd19498..4ba58d2d4b0e 100755 --- a/src/ceph.in +++ b/src/ceph.in @@ -183,6 +183,67 @@ def mdsids(): l.append(mdsdict['name']) return l +def mgrids(): + ret, outbuf, outs = json_command(cluster_handle, prefix='mgr dump', + argdict={'format': 'json'}) + if ret == -errno.EINVAL: + # try old mon + ret, outbuf, outs = send_command(cluster_handle, + cmd=['mgr', 'dump', '--format=json']) + if ret: + raise RuntimeError('Can\'t contact mon for mgr list') + + d = json.loads(outbuf.decode('utf-8')) + l = [] + l.append(d['active_name']) + for i in d['standbys']: + l.append(i['name']) + return l + +def validate_target(target): + """ + this function will return true iff target is a correct + target, such as mon.a/osd.2/mds.a/mgr. + + target: str + return: bool, or raise RuntimeError + """ + _target = target.split('.') + ret = False + + if len(_target) == 2: + # for case "service.id" + service_name, service_id = _target[0], _target[1] + exist_ids = [] + if service_name == "mon": + exist_ids = monids() + elif service_name == "osd": + exist_ids = osdids() + elif service_name == "mds": + exist_ids = mdsids() + elif service_name == "mgr": + exist_ids = mgrids() + else: + if verbose: + print('WARN: {0} is not a legal service name, should be one of mon/osd/mds/mgr'.format(service_name), + file=sys.stderr) + return False + + if service_id in exist_ids: + ret = True + + if not ret and verbose: + print('WARN: the service id you provided does not exist. service id should ' + 'be one of {0}.'.format('/'.join(exist_ids)), file=sys.stderr) + + elif len(_target) == 1 and _target[0] == "mgr": + ret = True + else: + if verbose: + print('WARN: \"{0}\" is not a legal target. it should be one of mon./osd./mds./mgr.'.format(target), file=sys.stderr) + + return ret + # these args must be passed to all child programs GLOBAL_ARGS = { 'client_id': '--id', @@ -856,7 +917,12 @@ def main(): # implement "tell service.id help" if len(childargs) >= 3 and childargs[0] == 'tell' and childargs[2] == 'help': - return do_extended_help(parser, childargs, childargs[1].split('.'), None) + if validate_target(childargs[1]): + return do_extended_help(parser, childargs, childargs[1].split('.'), None) + else: + print('target {0} doesn\'t exists, please pass correct target to tell command, such as mon.a/' + 'osd.1/mds.a/mgr'.format(childargs[1]), file=sys.stderr) + return 1 # implement -w/--watch_* # This is ugly, but Namespace() isn't quite rich enough.