Remove step_tags_to_when from config download and fix sorting

We are now going to have 'when' in the upgrade_tasks directly
so no need to overwrite anymore (see depends-on).

This also changes the sorting of tasks and the order in which
they are written to the ansible playbook. We were previously
sorting on tags and this no longer works. Instead sort on the
step number given in the 'when:' value.

Closes-Bug: 1743758
Depends-On: I6adc5619a28099f4e241351b63377f1e96933810
Change-Id: I68c032c5f23393a77109f182541cc6bfcc019a41
This commit is contained in:
marios 2017-12-20 18:47:10 +02:00 committed by Marios Andreou
parent 261e39d5fd
commit 0f221e7de5
3 changed files with 30 additions and 38 deletions

View File

@ -50,13 +50,15 @@ FAKE_STACK = {
'upgrade_batch_tasks': [],
'upgrade_tasks': [{'name': 'Stop fake service',
'service': 'name=fake state=stopped',
'tags': 'step1',
'when': 'existingcondition'},
'when': ['nova_api_enabled.rc == 0',
'httpd_enabled.rc != 0',
'step|int == 1']},
{'name': 'Stop nova-compute service',
'service': 'name=openstack-nova-compute '
'state=stopped',
'tags': 'step1',
'when': ['existing', 'list']}]
'when': ['nova_compute_enabled.rc == 0',
'step|int == 2', 'existing',
'list']}]
},
'FakeController': {
'config_settings': {'tripleo::haproxy::user': 'admin'},
@ -75,7 +77,7 @@ FAKE_STACK = {
'upgrade_batch_tasks': [],
'upgrade_tasks': [{'name': 'Stop fake service',
'service': 'name=fake state=stopped',
'tags': 'step1'}]}}}]}
'when': 'step|int == 1'}]}}}]}
def create_to_dict_mock(**kwargs):

View File

@ -124,22 +124,22 @@ class TestConfig(base.TestCase):
expected_tasks = {'FakeController': [{'name': 'Stop fake service',
'service': 'name=fake '
'state=stopped',
'tags': 'step1',
'when': 'step|int == 1'}],
'FakeCompute': [{'name': 'Stop fake service',
'service':
'name=fake state=stopped',
'tags': 'step1',
'when': ['step|int == 1',
'existingcondition']},
'when': ['nova_api_enabled.rc == 0',
'httpd_enabled.rc != 0',
'step|int == 1']},
{'name': 'Stop nova-'
'compute service',
'service':
'name=openstack-nova-'
'compute state=stopped',
'tags': 'step1',
'when': ['step|int == 1',
'existing', 'list']}]}
'when': [
'nova_compute_enabled.rc == 0',
'step|int == 2', 'existing',
'list']}]}
for role in fake_role:
filedir = os.path.join(self.tmp_dir, role)
os.makedirs(filedir)

View File

@ -101,34 +101,24 @@ class Config(object):
os.O_WRONLY | os.O_CREAT, 0o600),
'w')
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)
whenexpr = task.get('when', None)
if whenexpr is None:
task.update({"when": "step|int == %s" % step})
else:
# Handle when: foo and a list of when conditionals
if not isinstance(whenexpr, list):
whenexpr = [whenexpr]
for w in whenexpr:
when_exists = re.search('step|int == [0-9]', "%s" % w)
if when_exists:
break
if when_exists:
# Skip to the next task,
# there is an existing 'step|int == N'
continue
whenexpr.insert(0, "step|int == %s" % step)
task['when'] = whenexpr
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)
def get_key(task):
whenexpr = task.get('when', None)
if whenexpr is None:
return ''
if not isinstance(whenexpr, list):
whenexpr = [whenexpr]
for w in whenexpr:
# make \|int optional incase forgotten; use only step digit:
match = re.search('step(\|int)? == ([0-9]+)', "%s" % w)
if match:
matches = len(match.groups())
return match.group(matches)
return ''
sorted_tasks = sorted(tasks, key=get_key)
playbook.append({'name': '%s playbook' % role,
'hosts': role,
'tasks': sorted_tasks})