1
# Copyright 2012 OpenStack LLC.
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
20
from nova.openstack.common import cfg
21
from nova.openstack.common import log as logging
22
from nova.openstack.common.notifier import api as notifier_api
26
LOG = logging.getLogger(__name__)
29
class PluginManager(object):
30
"""Manages plugin entrypoints and loading.
32
For a service to implement this plugin interface for callback purposes:
34
- Make use of the openstack-common notifier system
35
- Instantiate this manager in each process (passing in
36
project and service name)
38
For an API service to extend itself using this plugin interface,
39
it needs to query the plugin_extension_factory provided by
40
the already-instantiated PluginManager.
43
def __init__(self, project_name, service_name):
44
""" Construct Plugin Manager; load and initialize plugins.
46
project_name (e.g. 'nova' or 'glance') is used
47
to construct the entry point that identifies plugins.
49
The service_name (e.g. 'compute') is passed on to
50
each plugin as a raw string for it to do what it will.
52
self._project_name = project_name
53
self._service_name = service_name
56
def load_plugins(self):
59
for entrypoint in pkg_resources.iter_entry_points('%s.plugin' %
62
pluginclass = entrypoint.load()
63
plugin = pluginclass(self._service_name)
64
self.plugins.append(plugin)
65
except Exception, exc:
66
LOG.error(_("Failed to load plugin %(plug)s: %(exc)s") %
67
{'plug': entrypoint, 'exc': exc})
69
# Register individual notifiers.
70
for plugin in self.plugins:
71
for notifier in plugin.notifiers:
72
notifier_api.add_driver(notifier)
74
def plugin_extension_factory(self, ext_mgr):
75
for plugin in self.plugins:
76
descriptors = plugin.api_extension_descriptors
77
for descriptor in descriptors:
78
ext_mgr.load_extension(descriptor)