]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: Refactor `send_command` into CephService
authorSebastian Wagner <sebastian.wagner@suse.com>
Tue, 13 Mar 2018 15:40:26 +0000 (16:40 +0100)
committerSebastian Wagner <sebastian.wagner@suse.com>
Fri, 23 Mar 2018 15:40:17 +0000 (16:40 +0100)
* `CephService.send_command` is much easier to use.
* Refactored `CephFSClients.get` and `Dashboard.load_bufer`

Signed-off-by: Sebastian Wagner <sebastian.wagner@suse.com>
src/pybind/mgr/dashboard/controllers/cephfs.py
src/pybind/mgr/dashboard/controllers/dashboard.py
src/pybind/mgr/dashboard/services/ceph_service.py

index c4786cebaf92fcf820849465fe9e50715a8dd40e..1036ff4a8add938c4855a4101995068092e61818 100644 (file)
@@ -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))
index 3457c2f14db5dc0e8337886a1f372cda081d55c4..882f5072eb167ff89936389f39ab4655db43c227 100644 (file)
@@ -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
index cb27e1ea6e9b0fad3c45020103e006efb00787b9..9f113cf907d57b6044b375c091f84e93f21b463f 100644 (file)
@@ -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 "<fs_id>: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