Introduce Flow DeployDriver

Partially implements blueprint: pluggable-do-actions

Change-Id: I1bb6a5e904741a8689d08e1939a3e098864a5d03
This commit is contained in:
Alexander Gordeev 2016-04-05 17:06:35 +03:00
parent ff2a4fdd26
commit f24eaf5712
6 changed files with 135 additions and 0 deletions

View File

41
bareon/actions/base.py Normal file
View File

@ -0,0 +1,41 @@
# Copyright 2016 Mirantis, Inc.
#
# 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 BaseAction(object):
"""BaseAction
Validates data and executes the action
"""
def __init__(self, driver):
self.driver = driver
@abc.abstractmethod
def validate(self):
"""Validate
Validates that data/objects of data driver satisfied all necessary
requirements
"""
@abc.abstractmethod
def execute(self):
"""Executes the action"""

View File

@ -28,6 +28,9 @@ class BaseDataDriver(object):
def __init__(self, data):
self.data = copy.deepcopy(data)
self.flow = []
if self.data and self.data.get('flow'):
self.flow = self.data.get('flow', [])
@six.add_metaclass(abc.ABCMeta)

View File

@ -0,0 +1,37 @@
#
# Copyright 2016 Mirantis, Inc.
#
# 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 bareon.drivers.deploy import base
from bareon.drivers.deploy import mixins
from bareon.openstack.common import log as logging
import stevedore.named
LOG = logging.getLogger(__name__)
class Flow(base.SimpleDeployDriver, mixins.MountableMixin):
def __init__(self, data_driver):
super(Flow, self).__init__(data_driver)
self.ext_mgr = stevedore.named.NamedExtensionManager(
'bareon.do_actions', names=self.driver.flow,
invoke_on_load=True, invoke_args=(self.driver, ),
name_order=True)
def execute_flow(self):
for action in self.ext_mgr:
action.validate()
action.execute()

View File

@ -0,0 +1,53 @@
# Copyright 2016 Mirantis, Inc.
#
# 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 six
import unittest2
from bareon.actions import base as base_action
from bareon.drivers.deploy import flow
import stevedore
if six.PY2:
import mock
elif six.PY3:
import unittest.mock as mock
class TestFlowDriver(unittest2.TestCase):
@mock.patch.object(flow.Flow, '__init__',
return_value=None)
def test_execute_flow(self, mock_init):
fake_ext = mock.Mock(spec=base_action.BaseAction)
fake_ext.name = 'foo'
self.drv = flow.Flow('fake_data_driver')
self.drv.ext_mgr = stevedore.NamedExtensionManager.make_test_instance(
[fake_ext], namespace='TESTING')
self.drv.execute_flow()
self.assertEqual(['foo'], self.drv.ext_mgr.names())
fake_ext.validate.assert_called_once_with()
fake_ext.execute.assert_called_once_with()
@mock.patch('stevedore.named.NamedExtensionManager')
def test_init(self, mock_stevedore):
fake_data_driver = mock.Mock()
expected_flow = ['action1', 'action3']
fake_data_driver.flow = expected_flow
self.drv = flow.Flow(fake_data_driver)
mock_stevedore.assert_called_once_with(
'bareon.do_actions', names=expected_flow,
invoke_on_load=True, invoke_args=(fake_data_driver,),
name_order=True)

View File

@ -37,6 +37,7 @@ bareon.drivers.deploy =
nailgun = bareon.drivers.deploy.nailgun:Manager
swift = bareon.drivers.deploy.swift:Swift
rsync = bareon.drivers.deploy.rsync:Rsync
flow = bareon.drivers.deploy.flow:Flow
oslo.config.opts =
bareon.manager = bareon.manager:list_opts