From: Sebastian Wagner Date: Tue, 13 Mar 2018 15:40:26 +0000 (+0100) Subject: mgr/dashboard: Refactor `send_command` into CephService X-Git-Tag: v13.1.0~506^2~6 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=a9767a2aa9df41dcac72e5e983d5022a2ac8fab2;p=ceph.git mgr/dashboard: Refactor `send_command` into CephService * `CephService.send_command` is much easier to use. * Refactored `CephFSClients.get` and `Dashboard.load_bufer` Signed-off-by: Sebastian Wagner --- diff --git a/src/pybind/mgr/dashboard/controllers/cephfs.py b/src/pybind/mgr/dashboard/controllers/cephfs.py index c4786cebaf92f..1036ff4a8add9 100644 --- a/src/pybind/mgr/dashboard/controllers/cephfs.py +++ b/src/pybind/mgr/dashboard/controllers/cephfs.py @@ -2,10 +2,10 @@ from __future__ import absolute_import from collections import defaultdict -import json import cherrypy -from mgr_module import CommandResult + +from ..services.ceph_service import CephService from .. import mgr from ..tools import ApiController, AuthRequired, BaseController, ViewCache @@ -264,6 +264,7 @@ class CephFS(BaseController): except AttributeError: raise cherrypy.HTTPError(404, "No cephfs with id {0}".format(fs_id)) + if clients is None: raise cherrypy.HTTPError(404, "No cephfs with id {0}".format(fs_id)) @@ -305,14 +306,5 @@ class CephFSClients(object): # pylint: disable=unused-variable @ViewCache() def get(self): - mds_spec = "{0}:0".format(self.fscid) - result = CommandResult("") - self._module.send_command(result, "mds", mds_spec, - json.dumps({ - "prefix": "session ls", - }), - "") - r, outb, outs = result.wait() # TODO handle nonzero returns, e.g. when rank isn't active - assert r == 0 - return json.loads(outb) + return CephService.send_command('mds', 'session ls', srv_spec='{0}:0'.format(self.fscid)) diff --git a/src/pybind/mgr/dashboard/controllers/dashboard.py b/src/pybind/mgr/dashboard/controllers/dashboard.py index 3457c2f14db5d..882f5072eb167 100644 --- a/src/pybind/mgr/dashboard/controllers/dashboard.py +++ b/src/pybind/mgr/dashboard/controllers/dashboard.py @@ -5,7 +5,6 @@ import collections import json import cherrypy -from mgr_module import CommandResult from .. import mgr from ..services.ceph_service import CephService @@ -33,26 +32,10 @@ class Dashboard(BaseController): self.log_buffer.appendleft(log_struct) def load_buffer(self, buf, channel_name): - result = CommandResult("") - mgr.send_command(result, "mon", "", json.dumps({ - "prefix": "log last", - "format": "json", - "channel": channel_name, - "num": LOG_BUFFER_SIZE - }), "") - r, outb, outs = result.wait() - if r != 0: - # Oh well. We won't let this stop us though. - self.log.error("Error fetching log history (r={0}, \"{1}\")".format( - r, outs)) - else: - try: - lines = json.loads(outb) - except ValueError: - self.log.error("Error decoding log history") - else: - for l in lines: - buf.appendleft(l) + lines = CephService.send_command('mon', 'log last', channel=channel_name, + num=LOG_BUFFER_SIZE) + for l in lines: + buf.appendleft(l) # pylint: disable=R0914 @cherrypy.expose diff --git a/src/pybind/mgr/dashboard/services/ceph_service.py b/src/pybind/mgr/dashboard/services/ceph_service.py index cb27e1ea6e9b0..9f113cf907d57 100644 --- a/src/pybind/mgr/dashboard/services/ceph_service.py +++ b/src/pybind/mgr/dashboard/services/ceph_service.py @@ -4,8 +4,10 @@ from __future__ import absolute_import import time import collections from collections import defaultdict +import json -from .. import mgr +from mgr_module import CommandResult +from .. import logger, mgr class CephService(object): @@ -101,3 +103,42 @@ class CephService(object): pool['stats'] = s pools_w_stats.append(pool) return pools_w_stats + + @classmethod + def send_command(cls, srv_type, prefix, srv_spec='', **kwargs): + """ + :type prefix: str + :param srv_type: mon | + :param kwargs: will be added to argdict + :param srv_spec: typically empty. or something like ":0" + + :raises PermissionError: See rados.make_ex + :raises ObjectNotFound: See rados.make_ex + :raises IOError: See rados.make_ex + :raises NoSpace: See rados.make_ex + :raises ObjectExists: See rados.make_ex + :raises ObjectBusy: See rados.make_ex + :raises NoData: See rados.make_ex + :raises InterruptedOrTimeoutError: See rados.make_ex + :raises TimedOut: See rados.make_ex + :raises ValueError: return code != 0 + """ + argdict = { + "prefix": prefix, + "format": "json", + } + argdict.update({k: v for k, v in kwargs.items() if v}) + + result = CommandResult("") + mgr.send_command(result, srv_type, srv_spec, json.dumps(argdict), "") + r, outb, outs = result.wait() + if r != 0: + msg = "send_command '{}' failed. (r={}, outs=\"{}\", kwargs={})".format(prefix, r, outs, + kwargs) + logger.error(msg) + raise ValueError(msg) + else: + try: + return json.loads(outb) + except Exception: # pylint: disable=broad-except + return outb