📚 Refactored backend setup script for improved readability and maintainability

This commit is contained in:
2025-06-10 09:53:12 +02:00
parent d619b8b22f
commit ccd11f0f41

View File

@ -4966,7 +4966,7 @@ test_remote_access() {
configure_hostname() { configure_hostname() {
log "=== KONFIGURIERE HOSTNAME ===" log "=== KONFIGURIERE HOSTNAME ==="
local target_hostname="raspberrypi" local target_hostname="m040tbaraspi001"
local current_hostname=$(hostname) local current_hostname=$(hostname)
if [ "$current_hostname" != "$target_hostname" ]; then if [ "$current_hostname" != "$target_hostname" ]; then
@ -4987,7 +4987,13 @@ configure_hostname() {
echo "127.0.1.1 $target_hostname" >> /etc/hosts echo "127.0.1.1 $target_hostname" >> /etc/hosts
fi fi
# Intranet-Eintrag hinzufügen für Corporate Network Access
if ! grep -q "m040tbaraspi001.de040.corpintra.net" /etc/hosts; then
echo "127.0.1.1 $target_hostname.de040.corpintra.net" >> /etc/hosts
fi
log "✅ Hostname erfolgreich auf '$target_hostname' gesetzt" log "✅ Hostname erfolgreich auf '$target_hostname' gesetzt"
log "✅ Intranet-Zugang konfiguriert: https://m040tbaraspi001.de040.corpintra.net"
else else
log "✅ Hostname bereits korrekt: '$target_hostname'" log "✅ Hostname bereits korrekt: '$target_hostname'"
fi fi
@ -5250,7 +5256,208 @@ EOF
log " 🔄 Service Worker für Caching installiert" log " 🔄 Service Worker für Caching installiert"
} }
# =========================== HAUPTPROGRAMM (AUTOMATISCH) =========================== # =========================== INTERAKTIVES AUSWAHLMENÜ ===========================
show_main_menu() {
clear
echo -e "${CYAN}=================================================================${NC}"
echo -e "${CYAN} $APP_NAME - Setup-Skript v$APP_VERSION${NC}"
echo -e "${CYAN} Hostname: m040tbaraspi001${NC}"
echo -e "${CYAN} Intranet: https://m040tbaraspi001.de040.corpintra.net${NC}"
echo -e "${CYAN}=================================================================${NC}"
echo ""
echo -e "${YELLOW}🚀 INSTALLATIONS-OPTIONEN:${NC}"
echo ""
echo -e "${GREEN}[1]${NC} ${BLUE}Abhängigkeiten installieren${NC}"
echo -e " → Python 3.11, Node.js, SSL-Zertifikate"
echo -e " → Flask-Anwendung deployen und konfigurieren"
echo -e " → System für manuelle Tests vorbereiten"
echo ""
echo -e "${GREEN}[2]${NC} ${BLUE}Produktionsbetrieb einrichten${NC}"
echo -e " → Vollständige Kiosk-Installation mit Autostart"
echo -e " → Remote-Zugang (RDP/SSH) konfigurieren"
echo -e " → Automatischer Browser-Start beim Boot"
echo -e " → Performance-Optimierungen für Raspberry Pi"
echo ""
echo -e "${GREEN}[3]${NC} ${YELLOW}System-Status anzeigen${NC}"
echo -e " → Aktuelle Konfiguration prüfen"
echo -e " → Services und Logs anzeigen"
echo ""
echo -e "${GREEN}[4]${NC} ${YELLOW}Credentials anpassen${NC}"
echo -e " → Passwörter und Zugangsdaten ändern"
echo ""
echo -e "${GREEN}[0]${NC} ${RED}Beenden${NC}"
echo ""
echo -e "${CYAN}=================================================================${NC}"
echo -n "Ihre Auswahl [0-4]: "
}
handle_menu_selection() {
local choice=$1
case $choice in
1)
log "🔧 Benutzer wählte: Abhängigkeiten installieren"
install_dependencies_interactive
;;
2)
log "🚀 Benutzer wählte: Produktionsbetrieb einrichten"
install_production_interactive
;;
3)
log "📊 Benutzer wählte: System-Status anzeigen"
show_system_status
;;
4)
log "🔑 Benutzer wählte: Credentials anpassen"
interactive_credentials_setup
save_credentials_override
;;
0)
log "👋 Benutzer beendet das Setup"
echo -e "\n${GREEN}Auf Wiedersehen! 👋${NC}"
exit 0
;;
*)
warning "❌ Ungültige Auswahl: $choice"
echo -e "\n${RED}Bitte wählen Sie eine Zahl zwischen 0 und 4.${NC}"
sleep 2
;;
esac
}
install_dependencies_interactive() {
clear
echo -e "${CYAN}=================================================================${NC}"
echo -e "${CYAN} ABHÄNGIGKEITEN INSTALLIEREN${NC}"
echo -e "${CYAN}=================================================================${NC}"
echo ""
echo -e "${YELLOW}📦 Was wird installiert:${NC}"
echo -e " • Python 3.11 und Flask-Abhängigkeiten"
echo -e " • Node.js und npm"
echo -e " • SSL-Zertifikate für HTTPS"
echo -e " • MYP-Anwendung nach /opt/myp"
echo -e " • Systemd-Services für automatischen Start"
echo ""
echo -n "Möchten Sie fortfahren? (j/N): "
read -r confirm
if [[ "$confirm" =~ ^[jJyY]$ ]]; then
log "✅ Benutzer bestätigte Abhängigkeiten-Installation"
install_dependencies_only_auto
echo ""
echo -e "${GREEN}✅ Abhängigkeiten erfolgreich installiert!${NC}"
echo -e "${BLUE}🌐 Anwendung verfügbar unter: $HTTP_URL${NC}"
echo ""
echo -n "Drücken Sie Enter um zum Hauptmenü zurückzukehren..."
read
else
log "❌ Benutzer brach Abhängigkeiten-Installation ab"
echo -e "${YELLOW}Installation abgebrochen.${NC}"
sleep 1
fi
}
install_production_interactive() {
clear
echo -e "${CYAN}=================================================================${NC}"
echo -e "${CYAN} PRODUKTIONSBETRIEB EINRICHTEN${NC}"
echo -e "${CYAN}=================================================================${NC}"
echo ""
echo -e "${YELLOW}🚀 Was wird konfiguriert:${NC}"
echo -e " • Vollständige Kiosk-Installation"
echo -e " • Automatischer Browser-Start beim Boot"
echo -e " • Remote-Zugang (RDP/SSH)"
echo -e " • Firewall und Sicherheitskonfiguration"
echo -e " • Performance-Optimierungen"
echo -e " • Hostname: m040tbaraspi001"
echo -e " • Intranet-Zugang: https://m040tbaraspi001.de040.corpintra.net"
echo ""
echo -e "${RED}⚠️ WARNUNG: System wird nach Installation automatisch neu gestartet!${NC}"
echo ""
echo -n "Möchten Sie den Produktionsbetrieb einrichten? (j/N): "
read -r confirm
if [[ "$confirm" =~ ^[jJyY]$ ]]; then
log "✅ Benutzer bestätigte Produktions-Installation"
install_full_production_system_auto
else
log "❌ Benutzer brach Produktions-Installation ab"
echo -e "${YELLOW}Installation abgebrochen.${NC}"
sleep 1
fi
}
show_system_status() {
clear
echo -e "${CYAN}=================================================================${NC}"
echo -e "${CYAN} SYSTEM-STATUS${NC}"
echo -e "${CYAN}=================================================================${NC}"
echo ""
# Grundlegende System-Informationen
echo -e "${YELLOW}🖥️ System-Informationen:${NC}"
echo -e " Hostname: $(hostname)"
echo -e " Betriebssystem: $(lsb_release -d 2>/dev/null | cut -f2 || echo 'Unbekannt')"
echo -e " Kernel: $(uname -r)"
echo -e " Architektur: $(uname -m)"
echo -e " RAM: $(free -h | awk '/^Mem:/ {print $2}')"
echo ""
# Anwendungs-Status
echo -e "${YELLOW}📦 Anwendungs-Status:${NC}"
if [ -d "$APP_DIR" ]; then
echo -e " ✅ Anwendung installiert: $APP_DIR"
if [ -f "$APP_DIR/app.py" ]; then
echo -e " ✅ Flask-App gefunden"
else
echo -e " ❌ Flask-App nicht gefunden"
fi
else
echo -e " ❌ Anwendung nicht installiert"
fi
echo ""
# Service-Status
echo -e "${YELLOW}⚙️ Service-Status:${NC}"
local services=("$HTTP_SERVICE_NAME" "$KIOSK_SERVICE_NAME" "ssh" "xrdp")
for service in "${services[@]}"; do
if systemctl is-active --quiet "$service" 2>/dev/null; then
echo -e "$service: Aktiv"
elif systemctl list-unit-files | grep -q "$service" 2>/dev/null; then
echo -e "$service: Inaktiv"
else
echo -e "$service: Nicht installiert"
fi
done
echo ""
# Netzwerk-Status
echo -e "${YELLOW}🌐 Netzwerk-Status:${NC}"
local ip=$(ip route get 1 2>/dev/null | awk '{print $7}' | head -1)
if [ -n "$ip" ]; then
echo -e " IP-Adresse: $ip"
echo -e " Webapp-URL: http://$ip:5000"
echo -e " Intranet-URL: https://m040tbaraspi001.de040.corpintra.net"
else
echo -e " ❌ Keine Netzwerkverbindung"
fi
echo ""
# Log-Dateien
echo -e "${YELLOW}📄 Log-Dateien:${NC}"
if [ -f "$INSTALL_LOG" ]; then
echo -e " 📄 Installation: $INSTALL_LOG"
fi
if [ -f "$ERROR_LOG" ] && [ -s "$ERROR_LOG" ]; then
echo -e " 🚨 Fehler: $ERROR_LOG"
fi
echo ""
echo -n "Drücken Sie Enter um zum Hauptmenü zurückzukehren..."
read
}
# =========================== HAUPTPROGRAMM (INTERAKTIV) ===========================
main() { main() {
# Stelle sicher, dass wir im richtigen Verzeichnis sind # Stelle sicher, dass wir im richtigen Verzeichnis sind
cd "$CURRENT_DIR" 2>/dev/null || true cd "$CURRENT_DIR" 2>/dev/null || true
@ -5258,41 +5465,43 @@ main() {
# Logging initialisieren # Logging initialisieren
init_logging init_logging
log "=== MYP VOLLAUTOMATISCHES SETUP GESTARTET ===" log "=== MYP INTERAKTIVES SETUP GESTARTET ==="
log "Version: $APP_VERSION" log "Version: $APP_VERSION"
log "Arbeitsverzeichnis: $CURRENT_DIR" log "Arbeitsverzeichnis: $CURRENT_DIR"
# Grundlegende Validierung # Grundlegende Validierung
check_root check_root
# Credentials validieren # Credentials laden
load_dynamic_credentials
validate_credentials_comprehensive validate_credentials_comprehensive
# Automatische Modus-Erkennung # Automatische Modus-Erkennung nur für Parameter
detect_installation_mode "$@" detect_installation_mode "$@"
# Banner anzeigen # Falls Kommandozeilen-Parameter gesetzt sind, direkt ausführen
show_installation_banner if [ "$FORCE_PRODUCTION" = true ]; then
log "🚀 Starte PRODUKTIONS-INSTALLATION (Parameter)..."
install_full_production_system_auto
show_final_summary
log "✅ MYP SETUP ABGESCHLOSSEN"
return
elif [ "$FORCE_DEVELOPMENT" = true ]; then
log "🔧 Starte ENTWICKLUNGS-INSTALLATION (Parameter)..."
install_dependencies_only_auto
show_final_summary
log "✅ MYP SETUP ABGESCHLOSSEN"
return
fi
# Installation basierend auf erkanntem Modus # Interaktives Menü
case "$AUTO_INSTALL_MODE" in while true; do
"production") show_main_menu
log "🚀 Starte PRODUKTIONS-INSTALLATION..." read -r choice
install_full_production_system_auto echo ""
;; handle_menu_selection "$choice"
"development") echo ""
log "🔧 Starte ENTWICKLUNGS-INSTALLATION..." done
install_dependencies_only_auto
;;
*)
error "Unbekannter Installationsmodus: $AUTO_INSTALL_MODE"
;;
esac
# Finale Zusammenfassung
show_final_summary
log "✅ MYP VOLLAUTOMATISCHES SETUP ABGESCHLOSSEN"
} }
show_installation_banner() { show_installation_banner() {