This commit is contained in:
105
.gitea/scripts/process-terraform-output.py
Normal file
105
.gitea/scripts/process-terraform-output.py
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Process Terraform output to extract applications_for_wiki data
|
||||||
|
Handles various output formats and cleans up invalid JSON
|
||||||
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
def clean_command_prefix(content):
|
||||||
|
"""Remove [command] prefix if present"""
|
||||||
|
if content.startswith('[command]'):
|
||||||
|
lines = content.split('\n', 1)
|
||||||
|
if len(lines) > 1:
|
||||||
|
return lines[1]
|
||||||
|
return content
|
||||||
|
|
||||||
|
def extract_valid_json(content):
|
||||||
|
"""Extract valid JSON from content that might have extra data"""
|
||||||
|
# Find first { and last matching }
|
||||||
|
start = content.find('{')
|
||||||
|
if start < 0:
|
||||||
|
return None
|
||||||
|
|
||||||
|
count = 0
|
||||||
|
end = start
|
||||||
|
for i in range(start, len(content)):
|
||||||
|
if content[i] == '{':
|
||||||
|
count += 1
|
||||||
|
elif content[i] == '}':
|
||||||
|
count -= 1
|
||||||
|
if count == 0:
|
||||||
|
end = i + 1
|
||||||
|
break
|
||||||
|
|
||||||
|
if end > start and count == 0:
|
||||||
|
return content[start:end]
|
||||||
|
return None
|
||||||
|
|
||||||
|
def extract_value(data):
|
||||||
|
"""Extract value from Terraform output format"""
|
||||||
|
if isinstance(data, dict) and 'value' in data:
|
||||||
|
return data['value']
|
||||||
|
return data
|
||||||
|
|
||||||
|
def main():
|
||||||
|
if len(sys.argv) != 3:
|
||||||
|
print("Usage: process-terraform-output.py <input-file> <output-file>")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
input_file = sys.argv[1]
|
||||||
|
output_file = sys.argv[2]
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Read input file
|
||||||
|
with open(input_file, 'r') as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
# Clean command prefix if present
|
||||||
|
content = clean_command_prefix(content)
|
||||||
|
|
||||||
|
# Try to parse JSON directly
|
||||||
|
try:
|
||||||
|
data = json.loads(content)
|
||||||
|
print("✅ Valid JSON parsed successfully")
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
print(f"⚠️ Initial JSON parse failed: {e}")
|
||||||
|
print("🔍 Attempting to extract valid JSON portion...")
|
||||||
|
|
||||||
|
# Try to extract valid JSON
|
||||||
|
valid_json = extract_valid_json(content)
|
||||||
|
if valid_json:
|
||||||
|
try:
|
||||||
|
data = json.loads(valid_json)
|
||||||
|
print("✅ Extracted valid JSON successfully")
|
||||||
|
except json.JSONDecodeError as e2:
|
||||||
|
print(f"❌ Failed to parse extracted JSON: {e2}")
|
||||||
|
sys.exit(1)
|
||||||
|
else:
|
||||||
|
print("❌ Could not extract valid JSON from content")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Extract value if it's wrapped in Terraform output format
|
||||||
|
result = extract_value(data)
|
||||||
|
|
||||||
|
# Write output
|
||||||
|
with open(output_file, 'w') as f:
|
||||||
|
json.dump(result, f, indent=2)
|
||||||
|
|
||||||
|
print(f"✅ Processed output written to {output_file}")
|
||||||
|
|
||||||
|
# Show preview
|
||||||
|
preview = json.dumps(result, indent=2)[:200]
|
||||||
|
print(f"📄 Preview: {preview}...")
|
||||||
|
|
||||||
|
except FileNotFoundError:
|
||||||
|
print(f"❌ Input file {input_file} not found")
|
||||||
|
sys.exit(1)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Error: {e}")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
@@ -47,101 +47,26 @@ jobs:
|
|||||||
echo "📋 Starting Wiki generation..."
|
echo "📋 Starting Wiki generation..."
|
||||||
cd ./terraform/authentik
|
cd ./terraform/authentik
|
||||||
|
|
||||||
# Generate terraform output
|
# Get terraform output
|
||||||
echo "🔍 Generating Terraform output..."
|
echo "🔍 Generating Terraform output..."
|
||||||
|
|
||||||
# Get only the specific output we need
|
|
||||||
echo "📤 Extracting applications_for_wiki output..."
|
|
||||||
terraform output -json applications_for_wiki > terraform-raw-output.json 2>&1
|
terraform output -json applications_for_wiki > terraform-raw-output.json 2>&1
|
||||||
|
|
||||||
# Check if output has command prefix
|
# Process output to extract clean JSON
|
||||||
if grep -q '^\[command\]' terraform-raw-output.json; then
|
echo "📤 Processing Terraform output..."
|
||||||
echo "⚠️ Detected command prefix, removing first line..."
|
python3 ../../.gitea/scripts/process-terraform-output.py terraform-raw-output.json terraform-output.json
|
||||||
tail -n +2 terraform-raw-output.json > terraform-output.json
|
|
||||||
else
|
|
||||||
cp terraform-raw-output.json terraform-output.json
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Validate JSON and extract value
|
# Run wiki generation
|
||||||
echo "🔍 Validating and extracting JSON..."
|
|
||||||
if python3 -c "import json; f=open('terraform-output.json'); data=json.load(f); f.close(); print('✅ Valid JSON')" 2>/dev/null; then
|
|
||||||
# Extract just the value field using Python (more reliable than jq)
|
|
||||||
cat > extract_value.py << 'EOF'
|
|
||||||
import json
|
|
||||||
with open('terraform-output.json', 'r') as f:
|
|
||||||
data = json.load(f)
|
|
||||||
# Handle both full output format and direct value
|
|
||||||
if isinstance(data, dict) and 'value' in data:
|
|
||||||
value = data['value']
|
|
||||||
else:
|
|
||||||
value = data
|
|
||||||
with open('terraform-output-value.json', 'w') as out:
|
|
||||||
json.dump(value, out, indent=2)
|
|
||||||
EOF
|
|
||||||
python3 extract_value.py
|
|
||||||
mv terraform-output-value.json terraform-output.json
|
|
||||||
echo "✅ JSON extracted successfully"
|
|
||||||
else
|
|
||||||
echo "❌ Invalid JSON detected, trying to fix..."
|
|
||||||
# Try to extract valid JSON part
|
|
||||||
cat > fix_json.py << 'EOF'
|
|
||||||
import json
|
|
||||||
import re
|
|
||||||
|
|
||||||
with open('terraform-output.json', 'r') as f:
|
|
||||||
content = f.read()
|
|
||||||
|
|
||||||
# Try to find and extract valid JSON
|
|
||||||
try:
|
|
||||||
# Find first { and last matching }
|
|
||||||
start = content.find('{')
|
|
||||||
if start >= 0:
|
|
||||||
# Count brackets to find matching close
|
|
||||||
count = 0
|
|
||||||
end = start
|
|
||||||
for i in range(start, len(content)):
|
|
||||||
if content[i] == '{':
|
|
||||||
count += 1
|
|
||||||
elif content[i] == '}':
|
|
||||||
count -= 1
|
|
||||||
if count == 0:
|
|
||||||
end = i + 1
|
|
||||||
break
|
|
||||||
|
|
||||||
if end > start:
|
|
||||||
json_str = content[start:end]
|
|
||||||
data = json.loads(json_str)
|
|
||||||
if 'value' in data:
|
|
||||||
data = data['value']
|
|
||||||
with open('terraform-output.json', 'w') as out:
|
|
||||||
json.dump(data, out, indent=2)
|
|
||||||
print('✅ Fixed JSON by extracting valid portion')
|
|
||||||
else:
|
|
||||||
print('❌ Could not find matching brackets')
|
|
||||||
else:
|
|
||||||
print('❌ No JSON found in output')
|
|
||||||
except Exception as e:
|
|
||||||
print(f'❌ Failed to fix JSON: {e}')
|
|
||||||
EOF
|
|
||||||
python3 fix_json.py
|
|
||||||
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..."
|
echo "📊 Running wiki generation script..."
|
||||||
if ! python3 ../../.gitea/scripts/generate-apps-wiki.py terraform-output.json; then
|
if python3 ../../.gitea/scripts/generate-apps-wiki.py terraform-output.json; then
|
||||||
echo "⚠️ First attempt failed, retrying with debug..."
|
echo "✅ Wiki content generated successfully"
|
||||||
|
else
|
||||||
|
echo "⚠️ Wiki generation failed, retrying with debug..."
|
||||||
python3 ../../.gitea/scripts/generate-apps-wiki.py terraform-output.json --debug || echo "⚠️ Wiki generation failed"
|
python3 ../../.gitea/scripts/generate-apps-wiki.py terraform-output.json --debug || echo "⚠️ Wiki generation failed"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check if wiki file was created
|
# Check results
|
||||||
if [ -f "Applications.md" ]; then
|
if [ -f "Applications.md" ]; then
|
||||||
echo "✅ Wiki content generated successfully"
|
echo "✅ Wiki file created: $(wc -l < Applications.md) lines"
|
||||||
else
|
else
|
||||||
echo "⚠️ Wiki content not generated"
|
echo "⚠️ Wiki content not generated"
|
||||||
exit 0
|
exit 0
|
||||||
|
Reference in New Issue
Block a user