🔧 Aktualisiert: setup.sh zur Unterstützung der intelligenten Pfad-Erkennung und Anpassung an Git-Clone-Umgebungen, einschließlich verbesserter Logging-Mechanismen und robustem Deployment-Prozess. 🚀

This commit is contained in:
2025-06-10 11:15:20 +02:00
parent f00198acda
commit faf3e2c522

View File

@ -22,7 +22,39 @@ readonly WATCHDOG_SERVICE_NAME="kiosk-watchdog"
readonly WATCHDOG_PYTHON_SERVICE_NAME="kiosk-watchdog-python"
readonly FIREWALL_SERVICE_NAME="myp-firewall"
readonly KIOSK_USER="kiosk"
readonly CURRENT_DIR="$(pwd)"
# =========================== INTELLIGENTE PFAD-ERKENNUNG ===========================
# Erkenne ob Skript im Git-Clone (backend/) oder Produktions-Verzeichnis läuft
detect_script_location() {
local script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
local script_basename="$(basename "$script_dir")"
if [[ "$script_basename" == "backend" ]]; then
# Skript läuft im Git-Clone backend/ Verzeichnis
readonly GIT_CLONE_MODE=true
readonly CURRENT_DIR="$script_dir"
readonly SOURCE_BACKEND_DIR="$script_dir"
readonly PROJECT_ROOT_DIR="$(dirname "$script_dir")"
log "🔍 GIT-CLONE-MODUS ERKANNT"
log " 📁 Skript-Verzeichnis: $CURRENT_DIR"
log " 📁 Backend-Quelle: $SOURCE_BACKEND_DIR"
log " 📁 Projekt-Root: $PROJECT_ROOT_DIR"
log " 🎯 Ziel: $APP_DIR"
else
# Skript läuft bereits im Produktions-Verzeichnis
readonly GIT_CLONE_MODE=false
readonly CURRENT_DIR="$script_dir"
readonly SOURCE_BACKEND_DIR="$script_dir"
readonly PROJECT_ROOT_DIR="$script_dir"
log "🏠 PRODUKTIONS-MODUS ERKANNT"
log " 📁 Produktions-Verzeichnis: $CURRENT_DIR"
fi
}
# Führe Pfad-Erkennung aus
detect_script_location
# Automatische Installationsmodus-Erkennung
AUTO_INSTALL_MODE=""
@ -60,8 +92,8 @@ load_dynamic_credentials() {
MYP_GITHUB_CLIENT_ID="${MYP_GITHUB_CLIENT_ID_OVERRIDE:-$MYP_GITHUB_CLIENT_ID}"
MYP_GITHUB_CLIENT_SECRET="${MYP_GITHUB_CLIENT_SECRET_OVERRIDE:-$MYP_GITHUB_CLIENT_SECRET}"
# Credentials aus externer Datei laden (falls vorhanden)
local credentials_file="$CURRENT_DIR/CREDENTIALS_OVERRIDE.env"
# Credentials aus externer Datei laden (Git-Clone-bewusst)
local credentials_file="$SOURCE_BACKEND_DIR/CREDENTIALS_OVERRIDE.env"
if [ -f "$credentials_file" ]; then
progress "Lade Credentials aus: $credentials_file"
source "$credentials_file"
@ -170,7 +202,7 @@ interactive_credentials_setup() {
# Funktion: Credentials in Override-Datei speichern
save_credentials_override() {
local override_file="$CURRENT_DIR/CREDENTIALS_OVERRIDE.env"
local override_file="$SOURCE_BACKEND_DIR/CREDENTIALS_OVERRIDE.env"
progress "Speichere Credentials-Override in: $override_file"
cat > "$override_file" << EOF
@ -430,13 +462,13 @@ validate_credentials_comprehensive() {
}
# =========================== LOG-DATEIEN INITIALISIERUNG ===========================
# Log-Dateien - verwende relatives logs-Verzeichnis (falls noch nicht definiert)
# Log-Dateien - verwende relatives logs-Verzeichnis (Git-Clone-bewusst)
if [ -z "$INSTALL_LOG" ]; then
mkdir -p "$CURRENT_DIR/logs" 2>/dev/null || true
INSTALL_LOG="$CURRENT_DIR/logs/install.log"
ERROR_LOG="$CURRENT_DIR/logs/errors.log"
WARNING_LOG="$CURRENT_DIR/logs/warnings.log"
DEBUG_LOG="$CURRENT_DIR/logs/debug.log"
mkdir -p "$SOURCE_BACKEND_DIR/logs" 2>/dev/null || true
INSTALL_LOG="$SOURCE_BACKEND_DIR/logs/install.log"
ERROR_LOG="$SOURCE_BACKEND_DIR/logs/errors.log"
WARNING_LOG="$SOURCE_BACKEND_DIR/logs/warnings.log"
DEBUG_LOG="$SOURCE_BACKEND_DIR/logs/debug.log"
fi
# =========================== VERBESSERTE LOGGING-FUNKTIONEN ===========================
@ -446,8 +478,8 @@ WARNING_COUNT=0
# Log-Dateien initialisieren
init_logging() {
# Sichere Log-Verzeichnis-Erstellung
if ! mkdir -p "$CURRENT_DIR/logs" 2>/dev/null; then
# Sichere Log-Verzeichnis-Erstellung (Git-Clone-bewusst)
if ! mkdir -p "$SOURCE_BACKEND_DIR/logs" 2>/dev/null; then
echo "FEHLER: Kann logs-Verzeichnis nicht erstellen - verwende /tmp" >&2
INSTALL_LOG="/tmp/myp-install.log"
ERROR_LOG="/tmp/myp-install-errors.log"
@ -461,7 +493,10 @@ init_logging() {
echo "MYP Installation Log - $(date '+%Y-%m-%d %H:%M:%S')"
echo "Script Version: $APP_VERSION"
echo "System: $(timeout 5 uname -a 2>/dev/null || echo 'System-Info nicht verfügbar')"
echo "Git-Clone-Modus: $GIT_CLONE_MODE"
echo "Backend-Quelle: $SOURCE_BACKEND_DIR"
echo "Arbeitsverzeichnis: $CURRENT_DIR"
echo "Ziel-Verzeichnis: $APP_DIR"
echo "Log-Verzeichnis: $(dirname "$INSTALL_LOG")"
echo "================================================================="
echo ""
@ -1356,8 +1391,8 @@ EOF
create_clean_requirements() {
log "=== ERSTELLE BEREINIGTE REQUIREMENTS.TXT ==="
local original_req="$CURRENT_DIR/requirements.txt"
local clean_req="$CURRENT_DIR/requirements_clean.txt"
local original_req="$SOURCE_BACKEND_DIR/requirements.txt"
local clean_req="$SOURCE_BACKEND_DIR/requirements_clean.txt"
# Problematische Pakete, die übersprungen werden sollen
local problematic_packages=(
@ -1493,7 +1528,7 @@ install_python_packages() {
local install_success=false
# Strategie 1: Bereinigte requirements.txt mit --break-system-packages
if python3 -m pip install -r "$CURRENT_DIR/requirements_clean.txt" --break-system-packages --no-cache-dir; then
if python3 -m pip install -r "$SOURCE_BACKEND_DIR/requirements_clean.txt" --break-system-packages --no-cache-dir; then
install_success=true
success "✅ Bereinigte requirements.txt erfolgreich installiert"
else
@ -1506,7 +1541,7 @@ install_python_packages() {
fi
# Aufräumen
rm -f "$CURRENT_DIR/requirements_clean.txt" 2>/dev/null || true
rm -f "$SOURCE_BACKEND_DIR/requirements_clean.txt" 2>/dev/null || true
# Validiere essenzielle Module
progress "Validiere essenzielle Python-Module..."
@ -1555,13 +1590,13 @@ install_python_packages_with_break_system() {
progress "Installiere Python-Pakete mit --break-system-packages..."
if [ ! -f "$CURRENT_DIR/requirements.txt" ]; then
error "requirements.txt nicht gefunden: $CURRENT_DIR/requirements.txt"
if [ ! -f "$SOURCE_BACKEND_DIR/requirements.txt" ]; then
error "requirements.txt nicht gefunden: $SOURCE_BACKEND_DIR/requirements.txt"
return 1
fi
# Kopiere requirements.txt
cp "$CURRENT_DIR/requirements.txt" "$APP_DIR/" 2>/dev/null || true
cp "$SOURCE_BACKEND_DIR/requirements.txt" "$APP_DIR/" 2>/dev/null || true
# Installiere alle Pakete aus requirements.txt mit --break-system-packages
progress "Installiere requirements.txt mit --break-system-packages..."
@ -1570,12 +1605,12 @@ install_python_packages_with_break_system() {
local install_success=false
# Erstelle bereinigte requirements.txt für Installation
if [ ! -f "$CURRENT_DIR/requirements_clean.txt" ]; then
if [ ! -f "$SOURCE_BACKEND_DIR/requirements_clean.txt" ]; then
create_clean_requirements
fi
# Strategie 1: Mit --break-system-packages und bereinigter requirements.txt
if python3.11 -m pip install -r "$CURRENT_DIR/requirements_clean.txt" --break-system-packages --force-reinstall --no-cache-dir; then
if python3.11 -m pip install -r "$SOURCE_BACKEND_DIR/requirements_clean.txt" --break-system-packages --force-reinstall --no-cache-dir; then
install_success=true
success "✅ Bereinigte requirements.txt mit --break-system-packages erfolgreich installiert"
else
@ -1585,7 +1620,7 @@ install_python_packages_with_break_system() {
progress "Installiere bereinigte Pakete einzeln..."
# Erstelle bereinigte requirements.txt falls nicht vorhanden
if [ ! -f "$CURRENT_DIR/requirements_clean.txt" ]; then
if [ ! -f "$SOURCE_BACKEND_DIR/requirements_clean.txt" ]; then
create_clean_requirements
fi
@ -1607,10 +1642,10 @@ install_python_packages_with_break_system() {
warning "⚠️ $package_name Installation fehlgeschlagen"
fi
fi
done < "$CURRENT_DIR/requirements_clean.txt"
done < "$SOURCE_BACKEND_DIR/requirements_clean.txt"
# Aufräumen
rm -f "$CURRENT_DIR/requirements_clean.txt" 2>/dev/null || true
rm -f "$SOURCE_BACKEND_DIR/requirements_clean.txt" 2>/dev/null || true
install_success=true
fi
@ -3247,68 +3282,90 @@ EOF' || warning "SSL-Umgebungsvariablen timeout"
deploy_application() {
log "=== ROBUSTES ANWENDUNGS-DEPLOYMENT ==="
if [ "$GIT_CLONE_MODE" = true ]; then
log "📦 GIT-CLONE zu PRODUKTIONS-DEPLOYMENT"
log " 📂 Quelle: $SOURCE_BACKEND_DIR"
log " 🎯 Ziel: $APP_DIR"
# Prüfe ob wir bereits im Zielverzeichnis sind (Schutz vor Selbst-Überschreibung)
if [[ "$SOURCE_BACKEND_DIR" == "$APP_DIR"* ]]; then
log "✅ Bereits im Produktions-Verzeichnis - überspringe Deployment"
return 0
fi
else
log "🏠 PRODUKTIONS-VERZEICHNIS-UPDATE"
fi
progress "Erstelle sicheres Zielverzeichnis: $APP_DIR"
mkdir -p "$APP_DIR" || error "Konnte Zielverzeichnis nicht erstellen"
# Validiere Source-Verzeichnis
# Validiere Source-Verzeichnis (angepasst für Git-Clone-Modus)
progress "Validiere Source-Dateien..."
if [ ! -f "$CURRENT_DIR/app.py" ]; then
error "Kritische Datei nicht gefunden: $CURRENT_DIR/app.py"
local source_app_py="$SOURCE_BACKEND_DIR/app.py"
local source_requirements="$SOURCE_BACKEND_DIR/requirements.txt"
if [ ! -f "$source_app_py" ]; then
error "Kritische Datei nicht gefunden: $source_app_py"
fi
if [ ! -f "$CURRENT_DIR/requirements.txt" ]; then
error "Kritische Datei nicht gefunden: $CURRENT_DIR/requirements.txt"
if [ ! -f "$source_requirements" ]; then
error "Kritische Datei nicht gefunden: $source_requirements"
fi
progress "Kopiere Anwendungsdateien (robust)..."
progress "Kopiere Anwendungsdateien (Git-Clone-bewusst)..."
# Kritische Dateien zuerst (mit Validierung)
# Kritische Backend-Dateien zuerst (mit Validierung)
local critical_files=(
"app.py"
"models.py"
"requirements.txt"
"setup.sh"
)
for file in "${critical_files[@]}"; do
if [ -f "$CURRENT_DIR/$file" ]; then
local source_file="$SOURCE_BACKEND_DIR/$file"
if [ -f "$source_file" ]; then
progress "Kopiere kritische Datei: $file"
if cp "$CURRENT_DIR/$file" "$APP_DIR/" 2>/dev/null; then
if cp "$source_file" "$APP_DIR/" 2>/dev/null; then
success "$file erfolgreich kopiert"
else
error "❌ Fehler beim Kopieren der kritischen Datei: $file"
fi
else
error "❌ Kritische Datei fehlt: $file"
error "❌ Kritische Datei fehlt: $source_file"
fi
done
# Verzeichnisse mit robuster Behandlung
# Backend-Verzeichnisse mit robuster Behandlung
local directories=(
"blueprints"
"config"
"database"
"static"
"static"
"templates"
"uploads"
"utils"
"logs"
"certs"
"systemd"
"scripts"
)
for dir in "${directories[@]}"; do
if [ -d "$CURRENT_DIR/$dir" ]; then
progress "Kopiere Verzeichnis: $dir"
if cp -r "$CURRENT_DIR/$dir" "$APP_DIR/" 2>/dev/null; then
local source_dir="$SOURCE_BACKEND_DIR/$dir"
if [ -d "$source_dir" ]; then
progress "Kopiere Backend-Verzeichnis: $dir"
if cp -r "$source_dir" "$APP_DIR/" 2>/dev/null; then
success "$dir erfolgreich kopiert"
else
warning "⚠️ Fehler beim Kopieren von $dir (möglicherweise nicht kritisch)"
fi
else
info "Verzeichnis nicht vorhanden: $dir"
info "Backend-Verzeichnis nicht vorhanden: $source_dir"
fi
done
# Optionale Dateien
# Optionale Backend-Dateien
local optional_files=(
"package.json"
"package-lock.json"
@ -3316,15 +3373,44 @@ deploy_application() {
"postcss.config.js"
"README.md"
".gitignore"
"requirements_clean.txt"
"fix_ssl_raspberry.sh"
"COMMON_ERRORS.md"
"RASPBERRY_PI_SSL_FIX.md"
)
for file in "${optional_files[@]}"; do
if [ -f "$CURRENT_DIR/$file" ]; then
progress "Kopiere optionale Datei: $file"
cp "$CURRENT_DIR/$file" "$APP_DIR/" 2>/dev/null || warning "⚠️ Kopieren von $file fehlgeschlagen"
local source_file="$SOURCE_BACKEND_DIR/$file"
if [ -f "$source_file" ]; then
progress "Kopiere optionale Backend-Datei: $file"
cp "$source_file" "$APP_DIR/" 2>/dev/null || warning "⚠️ Kopieren von $file fehlgeschlagen"
fi
done
# Git-Clone-Modus: Kopiere auch wichtige Root-Projekt-Dateien
if [ "$GIT_CLONE_MODE" = true ]; then
progress "Kopiere Projekt-Root-Dateien..."
local root_files=(
"README.md"
"CLAUDE.md"
)
for file in "${root_files[@]}"; do
local source_file="$PROJECT_ROOT_DIR/$file"
if [ -f "$source_file" ]; then
progress "Kopiere Root-Datei: $file"
cp "$source_file" "$APP_DIR/" 2>/dev/null || warning "⚠️ Kopieren von Root-$file fehlgeschlagen"
fi
done
# Dokumentationsverzeichnis falls vorhanden
if [ -d "$PROJECT_ROOT_DIR/docs" ]; then
progress "Kopiere Dokumentation..."
cp -r "$PROJECT_ROOT_DIR/docs" "$APP_DIR/" 2>/dev/null || warning "⚠️ Kopieren der Dokumentation fehlgeschlagen"
fi
fi
# Erstelle alle notwendigen Verzeichnisse mit korrekter Struktur
progress "Erstelle Verzeichnisstruktur..."
local required_dirs=(