[upgrade] Add inventory filters to map groups to roles

tripleo-upgrade previously relied on <service_group>.children.keys to determine
which roles include a particular service.

Since I2cc721676005536b14995980f7a042991c92adcc altered the inventory
structure this will no longer work.

Create filters to generate a group to role mapping instead. Use the
tripleo_role_name var to determine the role names instead of group names.

Change-Id: I3daf77f099fd0277a9d8495932ca3e28ae7b964d
This commit is contained in:
Oliver Walsh 2020-04-15 22:57:31 +01:00
parent a07f91603d
commit b7f027ff09
2 changed files with 63 additions and 0 deletions

View File

@ -50,6 +50,36 @@ def to_inventory_hostmap(data):
return group_host_map
def to_inventory_rolemap(data):
# Falttens inventory to a group->role mapping
if isinstance(data, six.string_types):
inventory = yaml.safe_load(data)
else:
inventory = data
group_role_map = {}
todo = collections.deque(inventory.keys())
while todo:
group = todo.popleft()
if 'tripleo_role_name' in inventory[group].get('vars', {}):
group_role_map[group] = [inventory[group]['vars']['tripleo_role_name']]
else:
if 'children' in inventory[group]:
for child in inventory[group]['children']:
# Children have not all been flattened yet
# so postpone flattening this group
if child in todo:
todo.append(group)
break
else:
group_role_map[group] = []
for child in inventory[group]['children']:
group_role_map[group] += group_role_map[child]
group_role_map[group].sort()
return group_role_map
def to_inventory_roles(data):
# Returns list of tripleo roles in inventory
if isinstance(data, six.string_types):
@ -69,5 +99,6 @@ class FilterModule(object):
def filters(self):
return {
'to_inventory_hostmap': to_inventory_hostmap,
'to_inventory_rolemap': to_inventory_rolemap,
'to_inventory_roles': to_inventory_roles,
}

View File

@ -28,11 +28,17 @@ allovercloud:
Controller: {}
vars: {}
Controller:
children:
overcloud_Controller: {}
overcloud_Controller:
hosts:
overcloud-controller-0: {foo_bar: baz}
vars:
tripleo_role_name: Controller
Compute:
children:
overcloud_Compute: {}
overcloud_Compute:
hosts:
overcloud-novacompute-0: {foo_bar: baz}
vars:
@ -49,10 +55,16 @@ dynamic_inventory = """
"vars" : {}
},
"Controller" : {
"children": [ "overcloud_Controller" ]
},
"overcloud_Controller" : {
"hosts": [ "overcloud-controller-0" ],
"vars": { "tripleo_role_name": "Controller" }
},
"Compute" : {
"children": [ "overcloud_Compute" ]
},
"overcloud_Compute" : {
"hosts": ["overcloud-novacompute-0" ],
"vars": { "tripleo_role_name": "Compute" }
}
@ -68,7 +80,9 @@ class TestInventoryFilters(tests_base.TestCase):
def _test_hostmap(self, inventory):
expected = {
"Compute": ["overcloud-novacompute-0"],
"overcloud_Compute": ["overcloud-novacompute-0"],
"Controller": ["overcloud-controller-0"],
"overcloud_Controller": ["overcloud-controller-0"],
"overcloud": ["overcloud-controller-0", "overcloud-novacompute-0"],
"allovercloud": ["overcloud-controller-0", "overcloud-novacompute-0"],
}
@ -81,6 +95,24 @@ class TestInventoryFilters(tests_base.TestCase):
def test_hostmap_dynamic(self):
self._test_hostmap(dynamic_inventory)
def _test_rolemap(self, inventory):
expected = {
"overcloud": ["Compute", "Controller"],
"allovercloud": ["Compute", "Controller"],
"Controller": ["Controller"],
"overcloud_Controller": ["Controller"],
"Compute": ["Compute"],
"overcloud_Compute": ["Compute"]
}
result = tripleo_inventory.to_inventory_rolemap(inventory)
self.assertEqual(expected, result)
def test_rolemap_static(self):
self._test_rolemap(static_inventory)
def test_rolemap_dynamic(self):
self._test_rolemap(dynamic_inventory)
def _test_roles(self, inventory):
expected = ["Compute", "Controller"]
result = tripleo_inventory.to_inventory_roles(inventory)