Пример #1
0
 def _setup_helpers(self):
     """Initializes protocol-specific NAS drivers."""
     for helper_str in self.configuration.share_helpers:
         share_proto, __, import_str = helper_str.partition('=')
         helper = importutils.import_class(import_str)
         self._helpers[share_proto.upper()] = helper(
             self._execute, self._ssh_exec, self.configuration)
Пример #2
0
 def __init__(self,
              host,
              binary,
              topic,
              manager,
              report_interval=None,
              periodic_interval=None,
              periodic_fuzzy_delay=None,
              service_name=None,
              *args,
              **kwargs):
     super(Service, self).__init__()
     if not rpc.initialized():
         rpc.init(CONF)
     self.host = host
     self.binary = binary
     self.topic = topic
     self.manager_class_name = manager
     manager_class = importutils.import_class(self.manager_class_name)
     self.manager = manager_class(host=self.host,
                                  service_name=service_name,
                                  *args,
                                  **kwargs)
     self.report_interval = report_interval
     self.periodic_interval = periodic_interval
     self.periodic_fuzzy_delay = periodic_fuzzy_delay
     self.saved_args, self.saved_kwargs = args, kwargs
     self.timers = []
Пример #3
0
    def __init__(self, application, limits=None, limiter=None, **kwargs):
        """Initialize new `RateLimitingMiddleware`.

        `RateLimitingMiddleware` wraps the given WSGI application and
        sets up the given limits.

        @param application: WSGI application to wrap
        @param limits: String describing limits
        @param limiter: String identifying class for representing limits

        Other parameters are passed to the constructor for the limiter.
        """
        base_wsgi.Middleware.__init__(self, application)

        # Select the limiter class
        if limiter is None:
            limiter = Limiter
        else:
            limiter = importutils.import_class(limiter)

        # Parse the limits, if any are provided
        if limits is not None:
            limits = limiter.parse_limits(limits)

        self._limiter = limiter(limits or DEFAULT_LIMITS, **kwargs)
Пример #4
0
 def __init__(self, db, _helpers, *args, **kwargs):
     """Do initialization."""
     super(ServiceInstanceManager, self).__init__(*args, **kwargs)
     if not CONF.service_instance_user:
         raise exception.ServiceInstanceException(_('Service instance user '
                                                    'is not specified'))
     self.admin_context = context.get_admin_context()
     self._execute = utils.execute
     self.compute_api = compute.API()
     self.neutron_api = neutron.API()
     self._helpers = _helpers
     self.db = db
     attempts = 5
     while attempts:
         try:
             self.service_tenant_id = self.neutron_api.admin_tenant_id
             break
         except exception.NetworkException:
             LOG.debug(_('Connection to neutron failed.'))
             attempts -= 1
             time.sleep(3)
     else:
         raise exception.ServiceInstanceException(_('Can not receive '
                                                    'service tenant id.'))
     self.share_networks_locks = {}
     self.share_networks_servers = {}
     self.service_network_id = self._get_service_network()
     self.vif_driver = importutils.import_class(CONF.interface_driver)()
     self._setup_connectivity_with_service_instances()
Пример #5
0
    def __init__(self, application, limits=None, limiter=None, **kwargs):
        """Initialize new `RateLimitingMiddleware`.

        `RateLimitingMiddleware` wraps the given WSGI application and
        sets up the given limits.

        @param application: WSGI application to wrap
        @param limits: String describing limits
        @param limiter: String identifying class for representing limits

        Other parameters are passed to the constructor for the limiter.
        """
        base_wsgi.Middleware.__init__(self, application)

        # Select the limiter class
        if limiter is None:
            limiter = Limiter
        else:
            limiter = importutils.import_class(limiter)

        # Parse the limits, if any are provided
        if limits is not None:
            limits = limiter.parse_limits(limits)

        self._limiter = limiter(limits or DEFAULT_LIMITS, **kwargs)
Пример #6
0
 def _setup_helpers(self):
     """Initializes protocol-specific NAS drivers."""
     for helper_str in self.configuration.share_helpers:
         share_proto, __, import_str = helper_str.partition('=')
         helper = importutils.import_class(import_str)
         self._helpers[share_proto.upper()] = helper(self._execute,
                                                 self.configuration,
                                                 self.share_networks_locks)
Пример #7
0
 def _setup_helpers(self):
     """Initializes protocol-specific NAS drivers."""
     self._helpers = {}
     for helper_str in self.configuration.share_lvm_helpers:
         share_proto, _, import_str = helper_str.partition('=')
         helper = importutils.import_class(import_str)
         #TODO(rushiagr): better way to handle configuration
         #                   instead of just passing to the helper
         self._helpers[share_proto.upper()] = helper(self._execute,
                                                     self.configuration)
Пример #8
0
 def test_do_setup(self):
     self.mox.StubOutWithMock(importutils, 'import_class')
     helpers = [
         (self._helper_cifs, 'manila.share.drivers.lvm.CIFSNetConfHelper'),
         (self._helper_nfs, 'manila.share.drivers.lvm.NFSHelper'),
     ]
     for helper, path in helpers:
         importutils.import_class(path).AndReturn(helper)
         helper.__call__(fake_utils.fake_execute,
                         self.fake_conf).\
             AndReturn(helper)
         helper.init()
     self.mox.ReplayAll()
     self._driver.do_setup(self._context)
     expected_helpers = {
         'CIFS': self._helper_cifs,
         'NFS': self._helper_nfs,
     }
     self.assertEqual(self._driver._helpers, expected_helpers)
Пример #9
0
 def _setup_helpers(self):
     """Initializes protocol-specific NAS drivers."""
     self._helpers = {}
     for helper_str in self.configuration.share_lvm_helpers:
         share_proto, _, import_str = helper_str.partition('=')
         helper = importutils.import_class(import_str)
         # TODO(rushiagr): better way to handle configuration
         #                 instead of just passing to the helper
         self._helpers[share_proto.upper()] = helper(
             self._execute, self.configuration)
Пример #10
0
def monkey_patch():
    """Patch decorator.

    If the Flags.monkey_patch set as True,
    this function patches a decorator
    for all functions in specified modules.
    You can set decorators for each modules
    using CONF.monkey_patch_modules.
    The format is "Module path:Decorator function".
    Example: 'manila.api.ec2.cloud:' \
     manila.openstack.common.notifier.api.notify_decorator'

    Parameters of the decorator is as follows.
    (See manila.openstack.common.notifier.api.notify_decorator)

    name - name of the function
    function - object of the function
    """
    # If CONF.monkey_patch is not True, this function do nothing.
    if not CONF.monkey_patch:
        return
    # Get list of modules and decorators
    for module_and_decorator in CONF.monkey_patch_modules:
        module, decorator_name = module_and_decorator.split(':')
        # import decorator function
        decorator = importutils.import_class(decorator_name)
        __import__(module)
        # Retrieve module information using pyclbr
        module_data = pyclbr.readmodule_ex(module)
        for key in module_data.keys():
            # set the decorator for the class methods
            if isinstance(module_data[key], pyclbr.Class):
                clz = importutils.import_class("%s.%s" % (module, key))
                for method, func in inspect.getmembers(clz, inspect.ismethod):
                    setattr(
                        clz, method,
                        decorator("%s.%s.%s" % (module, key, method), func))
            # set the decorator for the function
            if isinstance(module_data[key], pyclbr.Function):
                func = importutils.import_class("%s.%s" % (module, key))
                setattr(sys.modules[module], key,
                        decorator("%s.%s" % (module, key), func))
Пример #11
0
def monkey_patch():
    """  If the Flags.monkey_patch set as True,
    this function patches a decorator
    for all functions in specified modules.
    You can set decorators for each modules
    using CONF.monkey_patch_modules.
    The format is "Module path:Decorator function".
    Example: 'manila.api.ec2.cloud:' \
     manila.openstack.common.notifier.api.notify_decorator'

    Parameters of the decorator is as follows.
    (See manila.openstack.common.notifier.api.notify_decorator)

    name - name of the function
    function - object of the function
    """
    # If CONF.monkey_patch is not True, this function do nothing.
    if not CONF.monkey_patch:
        return
    # Get list of modules and decorators
    for module_and_decorator in CONF.monkey_patch_modules:
        module, decorator_name = module_and_decorator.split(':')
        # import decorator function
        decorator = importutils.import_class(decorator_name)
        __import__(module)
        # Retrieve module information using pyclbr
        module_data = pyclbr.readmodule_ex(module)
        for key in module_data.keys():
            # set the decorator for the class methods
            if isinstance(module_data[key], pyclbr.Class):
                clz = importutils.import_class("%s.%s" % (module, key))
                for method, func in inspect.getmembers(clz, inspect.ismethod):
                    setattr(
                        clz, method,
                        decorator("%s.%s.%s" % (module, key, method), func))
            # set the decorator for the function
            if isinstance(module_data[key], pyclbr.Function):
                func = importutils.import_class("%s.%s" % (module, key))
                setattr(sys.modules[module], key,
                        decorator("%s.%s" % (module, key), func))
Пример #12
0
 def __init__(self, host, binary, topic, manager, report_interval=None,
              periodic_interval=None, periodic_fuzzy_delay=None,
              service_name=None, *args, **kwargs):
     self.host = host
     self.binary = binary
     self.topic = topic
     self.manager_class_name = manager
     manager_class = importutils.import_class(self.manager_class_name)
     self.manager = manager_class(host=self.host,
                                  service_name=service_name,
                                  *args, **kwargs)
     self.report_interval = report_interval
     self.periodic_interval = periodic_interval
     self.periodic_fuzzy_delay = periodic_fuzzy_delay
     super(Service, self).__init__(*args, **kwargs)
     self.saved_args, self.saved_kwargs = args, kwargs
     self.timers = []
Пример #13
0
    def load_extension(self, ext_factory):
        """Execute an extension factory.

        Loads an extension.  The 'ext_factory' is the name of a
        callable that will be imported and called with one
        argument--the extension manager.  The factory callable is
        expected to call the register() method at least once.
        """

        LOG.debug("Loading extension %s", ext_factory)

        # Load the factory
        factory = importutils.import_class(ext_factory)

        # Call it
        LOG.debug("Calling extension factory %s", ext_factory)
        factory(self)
Пример #14
0
    def load_extension(self, ext_factory):
        """Execute an extension factory.

        Loads an extension.  The 'ext_factory' is the name of a
        callable that will be imported and called with one
        argument--the extension manager.  The factory callable is
        expected to call the register() method at least once.
        """

        LOG.debug("Loading extension %s", ext_factory)

        # Load the factory
        factory = importutils.import_class(ext_factory)

        # Call it
        LOG.debug("Calling extension factory %s", ext_factory)
        factory(self)
Пример #15
0
    def _get_manager(self):
        """Initialize a Manager object appropriate for this service.

        Use the service name to look up a Manager subclass from the
        configuration and initialize an instance. If no class name
        is configured, just return None.

        :returns: a Manager instance, or None.

        """
        fl = '%s_manager' % self.name
        if fl not in CONF:
            return None

        manager_class_name = CONF.get(fl, None)
        if not manager_class_name:
            return None

        manager_class = importutils.import_class(manager_class_name)
        return manager_class()
Пример #16
0
    def _get_manager(self):
        """Initialize a Manager object appropriate for this service.

        Use the service name to look up a Manager subclass from the
        configuration and initialize an instance. If no class name
        is configured, just return None.

        :returns: a Manager instance, or None.

        """
        fl = '%s_manager' % self.name
        if fl not in CONF:
            return None

        manager_class_name = CONF.get(fl, None)
        if not manager_class_name:
            return None

        manager_class = importutils.import_class(manager_class_name)
        return manager_class()
Пример #17
0
 def __init__(self, db, _helpers, *args, **kwargs):
     """Do initialization."""
     super(ServiceInstanceManager, self).__init__()
     self.driver_config = None
     if "driver_config" in kwargs:
         self.driver_config = kwargs["driver_config"]
     if not self.get_config_option("service_instance_user"):
         raise exception.ServiceInstanceException(
             _('Service instance user '
               'is not specified'))
     self.admin_context = context.get_admin_context()
     self._execute = utils.execute
     self.compute_api = compute.API()
     self.neutron_api = neutron.API()
     self._helpers = _helpers
     self.db = db
     attempts = 5
     while attempts:
         try:
             self.service_tenant_id = self.neutron_api.admin_tenant_id
             break
         except exception.NetworkException:
             LOG.debug('Connection to neutron failed.')
             attempts -= 1
             time.sleep(3)
     else:
         raise exception.ServiceInstanceException(
             _('Can not receive '
               'service tenant id.'))
     self.share_networks_servers = {}
     self.service_network_id = self._get_service_network()
     self.vif_driver = importutils.import_class(
         self.get_config_option("interface_driver"))()
     self._setup_connectivity_with_service_instances()
     self.max_time_to_build_instance = self.get_config_option(
         "max_time_to_build_instance")
     self.path_to_private_key = self.get_config_option(
         "path_to_private_key")
     self.path_to_public_key = self.get_config_option("path_to_public_key")
Пример #18
0
 def __init__(self, db, _helpers, *args, **kwargs):
     """Do initialization."""
     super(ServiceInstanceManager, self).__init__()
     self.driver_config = None
     if "driver_config" in kwargs:
         self.driver_config = kwargs["driver_config"]
     if not self.get_config_option("service_instance_user"):
         raise exception.ServiceInstanceException(_('Service instance user '
                                                    'is not specified'))
     self.admin_context = context.get_admin_context()
     self._execute = utils.execute
     self.compute_api = compute.API()
     self.neutron_api = neutron.API()
     self._helpers = _helpers
     self.db = db
     attempts = 5
     while attempts:
         try:
             self.service_tenant_id = self.neutron_api.admin_tenant_id
             break
         except exception.NetworkException:
             LOG.debug('Connection to neutron failed.')
             attempts -= 1
             time.sleep(3)
     else:
         raise exception.ServiceInstanceException(_('Can not receive '
                                                    'service tenant id.'))
     self.share_networks_servers = {}
     self.service_network_id = self._get_service_network()
     self.vif_driver = importutils.import_class(
         self.get_config_option("interface_driver"))()
     self._setup_connectivity_with_service_instances()
     self.max_time_to_build_instance = self.get_config_option(
         "max_time_to_build_instance")
     self.path_to_private_key = self.get_config_option(
         "path_to_private_key")
     self.path_to_public_key = self.get_config_option("path_to_public_key")
     self.connect_share_server_to_tenant_network = self.get_config_option(
         'connect_share_server_to_tenant_network')
Пример #19
0
def API():
    importutils = manila.openstack.common.importutils
    volume_api_class = oslo.config.cfg.CONF.volume_api_class
    cls = importutils.import_class(volume_api_class)
    return cls()
Пример #20
0
def load_standard_extensions(ext_mgr, logger, path, package, ext_list=None):
    """Registers all standard API extensions."""

    # Walk through all the modules in our directory...
    our_dir = path[0]
    for dirpath, dirnames, filenames in os.walk(our_dir):
        # Compute the relative package name from the dirpath
        relpath = os.path.relpath(dirpath, our_dir)
        if relpath == '.':
            relpkg = ''
        else:
            relpkg = '.%s' % '.'.join(relpath.split(os.sep))

        # Now, consider each file in turn, only considering .py files
        for fname in filenames:
            root, ext = os.path.splitext(fname)

            # Skip __init__ and anything that's not .py
            if ext != '.py' or root == '__init__':
                continue

            # Try loading it
            classname = "%s%s" % (root[0].upper(), root[1:])
            classpath = ("%s%s.%s.%s" % (package, relpkg, root, classname))

            if ext_list is not None and classname not in ext_list:
                logger.debug("Skipping extension: %s" % classpath)
                continue

            try:
                ext_mgr.load_extension(classpath)
            except Exception as exc:
                logger.warn(
                    _('Failed to load extension %(classpath)s: '
                      '%(exc)s') % {
                          "classpath": classpath,
                          "exc": exc
                      })

        # Now, let's consider any subdirectories we may have...
        subdirs = []
        for dname in dirnames:
            # Skip it if it does not have __init__.py
            if not os.path.exists(os.path.join(dirpath, dname, '__init__.py')):
                continue

            # If it has extension(), delegate...
            ext_name = ("%s%s.%s.extension" % (package, relpkg, dname))
            try:
                ext = importutils.import_class(ext_name)
            except ImportError:
                # extension() doesn't exist on it, so we'll explore
                # the directory for ourselves
                subdirs.append(dname)
            else:
                try:
                    ext(ext_mgr)
                except Exception as exc:
                    logger.warn(
                        _('Failed to load extension %(ext_name)s: '
                          '%(exc)s') % {
                              "ext_name": ext_name,
                              "exc": exc
                          })

        # Update the list of directories we'll explore...
        dirnames[:] = subdirs
Пример #21
0
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

# Importing full names to not pollute the namespace and cause possible
# collisions with use of 'from manila.share import <foo>' elsewhere.
from manila.common import config
import manila.openstack.common.importutils as import_utils

CONF = config.CONF

API = import_utils.import_class(CONF.share_api_class)
Пример #22
0
def API():
    importutils = manila.openstack.common.importutils
    network_api_class = cfg.CONF.network_api_class
    cls = importutils.import_class(network_api_class)
    return cls()
Пример #23
0
def API():
    importutils = manila.openstack.common.importutils
    network_api_class = cfg.CONF.network_api_class
    cls = importutils.import_class(network_api_class)
    return cls()
Пример #24
0
def load_standard_extensions(ext_mgr, logger, path, package, ext_list=None):
    """Registers all standard API extensions."""

    # Walk through all the modules in our directory...
    our_dir = path[0]
    for dirpath, dirnames, filenames in os.walk(our_dir):
        # Compute the relative package name from the dirpath
        relpath = os.path.relpath(dirpath, our_dir)
        if relpath == '.':
            relpkg = ''
        else:
            relpkg = '.%s' % '.'.join(relpath.split(os.sep))

        # Now, consider each file in turn, only considering .py files
        for fname in filenames:
            root, ext = os.path.splitext(fname)

            # Skip __init__ and anything that's not .py
            if ext != '.py' or root == '__init__':
                continue

            # Try loading it
            classname = "%s%s" % (root[0].upper(), root[1:])
            classpath = ("%s%s.%s.%s" %
                         (package, relpkg, root, classname))

            if ext_list is not None and classname not in ext_list:
                logger.debug("Skipping extension: %s" % classpath)
                continue

            try:
                ext_mgr.load_extension(classpath)
            except Exception as exc:
                logger.warn(_('Failed to load extension %(classpath)s: '
                              '%(exc)s') % locals())

        # Now, let's consider any subdirectories we may have...
        subdirs = []
        for dname in dirnames:
            # Skip it if it does not have __init__.py
            if not os.path.exists(os.path.join(dirpath, dname,
                                               '__init__.py')):
                continue

            # If it has extension(), delegate...
            ext_name = ("%s%s.%s.extension" %
                        (package, relpkg, dname))
            try:
                ext = importutils.import_class(ext_name)
            except ImportError:
                # extension() doesn't exist on it, so we'll explore
                # the directory for ourselves
                subdirs.append(dname)
            else:
                try:
                    ext(ext_mgr)
                except Exception as exc:
                    logger.warn(_('Failed to load extension %(ext_name)s: '
                                  '%(exc)s') % locals())

        # Update the list of directories we'll explore...
        dirnames[:] = subdirs