Files
Projektarbeit-MYP/backend/setup/aio_installer.sh

444 lines
14 KiB
Bash

#!/bin/bash
#######################################################################
# MYP AIO-Installer (All-in-One Installation System)
#
# Automatisierte Installation und Einrichtung des MYP-Systems für
# Mercedes-Benz 3D-Drucker-Management auf Debian/Linux-Systemen
#
# Autor: Till Tomczak
# Version: 1.0.0
# Datum: $(date +%Y-%m-%d)
#
# Funktionen:
# - Vollständige Systemeinrichtung
# - Desktop-Environment Installation (LXDE)
# - Chromium Kiosk-Modus
# - Python/Node.js Dependencies
# - SystemD Services
# - Firewall-Konfiguration
# - Upgrade-Funktionalität
#######################################################################
set -euo pipefail # Strenge Fehlerbehandlung
# === KONSTANTEN UND KONFIGURATION ===
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly PROJECT_ROOT="$(dirname "${SCRIPT_DIR}")"
readonly LOG_FILE="/var/log/myp-installer.log"
readonly CONFIG_FILE="${SCRIPT_DIR}/installer.conf"
# Projekt-Konfiguration
readonly PROJECT_NAME="MYP"
readonly PROJECT_USER="myp"
readonly PROJECT_GROUP="myp"
readonly INSTALL_PATH="/opt/myp"
readonly SERVICE_NAME="myp-https"
readonly KIOSK_SERVICE="myp-kiosk"
# System-Konfiguration
readonly HOSTNAME_DEFAULT="myp-terminal"
readonly CHROMIUM_FLAGS="--kiosk --disable-infobars --disable-session-crashed-bubble --disable-restore-session-state --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-renderer-backgrounding --disable-features=TranslateUI --disable-web-security --disable-features=VizDisplayCompositor --no-first-run --autoplay-policy=no-user-gesture-required"
# Farben für Ausgabe
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly BLUE='\033[0;34m'
readonly NC='\033[0m' # No Color
# === UTILITY-FUNKTIONEN ===
log() {
local level="$1"
shift
local message="$*"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
case "$level" in
"INFO") echo -e "${GREEN}[INFO]${NC} $message" | tee -a "$LOG_FILE" ;;
"WARN") echo -e "${YELLOW}[WARN]${NC} $message" | tee -a "$LOG_FILE" ;;
"ERROR") echo -e "${RED}[ERROR]${NC} $message" | tee -a "$LOG_FILE" ;;
"DEBUG") echo -e "${BLUE}[DEBUG]${NC} $message" | tee -a "$LOG_FILE" ;;
esac
echo "[$timestamp] [$level] $message" >> "$LOG_FILE"
}
check_root() {
if [[ $EUID -ne 0 ]]; then
log "ERROR" "Dieses Skript muss als root ausgeführt werden"
exit 1
fi
}
check_system() {
log "INFO" "Überprüfe Systemvoraussetzungen..."
# Debian/Ubuntu Check
if ! command -v apt &> /dev/null; then
log "ERROR" "Dieses System wird nicht unterstützt (kein APT Package Manager)"
exit 1
fi
# Internetverbindung
if ! ping -c 1 8.8.8.8 &> /dev/null; then
log "ERROR" "Keine Internetverbindung verfügbar"
exit 1
fi
# Speicherplatz (mindestens 2GB frei)
local free_space=$(df / | awk 'NR==2 {print $4}')
if [[ $free_space -lt 2097152 ]]; then
log "ERROR" "Nicht genügend Speicherplatz (mindestens 2GB erforderlich)"
exit 1
fi
log "INFO" "Systemvoraussetzungen erfüllt"
}
confirm_action() {
local message="$1"
local default="${2:-n}"
if [[ "${FORCE_YES:-}" == "true" ]]; then
return 0
fi
while true; do
read -p "$message [y/N]: " -n 1 -r
echo
case $REPLY in
[Yy]* ) return 0;;
[Nn]* ) return 1;;
"" ) [[ "$default" == "y" ]] && return 0 || return 1;;
* ) echo "Bitte mit 'y' oder 'n' antworten.";;
esac
done
}
# === INSTALLATION MODULES ===
# Lädt alle Installationsmodule
source "${SCRIPT_DIR}/modules/environment.sh"
source "${SCRIPT_DIR}/modules/packages.sh"
source "${SCRIPT_DIR}/modules/python_node.sh"
source "${SCRIPT_DIR}/modules/desktop.sh"
source "${SCRIPT_DIR}/modules/services.sh"
source "${SCRIPT_DIR}/modules/firewall.sh"
source "${SCRIPT_DIR}/modules/testing.sh"
# === HAUPTFUNKTIONEN ===
show_banner() {
clear
cat << 'EOF'
███╗ ███╗██╗ ██╗██████╗ ██╗███╗ ██╗███████╗████████╗ █████╗ ██╗ ██╗ ███████╗██████╗
████╗ ████║╚██╗ ██╔╝██╔══██╗ ██║████╗ ██║██╔════╝╚══██╔══╝██╔══██╗██║ ██║ ██╔════╝██╔══██╗
██╔████╔██║ ╚████╔╝ ██████╔╝ ██║██╔██╗ ██║███████╗ ██║ ███████║██║ ██║ █████╗ ██████╔╝
██║╚██╔╝██║ ╚██╔╝ ██╔═══╝ ██║██║╚██╗██║╚════██║ ██║ ██╔══██║██║ ██║ ██╔══╝ ██╔══██╗
██║ ╚═╝ ██║ ██║ ██║ ██║██║ ╚████║███████║ ██║ ██║ ██║███████╗███████╗███████╗██║ ██║
╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝ ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝╚═╝ ╚═╝
EOF
echo
echo " Mercedes-Benz 3D-Drucker Management System"
echo " AIO-Installer v1.0.0"
echo
echo "=================================================================================="
echo
}
show_menu() {
echo "Verfügbare Optionen:"
echo
echo "1) Vollinstallation (Empfohlen für neue Systeme)"
echo "2) Upgrade bestehender Installation"
echo "3) Nur System-Test durchführen"
echo "4) Einzelne Komponenten installieren"
echo "5) System-Information anzeigen"
echo "6) Deinstallation"
echo "0) Beenden"
echo
}
full_installation() {
log "INFO" "Starte Vollinstallation des MYP-Systems..."
if ! confirm_action "Vollinstallation durchführen? Dies wird das System erheblich verändern."; then
log "INFO" "Installation abgebrochen"
return 1
fi
# Installations-Schritte in definierter Reihenfolge
local steps=(
"setup_environment"
"update_system_packages"
"install_python_dependencies"
"install_node_dependencies"
"setup_desktop_environment"
"create_system_user"
"setup_systemd_services"
"configure_firewall"
"setup_autostart"
"test_installation"
)
local total_steps=${#steps[@]}
local current_step=0
for step in "${steps[@]}"; do
current_step=$((current_step + 1))
log "INFO" "Schritt $current_step/$total_steps: $step"
if ! $step; then
log "ERROR" "Fehler bei Schritt $step"
return 1
fi
log "INFO" "Schritt $current_step/$total_steps abgeschlossen"
done
log "INFO" "Vollinstallation erfolgreich abgeschlossen!"
show_completion_info
}
upgrade_installation() {
log "INFO" "Starte Upgrade bestehender Installation..."
if [[ ! -d "$INSTALL_PATH" ]]; then
log "ERROR" "Keine bestehende Installation gefunden in $INSTALL_PATH"
return 1
fi
# Backup erstellen
create_backup
# Upgrade-Schritte
update_system_packages
install_python_dependencies
install_node_dependencies
update_project_files
restart_services
test_installation
log "INFO" "Upgrade erfolgreich abgeschlossen!"
}
component_installation() {
echo "Einzelne Komponenten:"
echo "1) Umgebung einrichten"
echo "2) System-Packages aktualisieren"
echo "3) Python-Abhängigkeiten installieren"
echo "4) Node.js-Abhängigkeiten installieren"
echo "5) Desktop-Environment einrichten"
echo "6) Systemd-Services einrichten"
echo "7) Firewall konfigurieren"
echo "8) Tests durchführen"
echo "0) Zurück"
read -p "Komponente auswählen: " choice
case $choice in
1) setup_environment ;;
2) update_system_packages ;;
3) install_python_dependencies ;;
4) install_node_dependencies ;;
5) setup_desktop_environment ;;
6) setup_systemd_services ;;
7) configure_firewall ;;
8) test_installation ;;
0) return ;;
*) log "ERROR" "Ungültige Auswahl" ;;
esac
}
show_system_info() {
echo "=== SYSTEM-INFORMATION ==="
echo "Betriebssystem: $(lsb_release -d | cut -f2)"
echo "Kernel: $(uname -r)"
echo "Architektur: $(uname -m)"
echo "Verfügbarer Speicher: $(free -h | awk 'NR==2{print $7}')"
echo "Festplattenspeicher: $(df -h / | awk 'NR==2{print $4}')"
echo
if [[ -d "$INSTALL_PATH" ]]; then
echo "=== MYP-INSTALLATION ==="
echo "Installationspfad: $INSTALL_PATH"
echo "Status: $(systemctl is-active $SERVICE_NAME 2>/dev/null || echo 'Nicht installiert')"
echo "Version: $(cat $INSTALL_PATH/VERSION 2>/dev/null || echo 'Unbekannt')"
echo
fi
echo "=== NETZWERK ==="
echo "IP-Adresse: $(hostname -I | awk '{print $1}')"
echo "Hostname: $(hostname)"
echo
}
uninstall_system() {
log "WARN" "WARNUNG: Dies wird das gesamte MYP-System entfernen!"
if ! confirm_action "Sind Sie sicher, dass Sie das System deinstallieren möchten?"; then
return 1
fi
if ! confirm_action "LETZTE WARNUNG: Alle Daten gehen verloren! Fortfahren?"; then
return 1
fi
log "INFO" "Starte Deinstallation..."
# Services stoppen und entfernen
systemctl stop "$SERVICE_NAME" 2>/dev/null || true
systemctl stop "$KIOSK_SERVICE" 2>/dev/null || true
systemctl disable "$SERVICE_NAME" 2>/dev/null || true
systemctl disable "$KIOSK_SERVICE" 2>/dev/null || true
# Service-Dateien entfernen
rm -f "/etc/systemd/system/${SERVICE_NAME}.service"
rm -f "/etc/systemd/system/${KIOSK_SERVICE}.service"
systemctl daemon-reload
# Benutzer entfernen
if id "$PROJECT_USER" &>/dev/null; then
userdel -r "$PROJECT_USER" 2>/dev/null || true
fi
# Installationsverzeichnis entfernen
rm -rf "$INSTALL_PATH"
# Desktop-Konfiguration entfernen
rm -rf "/home/$PROJECT_USER"
log "INFO" "Deinstallation abgeschlossen"
}
show_completion_info() {
echo
echo "=================================================================================="
echo " INSTALLATION ABGESCHLOSSEN"
echo "=================================================================================="
echo
echo "Das MYP-System wurde erfolgreich installiert und konfiguriert."
echo
echo "Wichtige Informationen:"
echo
echo "• System-URL: https://$(hostname -I | awk '{print $1}')"
echo "• Installationspfad: $INSTALL_PATH"
echo "• Service: $SERVICE_NAME"
echo "• Benutzer: $PROJECT_USER"
echo "• Log-Datei: $LOG_FILE"
echo
echo "Nächste Schritte:"
echo "1. System neu starten: sudo reboot"
echo "2. Nach Neustart automatischer Kiosk-Modus"
echo "3. Admin-Zugang über: https://IP-ADRESSE/admin"
echo
echo "Bei Problemen:"
echo "• Logs prüfen: sudo journalctl -u $SERVICE_NAME -f"
echo "• Service-Status: sudo systemctl status $SERVICE_NAME"
echo "• Installer-Log: sudo cat $LOG_FILE"
echo
echo "=================================================================================="
}
create_backup() {
if [[ -d "$INSTALL_PATH" ]]; then
local backup_dir="/opt/myp-backup-$(date +%Y%m%d-%H%M%S)"
log "INFO" "Erstelle Backup in $backup_dir"
cp -r "$INSTALL_PATH" "$backup_dir"
log "INFO" "Backup erstellt: $backup_dir"
fi
}
# === HAUPTPROGRAMM ===
main() {
# Log-Datei initialisieren
touch "$LOG_FILE"
chmod 644 "$LOG_FILE"
log "INFO" "MYP AIO-Installer gestartet"
# Root-Rechte prüfen
check_root
# System prüfen
check_system
# Banner anzeigen
show_banner
# Hauptmenü
while true; do
show_menu
read -p "Option auswählen: " choice
case $choice in
1) full_installation ;;
2) upgrade_installation ;;
3) test_installation ;;
4) component_installation ;;
5) show_system_info ;;
6) uninstall_system ;;
0)
log "INFO" "Installation beendet"
exit 0
;;
*)
log "ERROR" "Ungültige Auswahl: $choice"
;;
esac
echo
read -p "Drücken Sie Enter zum Fortfahren..."
clear
show_banner
done
}
# Kommandozeilen-Argumente verarbeiten
while [[ $# -gt 0 ]]; do
case $1 in
--full)
FORCE_YES="true"
full_installation
exit $?
;;
--upgrade)
FORCE_YES="true"
upgrade_installation
exit $?
;;
--test)
test_installation
exit $?
;;
--uninstall)
uninstall_system
exit $?
;;
--help|-h)
echo "MYP AIO-Installer"
echo "Verwendung: $0 [OPTION]"
echo
echo "Optionen:"
echo " --full Vollinstallation ohne Bestätigung"
echo " --upgrade Upgrade bestehender Installation"
echo " --test Nur Tests durchführen"
echo " --uninstall System deinstallieren"
echo " --help, -h Diese Hilfe anzeigen"
exit 0
;;
*)
log "ERROR" "Unbekannte Option: $1"
exit 1
;;
esac
shift
done
# Hauptprogramm ausführen wenn keine Kommandozeilen-Argumente
main
exit 0