]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/dashboard: cephfs: added CephFS service class
authorRicardo Dias <rdias@suse.com>
Thu, 20 Dec 2018 09:25:35 +0000 (09:25 +0000)
committerRicardo Dias <rdias@suse.com>
Thu, 31 Jan 2019 09:42:27 +0000 (09:42 +0000)
Signed-off-by: Ricardo Dias <rdias@suse.com>
src/pybind/mgr/dashboard/.pylintrc
src/pybind/mgr/dashboard/services/cephfs.py [new file with mode: 0644]

index 12baef3aab51dabffb8f1cf6e71f991e26489d14..caa510fef7f26841048019979a663dd2e06f8e44 100644 (file)
@@ -3,7 +3,7 @@
 # A comma-separated list of package or module names from where C extensions may
 # be loaded. Extensions are loading into the active Python interpreter and may
 # run arbitrary code
-extension-pkg-whitelist=rados,rbd,math
+extension-pkg-whitelist=rados,rbd,math,cephfs
 
 # Add files or directories to the blacklist. They should be base names, not
 # paths.
diff --git a/src/pybind/mgr/dashboard/services/cephfs.py b/src/pybind/mgr/dashboard/services/cephfs.py
new file mode 100644 (file)
index 0000000..bb75b4e
--- /dev/null
@@ -0,0 +1,77 @@
+# -*- coding: utf-8 -*-
+from __future__ import absolute_import
+
+from contextlib import contextmanager
+
+import cephfs
+
+from .. import mgr, logger
+
+
+class CephFS(object):
+    @classmethod
+    def list_filesystems(cls):
+        fsmap = mgr.get("fs_map")
+        return [{'id': fs['id'], 'name': fs['mdsmap']['fs_name']}
+                for fs in fsmap['filesystems']]
+
+    def __init__(self, fs_name=None):
+        logger.debug("[CephFS] initializing cephfs connection")
+        self.cfs = cephfs.LibCephFS(rados_inst=mgr.rados)
+        logger.debug("[CephFS] mounting cephfs filesystem: %s", fs_name)
+        if fs_name:
+            self.cfs.mount(filesystem_name=fs_name)
+        else:
+            self.cfs.mount()
+        logger.debug("[CephFS] mounted cephfs filesystem")
+
+    def __del__(self):
+        logger.debug("[CephFS] shutting down cephfs filesystem")
+        self.cfs.shutdown()
+
+    @contextmanager
+    def opendir(self, dirpath):
+        d = None
+        try:
+            d = self.cfs.opendir(dirpath)
+            yield d
+        finally:
+            if d:
+                self.cfs.closedir(d)
+
+    def get_dir_list(self, dirpath, level):
+        logger.debug("[CephFS] get_dir_list dirpath=%s level=%s", dirpath,
+                     level)
+        if level == 0:
+            return [dirpath]
+        logger.debug("[CephFS] opening dirpath=%s", dirpath)
+        with self.opendir(dirpath) as d:
+            dent = self.cfs.readdir(d)
+            paths = [dirpath]
+            while dent:
+                logger.debug("[CephFS] found entry=%s", dent.d_name)
+                if dent.d_name in ['.', '..']:
+                    dent = self.cfs.readdir(d)
+                    continue
+                if dent.is_dir():
+                    logger.debug("[CephFS] found dir=%s", dent.d_name)
+                    subdirpath = '{}{}/'.format(dirpath, dent.d_name)
+                    paths.extend(self.get_dir_list(subdirpath, level-1))
+                dent = self.cfs.readdir(d)
+        return paths
+
+    def dir_exists(self, dirpath):
+        try:
+            with self.opendir(dirpath):
+                return True
+        except cephfs.ObjectNotFound:
+            return False
+
+    def mkdirs(self, dirpath):
+        if dirpath == '/':
+            raise Exception('Cannot create root directory "/"')
+        if self.dir_exists(dirpath):
+            return
+
+        logger.info("[CephFS] Creating directory: %s", dirpath)
+        self.cfs.mkdirs("{}".format(dirpath).encode('utf-8'), 0o755)