mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-07-18 14:34:24 +02:00
Overall optimations for application id naming
This commit is contained in:
parent
562603a8cd
commit
409e659143
2
Makefile
2
Makefile
@ -63,7 +63,7 @@ install: build
|
|||||||
|
|
||||||
partial-test:
|
partial-test:
|
||||||
@echo "🧪 Running Python tests…"
|
@echo "🧪 Running Python tests…"
|
||||||
python -m unittest discover -s tests
|
PYTHONPATH=. python -m unittest discover -s tests
|
||||||
@echo "📑 Checking Ansible syntax…"
|
@echo "📑 Checking Ansible syntax…"
|
||||||
ansible-playbook playbook.yml --syntax-check
|
ansible-playbook playbook.yml --syntax-check
|
||||||
|
|
||||||
|
@ -1,9 +1,15 @@
|
|||||||
def get_docker_paths(path_docker_compose_instances: str, application_id: str) -> dict:
|
import sys, os
|
||||||
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||||
|
from module_utils.entity_name_utils import get_entity_name
|
||||||
|
|
||||||
|
def get_docker_paths(application_id: str, path_docker_compose_instances: str) -> dict:
|
||||||
"""
|
"""
|
||||||
Build the docker_compose dict based on
|
Build the docker_compose dict based on
|
||||||
path_docker_compose_instances and application_id.
|
path_docker_compose_instances and application_id.
|
||||||
|
Uses get_entity_name to extract the entity name from application_id.
|
||||||
"""
|
"""
|
||||||
base = f"{path_docker_compose_instances}{application_id}/"
|
entity = get_entity_name(application_id)
|
||||||
|
base = f"{path_docker_compose_instances}{entity}/"
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'directories': {
|
'directories': {
|
||||||
|
@ -1,52 +1,6 @@
|
|||||||
import os
|
import sys, os
|
||||||
import yaml
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||||
|
from module_utils.entity_name_utils import get_entity_name
|
||||||
def _load_categories_tree(categories_file):
|
|
||||||
with open(categories_file, 'r', encoding='utf-8') as f:
|
|
||||||
categories = yaml.safe_load(f)['roles']
|
|
||||||
return categories
|
|
||||||
|
|
||||||
def _flatten_categories(tree, prefix=''):
|
|
||||||
"""Flattens nested category tree to all possible category paths."""
|
|
||||||
result = []
|
|
||||||
for k, v in tree.items():
|
|
||||||
current = f"{prefix}-{k}" if prefix else k
|
|
||||||
result.append(current)
|
|
||||||
if isinstance(v, dict):
|
|
||||||
for sk, sv in v.items():
|
|
||||||
if isinstance(sv, dict):
|
|
||||||
result.extend(_flatten_categories({sk: sv}, current))
|
|
||||||
return result
|
|
||||||
|
|
||||||
def get_entity_name(role_name):
|
|
||||||
"""
|
|
||||||
Automatically get the entity name from a role name by removing the
|
|
||||||
longest matching category path from categories.yml.
|
|
||||||
"""
|
|
||||||
possible_locations = [
|
|
||||||
os.path.join(os.getcwd(), 'roles', 'categories.yml'),
|
|
||||||
os.path.join(os.path.dirname(__file__), '..', 'roles', 'categories.yml'),
|
|
||||||
'roles/categories.yml',
|
|
||||||
]
|
|
||||||
categories_file = None
|
|
||||||
for loc in possible_locations:
|
|
||||||
if os.path.exists(loc):
|
|
||||||
categories_file = loc
|
|
||||||
break
|
|
||||||
if not categories_file:
|
|
||||||
return role_name
|
|
||||||
|
|
||||||
categories_tree = _load_categories_tree(categories_file)
|
|
||||||
all_category_paths = _flatten_categories(categories_tree)
|
|
||||||
|
|
||||||
role_name_lc = role_name.lower()
|
|
||||||
all_category_paths = [cat.lower() for cat in all_category_paths]
|
|
||||||
for cat in sorted(all_category_paths, key=len, reverse=True):
|
|
||||||
if role_name_lc.startswith(cat + "-"):
|
|
||||||
return role_name[len(cat) + 1:]
|
|
||||||
if role_name_lc == cat:
|
|
||||||
return ""
|
|
||||||
return role_name
|
|
||||||
|
|
||||||
class FilterModule(object):
|
class FilterModule(object):
|
||||||
def filters(self):
|
def filters(self):
|
||||||
|
49
module_utils/entity_name_utils.py
Normal file
49
module_utils/entity_name_utils.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import os
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
def load_categories_tree(categories_file):
|
||||||
|
with open(categories_file, 'r', encoding='utf-8') as f:
|
||||||
|
categories = yaml.safe_load(f)['roles']
|
||||||
|
return categories
|
||||||
|
|
||||||
|
def flatten_categories(tree, prefix=''):
|
||||||
|
"""Flattens nested category tree to all possible category paths."""
|
||||||
|
result = []
|
||||||
|
for k, v in tree.items():
|
||||||
|
current = f"{prefix}-{k}" if prefix else k
|
||||||
|
result.append(current)
|
||||||
|
if isinstance(v, dict):
|
||||||
|
for sk, sv in v.items():
|
||||||
|
if isinstance(sv, dict):
|
||||||
|
result.extend(flatten_categories({sk: sv}, current))
|
||||||
|
return result
|
||||||
|
|
||||||
|
def get_entity_name(role_name):
|
||||||
|
"""
|
||||||
|
Get the entity name from a role name by removing the
|
||||||
|
longest matching category path from categories.yml.
|
||||||
|
"""
|
||||||
|
possible_locations = [
|
||||||
|
os.path.join(os.getcwd(), 'roles', 'categories.yml'),
|
||||||
|
os.path.join(os.path.dirname(__file__), '..', 'roles', 'categories.yml'),
|
||||||
|
'roles/categories.yml',
|
||||||
|
]
|
||||||
|
categories_file = None
|
||||||
|
for loc in possible_locations:
|
||||||
|
if os.path.exists(loc):
|
||||||
|
categories_file = loc
|
||||||
|
break
|
||||||
|
if not categories_file:
|
||||||
|
return role_name
|
||||||
|
|
||||||
|
categories_tree = load_categories_tree(categories_file)
|
||||||
|
all_category_paths = flatten_categories(categories_tree)
|
||||||
|
|
||||||
|
role_name_lc = role_name.lower()
|
||||||
|
all_category_paths = [cat.lower() for cat in all_category_paths]
|
||||||
|
for cat in sorted(all_category_paths, key=len, reverse=True):
|
||||||
|
if role_name_lc.startswith(cat + "-"):
|
||||||
|
return role_name[len(cat) + 1:]
|
||||||
|
if role_name_lc == cat:
|
||||||
|
return ""
|
||||||
|
return role_name
|
@ -1,6 +1,6 @@
|
|||||||
{# This template needs to be included in docker-compose.yml which contain a database and additional volumes #}
|
{# This template needs to be included in docker-compose.yml which contain a database and additional volumes #}
|
||||||
volumes:
|
volumes:
|
||||||
{% if not applications | get_app_conf(application_id, 'features.central_database', False)%}
|
{% if not applications | get_app_conf(application_id, 'features.central_database', False) and applications | get_app_conf(application_id, 'docker.services.database.enabled', False) %}
|
||||||
database:
|
database:
|
||||||
name: {{ database_volume }}
|
name: {{ database_volume }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
# @See https://chatgpt.com/share/67a23d18-fb54-800f-983c-d6d00752b0b4
|
# @See https://chatgpt.com/share/67a23d18-fb54-800f-983c-d6d00752b0b4
|
||||||
docker_compose: "{{ path_docker_compose_instances | get_docker_paths(application_id) }}"
|
docker_compose: "{{ application_id | get_docker_paths(path_docker_compose_instances) }}"
|
0
tests/integration/application_id/__init__.py
Normal file
0
tests/integration/application_id/__init__.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
import yaml
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
# Dynamically determine the path to the roles directory
|
||||||
|
ROLES_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'roles'))
|
||||||
|
|
||||||
|
class TestApplicationIdDeprecation(unittest.TestCase):
|
||||||
|
def test_application_id_matches_role_name(self):
|
||||||
|
"""
|
||||||
|
Deprecation: application_id in vars/main.yml must match the role name.
|
||||||
|
"""
|
||||||
|
for role in os.listdir(ROLES_DIR):
|
||||||
|
role_path = os.path.join(ROLES_DIR, role)
|
||||||
|
vars_main_yml = os.path.join(role_path, 'vars', 'main.yml')
|
||||||
|
if not os.path.isfile(vars_main_yml):
|
||||||
|
continue
|
||||||
|
with open(vars_main_yml, 'r', encoding='utf-8') as f:
|
||||||
|
try:
|
||||||
|
data = yaml.safe_load(f)
|
||||||
|
except Exception as e:
|
||||||
|
self.fail(f"Could not parse {vars_main_yml}: {e}")
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
continue
|
||||||
|
app_id = data.get('application_id')
|
||||||
|
if app_id is not None and app_id != role:
|
||||||
|
warnings.warn(
|
||||||
|
f"[DEPRECATION WARNING] application_id '{app_id}' in {vars_main_yml} "
|
||||||
|
f"does not match its role directory '{role}'.\n"
|
||||||
|
f"Please update 'application_id' to match the role name for future compatibility.",
|
||||||
|
DeprecationWarning
|
||||||
|
)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
Loading…
x
Reference in New Issue
Block a user