| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from oslo_log import log as logging |
| 14 |
from stevedore import extension |
| 15 |
|
| 16 |
import os_vif.exception |
| 17 |
import os_vif.i18n |
| 18 |
import os_vif.objects |
| 19 |
|
| 20 |
_EXT_MANAGER = None |
| 21 |
LOG = logging.getLogger(__name__) |
| 22 |
|
| 23 |
|
| 24 |
def initialize(reset=False): |
| 25 |
""" |
| 26 |
Loads all os_vif plugins and initializes them with a dictionary of |
| 27 |
configuration options. These configuration options are passed as-is |
| 28 |
to the individual VIF plugins that are loaded via stevedore. |
| 29 |
|
| 30 |
:param reset: Recreate and load the VIF plugin extensions. |
| 31 |
|
| 32 |
""" |
| 33 |
global _EXT_MANAGER |
| 34 |
if _EXT_MANAGER is None: |
| 35 |
os_vif.objects.register_all() |
| 36 |
|
| 37 |
if reset or (_EXT_MANAGER is None): |
| 38 |
_EXT_MANAGER = extension.ExtensionManager(namespace='os_vif', |
| 39 |
invoke_on_load=False) |
| 40 |
loaded_plugins = [] |
| 41 |
for plugin_name in _EXT_MANAGER.names(): |
| 42 |
cls = _EXT_MANAGER[plugin_name].plugin |
| 43 |
obj = cls.load(plugin_name) |
| 44 |
LOG.debug(("Loaded VIF plugin class '%(cls)s' " |
| 45 |
"with name '%(plugin_name)s'"), |
| 46 |
{'cls': cls, 'plugin_name': plugin_name}) |
| 47 |
loaded_plugins.append(plugin_name) |
| 48 |
_EXT_MANAGER[plugin_name].obj = obj |
| 49 |
LOG.info("Loaded VIF plugins: %s", ", ".join(loaded_plugins)) |
| 50 |
|
| 51 |
|
| 52 |
def plug(vif, instance_info): |
| 53 |
""" |
| 54 |
Given a model of a VIF, perform operations to plug the VIF properly. |
| 55 |
|
| 56 |
:param vif: Instance of a subclass of ``os_vif.objects.vif.VIFBase``. |
| 57 |
:param instance_info: ``os_vif.objects.instance_info.InstanceInfo`` object. |
| 58 |
:raises ``exception.LibraryNotInitialized`` if the user of the library |
| 59 |
did not call ``os_vif.initialize(**config)`` before trying to |
| 60 |
plug a VIF. |
| 61 |
:raises ``exception.NoMatchingPlugin`` if there is no plugin for the |
| 62 |
type of VIF supplied. |
| 63 |
:raises ``exception.PlugException`` if anything fails during unplug |
| 64 |
operations. |
| 65 |
""" |
| 66 |
if _EXT_MANAGER is None: |
| 67 |
raise os_vif.exception.LibraryNotInitialized() |
| 68 |
|
| 69 |
plugin_name = vif.plugin |
| 70 |
try: |
| 71 |
plugin = _EXT_MANAGER[plugin_name].obj |
| 72 |
except KeyError: |
| 73 |
raise os_vif.exception.NoMatchingPlugin(plugin_name=plugin_name) |
| 74 |
|
| 75 |
try: |
| 76 |
LOG.debug("Plugging vif %s", vif) |
| 77 |
plugin.plug(vif, instance_info) |
| 78 |
LOG.info("Successfully plugged vif %s", vif) |
| 79 |
except Exception as err: |
| 80 |
LOG.error("Failed to plug vif %(vif)s", |
| 81 |
{"vif": vif}, exc_info=True) |
| 82 |
raise os_vif.exception.PlugException(vif=vif, err=err) |
| 83 |
|
| 84 |
|
| 85 |
def unplug(vif, instance_info): |
| 86 |
""" |
| 87 |
Given a model of a VIF, perform operations to unplug the VIF properly. |
| 88 |
|
| 89 |
:param vif: Instance of a subclass of `os_vif.objects.vif.VIFBase`. |
| 90 |
:param instance_info: `os_vif.objects.instance_info.InstanceInfo` object. |
| 91 |
:raises `exception.LibraryNotInitialized` if the user of the library |
| 92 |
did not call os_vif.initialize(**config) before trying to |
| 93 |
plug a VIF. |
| 94 |
:raises `exception.NoMatchingPlugin` if there is no plugin for the |
| 95 |
type of VIF supplied. |
| 96 |
:raises `exception.UnplugException` if anything fails during unplug |
| 97 |
operations. |
| 98 |
""" |
| 99 |
if _EXT_MANAGER is None: |
| 100 |
raise os_vif.exception.LibraryNotInitialized() |
| 101 |
|
| 102 |
plugin_name = vif.plugin |
| 103 |
try: |
| 104 |
plugin = _EXT_MANAGER[plugin_name].obj |
| 105 |
except KeyError: |
| 106 |
raise os_vif.exception.NoMatchingPlugin(plugin_name=plugin_name) |
| 107 |
|
| 108 |
try: |
| 109 |
LOG.debug("Unplugging vif %s", vif) |
| 110 |
plugin.unplug(vif, instance_info) |
| 111 |
LOG.info("Successfully unplugged vif %s", vif) |
| 112 |
except Exception as err: |
| 113 |
LOG.error("Failed to unplug vif %(vif)s", |
| 114 |
{"vif": vif}, exc_info=True) |
| 115 |
raise os_vif.exception.UnplugException(vif=vif, err=err) |
| 116 |
|
| 117 |
|
| 118 |
def host_info(permitted_vif_type_names=None): |
| 119 |
""" |
| 120 |
:param permitted_vif_type_names: list of VIF object names |
| 121 |
|
| 122 |
Get information about the host platform configuration to be |
| 123 |
provided to the network manager. This will include information |
| 124 |
about what plugins are installed in the host |
| 125 |
|
| 126 |
If permitted_vif_type_names is not None, the returned HostInfo |
| 127 |
will be filtered such that it only includes plugins which |
| 128 |
support one of the listed VIF types. This allows the caller |
| 129 |
to filter out impls which are not compatible with the current |
| 130 |
usage configuration. For example, to remove VIFVHostUser if |
| 131 |
the guest does not support shared memory. |
| 132 |
|
| 133 |
:returns: a os_vif.host_info.HostInfo class instance |
| 134 |
""" |
| 135 |
|
| 136 |
if _EXT_MANAGER is None: |
| 137 |
raise os_vif.exception.LibraryNotInitialized() |
| 138 |
|
| 139 |
plugins = [ |
| 140 |
_EXT_MANAGER[name].obj.describe() |
| 141 |
for name in sorted(_EXT_MANAGER.names()) |
| 142 |
] |
| 143 |
|
| 144 |
info = os_vif.objects.host_info.HostInfo(plugin_info=plugins) |
| 145 |
if permitted_vif_type_names is not None: |
| 146 |
info.filter_vif_types(permitted_vif_type_names) |
| 147 |
return info |