]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/nfs/utils: add helper func to check cephfs path
authorDhairya Parmar <dparmar@redhat.com>
Wed, 29 Mar 2023 17:50:50 +0000 (23:20 +0530)
committerDhairya Parmar <dparmar@redhat.com>
Fri, 31 Mar 2023 08:17:41 +0000 (13:47 +0530)
this helper instantiates CephfsClient, however this was
initially planned in ExportMgr class in export.py but
due to make check failure where main python thread
experienced a dead lock which after several efforts
pointed at instantiation of CephfsClient in ExportMgr
was problematic, it was decided in order to achieve
singleton behavior, func has been added inside this
helper func that restricts instantiation using functool's
lru_cache.

Signed-off-by: Dhairya Parmar <dparmar@redhat.com>
(cherry picked from commit 1356992c1c1362a994bc5e17d857d2dfc01791c5)

src/pybind/mgr/nfs/utils.py

index ac857d6d920e7af5805ded89028f8ce4bd367643..ebf2c0d64835689ad24686ed9bc733c428e79715 100644 (file)
@@ -1,6 +1,12 @@
+import errno
+import functools
+import logging
+import stat
 from typing import List, TYPE_CHECKING
 
 import orchestrator
+import cephfs
+from mgr_util import CephfsClient, open_filesystem
 
 if TYPE_CHECKING:
     from nfs.module import Module
@@ -9,6 +15,8 @@ EXPORT_PREFIX: str = "export-"
 CONF_PREFIX: str = "conf-nfs."
 USER_CONF_PREFIX: str = "userconf-nfs."
 
+log = logging.getLogger(__name__)
+
 
 def export_obj_name(export_id: int) -> str:
     """Return a rados object name for the export."""
@@ -57,3 +65,25 @@ def check_fs(mgr: 'Module', fs_name: str) -> bool:
     '''
     fs_map = mgr.get('fs_map')
     return fs_name in [fs['mdsmap']['fs_name'] for fs in fs_map['filesystems']]
+
+
+def check_cephfs_path(mgr: 'Module', fs: str, path: str) -> None:
+    @functools.lru_cache(maxsize=1)
+    def _get_cephfs_client() -> CephfsClient:
+        return CephfsClient(mgr)
+    try:
+        cephfs_client = _get_cephfs_client()
+        with open_filesystem(cephfs_client, fs) as fs_handle:
+            stx = fs_handle.statx(path.encode('utf-8'),
+                                  cephfs.CEPH_STATX_MODE, 0)
+            if not stat.S_ISDIR(stx.get('mode')):
+                raise NotADirectoryError(f"{path} is not a dir")
+    except cephfs.ObjectNotFound as e:
+        log.exception(f"{-errno.ENOENT}: {e.args[1]}")
+        raise e
+    except cephfs.Error as e:
+        log.exception(f"{e.args[0]}: {e.args[1]}")
+        raise e
+    except Exception as e:
+        log.exception(f"unknown exception occurred: {e}")
+        raise e