From: Paul Cuzner Date: Tue, 8 Aug 2017 21:48:55 +0000 (+1200) Subject: iscsi.py: fix to defer the import of rtslib_fb X-Git-Tag: v1.0~33^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F95%2Fhead;p=cephmetrics.git iscsi.py: fix to defer the import of rtslib_fb 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. --- diff --git a/collectors/iscsi.py b/collectors/iscsi.py index 284ffb6..df14acb 100644 --- a/collectors/iscsi.py +++ b/collectors/iscsi.py @@ -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]