Merge "Rename Command to Action"

This commit is contained in:
Jenkins
2015-12-10 16:33:04 +00:00
committed by Gerrit Code Review
7 changed files with 43 additions and 25 deletions

View File

@@ -17,7 +17,7 @@
# limitations under the License. # limitations under the License.
# #
from watcher.applier.base import Applier from watcher.applier.base import Applier
from watcher.applier.execution.executor import CommandExecutor from watcher.applier.execution.executor import ActionPlanExecutor
from watcher.objects import Action from watcher.objects import Action
from watcher.objects import ActionPlan from watcher.objects import ActionPlan
@@ -27,7 +27,7 @@ class DefaultApplier(Applier):
super(DefaultApplier, self).__init__() super(DefaultApplier, self).__init__()
self.manager_applier = manager_applier self.manager_applier = manager_applier
self.context = context self.context = context
self.executor = CommandExecutor(manager_applier, context) self.executor = ActionPlanExecutor(manager_applier, context)
def execute(self, action_plan_uuid): def execute(self, action_plan_uuid):
action_plan = ActionPlan.get_by_uuid(self.context, action_plan_uuid) action_plan = ActionPlan.get_by_uuid(self.context, action_plan_uuid)

View File

@@ -24,19 +24,31 @@ LOG = log.getLogger(__name__)
class DeployPhase(object): class DeployPhase(object):
def __init__(self, executor): def __init__(self, executor):
# todo(jed) oslo_conf 10 secondes # todo(jed) oslo_conf 10 secondes
self.max_timeout = 100000 self._max_timeout = 100000
self.commands = [] self._actions = []
self.executor = executor self._executor = executor
@property
def actions(self):
return self._actions
@property
def max_timeout(self):
return self._max_timeout
@max_timeout.setter
def max_timeout(self, m):
self._max_timeout = m
def populate(self, action): def populate(self, action):
self.commands.append(action) self._actions.append(action)
def execute_primitive(self, primitive): def execute_primitive(self, primitive):
future = primitive.execute(primitive) future = primitive.execute(primitive)
return future.result(self.max_timeout) return future.result(self.max_timeout)
def rollback(self): def rollback(self):
reverted = sorted(self.commands, reverse=True) reverted = sorted(self.actions, reverse=True)
for primitive in reverted: for primitive in reverted:
try: try:
self.execute_primitive(primitive) self.execute_primitive(primitive)

View File

@@ -18,7 +18,7 @@
# #
from oslo_log import log from oslo_log import log
from watcher.applier.mapper.default import DefaultCommandMapper from watcher.applier.mapper.default import DefaultActionMapper
from watcher.applier.execution.deploy_phase import DeployPhase from watcher.applier.execution.deploy_phase import DeployPhase
from watcher.applier.messaging.events import Events from watcher.applier.messaging.events import Events
@@ -29,15 +29,15 @@ from watcher.objects.action_plan import Status
LOG = log.getLogger(__name__) LOG = log.getLogger(__name__)
class CommandExecutor(object): class ActionPlanExecutor(object):
def __init__(self, manager_applier, context): def __init__(self, manager_applier, context):
self.manager_applier = manager_applier self.manager_applier = manager_applier
self.context = context self.context = context
self.deploy = DeployPhase(self) self.deploy = DeployPhase(self)
self.mapper = DefaultCommandMapper() self.mapper = DefaultActionMapper()
def get_primitive(self, action): def get_primitive(self, action):
return self.mapper.build_primitive_command(action) return self.mapper.build_primitive_from_action(action)
def notify(self, action, state): def notify(self, action, state):
db_action = Action.get_by_uuid(self.context, action.uuid) db_action = Action.get_by_uuid(self.context, action.uuid)

View File

@@ -22,8 +22,13 @@ import six
@six.add_metaclass(abc.ABCMeta) @six.add_metaclass(abc.ABCMeta)
class CommandMapper(object): class BaseActionMapper(object):
@abc.abstractmethod @abc.abstractmethod
def build_primitive_command(self, action): def build_primitive_from_action(self, action):
"""Transform an action to a primitive
:type action: watcher.decision_engine.action.BaseAction
:return: the associated Primitive
"""
raise NotImplementedError( raise NotImplementedError(
"Should have implemented this") # pragma:no cover "Should have implemented this") # pragma:no cover

View File

@@ -18,7 +18,7 @@
# #
from watcher.applier.mapper.base import CommandMapper from watcher.applier.mapper.base import BaseActionMapper
from watcher.applier.primitive.hypervisor_state import HypervisorStateCommand from watcher.applier.primitive.hypervisor_state import HypervisorStateCommand
from watcher.applier.primitive.migration import MigrateCommand from watcher.applier.primitive.migration import MigrateCommand
from watcher.applier.primitive.nop import NopCommand from watcher.applier.primitive.nop import NopCommand
@@ -27,8 +27,8 @@ from watcher.common.exception import ActionNotFound
from watcher.decision_engine.planner.default import Primitives from watcher.decision_engine.planner.default import Primitives
class DefaultCommandMapper(CommandMapper): class DefaultActionMapper(BaseActionMapper):
def build_primitive_command(self, action): def build_primitive_from_action(self, action):
if action.action_type == Primitives.COLD_MIGRATE.value: if action.action_type == Primitives.COLD_MIGRATE.value:
return MigrateCommand(action.applies_to, Primitives.COLD_MIGRATE, return MigrateCommand(action.applies_to, Primitives.COLD_MIGRATE,
action.src, action.src,

View File

@@ -18,7 +18,7 @@
# #
import mock import mock
from watcher.applier.execution.executor import CommandExecutor from watcher.applier.execution.executor import ActionPlanExecutor
from watcher import objects from watcher import objects
from watcher.common import utils from watcher.common import utils
@@ -32,7 +32,7 @@ class TestCommandExecutor(DbTestCase):
def setUp(self): def setUp(self):
super(TestCommandExecutor, self).setUp() super(TestCommandExecutor, self).setUp()
self.applier = mock.MagicMock() self.applier = mock.MagicMock()
self.executor = CommandExecutor(self.applier, self.context) self.executor = ActionPlanExecutor(self.applier, self.context)
def test_execute(self): def test_execute(self):
actions = mock.MagicMock() actions = mock.MagicMock()

View File

@@ -17,7 +17,7 @@
# limitations under the License. # limitations under the License.
# #
import mock import mock
from watcher.applier.mapper.default import DefaultCommandMapper from watcher.applier.mapper.default import DefaultActionMapper
from watcher.decision_engine.planner.default import Primitives from watcher.decision_engine.planner.default import Primitives
from watcher.tests import base from watcher.tests import base
@@ -25,33 +25,34 @@ from watcher.tests import base
class TestCommandMapper(base.TestCase): class TestCommandMapper(base.TestCase):
def setUp(self): def setUp(self):
super(TestCommandMapper, self).setUp() super(TestCommandMapper, self).setUp()
self.mapper = DefaultCommandMapper() self.mapper = DefaultActionMapper()
def test_build_command_cold(self): def test_build_command_cold(self):
action = mock.MagicMock() action = mock.MagicMock()
action.action_type = Primitives.COLD_MIGRATE.value action.action_type = Primitives.COLD_MIGRATE.value
cmd = self.mapper.build_primitive_command(action) cmd = self.mapper.build_primitive_from_action(action)
self.assertIsNotNone(cmd) self.assertIsNotNone(cmd)
def test_build_command_live(self): def test_build_command_live(self):
action = mock.MagicMock() action = mock.MagicMock()
action.action_type = Primitives.LIVE_MIGRATE.value action.action_type = Primitives.LIVE_MIGRATE.value
cmd = self.mapper.build_primitive_command(action) cmd = self.mapper.build_primitive_from_action(action)
self.assertIsNotNone(cmd) self.assertIsNotNone(cmd)
def test_build_command_h_s(self): def test_build_command_h_s(self):
action = mock.MagicMock() action = mock.MagicMock()
action.action_type = Primitives.HYPERVISOR_STATE.value action.action_type = Primitives.HYPERVISOR_STATE.value
cmd = self.mapper.build_primitive_command(action) cmd = self.mapper.build_primitive_from_action(action)
self.assertIsNotNone(cmd) self.assertIsNotNone(cmd)
def test_build_command_p_s(self): def test_build_command_p_s(self):
action = mock.MagicMock() action = mock.MagicMock()
action.action_type = Primitives.POWER_STATE.value action.action_type = Primitives.POWER_STATE.value
cmd = self.mapper.build_primitive_command(action) cmd = self.mapper.build_primitive_from_action(action)
self.assertIsNotNone(cmd) self.assertIsNotNone(cmd)
def test_build_command_exception_attribute(self): def test_build_command_exception_attribute(self):
action = mock.MagicMock action = mock.MagicMock
self.assertRaises(AttributeError, self.mapper.build_primitive_command, self.assertRaises(AttributeError,
self.mapper.build_primitive_from_action,
action) action)