Merge "Remove step_tags_to_when from config download and fix sorting"

This commit is contained in:
Zuul 2018-02-08 01:27:40 +00:00 committed by Gerrit Code Review
commit 4583d13638
3 changed files with 30 additions and 38 deletions

View File

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

View File

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

View File

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