debug: erforce globale fehler-diagnose für consul-weiche
Some checks failed
cloudmon GitOps Deploy / build-and-deploy (push) Failing after 22s

This commit is contained in:
Bast 2026-11-02 11:52:07 -06:00
parent 41566268e8
commit 5b87285cc5

View file

@ -1,34 +1,46 @@
import json import json
import base64 import base64
import requests import requests
import logging import traceback
from django.http import JsonResponse from django.http import JsonResponse
from django.views.generic import TemplateView, View from django.views.generic import TemplateView, View
logger = logging.getLogger(__name__)
class DashboardView(TemplateView): class DashboardView(TemplateView):
template_name = "monitor/dashboard.html" template_name = "monitor/dashboard.html"
class ClusterGraphDataView(View): class ClusterGraphDataView(View):
"""
Die Diagnose-Schnittstelle: Fängt jeden Fehler global ab
und liefert den echten Python-Fehler als Text zurück!
"""
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
repo_name = request.GET.get("repo", "boday_tf") # Wir umschließen die ABSOLUT GESAMTE Funktion mit einem globalen Sicherheitsnetz
consul_url = f"http://consul:8500/v1/kv/terraform/states/{repo_name}"
# ABSOLUTER SICHERHEITSGURT: Fängt jeden Parsing-Fehler ab, damit das Dashboard NIEMALS mit 500 abstürzt
try: try:
repo_name = request.GET.get("repo", "boday_tf")
consul_url = f"http://consul:8500/v1/kv/terraform/states/{repo_name}"
response = requests.get(consul_url, timeout=5) response = requests.get(consul_url, timeout=5)
if response.status_code == 404: if response.status_code == 404:
return JsonResponse({"name": f"Warte auf korrekten Consul-Key '{repo_name}'...", "type": "Root", "children": []}) return JsonResponse({
"name": f"Warte auf Consul-Key '{repo_name}'...",
"type": "Root",
"children": []
})
consul_json = response.json() consul_data = response.json()
if isinstance(consul_json, list) and len(consul_json) > 0:
raw_value = consul_json[0].get("Value") # WICHTIG: Consul liefert im KV-Store IMMER eine Liste zurück! [ {"LockIndex": 0, "Key": "...", "Value": "..."} ]
if isinstance(consul_data, list) and len(consul_data) > 0:
raw_value = consul_data[0].get("Value")
else: else:
raw_value = consul_json.get("Value") raw_value = consul_data.get("Value") if hasattr(consul_data, "get") else None
if not raw_value: if not raw_value:
return JsonResponse({"name": f"Key '{repo_name}' leer.", "type": "Root", "children": []}) return JsonResponse({
"name": f"Key '{repo_name}' geladen, aber Value ist leer.",
"type": "Root",
"children": []
})
# Base64 decodieren # Base64 decodieren
state_json = json.loads(base64.b64decode(raw_value).decode('utf-8')) state_json = json.loads(base64.b64decode(raw_value).decode('utf-8'))
@ -42,9 +54,16 @@ class ClusterGraphDataView(View):
for res in resources: for res in resources:
res_type = res.get("type") res_type = res.get("type")
res_name = res.get("name") res_name = res.get("name")
# Attribut-Struktur sicher auflösen
instances = res.get("instances", []) instances = res.get("instances", [])
if not instances or not isinstance(instances, list): continue if isinstance(instances, list) and len(instances) > 0:
attributes = instances[0].get("attributes", {}) attributes = instances[0].get("attributes", {})
else:
attributes = res.get("values", {})
if not attributes:
continue
metadata = attributes.get("metadata", [{}]) metadata = attributes.get("metadata", [{}])
actual_name = metadata[0].get("name", res_name) if isinstance(metadata, list) else metadata.get("name", res_name) actual_name = metadata[0].get("name", res_name) if isinstance(metadata, list) else metadata.get("name", res_name)
@ -85,10 +104,11 @@ class ClusterGraphDataView(View):
return JsonResponse(root_node) return JsonResponse(root_node)
except Exception as e: except Exception as e:
logger.error(f"Sicherheits-Fallback gegriffen: {str(e)}") # DIE RETTUNG: Falls IRGENDWAS crasht, fangen wir es ab und senden den Fehler als HTTP 200 Text!
# Liefert im Fehlerfall ein valides leeres Baumdiagramm (HTTP 200 OK statt 500!) error_msg = f"Fehler: {str(e)} -> Traceback: {traceback.format_exc()}"
return JsonResponse({ return JsonResponse({
"name": f"Boday-IT Monitor // Synchronisiere '{repo_name}'...", "name": "cloudmon // Diagnose-Modus",
"type": "Root", "type": "Root",
"error_details": error_msg,
"children": [] "children": []
}) })