From: Kefu Chai Date: Sun, 17 Jan 2021 02:37:40 +0000 (+0800) Subject: pybind/mgr/insights: use CLICommand to define command X-Git-Tag: v17.1.0~3164^2~19 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=352cd2d46199b84c4151f7627964e7773dc8463d;p=ceph.git pybind/mgr/insights: use CLICommand to define command Signed-off-by: Kefu Chai --- diff --git a/src/pybind/mgr/insights/module.py b/src/pybind/mgr/insights/module.py index 1b6865bcbd331..ac082b69a17a6 100644 --- a/src/pybind/mgr/insights/module.py +++ b/src/pybind/mgr/insights/module.py @@ -3,6 +3,7 @@ import json import re import threading +from mgr_module import CLICommand, CLIReadCommand, HandleCommandResult from mgr_module import MgrModule, CommandResult from . import health as health_util @@ -17,22 +18,8 @@ INSIGHTS_HEALTH_CHECK = "MGR_INSIGHTS_WARNING" # version tag for persistent data format ON_DISK_VERSION = 1 -class Module(MgrModule): - COMMANDS = [ - { - "cmd": "insights", - "desc": "Retrieve insights report", - "perm": "r", - "poll": False, - }, - { - 'cmd': 'insights prune-health name=hours,type=CephString', - 'desc': 'Remove health history older than hours', - 'perm': 'rw', - "poll": False, - }, - ] +class Module(MgrModule): def __init__(self, *args, **kwargs): super(Module, self).__init__(*args, **kwargs) @@ -240,7 +227,11 @@ class Module(MgrModule): ret={}, outs=\"{}\"".format(ret, outs)) return [], ["Failed to read monitor config dump"] - def do_report(self, inbuf, command): + @CLIReadCommand('insights') + def do_report(self): + ''' + Retrieve insights report + ''' health_check_details = [] report = {} @@ -291,17 +282,16 @@ class Module(MgrModule): } }) - return 0, json.dumps(report, indent=2, cls=health_util.HealthEncoder), "" - - def do_prune_health(self, inbuf, command): - try: - hours = int(command['hours']) - except ValueError: - return errno.EINVAL, '', 'hours argument must be integer' + result = json.dumps(report, indent=2, cls=health_util.HealthEncoder) + return HandleCommandResult(stdout=result) + @CLICommand('insights prune-health') + def do_prune_health(self, hours: int): + ''' + Remove health history older than hours + ''' self._health_prune_history(hours) - - return 0, "", "" + return HandleCommandResult() def testing_set_now_time_offset(self, hours): """ @@ -315,11 +305,3 @@ class Module(MgrModule): hours = int(hours) health_util.NOW_OFFSET = datetime.timedelta(hours = hours) self.log.warning("Setting now time offset {}".format(health_util.NOW_OFFSET)) - - def handle_command(self, inbuf, command): - if command["prefix"] == "insights": - return self.do_report(inbuf, command) - elif command["prefix"] == "insights prune-health": - return self.do_prune_health(inbuf, command) - else: - raise NotImplementedError(cmd["prefix"])