mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-30 07:18:09 +02:00
Adapted roles to new architecture
This commit is contained in:
54
filter_plugins/get_all_invokable_apps.py
Normal file
54
filter_plugins/get_all_invokable_apps.py
Normal file
@@ -0,0 +1,54 @@
|
||||
import os
|
||||
import yaml
|
||||
|
||||
def get_all_invokable_apps(
|
||||
categories_file=None,
|
||||
roles_dir=None
|
||||
):
|
||||
"""
|
||||
Return all application_ids (or role names) for roles whose directory names match invokable paths from categories.yml.
|
||||
:param categories_file: Path to categories.yml (default: roles/categories.yml at project root)
|
||||
:param roles_dir: Path to roles directory (default: roles/ at project root)
|
||||
:return: List of application_ids (or role names)
|
||||
"""
|
||||
# Resolve defaults
|
||||
here = os.path.dirname(os.path.abspath(__file__))
|
||||
project_root = os.path.abspath(os.path.join(here, '..'))
|
||||
if not categories_file:
|
||||
categories_file = os.path.join(project_root, 'roles', 'categories.yml')
|
||||
if not roles_dir:
|
||||
roles_dir = os.path.join(project_root, 'roles')
|
||||
|
||||
# Get invokable paths
|
||||
from filter_plugins.invokable_paths import get_invokable_paths
|
||||
invokable_paths = get_invokable_paths(categories_file)
|
||||
if not invokable_paths:
|
||||
return []
|
||||
|
||||
result = []
|
||||
if not os.path.isdir(roles_dir):
|
||||
return []
|
||||
|
||||
for role in sorted(os.listdir(roles_dir)):
|
||||
role_path = os.path.join(roles_dir, role)
|
||||
if not os.path.isdir(role_path):
|
||||
continue
|
||||
if any(role == p or role.startswith(p + '-') for p in invokable_paths):
|
||||
vars_file = os.path.join(role_path, 'vars', 'main.yml')
|
||||
if os.path.isfile(vars_file):
|
||||
try:
|
||||
with open(vars_file, 'r', encoding='utf-8') as f:
|
||||
data = yaml.safe_load(f) or {}
|
||||
app_id = data.get('application_id', role)
|
||||
except Exception:
|
||||
app_id = role
|
||||
else:
|
||||
app_id = role
|
||||
result.append(app_id)
|
||||
return sorted(result)
|
||||
|
||||
class FilterModule(object):
|
||||
def filters(self):
|
||||
return {
|
||||
'get_all_invokable_apps': get_all_invokable_apps
|
||||
}
|
55
filter_plugins/get_entity_name.py
Normal file
55
filter_plugins/get_entity_name.py
Normal file
@@ -0,0 +1,55 @@
|
||||
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):
|
||||
"""
|
||||
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):
|
||||
def filters(self):
|
||||
return {
|
||||
'get_entity_name': get_entity_name,
|
||||
}
|
Reference in New Issue
Block a user