mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 15:06:26 +02:00
Added run_after integration test
This commit is contained in:
65
cli/generate/roles_list.py
Normal file
65
cli/generate/roles_list.py
Normal file
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Generate a JSON file listing all Ansible role directories.
|
||||
|
||||
Usage:
|
||||
python roles_list.py [--roles-dir path/to/roles] [--output path/to/roles/list.json | console]
|
||||
"""
|
||||
import os
|
||||
import json
|
||||
import argparse
|
||||
|
||||
|
||||
def find_roles(roles_dir: str):
|
||||
"""Return sorted list of role names under roles_dir."""
|
||||
return sorted([
|
||||
entry for entry in os.listdir(roles_dir)
|
||||
if os.path.isdir(os.path.join(roles_dir, entry))
|
||||
])
|
||||
|
||||
|
||||
def write_roles_list(roles, out_file):
|
||||
"""Write the list of roles to out_file as JSON."""
|
||||
os.makedirs(os.path.dirname(out_file), exist_ok=True)
|
||||
with open(out_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(roles, f, indent=2)
|
||||
print(f"Wrote roles list to {out_file}")
|
||||
|
||||
|
||||
def main():
|
||||
# Determine default roles_dir relative to this script: ../../.. -> roles
|
||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
default_roles_dir = os.path.abspath(
|
||||
os.path.join(script_dir, '..', '..', 'roles')
|
||||
)
|
||||
default_output = os.path.join(default_roles_dir, 'list.json')
|
||||
|
||||
parser = argparse.ArgumentParser(description='Generate roles/list.json')
|
||||
parser.add_argument(
|
||||
'--roles-dir', '-r',
|
||||
default=default_roles_dir,
|
||||
help=f'Directory containing role subfolders (default: {default_roles_dir})'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--output', '-o',
|
||||
default=default_output,
|
||||
help=(
|
||||
'Output path for roles list JSON '
|
||||
'(or "console" to print to stdout, default: %(default)s)'
|
||||
)
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
if not os.path.isdir(args.roles_dir):
|
||||
parser.error(f"Roles directory not found: {args.roles_dir}")
|
||||
|
||||
roles = find_roles(args.roles_dir)
|
||||
|
||||
if args.output.lower() == 'console':
|
||||
# Print JSON to stdout
|
||||
print(json.dumps(roles, indent=2))
|
||||
else:
|
||||
write_roles_list(roles, args.output)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
76
cli/meta/role_folder.py
Normal file
76
cli/meta/role_folder.py
Normal file
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
CLI Script: get_role_folder_cli.py
|
||||
|
||||
This script determines the appropriate Ansible role folder based on the provided application_id
|
||||
by inspecting each role's vars/main.yml within the roles directory. By default, it assumes the
|
||||
roles directory is located at the project root, relative to this script's location.
|
||||
|
||||
Example:
|
||||
./get_role_folder_cli.py --application-id my-app-id
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import argparse
|
||||
import yaml
|
||||
|
||||
|
||||
def get_role_folder(application_id, roles_path):
|
||||
"""
|
||||
Find the role directory under `roles_path` whose vars/main.yml contains the specified application_id.
|
||||
|
||||
:param application_id: The application_id to match.
|
||||
:param roles_path: Path to the roles directory.
|
||||
:return: The name of the matching role directory.
|
||||
:raises RuntimeError: If no match is found or if an error occurs while reading files.
|
||||
"""
|
||||
if not os.path.isdir(roles_path):
|
||||
raise RuntimeError(f"Roles path not found: {roles_path}")
|
||||
|
||||
for role in sorted(os.listdir(roles_path)):
|
||||
role_dir = os.path.join(roles_path, role)
|
||||
vars_file = os.path.join(role_dir, 'vars', 'main.yml')
|
||||
if os.path.isfile(vars_file):
|
||||
try:
|
||||
with open(vars_file, 'r') as f:
|
||||
data = yaml.safe_load(f) or {}
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"Failed to load {vars_file}: {e}")
|
||||
|
||||
if data.get('application_id') == application_id:
|
||||
return role
|
||||
|
||||
raise RuntimeError(f"No role found with application_id '{application_id}' in {roles_path}")
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Determine the Ansible role folder by application_id'
|
||||
)
|
||||
parser.add_argument(
|
||||
'application_id',
|
||||
help='The application_id defined in vars/main.yml to search for'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-r', '--roles-path',
|
||||
default=os.path.join(
|
||||
os.path.dirname(os.path.realpath(__file__)),
|
||||
os.pardir, os.pardir,
|
||||
'roles'
|
||||
),
|
||||
help='Path to the roles directory (default: roles/ at project root)'
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
folder = get_role_folder(args.application_id, args.roles_path)
|
||||
print(folder)
|
||||
sys.exit(0)
|
||||
except RuntimeError as err:
|
||||
print(f"Error: {err}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Reference in New Issue
Block a user