Add a common generic dynamic loader for watcher

In watcher, an audit generates a set of actions which
aims at achieving a given goal (lower energy consumption, ...).
It is possible to configure different strategies in order to achieve
each goal. Each strategy is written as a Python class which produces
a set of actions. Today, the set of possible actions is fixed for a
given version of Watcher and enables optimization algorithms to
include actions such as instance migration, changing hypervisor state,
changing power state (ACPI level, ...).

This patchset add a common generic dynamic loader for plugins,
such as for custom Actions, Strategies, Planners, etc.

Partially implements: blueprint watcher-add-actions-via-conf

Change-Id: I59d031b93865fff2540e3973921e1bdafa95f88e
This commit is contained in:
Jean-Emile DARTOIS
2016-01-07 14:47:44 +01:00
parent 34ccb7c23e
commit c0306ea8f4
15 changed files with 244 additions and 65 deletions

View File

@@ -0,0 +1,29 @@
# -*- encoding: utf-8 -*-
# Copyright (c) 2015 b<>com
#
# 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.
#
from __future__ import unicode_literals
from oslo_log import log
from watcher.common.loader.default import DefaultLoader
LOG = log.getLogger(__name__)
class DefaultActionLoader(DefaultLoader):
def __init__(self):
super(DefaultActionLoader, self).__init__(namespace='watcher_actions')

View File

@@ -297,3 +297,7 @@ class VMNotFound(WatcherException):
class HypervisorNotFound(WatcherException):
message = _("The hypervisor could not be found")
class LoadingError(WatcherException):
message = _("Error loading plugin '%(name)s'")

View File

View File

@@ -0,0 +1,32 @@
# -*- encoding: utf-8 -*-
# Copyright (c) 2015 b<>com
#
# 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.
from __future__ import unicode_literals
import abc
import six
@six.add_metaclass(abc.ABCMeta)
class BaseLoader(object):
@abc.abstractmethod
def list_available(self):
raise NotImplementedError()
@abc.abstractmethod
def load(self, name):
raise NotImplementedError()

View File

@@ -0,0 +1,48 @@
# -*- encoding: utf-8 -*-
# Copyright (c) 2015 b<>com
#
# 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.
from __future__ import unicode_literals
from oslo_log import log
from stevedore.driver import DriverManager
from stevedore import ExtensionManager
from watcher.common import exception
from watcher.common.loader.base import BaseLoader
LOG = log.getLogger(__name__)
class DefaultLoader(BaseLoader):
def __init__(self, namespace):
super(DefaultLoader, self).__init__()
self.namespace = namespace
def load(self, name):
try:
LOG.debug("Loading in namespace %s => %s ", self.namespace, name)
driver_manager = DriverManager(namespace=self.namespace,
name=name)
loaded = driver_manager.driver
except Exception as exc:
LOG.exception(exc)
raise exception.LoadingError(name=name)
return loaded()
def list_available(self):
extension_manager = ExtensionManager(namespace=self.namespace)
return {ext.name: ext.plugin for ext in extension_manager.extensions}

View File

@@ -0,0 +1,30 @@
# -*- encoding: utf-8 -*-
# Copyright (c) 2015 b<>com
#
# 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.
#
from __future__ import unicode_literals
from oslo_log import log
from watcher.common.loader.default import DefaultLoader
LOG = log.getLogger(__name__)
class DefaultPlannerLoader(DefaultLoader):
def __init__(self):
super(DefaultPlannerLoader, self).__init__(
namespace='watcher_planners')

View File

@@ -1,49 +0,0 @@
# -*- encoding: utf-8 -*-
# Copyright (c) 2015 b<>com
#
# Authors: Jean-Emile DARTOIS <jean-emile.dartois@b-com.com>
#
# 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.
#
import abc
from oslo_log import log
import six
from watcher.decision_engine.strategy.strategies.dummy_strategy import \
DummyStrategy
LOG = log.getLogger(__name__)
@six.add_metaclass(abc.ABCMeta)
class BaseStrategyLoader(object):
default_strategy_cls = DummyStrategy
@abc.abstractmethod
def load_available_strategies(self):
raise NotImplementedError()
def load(self, strategy_to_load=None):
strategy_selected = None
try:
available_strategies = self.load_available_strategies()
LOG.debug("Available strategies: %s ", available_strategies)
strategy_cls = available_strategies.get(
strategy_to_load, self.default_strategy_cls
)
strategy_selected = strategy_cls()
except Exception as exc:
LOG.exception(exc)
return strategy_selected

View File

@@ -20,19 +20,13 @@
from __future__ import unicode_literals
from oslo_log import log
from stevedore import ExtensionManager
from watcher.decision_engine.strategy.loading.base import BaseStrategyLoader
from watcher.common.loader.default import DefaultLoader
LOG = log.getLogger(__name__)
class DefaultStrategyLoader(BaseStrategyLoader):
def load_available_strategies(self):
extension_manager = ExtensionManager(
namespace='watcher_strategies',
invoke_on_load=False,
)
return {ext.name: ext.plugin for ext in extension_manager.extensions}
class DefaultStrategyLoader(DefaultLoader):
def __init__(self):
super(DefaultStrategyLoader, self).__init__(
namespace='watcher_strategies')

View File

@@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: python-watcher 0.21.1.dev32\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2016-01-05 14:22+0100\n"
"POT-Creation-Date: 2016-01-12 18:12+0100\n"
"PO-Revision-Date: 2015-12-11 15:42+0100\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: fr\n"
@@ -258,6 +258,11 @@ msgstr ""
msgid "The hypervisor could not be found"
msgstr ""
#: watcher/common/exception.py:303
#, fuzzy, python-format
msgid "Error loading plugin '%(name)s'"
msgstr "Erreur lors du chargement du module '%(name)s'"
#: watcher/common/keystone.py:59
msgid "No Keystone service catalog loaded"
msgstr ""
@@ -291,7 +296,7 @@ msgstr ""
msgid "'obj' argument type is not valid"
msgstr ""
#: watcher/decision_engine/strategy/selection/default.py:58
#: watcher/decision_engine/strategy/selection/default.py:59
#, python-format
msgid "Incorrect mapping: could not find associated strategy for '%s'"
msgstr ""

View File

@@ -7,9 +7,9 @@
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: python-watcher 0.21.1.dev79\n"
"Project-Id-Version: python-watcher 0.22.1.dev9\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2016-01-05 14:22+0100\n"
"POT-Creation-Date: 2016-01-12 18:12+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -256,6 +256,11 @@ msgstr ""
msgid "The hypervisor could not be found"
msgstr ""
#: watcher/common/exception.py:303
#, python-format
msgid "Error loading plugin '%(name)s'"
msgstr ""
#: watcher/common/keystone.py:59
msgid "No Keystone service catalog loaded"
msgstr ""
@@ -289,7 +294,7 @@ msgstr ""
msgid "'obj' argument type is not valid"
msgstr ""
#: watcher/decision_engine/strategy/selection/default.py:58
#: watcher/decision_engine/strategy/selection/default.py:59
#, python-format
msgid "Incorrect mapping: could not find associated strategy for '%s'"
msgstr ""

View File

@@ -0,0 +1,28 @@
# -*- encoding: utf-8 -*-
# Copyright (c) 2015 b<>com
#
# 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.
import abc
import six
@six.add_metaclass(abc.ABCMeta)
class FakeLoadable(object):
@classmethod
def namespace(cls):
return "TESTING"
@classmethod
def get_name(cls):
return 'fake'

View File

View File

@@ -0,0 +1,53 @@
# -*- encoding: utf-8 -*-
# Copyright (c) 2015 b<>com
#
# 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.
from __future__ import unicode_literals
import mock
from oslotest.base import BaseTestCase
from stevedore.driver import DriverManager
from stevedore.extension import Extension
from watcher.common import exception
from watcher.common.loader.default import DefaultLoader
from watcher.tests.common.loader.FakeLoadable import FakeLoadable
class TestLoader(BaseTestCase):
@mock.patch("watcher.common.loader.default.DriverManager")
def test_load_driver_no_opt(self, m_driver_manager):
m_driver_manager.return_value = DriverManager.make_test_instance(
extension=Extension(name=FakeLoadable.get_name(),
entry_point="%s:%s" % (
FakeLoadable.__module__,
FakeLoadable.__name__),
plugin=FakeLoadable,
obj=None),
namespace=FakeLoadable.namespace())
loader_manager = DefaultLoader(namespace='TESTING')
loaded_driver = loader_manager.load(name='fake')
self.assertEqual(loaded_driver.get_name(), FakeLoadable.get_name())
@mock.patch("watcher.common.loader.default.DriverManager")
def test_load_driver_bad_plugin(self, m_driver_manager):
m_driver_manager.side_effect = Exception()
loader_manager = DefaultLoader(namespace='TESTING')
self.assertRaises(exception.LoadingError, loader_manager.load,
name='bad_driver')