Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / pybind / mgr / dashboard / rbd_ls.py
1
2 import rbd
3 import rados
4 from types import OsdMap
5 from remote_view_cache import RemoteViewCache
6
7 class RbdPoolLs(RemoteViewCache):
8     def _get(self):
9         ctx_capsule = self._module.get_context()
10
11
12         osd_map = self._module.get_sync_object(OsdMap).data
13         osd_pools = [pool['pool_name'] for pool in osd_map['pools']]
14
15         rbd_pools = []
16         for pool in osd_pools:
17             self.log.debug("Constructing IOCtx " + pool)
18             try:
19                 ioctx = self._module.rados.open_ioctx(pool)
20                 ioctx.stat("rbd_directory")
21                 rbd_pools.append(pool)
22             except (rados.PermissionError, rados.ObjectNotFound):
23                 self.log.debug("No RBD directory in " + pool)
24             except:
25                 self.log.exception("Failed to open pool " + pool)
26
27         return rbd_pools
28
29 class RbdLs(RemoteViewCache):
30     def __init__(self, module_inst, pool):
31         super(RbdLs, self).__init__(module_inst)
32
33         self.pool = pool
34
35         self.ioctx = None
36         self.rbd = None
37
38     def _init(self):
39         self.log.debug("Constructing IOCtx")
40         self.ioctx = self._module.rados.open_ioctx(self.pool)
41
42         self.log.debug("Constructing RBD")
43         self.rbd = rbd.RBD()
44
45     def _get(self):
46         self.log.debug("rbd.list")
47         names = self.rbd.list(self.ioctx)
48         result = []
49         for name in names:
50             i = rbd.Image(self.ioctx, name)
51             stat = i.stat()
52             stat['name'] = name
53             features = i.features()
54             stat['features'] = features
55             stat['features_name'] = self._format_bitmask(features)
56
57             try:
58                 parent_info = i.parent_info()
59                 parent = "{}@{}".format(parent_info[0], parent_info[1])
60                 if parent_info[0] != self.pool:
61                     parent = "{}/{}".format(parent_info[0], parent)
62                 stat['parent'] = parent
63             except rbd.ImageNotFound:
64                 pass
65             result.append(stat)
66         return result
67
68     def _format_bitmask(self, features):
69         names = ""
70         RBD_FEATURES_NAME_MAPPING = {
71             rbd.RBD_FEATURE_LAYERING: "layering",
72             rbd.RBD_FEATURE_STRIPINGV2: "striping",
73             rbd.RBD_FEATURE_EXCLUSIVE_LOCK: "exclusive-lock",
74             rbd.RBD_FEATURE_OBJECT_MAP: "object-map",
75             rbd.RBD_FEATURE_FAST_DIFF: "fast-diff",
76             rbd.RBD_FEATURE_DEEP_FLATTEN: "deep-flatten",
77             rbd.RBD_FEATURE_JOURNALING: "journaling",
78             rbd.RBD_FEATURE_DATA_POOL: "data-pool",
79         }
80
81         for key in RBD_FEATURES_NAME_MAPPING.keys():
82             if (key & features == 0):
83                 continue
84
85             if names:
86                 names = names + ", "
87             names = names + RBD_FEATURES_NAME_MAPPING.get(key)
88
89         return names