diff --git a/.gitea/scripts/generate-apps-wiki.py b/.gitea/scripts/generate-apps-wiki.py index 313bb17..679bdea 100644 --- a/.gitea/scripts/generate-apps-wiki.py +++ b/.gitea/scripts/generate-apps-wiki.py @@ -100,12 +100,27 @@ def generate_markdown_table(apps_data): return "\n".join(markdown) +def parse_terraform_output(output_data): + """Parse Terraform output JSON structure""" + # Check if this is a full terraform output (with value, type, sensitive fields) + if isinstance(output_data, dict) and 'applications_for_wiki' in output_data: + # This is full terraform output format + app_output = output_data.get('applications_for_wiki', {}) + if isinstance(app_output, dict) and 'value' in app_output: + return app_output['value'] + else: + return app_output + else: + # This is already the value extracted + return output_data + def main(): - if len(sys.argv) != 2: - print("Usage: python3 generate-apps-wiki.py ") + if len(sys.argv) < 2: + print("Usage: python3 generate-apps-wiki.py [--debug]") sys.exit(1) output_file = sys.argv[1] + debug = "--debug" in sys.argv try: # Check if file exists and has content @@ -120,10 +135,12 @@ def main(): print(f"📄 Reading Terraform output file: {output_file} ({file_size} bytes)") - # Read and show first few chars for debugging + # Read file content with open(output_file, 'r') as f: content = f.read() - print(f"🔍 File content preview: {content[:100]}...") + + if debug: + print(f"🔍 File content preview: {content[:200]}...") # Clean content - remove command line if present if content.startswith('[command]'): @@ -131,7 +148,8 @@ def main(): lines = content.split('\n', 1) if len(lines) > 1: content = lines[1] - print(f"🔍 Cleaned content preview: {content[:100]}...") + if debug: + print(f"🔍 Cleaned content preview: {content[:200]}...") else: print("ERROR: File contains only command line, no JSON data") sys.exit(1) @@ -141,21 +159,35 @@ def main(): 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) + print(f"Content starts with: {repr(content[:100])}") + # Try to find where JSON starts + json_start = content.find('{') + if json_start > 0: + print(f"Found JSON starting at position {json_start}, retrying...") + content = content[json_start:] + try: + terraform_output = json.loads(content) + except json.JSONDecodeError as e2: + print(f"ERROR: Still invalid JSON: {e2}") + sys.exit(1) + else: + sys.exit(1) - # Extract application data - now terraform_output IS the value - apps_data = terraform_output + # Extract application data using helper function + apps_data = parse_terraform_output(terraform_output) if not apps_data: print("ERROR: No applications data found in Terraform output") - print(f"Output content: {terraform_output}") + if debug: + print(f"Full output structure: {json.dumps(terraform_output, indent=2)[:500]}...") sys.exit(1) # Check if we have correct structure if 'proxy_apps' not in apps_data and 'oauth_apps' not in apps_data: print("ERROR: Expected 'proxy_apps' or 'oauth_apps' in output") print(f"Available keys: {list(apps_data.keys())}") + if debug and apps_data: + print(f"Data structure: {json.dumps(apps_data, indent=2)[:500]}...") sys.exit(1) print(f"📊 Found {len(apps_data.get('proxy_apps', {}))} proxy apps, {len(apps_data.get('oauth_apps', {}))} oauth apps") diff --git a/.gitea/workflows/authentik-apps.yaml b/.gitea/workflows/authentik-apps.yaml index ec61352..9460435 100644 --- a/.gitea/workflows/authentik-apps.yaml +++ b/.gitea/workflows/authentik-apps.yaml @@ -40,3 +40,106 @@ jobs: run: terraform apply -var-file proxy-apps.tfvars -var-file oauth2-apps.tfvars -var-file terraform.tfvars -var-file groups.tfvars -input=false -auto-approve -parallelism=100 working-directory: ./terraform/authentik + - name: Generate Wiki Content + if: success() + continue-on-error: true + run: | + echo "📋 Starting Wiki generation..." + cd ./terraform/authentik + + # Generate terraform output + echo "🔍 Generating Terraform output..." + + # Method 1: Try to get clean JSON output + if terraform output -json > terraform-output.json 2>/dev/null; then + echo "✅ Direct JSON output successful" + else + echo "⚠️ Direct JSON failed, trying alternative method..." + # Method 2: Get specific output + terraform output -json applications_for_wiki > terraform-output.json 2>&1 + fi + + # Clean up the output if needed + if grep -q '^\[command\]' terraform-output.json; then + echo "⚠️ Detected command prefix in output, cleaning..." + tail -n +2 terraform-output.json > terraform-output-clean.json + mv terraform-output-clean.json terraform-output.json + fi + + # Debug output + echo "📄 Output file size: $(wc -c < terraform-output.json) bytes" + echo "🔍 Content preview:" + head -c 500 terraform-output.json + echo "" + + # Run wiki generation with debug if first attempt fails + echo "📊 Running wiki generation script..." + if ! python3 ../../.gitea/scripts/generate-apps-wiki.py terraform-output.json; then + echo "⚠️ First attempt failed, retrying with debug..." + python3 ../../.gitea/scripts/generate-apps-wiki.py terraform-output.json --debug || echo "⚠️ Wiki generation failed" + fi + + # Check if wiki file was created + if [ -f "Applications.md" ]; then + echo "✅ Wiki content generated successfully" + else + echo "⚠️ Wiki content not generated" + exit 0 + fi + working-directory: ./ + + - name: Upload Wiki to Gitea + if: success() + continue-on-error: true + run: | + cd ./terraform/authentik + + # Set variables + GITEA_URL="${{ secrets.GT_WIKI_URL }}" + GITEA_TOKEN="${{ secrets.GT_WIKI_TOKEN }}" + GITEA_OWNER="${{ secrets.GT_WIKI_OWNER }}" + GITEA_REPO="${{ secrets.GT_WIKI_REPO }}" + + # Check if file exists + if [ ! -f "Applications.md" ]; then + echo "⚠️ Applications.md not found, skipping wiki update" + exit 0 + fi + + echo "📤 Uploading to Gitea Wiki..." + + # Encode content to base64 + CONTENT=$(base64 -w 0 Applications.md) + + # Check if wiki page exists + WIKI_PAGE_EXISTS=$(curl -s -o /dev/null -w "%{http_code}" \ + -H "Authorization: token $GITEA_TOKEN" \ + "$GITEA_URL/api/v1/repos/$GITEA_OWNER/$GITEA_REPO/wiki/page/Applications" || echo "000") + + if [ "$WIKI_PAGE_EXISTS" = "200" ]; then + echo "📝 Updating existing wiki page..." + curl -X PATCH \ + -H "Authorization: token $GITEA_TOKEN" \ + -H "Content-Type: application/json" \ + -d "{ + \"title\": \"Applications\", + \"content_base64\": \"$CONTENT\", + \"message\": \"Update applications list from CI/CD [$(date)]\" + }" \ + "$GITEA_URL/api/v1/repos/$GITEA_OWNER/$GITEA_REPO/wiki/page/Applications" || echo "⚠️ Wiki update failed" + else + echo "📄 Creating new wiki page..." + curl -X POST \ + -H "Authorization: token $GITEA_TOKEN" \ + -H "Content-Type: application/json" \ + -d "{ + \"title\": \"Applications\", + \"content_base64\": \"$CONTENT\", + \"message\": \"Create applications list from CI/CD [$(date)]\" + }" \ + "$GITEA_URL/api/v1/repos/$GITEA_OWNER/$GITEA_REPO/wiki/new" || echo "⚠️ Wiki creation failed" + fi + + echo "✅ Wiki update process completed" + working-directory: ./ + diff --git a/.gitea/workflows/update-wiki.yml b/.gitea/workflows/update-wiki.yml deleted file mode 100644 index 9af0e5d..0000000 --- a/.gitea/workflows/update-wiki.yml +++ /dev/null @@ -1,125 +0,0 @@ -name: 'Update Authentik Applications Wiki' - -on: - push: - branches: [ "main" ] -# paths: -# - 'terraform/authentik/**' - workflow_dispatch: - -permissions: - contents: read - -jobs: - update-wiki: - name: 'Generate and Update Wiki' - runs-on: ubuntu-latest - environment: production - - defaults: - run: - shell: bash - working-directory: ./terraform/authentik - - steps: - - name: Checkout - uses: actions/checkout@v3 - - - name: Setup Terraform - uses: hashicorp/setup-terraform@v2 - with: - cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }} - - - name: Terraform Init - run: terraform init - - - name: Generate Terraform Output - run: | - echo "🔍 Generating Terraform output..." - # Use terraform-bin directly to avoid wrapper output - terraform output -json applications_for_wiki > terraform-output-raw.json - - # Extract just the value field - jq '.value' terraform-output-raw.json > terraform-output.json - - # Debug output file - echo "📄 Output file size: $(wc -c < terraform-output.json) bytes" - echo "🔍 First 200 chars of output:" - head -c 200 terraform-output.json - echo "" - echo "✅ Terraform output generated" - - - name: Generate Wiki Content - run: | - echo "🔍 Checking output file..." - if [ ! -f "terraform-output.json" ]; then - echo "❌ terraform-output.json not found" - exit 1 - fi - - echo "📊 Running wiki generation script..." - python3 ../../.gitea/scripts/generate-apps-wiki.py terraform-output.json - echo "✅ Wiki content generated" - - - name: Upload Wiki to Gitea - run: | - # Set variables - GITEA_URL="${{ secrets.GT_WIKI_URL }}" - GITEA_TOKEN="${{ secrets.GT_WIKI_TOKEN }}" - GITEA_OWNER="${{ secrets.GT_WIKI_OWNER }}" - GITEA_REPO="${{ secrets.GT_WIKI_REPO }}" - - # Check if file was created - if [ ! -f "Applications.md" ]; then - echo "❌ Applications.md not found" - exit 1 - fi - - echo "📤 Uploading to Gitea Wiki..." - - # Encode content to base64 - CONTENT=$(base64 -w 0 Applications.md) - - # Check if wiki page exists - WIKI_PAGE_EXISTS=$(curl -s -o /dev/null -w "%{http_code}" \ - -H "Authorization: token $GITEA_TOKEN" \ - "$GITEA_URL/api/v1/repos/$GITEA_OWNER/$GITEA_REPO/wiki/page/Applications") - - if [ "$WIKI_PAGE_EXISTS" = "200" ]; then - echo "📝 Updating existing wiki page..." - # Update existing page - curl -X PATCH \ - -H "Authorization: token $GITEA_TOKEN" \ - -H "Content-Type: application/json" \ - -d "{ - \"title\": \"Applications\", - \"content_base64\": \"$CONTENT\", - \"message\": \"Update applications list from CI/CD [$(date)]\" - }" \ - "$GITEA_URL/api/v1/repos/$GITEA_OWNER/$GITEA_REPO/wiki/page/Applications" - else - echo "📄 Creating new wiki page..." - # Create new page - curl -X POST \ - -H "Authorization: token $GITEA_TOKEN" \ - -H "Content-Type: application/json" \ - -d "{ - \"title\": \"Applications\", - \"content_base64\": \"$CONTENT\", - \"message\": \"Create applications list from CI/CD [$(date)]\" - }" \ - "$GITEA_URL/api/v1/repos/$GITEA_OWNER/$GITEA_REPO/wiki/new" - fi - - echo "✅ Wiki updated successfully!" - echo "🔗 Wiki URL: $GITEA_URL/$GITEA_OWNER/$GITEA_REPO/wiki/Applications" - - - name: Summary - run: | - echo "## 📊 Wiki Update Summary" >> $GITHUB_STEP_SUMMARY - echo "- ✅ Terraform output extracted" >> $GITHUB_STEP_SUMMARY - echo "- ✅ Applications table generated" >> $GITHUB_STEP_SUMMARY - echo "- ✅ Wiki page updated in Gitea" >> $GITHUB_STEP_SUMMARY - echo "" >> $GITHUB_STEP_SUMMARY - echo "**Applications count:** $(grep -c '|.*|.*|.*|.*|.*|' Applications.md || echo 0)" >> $GITHUB_STEP_SUMMARY - echo "**Generated at:** $(date)" >> $GITHUB_STEP_SUMMARY