Allow env.d to contain underscores in physical_skel

At the moment our dynamic_inventory does have assumption that group names
defined in env.d will not contain underscores, except when it's ending
with `_hosts` or `_containers` since inventory script uses split on `_` and taking
the last argument. So in cases when underscore was used elsewhere in the
group name it will result in unexpected behaviour.

Instead of this approach we now use regexp which replaces the last octet separated with underscore with intended suffix to preserve
rest behaviour.

Change-Id: Id9ba56292972b8b52b4786c78684f2d6f289d88a
This commit is contained in:
Dmitriy Rabotyagov 2024-01-12 14:31:42 +01:00 committed by Dmitriy Rabotyagov
parent d74d038032
commit 8d46ed8842
2 changed files with 20 additions and 3 deletions

View File

@ -252,7 +252,11 @@ def _append_to_host_groups(inventory, container_type, assignment, host_type,
:param host_type: ``str`` Name of the host type
:param type_and_name: ``str`` Combined name of host and container name
"""
physical_group_type = '{}_all'.format(container_type.split('_')[0])
physical_group_type = re.sub(
r"(?P<group>.*)_(?P<type>.*)$",
r'\g<group>_all',
container_type
)
if physical_group_type not in inventory:
logger.debug("Added %s group to inventory", physical_group_type)
inventory[physical_group_type] = {'hosts': []}
@ -336,7 +340,11 @@ def _add_container_hosts(assignment, config, container_group, container_type,
:param inventory: ``dict`` Living dictionary of inventory
:param properties: ``dict`` Dict of container properties
"""
physical_host_type = '{}_hosts'.format(container_type.split('_')[0])
physical_host_type = re.sub(
r"(?P<group>.*)_(?P<type>.*)$",
r'\g<group>_hosts',
container_type
)
container_name = re.sub(r'_', '-', f'{container_group}')
# If the physical host type is not in config return
if physical_host_type not in config:
@ -733,7 +741,11 @@ def container_skel_load(container_skel, inventory, config):
inventory=inventory
)
if properties.get('is_nest', False):
physical_host_type = '{}_hosts'.format(key.split('_')[0])
physical_host_type = re.sub(
r"(?P<group>.*)_(?P<type>.*)$",
r'\g<group>_hosts',
key
)
for host_type in inventory[physical_host_type]['hosts']:
container_mapping = inventory[key]['children']
host_type_containers = '{}-host_containers'.format(host_type)

View File

@ -0,0 +1,5 @@
---
fixes:
- |
Limitation on group naming in `physical_skel` section of env.d files
regarding usage of underscore symbol was released.