]> git-server-git.apps.pok.os.sepia.ceph.com Git - cephmetrics.git/commitdiff
iscsi.py: fix to defer the import of rtslib_fb 95/head
authorPaul Cuzner <pcuzner@redhat.com>
Tue, 8 Aug 2017 21:48:55 +0000 (09:48 +1200)
committerPaul Cuzner <pcuzner@redhat.com>
Tue, 8 Aug 2017 21:48:55 +0000 (09:48 +1200)
the goal of the parent module cephmetrics is to be generic across the
different ceph roles. By deferring the import of rtslib to the instantiation
of the first (and only!) ISCSIGateway object cephmetrics can import this
iscsi module without a problem regardless of the runtime environment.

collectors/iscsi.py

index 284ffb667051fea300370ec688031b3402c29604..df14acbe397194b3bebed69ac5b68f67aa094465 100644 (file)
@@ -1,11 +1,16 @@
 #!/usr/bin/env python2
 
 # requires python-rtslib_fb for LIO interaction
-
+#
+# NB. the rtslib_fb module is dynamically loaded by the ISCSIGateway
+# class instantiation. This prevents import errors within the generic parent
+# module cephmetrics
+#
 import os
+import sys
 import time
+
 from collectors.base import BaseCollector
-from rtslib_fb import RTSRoot
 from collectors.common import fread
 
 
@@ -92,6 +97,17 @@ class ISCSIGateway(BaseCollector):
     def __init__(self, *args, **kwargs):
         BaseCollector.__init__(self, *args, **kwargs)
 
+        # Since the module can be imported by a parent class but not
+        # instantiated, the rtslib import is deferred until the first instance
+        # of the the class is created. This keeps the parent module simple
+        # and more importantly generic
+        if 'rtslib_fb.root' not in sys.modules.keys():
+
+            try:
+                import rtslib_fb.root as RTSRoot
+            except ImportError:
+                raise ImportError("rtslib_fb python package is missing")
+
         self._root = RTSRoot()
 
         self.clients = {}
@@ -188,8 +204,6 @@ class ISCSIGateway(BaseCollector):
                          }
                }
 
-
-
     def _get_so(self):
         return [so for so in self._root.storage_objects]