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:
@@ -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
|
||||
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user