diff --git a/tripleoclient/tests/v1/overcloud_config/test_overcloud_config.py b/tripleoclient/tests/v1/overcloud_config/test_overcloud_config.py index e56118447..96603d5ca 100644 --- a/tripleoclient/tests/v1/overcloud_config/test_overcloud_config.py +++ b/tripleoclient/tests/v1/overcloud_config/test_overcloud_config.py @@ -141,38 +141,31 @@ class TestOvercloudConfig(utils.TestCommand): mock_tmpdir.return_value = "/tmp/tht" fake_role = [role for role in fakes.FAKE_STACK['outputs'][0]['output_value']] - fake_playbook = {'FakeController': [{'hosts': 'FakeController', - 'name': 'FakeController playbook', - 'tasks': [{'name': 'Stop fake ' - 'service', - 'service': - 'name=fake ' - 'state=stopped', - 'tags': 'step1'}] - }], - 'FakeCompute': [{'hosts': 'FakeCompute', - 'name': 'FakeCompute playbook', - 'tasks': [{'name': 'Stop fake ' - 'service', - 'service': - 'name=fake state=stopped', - 'tags': 'step1'}, - {'name': 'Stop nova-' - 'compute service', - 'service': - 'name=openstack-nova-' - 'compute state=stopped', - 'tags': 'step1'}] - }] - } + fake_tasks = {'FakeController': [{'name': 'Stop fake service', + 'service': 'name=fake ' + 'state=stopped', + 'tags': 'step1', + 'when': 'step == 1'}], + 'FakeCompute': [{'name': 'Stop fake service', + 'service': 'name=fake state=stopped', + 'tags': 'step1', + 'when': 'step == 1'}, + {'name': 'Stop nova-' + 'compute service', + 'service': + 'name=openstack-nova-' + 'compute state=stopped', + 'tags': 'step1', + 'when': 'step == 1'}] + } mock_get_role_data.return_value = fake_role for role in fake_role: - playbook = self.cmd._convert_playbook(fakes. - FAKE_STACK['outputs'] - [0] - ['output_value'] - [role] - ['upgrade_tasks'], - role) - self.assertEqual(fake_playbook[role], playbook) + filepath = "/tmp/tht/%s/upgrade_tasks_playbook" % role + with mock.patch('os.fdopen'), \ + mock.patch('os.mkdir'), mock.patch('os.open') as open: + playbook_tasks = self.cmd._write_playbook_get_tasks( + fakes.FAKE_STACK['outputs'][0]['output_value'][role] + ['upgrade_tasks'], role, filepath) + self.assertEqual(fake_tasks[role], playbook_tasks) + open.assert_called() diff --git a/tripleoclient/v1/overcloud_config.py b/tripleoclient/v1/overcloud_config.py index dd36ec365..a498131b6 100644 --- a/tripleoclient/v1/overcloud_config.py +++ b/tripleoclient/v1/overcloud_config.py @@ -12,6 +12,7 @@ import logging import os +import re import six import tempfile import yaml @@ -53,13 +54,32 @@ class DownloadConfig(command.Command): ) return parser - def _convert_playbook(self, tasks, role): + def _step_tags_to_when(self, sorted_tasks): + for task in sorted_tasks: + tag = task.get('tags', '') + match = re.search('step([0-9]+)', tag) + if match: + step = match.group(1) + whenline = task.get('when', None) + if whenline: # how about list of when conditionals + when_exists = re.search('step == [0-9]', whenline) + if when_exists: # skip if there is an existing 'step == N' + continue + task['when'] = "(%s) and (step == %s)" % (whenline, step) + else: + task.update({"when": "step == %s" % step}) + + def _write_playbook_get_tasks(self, tasks, role, filepath): playbook = [] sorted_tasks = sorted(tasks, key=lambda x: x.get('tags', None)) + self._step_tags_to_when(sorted_tasks) playbook.append({'name': '%s playbook' % role, 'hosts': role, 'tasks': sorted_tasks}) - return playbook + with os.fdopen(os.open(filepath, os.O_WRONLY | os.O_CREAT, 0o600), + 'w') as conf_file: + yaml.safe_dump(playbook, conf_file, default_flow_style=False) + return sorted_tasks def _mkdir(self, dirname): if not os.path.exists(dirname): @@ -98,8 +118,10 @@ class DownloadConfig(command.Command): if step is not None)) else: if 'upgrade_tasks' in config: - data = self._convert_playbook(role[config], - role_name) + filepath = os.path.join(role_path, '%s_playbook.yaml' % + config) + data = self._write_playbook_get_tasks( + role[config], role_name, filepath) else: try: data = role[config]