Adds when in upgrade_tasks playbook written by config download

As part of the bug below, this can be used to make sure the
playbooks generated from the heat output can be iterated over
using the loop variable.

This adds a pre-processor for upgrade_tasks that adds a "when
step == N" condition based on the value of the tags. That is
"tags: step1" becomes "when: step == 1". When there is an
existing when statement the new step condition is appended.

Change-Id: Ief593dc758a2ffe33c1cbcbda9289393fcf023e4
Related-Bug: 1708115
This commit is contained in:
marios 2017-08-08 13:34:48 +03:00
parent 66c902a2b6
commit c2ea613919
2 changed files with 51 additions and 36 deletions

View File

@ -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()

View File

@ -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]