Files
yourpart3/cmake/install-config.cmake
Torsten Schulz (local) 753c5929e1 Refactor configuration file installation and template handling
- Update CMakeLists.txt to install the template configuration file as an example, ensuring it is available for reference.
- Modify install-config.cmake to prioritize the installed template file, with fallbacks to source directory templates if the installed one is missing, enhancing the robustness of the configuration setup.
2026-01-14 14:38:43 +01:00

160 lines
5.6 KiB
CMake

# CMake-Skript für intelligente Konfigurationsdatei-Installation
# Fügt nur fehlende Keys hinzu, ohne bestehende Konfiguration zu überschreiben
# Pfade setzen
set(CONFIG_FILE "/etc/yourpart/daemon.conf")
set(TEMPLATE_FILE "/etc/yourpart/daemon.conf.example")
# Prüfe ob Template existiert (wurde von CMake installiert)
if(NOT EXISTS "${TEMPLATE_FILE}")
# Fallback 1: Versuche Template im Source-Verzeichnis zu finden
# CMAKE_CURRENT_LIST_DIR zeigt auf cmake/ während der Installation
get_filename_component(PROJECT_ROOT "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
set(TEMPLATE_FILE_FALLBACK "${PROJECT_ROOT}/daemon.conf")
# Fallback 2: Versuche über CMAKE_SOURCE_DIR (falls verfügbar)
if(DEFINED CMAKE_SOURCE_DIR AND EXISTS "${CMAKE_SOURCE_DIR}/daemon.conf")
set(TEMPLATE_FILE "${CMAKE_SOURCE_DIR}/daemon.conf")
message(STATUS "Verwende Template aus CMAKE_SOURCE_DIR: ${TEMPLATE_FILE}")
elseif(EXISTS "${TEMPLATE_FILE_FALLBACK}")
set(TEMPLATE_FILE "${TEMPLATE_FILE_FALLBACK}")
message(STATUS "Verwende Template aus Source-Verzeichnis: ${TEMPLATE_FILE}")
else()
message(FATAL_ERROR "Template-Datei nicht gefunden!")
message(FATAL_ERROR " Gesucht in: ${TEMPLATE_FILE}")
message(FATAL_ERROR " Fallback 1: ${TEMPLATE_FILE_FALLBACK}")
if(DEFINED CMAKE_SOURCE_DIR)
message(FATAL_ERROR " Fallback 2: ${CMAKE_SOURCE_DIR}/daemon.conf")
endif()
endif()
else()
message(STATUS "Verwende installierte Template-Datei: ${TEMPLATE_FILE}")
endif()
# Prüfe ob Ziel-Verzeichnis existiert
if(NOT EXISTS "/etc/yourpart")
message(STATUS "Erstelle Verzeichnis /etc/yourpart...")
execute_process(
COMMAND ${CMAKE_COMMAND} -E make_directory "/etc/yourpart"
COMMAND_ERROR_IS_FATAL ANY
)
endif()
# Prüfe ob Config-Datei existiert
if(NOT EXISTS "${CONFIG_FILE}")
message(STATUS "Konfigurationsdatei existiert nicht, erstelle neue...")
execute_process(
COMMAND ${CMAKE_COMMAND} -E copy "${TEMPLATE_FILE}" "${CONFIG_FILE}"
COMMAND_ERROR_IS_FATAL ANY
)
message(STATUS "Neue Konfigurationsdatei erstellt: ${CONFIG_FILE}")
else()
message(STATUS "Konfigurationsdatei existiert bereits, prüfe auf fehlende Keys...")
# Verwende ein Python-Skript für intelligentes Merging
# (CMake hat keine gute Unterstützung für komplexe String-Manipulation)
# Erstelle temporäres Python-Skript im Build-Verzeichnis
set(MERGE_SCRIPT "${CMAKE_CURRENT_BINARY_DIR}/merge-config.py")
# Erstelle Python-Skript
file(WRITE "${MERGE_SCRIPT}"
"#!/usr/bin/env python3
import sys
import re
import os
def merge_config(template_file, config_file):
\"\"\"Fügt fehlende Keys aus Template zur Config hinzu, ohne bestehende zu überschreiben\"\"\"
# Lese bestehende Config
existing_keys = {}
existing_lines = []
if os.path.exists(config_file):
with open(config_file, 'r') as f:
for line in f:
existing_lines.append(line.rstrip())
# Extrahiere Key=Value Paare
match = re.match(r'^\\s*([^#=]+?)\\s*=\\s*(.+?)\\s*$', line)
if match:
key = match.group(1).strip()
value = match.group(2).strip()
existing_keys[key] = value
# Lese Template
new_keys = {}
if not os.path.exists(template_file):
print(f'Fehler: Template-Datei {template_file} nicht gefunden!', file=sys.stderr)
return False
with open(template_file, 'r') as f:
for line in f:
# Extrahiere Key=Value Paare
match = re.match(r'^\\s*([^#=]+?)\\s*=\\s*(.+?)\\s*$', line)
if match:
key = match.group(1).strip()
value = match.group(2).strip()
new_keys[key] = value
# Füge fehlende Keys hinzu
added_count = 0
for key, value in new_keys.items():
if key not in existing_keys:
existing_lines.append(f'{key}={value}')
print(f'Füge fehlenden Key hinzu: {key}')
added_count += 1
# Schreibe aktualisierte Config
if added_count > 0:
with open(config_file, 'w') as f:
for line in existing_lines:
f.write(line + '\\n')
print(f'{added_count} neue Keys hinzugefügt')
else:
print('Keine neuen Keys hinzugefügt - Konfiguration ist aktuell')
return True
if __name__ == '__main__':
if len(sys.argv) != 3:
print('Verwendung: merge-config.py <template> <config>', file=sys.stderr)
sys.exit(1)
template_file = sys.argv[1]
config_file = sys.argv[2]
if not merge_config(template_file, config_file):
sys.exit(1)
")
# Setze Ausführungsrechte
file(CHMOD "${MERGE_SCRIPT}" PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
# Führe Merge-Skript aus
execute_process(
COMMAND python3 "${MERGE_SCRIPT}" "${TEMPLATE_FILE}" "${CONFIG_FILE}"
RESULT_VARIABLE MERGE_RESULT
OUTPUT_VARIABLE MERGE_OUTPUT
ERROR_VARIABLE MERGE_ERROR
)
if(NOT MERGE_RESULT EQUAL 0)
message(WARNING "Fehler beim Mergen der Config: ${MERGE_ERROR}")
else()
message(STATUS "${MERGE_OUTPUT}")
endif()
endif()
# Setze korrekte Berechtigungen
execute_process(
COMMAND chown yourpart:yourpart "${CONFIG_FILE}"
COMMAND_ERROR_IS_FATAL FALSE
)
execute_process(
COMMAND chmod 600 "${CONFIG_FILE}"
COMMAND_ERROR_IS_FATAL FALSE
)
message(STATUS "Konfigurationsdatei-Verwaltung abgeschlossen: ${CONFIG_FILE}")