Files
yourchat/deploy/update_config.sh
Torsten Schulz (local) f5a5f5ae2c Aktualisiere Installations- und Konfigurationsskripte zur Verwendung eines neuen Pfads für die Konfigurationsdatei
- Ändere den Zielpfad der Konfigurationsdatei in `install.sh` und `update_config.sh` von `/opt/yourchat/config/chatconfig.json` nach `/etc/yourpart/chatconfig.json`.
- Füge die Erstellung des Verzeichnisses `/etc/yourpart` im Installationsskript hinzu und passe die Berechtigungen entsprechend an.
2025-09-01 16:39:52 +02:00

169 lines
5.9 KiB
Bash
Executable File

#!/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 erstellen
cat > /tmp/merge_config.py << 'EOF'
#!/usr/bin/env python3
import json
import sys
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)
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 <source> <target> <output>")
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"