#!/usr/bin/env python3 """ Script for generating Wiki page with applications list from Terraform outputs """ import json import sys import os from datetime import datetime def generate_markdown_table(apps_data): """Generates Markdown table for applications""" # Combine all applications all_apps = [] if 'proxy_apps' in apps_data: for key, app in apps_data['proxy_apps'].items(): all_apps.append({ 'key': key, 'name': app['name'], 'type': app['type'], 'url': app['url'], 'group': app['group'], 'description': app['description'], 'icon': app['icon'], 'slug': app['slug'] }) if 'oauth_apps' in apps_data: for key, app in apps_data['oauth_apps'].items(): all_apps.append({ 'key': key, 'name': app['name'], 'type': app['type'], 'url': app['url'], 'group': app['group'], 'description': app['description'], 'icon': app['icon'], 'slug': app['slug'] }) # Sort by groups, then by name all_apps.sort(key=lambda x: (x['group'], x['name'])) # Generate Markdown markdown = [] markdown.append("# Authentik Applications") markdown.append("") markdown.append(f"*Automatically generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S UTC')}*") markdown.append("") markdown.append("## All Applications") markdown.append("") # Table markdown.append("| Icon | Name | Type | URL | Group | Description |") markdown.append("|------|------|------|-----|-------|-------------|") for app in all_apps: icon = f"![]({app['icon']})" if app['icon'] else "📱" url_link = f"[🔗 Open]({app['url']})" if app['url'] else "-" description = app['description'] if app['description'] else "-" markdown.append(f"| {icon} | **{app['name']}** | {app['type']} | {url_link} | {app['group']} | {description} |") markdown.append("") # Statistics proxy_count = len(apps_data.get('proxy_apps', {})) oauth_count = len(apps_data.get('oauth_apps', {})) total_count = proxy_count + oauth_count markdown.append("## Statistics") markdown.append("") markdown.append(f"- **Total applications**: {total_count}") markdown.append(f"- **Proxy applications**: {proxy_count}") markdown.append(f"- **OAuth2/OpenID applications**: {oauth_count}") markdown.append("") # Grouping by types groups = {} for app in all_apps: group = app['group'] if group not in groups: groups[group] = {'proxy': 0, 'oauth': 0} if app['type'] == 'Proxy': groups[group]['proxy'] += 1 else: groups[group]['oauth'] += 1 markdown.append("## Applications by Groups") markdown.append("") for group, counts in sorted(groups.items()): total = counts['proxy'] + counts['oauth'] markdown.append(f"- **{group}**: {total} applications (Proxy: {counts['proxy']}, OAuth: {counts['oauth']})") markdown.append("") markdown.append("---") markdown.append("*This page is automatically generated via Terraform CI/CD*") return "\n".join(markdown) def main(): if len(sys.argv) != 2: print("Usage: python3 generate-apps-wiki.py ") sys.exit(1) output_file = sys.argv[1] try: # Check if file exists and has content if not os.path.exists(output_file): print(f"ERROR: File {output_file} not found") sys.exit(1) file_size = os.path.getsize(output_file) if file_size == 0: print(f"ERROR: File {output_file} is empty") sys.exit(1) print(f"📄 Reading Terraform output file: {output_file} ({file_size} bytes)") # Read and show first few chars for debugging with open(output_file, 'r') as f: content = f.read() print(f"🔍 File content preview: {content[:100]}...") # Parse JSON try: terraform_output = json.loads(content) except json.JSONDecodeError as e: print(f"ERROR: Invalid JSON in {output_file}: {e}") print(f"Content starts with: {content[:50]}") sys.exit(1) # Extract application data apps_data = terraform_output.get('applications_for_wiki', {}).get('value', {}) if not apps_data: print("ERROR: No applications_for_wiki output found in Terraform output") print(f"Available outputs: {list(terraform_output.keys())}") sys.exit(1) print(f"📊 Found {len(apps_data.get('proxy_apps', {}))} proxy apps, {len(apps_data.get('oauth_apps', {}))} oauth apps") # Generate Markdown markdown_content = generate_markdown_table(apps_data) # Write result wiki_file = "Applications.md" with open(wiki_file, 'w', encoding='utf-8') as f: f.write(markdown_content) print(f"✅ Wiki page generated: {wiki_file}") print(f"📊 Total applications: {len(apps_data.get('proxy_apps', {})) + len(apps_data.get('oauth_apps', {}))}") except Exception as e: print(f"ERROR: {e}") sys.exit(1) if __name__ == "__main__": main()