]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/volumes: add fs_util helper module
authorVenky Shankar <vshankar@redhat.com>
Wed, 20 Nov 2019 13:24:41 +0000 (08:24 -0500)
committerRamana Raja <rraja@redhat.com>
Wed, 12 Feb 2020 10:11:59 +0000 (05:11 -0500)
helpers for various filesystem querying routines, utils
for creating/removing filesystem, pool and MDSs.

Signed-off-by: Venky Shankar <vshankar@redhat.com>
(cherry picked from commit 6682c7736f4d6462aefd5387ea9f34f97ce28264)

src/pybind/mgr/volumes/fs/fs_util.py [new file with mode: 0644]

diff --git a/src/pybind/mgr/volumes/fs/fs_util.py b/src/pybind/mgr/volumes/fs/fs_util.py
new file mode 100644 (file)
index 0000000..7eedbfa
--- /dev/null
@@ -0,0 +1,86 @@
+import os
+import errno
+import logging
+
+import cephfs
+import orchestrator
+
+from .exception import VolumeException
+
+log = logging.getLogger(__name__)
+
+def create_pool(mgr, pool_name, pg_num):
+    # create the given pool
+    command = {'prefix': 'osd pool create', 'pool': pool_name, 'pg_num': pg_num}
+    return mgr.mon_command(command)
+
+def remove_pool(mgr, pool_name):
+    command = {'prefix': 'osd pool rm', 'pool': pool_name, 'pool2': pool_name,
+               'yes_i_really_really_mean_it': True}
+    return mgr.mon_command(command)
+
+def create_filesystem(mgr, fs_name, metadata_pool, data_pool):
+    command = {'prefix': 'fs new', 'fs_name': fs_name, 'metadata': metadata_pool,
+               'data': data_pool}
+    return mgr.mon_command(command)
+
+def remove_filesystem(mgr, fs_name):
+    command = {'prefix': 'fs fail', 'fs_name': fs_name}
+    r, outb, outs = mgr.mon_command(command)
+    if r != 0:
+        return r, outb, outs
+
+    command = {'prefix': 'fs rm', 'fs_name': fs_name, 'yes_i_really_mean_it': True}
+    return mgr.mon_command(command)
+
+def create_mds(mgr, fs_name):
+    spec = orchestrator.StatelessServiceSpec()
+    spec.name = fs_name
+    try:
+        completion = mgr.add_stateless_service("mds", spec)
+        mgr._orchestrator_wait([completion])
+        orchestrator.raise_if_exception(completion)
+    except (ImportError, orchestrator.OrchestratorError):
+        return 0, "", "Volume created successfully (no MDS daemons created)"
+    except Exception as e:
+        # Don't let detailed orchestrator exceptions (python backtraces)
+        # bubble out to the user
+        log.exception("Failed to create MDS daemons")
+        return -errno.EINVAL, "", str(e)
+    return 0, "", ""
+
+def volume_exists(mgr, fs_name):
+    fs_map = mgr.get('fs_map')
+    for fs in fs_map['filesystems']:
+        if fs['mdsmap']['fs_name'] == fs_name:
+            return True
+    return False
+
+def listdir(fs, dirpath):
+    """
+    Get the directory names (only dirs) for a given path
+    """
+    dirs = []
+    try:
+        with fs.opendir(dirpath) as dir_handle:
+            d = fs.readdir(dir_handle)
+            while d:
+                if (d.d_name not in (b".", b"..")) and d.is_dir():
+                    dirs.append(d.d_name)
+                d = fs.readdir(dir_handle)
+    except cephfs.Error as e:
+        raise VolumeException(-e.args[0], e.args[1])
+    return dirs
+
+def get_ancestor_xattr(fs, path, attr):
+    """
+    Helper for reading layout information: if this xattr is missing
+    on the requested path, keep checking parents until we find it.
+    """
+    try:
+        return fs.getxattr(path, attr).decode('utf-8')
+    except cephfs.NoData as e:
+        if path == "/":
+            raise VolumeException(-e.args[0]. e.args[1])
+        else:
+            return get_ancestor_xattr(fs, os.path.split(path)[0], attr)