#!/bin/bash # YourChat Konfigurations-Update Script # Ergänzt fehlende Einträge in der Konfigurationsdatei ohne bestehende zu überschreiben set -e # Beende bei Fehlern echo "=== YourChat - Konfigurations-Update Script ===" CONFIG_SOURCE="config/chatconfig.json" CONFIG_TARGET="/etc/yourpart/chatconfig.json" # Korrigiere falls die Datei am falschen Ort liegt if [ -f "/opt/yourchat/chatconfig.json" ] && [ ! -f "$CONFIG_TARGET" ]; then echo "Korrigiere Konfigurationsdatei-Pfad..." sudo mv /opt/yourchat/chatconfig.json "$CONFIG_TARGET" sudo chown yourchat:yourchat "$CONFIG_TARGET" sudo chmod 644 "$CONFIG_TARGET" fi # Prüfe ob Quell-Konfiguration existiert if [ ! -f "$CONFIG_SOURCE" ]; then echo "Fehler: Quell-Konfiguration $CONFIG_SOURCE nicht gefunden." exit 1 fi # Prüfe ob Ziel-Konfiguration existiert if [ ! -f "$CONFIG_TARGET" ]; then echo "Ziel-Konfiguration nicht gefunden. Kopiere komplette Konfiguration..." sudo cp "$CONFIG_SOURCE" "$CONFIG_TARGET" sudo chown yourchat:yourchat "$CONFIG_TARGET" sudo chmod 644 "$CONFIG_TARGET" echo "Konfiguration kopiert." exit 0 fi echo "=== Aktualisiere Konfiguration (fehlende Einträge ergänzen) ===" # Temporäre Dateien erstellen TEMP_SOURCE=$(mktemp) TEMP_TARGET=$(mktemp) TEMP_MERGED=$(mktemp) # JSON-Dateien in temporäre Dateien kopieren cp "$CONFIG_SOURCE" "$TEMP_SOURCE" cp "$CONFIG_TARGET" "$TEMP_TARGET" # Python-Script für JSON-Merge und connectstring-Parsing erstellen cat > /tmp/merge_config.py << 'EOF' #!/usr/bin/env python3 import json import sys import re def parse_connectstring(connectstring): """Parst einen connectstring und extrahiert host, port und database""" # Verschiedene Formate unterstützen: # - localhost:5432/yp3 # - tsschulz.de:1521/yourpart # - host:port/database if not connectstring: return None, None, None # Regex für host:port/database Format match = re.match(r'^([^:]+):(\d+)/(.+)$', connectstring) if match: host = match.group(1) port = match.group(2) database = match.group(3) return host, port, database return None, None, None def merge_json(source_file, target_file, output_file): """Führt JSON-Dateien zusammen, ohne bestehende Einträge zu überschreiben""" with open(source_file, 'r') as f: source_data = json.load(f) with open(target_file, 'r') as f: target_data = json.load(f) def merge_dict(source, target): """Rekursiv Dictionaries zusammenführen""" result = target.copy() for key, value in source.items(): if key not in result: # Neuer Schlüssel - hinzufügen result[key] = value elif isinstance(value, dict) and isinstance(result[key], dict): # Beide sind Dictionaries - rekursiv zusammenführen result[key] = merge_dict(value, result[key]) elif isinstance(value, list) and isinstance(result[key], list): # Beide sind Listen - nur neue Einträge hinzufügen existing_items = set() for item in result[key]: if isinstance(item, dict) and 'name' in item: existing_items.add(item['name']) else: existing_items.add(str(item)) for item in value: if isinstance(item, dict) and 'name' in item: if item['name'] not in existing_items: result[key].append(item) else: if str(item) not in existing_items: result[key].append(item) # Für andere Typen: bestehenden Wert beibehalten return result merged_data = merge_dict(source_data, target_data) # Spezielle Behandlung für database-Konfiguration if 'database' in merged_data and isinstance(merged_data['database'], dict): db_config = merged_data['database'] # Falls connectstring vorhanden ist, aber host/database/port fehlen if 'connectstring' in db_config and ('host' not in db_config or 'database' not in db_config or 'port' not in db_config): connectstring = db_config.get('connectstring', '') host, port, database = parse_connectstring(connectstring) if host and port and database: print(f"Parsing connectstring '{connectstring}':") print(f" -> host: {host}") print(f" -> port: {port}") print(f" -> database: {database}") # Neue Felder hinzufügen (nur falls nicht vorhanden) if 'host' not in db_config: db_config['host'] = host if 'port' not in db_config: db_config['port'] = port if 'database' not in db_config: db_config['database'] = database with open(output_file, 'w') as f: json.dump(merged_data, f, indent=4, ensure_ascii=False) return merged_data if __name__ == "__main__": if len(sys.argv) != 4: print("Verwendung: python3 merge_config.py ") sys.exit(1) source_file = sys.argv[1] target_file = sys.argv[2] output_file = sys.argv[3] try: merged_data = merge_json(source_file, target_file, output_file) print("Konfiguration erfolgreich zusammengeführt!") # Zeige Änderungen an print("\nNeue/aktualisierte Einträge:") with open(target_file, 'r') as f: original_data = json.load(f) def show_changes(source, target, path=""): for key, value in source.items(): current_path = f"{path}.{key}" if path else key if key not in target: print(f" + {current_path}: {value}") elif isinstance(value, dict) and isinstance(target[key], dict): show_changes(value, target[key], current_path) elif isinstance(value, list) and isinstance(target[key], list): # Zeige neue Listeneinträge existing_names = set() for item in target[key]: if isinstance(item, dict) and 'name' in item: existing_names.add(item['name']) for item in value: if isinstance(item, dict) and 'name' in item: if item['name'] not in existing_names: print(f" + {current_path}: {item}") show_changes(merged_data, original_data) except Exception as e: print(f"Fehler beim Zusammenführen: {e}") sys.exit(1) EOF # Python-Script ausführbar machen und ausführen chmod +x /tmp/merge_config.py echo "Führe Konfigurationen zusammen..." python3 /tmp/merge_config.py "$TEMP_SOURCE" "$TEMP_TARGET" "$TEMP_MERGED" # Backup der alten Konfiguration erstellen echo "Erstelle Backup der alten Konfiguration..." sudo cp "$CONFIG_TARGET" "${CONFIG_TARGET}.backup.$(date +%Y%m%d_%H%M%S)" # Neue Konfiguration installieren echo "Installiere neue Konfiguration..." sudo cp "$TEMP_MERGED" "$CONFIG_TARGET" sudo chown yourchat:yourchat "$CONFIG_TARGET" sudo chmod 644 "$CONFIG_TARGET" # Aufräumen rm -f "$TEMP_SOURCE" "$TEMP_TARGET" "$TEMP_MERGED" /tmp/merge_config.py echo "=== Konfigurations-Update abgeschlossen! ===" echo "Backup erstellt: ${CONFIG_TARGET}.backup.*" echo "" echo "Service neu starten mit: sudo systemctl restart yourchat"