Merge "Update the helper filter for needs_delete" into stable/train

This commit is contained in:
Zuul 2020-02-07 21:25:02 +00:00 committed by Gerrit Code Review
commit e2434d2335
2 changed files with 55 additions and 30 deletions

View File

@ -16,6 +16,7 @@
import ast import ast
import json import json
import re
import six import six
from collections import OrderedDict from collections import OrderedDict
@ -88,26 +89,28 @@ class FilterModule(object):
for c in container_infos: for c in container_infos:
c_name = c['Name'] c_name = c['Name']
installed_containers.append(c_name) installed_containers.append(c_name)
labels = c['Config'].get('Labels')
if not labels:
labels = dict()
managed_by = labels.get('managed_by', 'unknown').lower()
# Don't delete containers not managed by tripleo-ansible # Check containers have a label
if (c['Config']['Labels'] is None if not labels:
or c['Config']['Labels'].get( to_skip += [c_name]
'managed_by') != 'tripleo_ansible'):
# Don't delete containers NOT managed by tripleo* or paunch*
elif not re.findall(r"(?=("+'|'.join(['tripleo', 'paunch'])+r"))",
managed_by):
to_skip += [c_name] to_skip += [c_name]
continue
# Only remove containers managed in this config_id # Only remove containers managed in this config_id
if (c['Config']['Labels'] is None elif labels.get('config_id') != config_id:
or c['Config']['Labels'].get('config_id') != config_id):
to_skip += [c_name] to_skip += [c_name]
continue
# Remove containers with no config_data # Remove containers with no config_data
# e.g. broken config containers # e.g. broken config containers
if (c['Config']['Labels'] is not None elif 'config_data' not in labels:
and 'config_data' not in c['Config']['Labels']):
to_delete += [c_name] to_delete += [c_name]
continue
for c_name, config_data in config.items(): for c_name, config_data in config.items():
# don't try to remove a container which doesn't exist # don't try to remove a container which doesn't exist
@ -125,18 +128,26 @@ class FilterModule(object):
# changed. Since we already cleaned the containers not in config, # changed. Since we already cleaned the containers not in config,
# this check needs to be in that loop. # this check needs to be in that loop.
# e.g. new TRIPLEO_CONFIG_HASH during a minor update # e.g. new TRIPLEO_CONFIG_HASH during a minor update
try: c_datas = list()
c_facts = [c['Config']['Labels']['config_data'] for c in container_infos:
for c in container_infos if c_name == c['Name']] if c_name == c['Name']:
except KeyError: try:
continue c_datas.append(c['Config']['Labels']['config_data'])
except KeyError:
pass
# Build c_facts so it can be compared later with config_data # Build c_facts so it can be compared later with config_data
c_facts = ast.literal_eval(c_facts[0]) if ( for c_data in c_datas:
len(c_facts)) == 1 else dict() try:
c_data = ast.literal_eval(c_data)
except (ValueError, SyntaxError): # may already be data
try:
c_data = dict(c_data) # Confirms c_data is type safe
except ValueError: # c_data is not data
c_data = dict()
if cmp(c_facts, config_data) != 0: if cmp(c_data, config_data) != 0:
to_delete += [c_name] to_delete += [c_name]
return to_delete return to_delete

View File

@ -263,8 +263,7 @@ class TestHelperFilters(tests_base.TestCase):
'Name': 'mysql', 'Name': 'mysql',
'Config': { 'Config': {
'Labels': { 'Labels': {
'config_id': 'dontdeleteme', 'config_id': 'tripleo_step1'
'managed_by': 'triple_ansible',
} }
} }
}, },
@ -275,7 +274,7 @@ class TestHelperFilters(tests_base.TestCase):
'managed_by': 'tripleo_ansible', 'managed_by': 'tripleo_ansible',
'config_id': 'tripleo_step1', 'config_id': 'tripleo_step1',
'container_name': 'rabbitmq', 'container_name': 'rabbitmq',
'name': 'rabbitmq', 'name': 'rabbitmq'
} }
} }
}, },
@ -283,11 +282,11 @@ class TestHelperFilters(tests_base.TestCase):
'Name': 'swift', 'Name': 'swift',
'Config': { 'Config': {
'Labels': { 'Labels': {
'managed_by': 'tripleo_ansible', 'managed_by': 'tripleo',
'config_id': 'tripleo_step1', 'config_id': 'tripleo_step1',
'container_name': 'swift', 'container_name': 'swift',
'name': 'swift', 'name': 'swift',
'config_data': "{'foo': 'bar'}", 'config_data': {'foo': 'bar'}
} }
} }
}, },
@ -295,11 +294,23 @@ class TestHelperFilters(tests_base.TestCase):
'Name': 'heat', 'Name': 'heat',
'Config': { 'Config': {
'Labels': { 'Labels': {
'managed_by': 'tripleo_ansible', 'managed_by': 'tripleo-Undercloud',
'config_id': 'tripleo_step1', 'config_id': 'tripleo_step1',
'container_name': 'heat', 'container_name': 'heat',
'name': 'heat', 'name': 'heat',
'config_data': "{'start_order': 0}", 'config_data': "{'start_order': 0}"
}
}
},
{
'Name': 'test1',
'Config': {
'Labels': {
'managed_by': 'tripleo-other',
'config_id': 'tripleo_step1',
'container_name': 'test1',
'name': 'test1',
'config_data': {'start_order': 0}
} }
} }
}, },
@ -307,8 +318,9 @@ class TestHelperFilters(tests_base.TestCase):
'Name': 'haproxy', 'Name': 'haproxy',
'Config': { 'Config': {
'Labels': { 'Labels': {
'managed_by': 'tripleo_ansible', 'managed_by': 'paunch',
'config_id': 'tripleo_step1', 'config_id': 'tripleo_step1',
'config_data': ""
} }
} }
}, },
@ -323,7 +335,7 @@ class TestHelperFilters(tests_base.TestCase):
{ {
'Name': 'none_tripleo', 'Name': 'none_tripleo',
'Config': { 'Config': {
'Labels': None, 'Labels': None
} }
}, },
] ]
@ -343,8 +355,10 @@ class TestHelperFilters(tests_base.TestCase):
'swift': {'foo': 'bar'}, 'swift': {'foo': 'bar'},
# config_data changed: restart needed # config_data changed: restart needed
'heat': {'start_order': 1}, 'heat': {'start_order': 1},
# config_data changed: restart needed
'test1': {'start_order': 2},
} }
expected_list = ['rabbitmq', 'haproxy', 'heat'] expected_list = ['rabbitmq', 'haproxy', 'heat', 'test1']
result = self.filters.needs_delete(container_infos=data, result = self.filters.needs_delete(container_infos=data,
config=config, config=config,
config_id='tripleo_step1') config_id='tripleo_step1')