From 8be9cac0a93bd78a665b7268e77e42243733535e Mon Sep 17 00:00:00 2001 From: Paul Cuzner Date: Wed, 9 Aug 2017 09:48:55 +1200 Subject: [PATCH] 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. --- collectors/iscsi.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) 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] -- 2.47.3