From: Ricardo Dias Date: Thu, 20 Dec 2018 09:25:35 +0000 (+0000) Subject: mgr/dashboard: cephfs: added CephFS service class X-Git-Tag: v14.1.0~199^2~15 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=56506ec83dfbd465a2b7d6242975c845aa8513e9;p=ceph-ci.git mgr/dashboard: cephfs: added CephFS service class Signed-off-by: Ricardo Dias --- diff --git a/src/pybind/mgr/dashboard/.pylintrc b/src/pybind/mgr/dashboard/.pylintrc index 12baef3aab5..caa510fef7f 100644 --- a/src/pybind/mgr/dashboard/.pylintrc +++ b/src/pybind/mgr/dashboard/.pylintrc @@ -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 index 00000000000..bb75b4e2573 --- /dev/null +++ b/src/pybind/mgr/dashboard/services/cephfs.py @@ -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)