Projektarbeit-MYP/backend/app/install_raspberry_pi.sh

517 lines
13 KiB
Bash

#!/bin/bash
# ===================================================================
# Raspberry Pi Vollautomatische Installation - MYP Druckerverwaltung
# Für Debian 12 (Bookworm) auf Raspberry Pi
# ===================================================================
set -e # Beende bei Fehlern
# Farben für Ausgabe
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging-Funktion
log() {
echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] $1${NC}"
}
error() {
echo -e "${RED}[FEHLER] $1${NC}"
exit 1
}
warning() {
echo -e "${YELLOW}[WARNUNG] $1${NC}"
}
info() {
echo -e "${BLUE}[INFO] $1${NC}"
}
# Prüfe ob als Root ausgeführt
if [ "$EUID" -ne 0 ]; then
error "Dieses Skript muss als Root ausgeführt werden. Verwende: sudo $0"
fi
log "=== MYP Druckerverwaltung - Raspberry Pi Installation ==="
log "Debian 12 (Bookworm) Vollautomatische Einrichtung"
echo
# Konfigurationsvariablen
APP_USER="myp"
APP_DIR="/opt/myp-druckerverwaltung"
SERVICE_NAME="myp-druckerverwaltung"
KIOSK_SERVICE_NAME="myp-kiosk"
GITHUB_REPO="https://github.com/your-repo/myp-druckerverwaltung.git" # Anpassen!
DISPLAY_PORT=":0"
# Systemupdate
log "Aktualisiere Paketlisten und System..."
export DEBIAN_FRONTEND=noninteractive
apt-get update -y
apt-get upgrade -y --allow-downgrades --allow-remove-essential --allow-change-held-packages
# Installiere erforderliche Pakete
log "Installiere Systempakete..."
apt-get install -y \
python3 \
python3-pip \
python3-venv \
python3-dev \
git \
curl \
wget \
unzip \
sqlite3 \
nginx \
supervisor \
chromium-browser \
xorg \
openbox \
lightdm \
x11-xserver-utils \
unclutter \
build-essential \
libssl-dev \
libffi-dev \
libjpeg-dev \
zlib1g-dev \
nodejs \
npm \
--break-system-packages || true
# Erstelle Anwendungsbenutzer
log "Erstelle Anwendungsbenutzer '$APP_USER'..."
if ! id "$APP_USER" &>/dev/null; then
useradd -m -s /bin/bash "$APP_USER"
usermod -aG sudo "$APP_USER"
log "Benutzer '$APP_USER' erstellt"
else
log "Benutzer '$APP_USER' existiert bereits"
fi
# Erstelle Anwendungsverzeichnis
log "Erstelle Anwendungsverzeichnis..."
mkdir -p "$APP_DIR"
chown "$APP_USER:$APP_USER" "$APP_DIR"
# Klone Repository (falls nicht lokal vorhanden)
if [ ! -d "$APP_DIR/.git" ]; then
log "Klone Anwendung von Repository..."
# Für lokale Installation: kopiere aktuelles Verzeichnis
if [ -f "app.py" ]; then
log "Kopiere lokale Anwendung..."
cp -r . "$APP_DIR/"
chown -R "$APP_USER:$APP_USER" "$APP_DIR"
else
error "Anwendungsdateien nicht gefunden. Führe das Skript im Anwendungsverzeichnis aus."
fi
else
log "Repository bereits vorhanden, aktualisiere..."
cd "$APP_DIR"
sudo -u "$APP_USER" git pull
fi
cd "$APP_DIR"
# Python Virtual Environment erstellen
log "Erstelle Python Virtual Environment..."
sudo -u "$APP_USER" python3 -m venv venv
sudo -u "$APP_USER" ./venv/bin/pip install --upgrade pip
# Installiere Python-Abhängigkeiten
log "Installiere Python-Abhängigkeiten..."
if [ -f "requirements.txt" ]; then
sudo -u "$APP_USER" ./venv/bin/pip install -r requirements.txt --break-system-packages || true
else
# Fallback: Installiere grundlegende Pakete
sudo -u "$APP_USER" ./venv/bin/pip install \
flask \
flask-login \
flask-sqlalchemy \
werkzeug \
requests \
python-dotenv \
gunicorn \
--break-system-packages || true
fi
# Node.js Abhängigkeiten installieren (falls package.json vorhanden)
if [ -f "package.json" ]; then
log "Installiere Node.js Abhängigkeiten..."
sudo -u "$APP_USER" npm install --unsafe-perm=true --allow-root
# Baue Frontend-Assets
if [ -f "tailwind.config.js" ]; then
log "Baue Frontend-Assets..."
sudo -u "$APP_USER" npm run build || true
fi
fi
# Datenbank initialisieren
log "Initialisiere Datenbank..."
if [ -f "init_db.py" ]; then
sudo -u "$APP_USER" ./venv/bin/python init_db.py
else
# Erstelle einfache SQLite-Datenbank
sudo -u "$APP_USER" touch database.db
fi
# Erstelle Konfigurationsdatei
log "Erstelle Anwendungskonfiguration..."
cat > "$APP_DIR/.env" << EOF
# MYP Druckerverwaltung Konfiguration
FLASK_ENV=production
SECRET_KEY=$(openssl rand -hex 32)
DATABASE_URL=sqlite:///database.db
HOST=0.0.0.0
PORT=5000
DEBUG=False
# Kiosk-Modus
KIOSK_MODE=true
AUTO_LOGIN=true
EOF
chown "$APP_USER:$APP_USER" "$APP_DIR/.env"
# Erstelle Systemd-Service für die Anwendung
log "Erstelle Systemd-Service..."
cat > "/etc/systemd/system/$SERVICE_NAME.service" << EOF
[Unit]
Description=MYP Druckerverwaltung Flask Application
After=network.target
[Service]
Type=simple
User=$APP_USER
Group=$APP_USER
WorkingDirectory=$APP_DIR
Environment=PATH=$APP_DIR/venv/bin
ExecStart=$APP_DIR/venv/bin/python app.py
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
EOF
# Nginx-Konfiguration
log "Konfiguriere Nginx..."
cat > "/etc/nginx/sites-available/myp-druckerverwaltung" << EOF
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
# WebSocket-Support
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
}
# Statische Dateien direkt servieren
location /static/ {
alias $APP_DIR/static/;
expires 1y;
add_header Cache-Control "public, immutable";
}
}
EOF
# Aktiviere Nginx-Site
rm -f /etc/nginx/sites-enabled/default
ln -sf /etc/nginx/sites-available/myp-druckerverwaltung /etc/nginx/sites-enabled/
# Kiosk-Modus konfigurieren
log "Konfiguriere Kiosk-Modus..."
# Erstelle Kiosk-Benutzer
if ! id "kiosk" &>/dev/null; then
useradd -m -s /bin/bash kiosk
usermod -aG audio,video kiosk
fi
# Erstelle Kiosk-Startskript
cat > "/home/kiosk/start-kiosk.sh" << 'EOF'
#!/bin/bash
# Warte auf Netzwerk
sleep 10
# Setze Display
export DISPLAY=:0
# Deaktiviere Bildschirmschoner und Energiesparmodus
xset s off
xset -dpms
xset s noblank
# Verstecke Mauszeiger
unclutter -idle 0.5 -root &
# Starte Chromium im Kiosk-Modus
chromium-browser \
--kiosk \
--no-sandbox \
--disable-web-security \
--disable-features=TranslateUI \
--disable-ipc-flooding-protection \
--disable-renderer-backgrounding \
--disable-backgrounding-occluded-windows \
--disable-background-timer-throttling \
--disable-background-networking \
--disable-breakpad \
--disable-component-extensions-with-background-pages \
--disable-dev-shm-usage \
--disable-extensions \
--disable-features=TranslateUI,BlinkGenPropertyTrees \
--disable-hang-monitor \
--disable-ipc-flooding-protection \
--disable-popup-blocking \
--disable-prompt-on-repost \
--disable-renderer-backgrounding \
--disable-sync \
--disable-translate \
--disable-windows10-custom-titlebar \
--force-color-profile=srgb \
--metrics-recording-only \
--no-first-run \
--safebrowsing-disable-auto-update \
--enable-automation \
--password-store=basic \
--use-mock-keychain \
--autoplay-policy=no-user-gesture-required \
--start-fullscreen \
--window-position=0,0 \
--window-size=1920,1080 \
http://localhost/
EOF
chmod +x /home/kiosk/start-kiosk.sh
chown kiosk:kiosk /home/kiosk/start-kiosk.sh
# Erstelle Systemd-Service für Kiosk
cat > "/etc/systemd/system/$KIOSK_SERVICE_NAME.service" << EOF
[Unit]
Description=MYP Kiosk Mode
After=graphical-session.target network.target $SERVICE_NAME.service
Wants=graphical-session.target
[Service]
Type=simple
User=kiosk
Group=kiosk
Environment=DISPLAY=:0
ExecStart=/home/kiosk/start-kiosk.sh
Restart=always
RestartSec=10
[Install]
WantedBy=graphical.target
EOF
# Konfiguriere LightDM für automatischen Login
log "Konfiguriere automatischen Login..."
cat > "/etc/lightdm/lightdm.conf" << EOF
[Seat:*]
autologin-user=kiosk
autologin-user-timeout=0
user-session=openbox
EOF
# Erstelle Openbox-Konfiguration für Kiosk-Benutzer
mkdir -p /home/kiosk/.config/openbox
cat > "/home/kiosk/.config/openbox/autostart" << EOF
# Starte Kiosk-Anwendung
/home/kiosk/start-kiosk.sh &
EOF
chown -R kiosk:kiosk /home/kiosk/.config
# Erstelle Wartungsskript
log "Erstelle Wartungsskript..."
cat > "/usr/local/bin/myp-maintenance" << 'EOF'
#!/bin/bash
# MYP Druckerverwaltung Wartungsskript
case "$1" in
start)
echo "Starte MYP Druckerverwaltung..."
systemctl start myp-druckerverwaltung
systemctl start nginx
systemctl start myp-kiosk
;;
stop)
echo "Stoppe MYP Druckerverwaltung..."
systemctl stop myp-kiosk
systemctl stop myp-druckerverwaltung
systemctl stop nginx
;;
restart)
echo "Starte MYP Druckerverwaltung neu..."
systemctl restart myp-druckerverwaltung
systemctl restart nginx
systemctl restart myp-kiosk
;;
status)
echo "=== MYP Druckerverwaltung Status ==="
systemctl status myp-druckerverwaltung --no-pager
echo
echo "=== Nginx Status ==="
systemctl status nginx --no-pager
echo
echo "=== Kiosk Status ==="
systemctl status myp-kiosk --no-pager
;;
logs)
echo "=== Anwendungslogs ==="
journalctl -u myp-druckerverwaltung -f
;;
update)
echo "Aktualisiere MYP Druckerverwaltung..."
cd /opt/myp-druckerverwaltung
sudo -u myp git pull
sudo -u myp ./venv/bin/pip install -r requirements.txt --break-system-packages
systemctl restart myp-druckerverwaltung
;;
*)
echo "Verwendung: $0 {start|stop|restart|status|logs|update}"
exit 1
;;
esac
EOF
chmod +x /usr/local/bin/myp-maintenance
# Aktiviere und starte Services
log "Aktiviere und starte Services..."
systemctl daemon-reload
systemctl enable "$SERVICE_NAME"
systemctl enable nginx
systemctl enable "$KIOSK_SERVICE_NAME"
# Starte Services
systemctl start "$SERVICE_NAME"
systemctl start nginx
# Warte kurz und prüfe Status
sleep 5
if systemctl is-active --quiet "$SERVICE_NAME"; then
log "✅ MYP Druckerverwaltung Service läuft"
else
warning "⚠️ MYP Druckerverwaltung Service nicht aktiv"
fi
if systemctl is-active --quiet nginx; then
log "✅ Nginx läuft"
else
warning "⚠️ Nginx nicht aktiv"
fi
# Konfiguriere Firewall (falls ufw installiert)
if command -v ufw &> /dev/null; then
log "Konfiguriere Firewall..."
ufw --force enable
ufw allow 80/tcp
ufw allow 22/tcp
fi
# Erstelle Desktop-Verknüpfung für Wartung
cat > "/home/$APP_USER/Desktop/MYP-Wartung.desktop" << EOF
[Desktop Entry]
Version=1.0
Type=Application
Name=MYP Wartung
Comment=MYP Druckerverwaltung Wartung
Exec=gnome-terminal -- sudo myp-maintenance status
Icon=applications-system
Terminal=true
Categories=System;
EOF
chown "$APP_USER:$APP_USER" "/home/$APP_USER/Desktop/MYP-Wartung.desktop"
chmod +x "/home/$APP_USER/Desktop/MYP-Wartung.desktop"
# Erstelle Backup-Skript
cat > "/usr/local/bin/myp-backup" << 'EOF'
#!/bin/bash
BACKUP_DIR="/opt/myp-backups"
DATE=$(date +%Y%m%d_%H%M%S)
APP_DIR="/opt/myp-druckerverwaltung"
mkdir -p "$BACKUP_DIR"
echo "Erstelle Backup: $DATE"
# Stoppe Anwendung
systemctl stop myp-druckerverwaltung
# Erstelle Backup
tar -czf "$BACKUP_DIR/myp_backup_$DATE.tar.gz" \
-C "$APP_DIR" \
database.db \
.env \
uploads/ \
logs/ 2>/dev/null || true
# Starte Anwendung
systemctl start myp-druckerverwaltung
echo "Backup erstellt: $BACKUP_DIR/myp_backup_$DATE.tar.gz"
# Lösche alte Backups (älter als 30 Tage)
find "$BACKUP_DIR" -name "myp_backup_*.tar.gz" -mtime +30 -delete
EOF
chmod +x /usr/local/bin/myp-backup
# Erstelle Cron-Job für automatische Backups
echo "0 2 * * * root /usr/local/bin/myp-backup" > /etc/cron.d/myp-backup
# Abschlussmeldung
log "=== Installation abgeschlossen! ==="
echo
info "🎉 MYP Druckerverwaltung wurde erfolgreich installiert!"
echo
info "📋 Wichtige Informationen:"
info " • Anwendung läuft auf: http://$(hostname -I | awk '{print $1}')"
info " • Anwendungsverzeichnis: $APP_DIR"
info " • Anwendungsbenutzer: $APP_USER"
info " • Service-Name: $SERVICE_NAME"
info " • Kiosk-Service: $KIOSK_SERVICE_NAME"
echo
info "🔧 Wartungskommandos:"
info " • Status prüfen: myp-maintenance status"
info " • Neustart: myp-maintenance restart"
info " • Logs anzeigen: myp-maintenance logs"
info " • Update: myp-maintenance update"
info " • Backup erstellen: myp-backup"
echo
info "🖥️ Kiosk-Modus:"
info " • Wird beim nächsten Neustart automatisch gestartet"
info " • Vollbild-Browser auf http://localhost"
info " • Automatischer Login als 'kiosk' Benutzer"
echo
warning "⚠️ Wichtiger Hinweis:"
warning " Starte das System neu, um den Kiosk-Modus zu aktivieren:"
warning " sudo reboot"
echo
log "Installation erfolgreich abgeschlossen! 🚀"