Bereinige und vereinfache Installations-Skripte

- Entferne alle überflüssigen Installations- und Konfigurationsskripte
- Erstelle zwei vereinfachte Docker-Installationsskripte:
  - install-frontend.sh für Frontend-Installation
  - install-backend.sh für Backend-Installation
- Verbessere Frontend Dockerfile mit besserer Unterstützung für native Dependencies
- Aktualisiere Backend Dockerfile für automatische DB-Initialisierung
- Korrigiere TypeScript-Fehler in personalized-cards.tsx
- Erstelle env.ts für Umgebungsvariablen-Verwaltung
- Füge ausführliche Installationsanleitung in INSTALL.md hinzu
- Konfiguriere Docker-Compose für Host-Netzwerkmodus
- Erweitere Dockerfiles mit Healthchecks für bessere Zuverlässigkeit

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-03-31 14:22:07 +02:00
parent fc62086a50
commit f1541478ad
198 changed files with 1903 additions and 17934 deletions

View File

@@ -2,17 +2,51 @@ FROM python:3.11-slim
WORKDIR /app
# Install system dependencies (curl, sqlite3 for database, wget for healthcheck)
RUN apt-get update && apt-get install -y \
curl \
sqlite3 \
wget \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy application code
COPY . .
RUN mkdir -p logs
# Create required directories
RUN mkdir -p logs instance
ENV FLASK_APP=app.py
ENV PYTHONUNBUFFERED=1
# Add health check endpoint
RUN echo 'from flask import Blueprint\n\
health_bp = Blueprint("health", __name__)\n\
\n\
@health_bp.route("/health")\n\
def health_check():\n\
return {"status": "healthy"}, 200\n'\
> /app/health.py
# Add the health blueprint to app.py if it doesn't exist
RUN grep -q "health_bp" app.py || sed -i '/from flask import/a from health import health_bp' app.py
RUN grep -q "app.register_blueprint(health_bp)" app.py || sed -i '/app = Flask/a app.register_blueprint(health_bp)' app.py
EXPOSE 5000
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "app:app"]
# Add startup script to initialize database if needed
RUN echo '#!/bin/bash\n\
if [ ! -f "instance/myp.db" ] || [ ! -s "instance/myp.db" ]; then\n\
echo "Initializing database..."\n\
python -c "from app import init_db; init_db()"\n\
fi\n\
\n\
echo "Starting gunicorn server..."\n\
gunicorn --bind 0.0.0.0:5000 app:app\n'\
> /app/start.sh && chmod +x /app/start.sh
CMD ["/app/start.sh"]

View File

@@ -1,44 +0,0 @@
#!/bin/bash
# MYP Backend Autostart-Skript
# Installiert den MYP Backend-Dienst für automatischen Start beim Hochfahren
# Skript muss als root ausgeführt werden
if [ "$EUID" -ne 0 ]; then
echo "Dieses Skript muss als root ausgeführt werden."
echo "Bitte mit 'sudo' ausführen."
exit 1
fi
# Aktuelles Verzeichnis bestimmen
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo "Installiere MYP Backend Service aus: $SCRIPT_DIR"
# Überprüfen, ob die Service-Datei existiert
if [ ! -f "$SCRIPT_DIR/myp-backend.service" ]; then
echo "Fehler: myp-backend.service nicht gefunden!"
exit 1
fi
# Service-Datei in systemd-Verzeichnis kopieren
cp "$SCRIPT_DIR/myp-backend.service" /etc/systemd/system/
echo "Service-Datei nach /etc/systemd/system/ kopiert."
# Systemd neu laden und Service aktivieren
systemctl daemon-reload
systemctl enable myp-backend.service
echo "MYP Backend Service wurde für Autostart beim Systemstart aktiviert."
# Aktuellen Status anzeigen
echo "Starte den MYP Backend Service..."
systemctl start myp-backend.service
systemctl status myp-backend.service
echo ""
echo "Installation abgeschlossen!"
echo "Verwende folgende Befehle zur Steuerung des Dienstes:"
echo " sudo systemctl start myp-backend # Service starten"
echo " sudo systemctl stop myp-backend # Service stoppen"
echo " sudo systemctl restart myp-backend # Service neu starten"
echo " sudo systemctl status myp-backend # Status anzeigen"
echo " journalctl -u myp-backend -f # Logs anzeigen"

View File

@@ -1,73 +0,0 @@
#!/bin/bash
# Installation Script für MYP Backend
echo "=== MYP Backend Installation ==="
echo ""
# Prüfe Python-Version
python_version=$(python3 --version 2>&1 | awk '{print $2}')
echo "Python-Version: $python_version"
# Prüfe, ob die Python-Version mindestens 3.8 ist
required_version="3.8.0"
if [[ "$(printf '%s\n' "$required_version" "$python_version" | sort -V | head -n1)" != "$required_version" ]]; then
echo "FEHLER: Python $required_version oder höher wird benötigt"
exit 1
fi
# Prüfe, ob sqlite3 installiert ist
if ! command -v sqlite3 &> /dev/null; then
echo "FEHLER: sqlite3 ist nicht installiert."
echo "Bitte installiere sqlite3 mit deinem Paketmanager, z.B. 'apt install sqlite3'"
exit 1
fi
# Erstelle virtuelle Umgebung
echo ""
echo "Erstelle virtuelle Python-Umgebung..."
python3 -m venv venv
source venv/bin/activate
# Installiere Abhängigkeiten
echo ""
echo "Installiere Abhängigkeiten..."
pip install --upgrade pip
pip install -r requirements.txt
# Erstelle .env-Datei
echo ""
echo "Erstelle .env-Datei..."
if [ ! -f .env ]; then
cp .env.example .env
echo "Die .env-Datei wurde aus der Beispieldatei erstellt."
echo "Bitte passe die Konfiguration an, falls nötig."
else
echo ".env-Datei existiert bereits."
fi
# Erstelle Logs-Ordner
echo ""
echo "Erstelle logs-Ordner..."
mkdir -p logs
# Initialisiere die Datenbank
echo ""
echo "Initialisiere die Datenbank..."
bash initialize_myp_database.sh
echo ""
echo "=== Installation abgeschlossen ==="
echo ""
echo "Wichtige Schritte vor dem Start:"
echo "1. Passe die Konfigurationen in der .env-Datei an"
echo "2. Konfiguriere die Tapo-Steckdosen-Zugangsdaten in der .env-Datei (optional)"
echo "3. Passe die crontab-example an und installiere den Cron-Job (optional)"
echo ""
echo "Starte den Server mit:"
echo "source venv/bin/activate"
echo "python app.py"
echo ""
echo "Oder mit Gunicorn für Produktion:"
echo "gunicorn --bind 0.0.0.0:5000 app:app"
echo ""

View File

@@ -6,12 +6,20 @@ services:
container_name: myp-backend
network_mode: host
environment:
- SECRET_KEY=7445630171969DFAC92C53CEC92E67A9CB2E00B3CB2F
- DATABASE_PATH=instance/myp.db
- TAPO_USERNAME=till.tomczak@mercedes-benz.com
- TAPO_PASSWORD=744563017196A
- PRINTERS={"Printer 1": {"ip": "192.168.0.100"}, "Printer 2": {"ip": "192.168.0.101"}, "Printer 3": {"ip": "192.168.0.102"}, "Printer 4": {"ip": "192.168.0.103"}, "Printer 5": {"ip": "192.168.0.104"}, "Printer 6": {"ip": "192.168.0.106"}}
- SECRET_KEY=${SECRET_KEY:-7445630171969DFAC92C53CEC92E67A9CB2E00B3CB2F}
- DATABASE_PATH=${DATABASE_PATH:-instance/myp.db}
- TAPO_USERNAME=${TAPO_USERNAME:-till.tomczak@mercedes-benz.com}
- TAPO_PASSWORD=${TAPO_PASSWORD:-744563017196A}
- PRINTERS=${PRINTERS:-{"Printer 1": {"ip": "192.168.0.100"}, "Printer 2": {"ip": "192.168.0.101"}, "Printer 3": {"ip": "192.168.0.102"}, "Printer 4": {"ip": "192.168.0.103"}, "Printer 5": {"ip": "192.168.0.104"}, "Printer 6": {"ip": "192.168.0.106"}}}
- FLASK_APP=app.py
- PYTHONUNBUFFERED=1
volumes:
- ./logs:/app/logs
- ./instance:/app/instance
restart: unless-stopped
restart: unless-stopped
healthcheck:
test: ["CMD", "wget", "--spider", "http://localhost:5000/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s

View File

@@ -1,14 +0,0 @@
#!/bin/bash
# Skript zum Herunterladen der vollständigen Bootstrap CSS
# Verzeichnis erstellen falls es nicht existiert
mkdir -p "$(dirname "$0")/static/css"
# Bootstrap CSS herunterladen
wget -O "$(dirname "$0")/static/css/bootstrap.css" https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.css
# Optional: JavaScript-Datei auch herunterladen
wget -O "$(dirname "$0")/static/js/bootstrap.bundle.js" https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.js
echo "Bootstrap-Dateien wurden erfolgreich heruntergeladen."

View File

@@ -1,182 +0,0 @@
#!/bin/bash
# MYP Backend Installation Script for Debian
# This script installs and configures the MYP backend on a Debian-based system
set -e # Exit immediately if a command exits with non-zero status
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
LOG_FILE="$SCRIPT_DIR/backend-install.log"
# Function for logging with timestamps
log() {
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
echo -e "[$timestamp] $1" | tee -a "$LOG_FILE"
}
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Clear log file
> "$LOG_FILE"
log "===== Starting MYP Backend Installation ====="
log "Installation directory: $SCRIPT_DIR"
# Check for root privileges
if [ "$EUID" -ne 0 ]; then
log "ERROR: This script must be run as root"
exit 1
fi
# System update
log "Updating system packages..."
apt update -y >> "$LOG_FILE" 2>&1
apt upgrade -y >> "$LOG_FILE" 2>&1
# Install required packages
log "Installing required packages..."
apt install -y python3 python3-venv python3-pip sqlite3 >> "$LOG_FILE" 2>&1
# Create Python virtual environment
log "Creating Python virtual environment..."
cd "$SCRIPT_DIR"
if [ -d "venv" ]; then
log "Found existing virtual environment, removing..."
rm -rf venv
fi
python3 -m venv venv >> "$LOG_FILE" 2>&1
source venv/bin/activate
log "Upgrading pip..."
pip install --upgrade pip >> "$LOG_FILE" 2>&1
# Install Python dependencies
log "Installing Python dependencies..."
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt >> "$LOG_FILE" 2>&1
else
log "ERROR: requirements.txt not found"
exit 1
fi
# Setup Bootstrap for offline usage
log "Setting up Bootstrap for offline usage..."
mkdir -p "$SCRIPT_DIR/static/css" "$SCRIPT_DIR/static/js"
# Download non-minified Bootstrap CSS and JS files
log "Downloading Bootstrap files for offline usage..."
wget -q -O "$SCRIPT_DIR/static/css/bootstrap.css" "https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.css" || {
log "WARNING: Could not download Bootstrap CSS. Creating placeholder..."
echo "/* Bootstrap 5.3.2 offline placeholder */" > "$SCRIPT_DIR/static/css/bootstrap.css"
echo "/* Please manually download the full unminified version from: */" >> "$SCRIPT_DIR/static/css/bootstrap.css"
echo "/* https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.css */" >> "$SCRIPT_DIR/static/css/bootstrap.css"
}
wget -q -O "$SCRIPT_DIR/static/js/bootstrap.bundle.js" "https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.js" || {
log "WARNING: Could not download Bootstrap JS. Creating placeholder..."
echo "/* Bootstrap 5.3.2 bundle offline placeholder */" > "$SCRIPT_DIR/static/js/bootstrap.bundle.js"
echo "/* Please manually download the full unminified version from: */" >> "$SCRIPT_DIR/static/js/bootstrap.bundle.js"
echo "/* https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.js */" >> "$SCRIPT_DIR/static/js/bootstrap.bundle.js"
}
# Create database directory if it doesn't exist
log "Setting up database directories..."
mkdir -p instance/backups
# Check if .env file exists
if [ ! -f "$SCRIPT_DIR/.env" ]; then
log "Creating .env file template (IMPORTANT: Edit with your configuration)..."
cat > "$SCRIPT_DIR/.env" << EOF
# MYP Backend Environment Configuration
# IMPORTANT: Replace these values with your actual configuration!
SECRET_KEY=generate_a_secure_random_key
DATABASE_PATH=instance/myp_backend.db
# Tapo P115 Smart Plug credentials
TAPO_USERNAME=your_tapo_email
TAPO_PASSWORD=your_tapo_password
# Printer to Smart Plug mapping (JSON format)
# Format: {"Printer Name": "192.168.x.x", "Another Printer": "192.168.x.y"}
PRINTERS={"Example Printer": "192.168.1.100"}
EOF
log "ATTENTION: .env file has been created with placeholder values"
log " Please edit .env with your actual configuration before continuing"
read -p "Press Enter to continue after editing .env..."
fi
# Initialize the database
log "Initializing database..."
if [ -f "$SCRIPT_DIR/development/initialize_myp_database.sh" ]; then
bash "$SCRIPT_DIR/development/initialize_myp_database.sh" >> "$LOG_FILE" 2>&1
log "Database initialized successfully"
else
log "WARNING: initialize_myp_database.sh not found, manual setup may be required"
# Create empty database
touch instance/myp_backend.db
log "Created empty database file"
fi
# Setup cron job
log "Setting up cron job for maintenance tasks..."
CURRENT_USER=$(who am i | awk '{print $1}')
CRON_JOB="*/5 * * * * cd $SCRIPT_DIR && source venv/bin/activate && flask check-jobs"
# Create a temporary file with the current crontab plus our new job
TEMP_CRON=$(mktemp)
crontab -l 2>/dev/null | grep -v "$SCRIPT_DIR.*check-jobs" > "$TEMP_CRON" || true
echo "$CRON_JOB" >> "$TEMP_CRON"
crontab "$TEMP_CRON"
rm "$TEMP_CRON"
log "Cron job installed. Maintenance tasks will run every 5 minutes"
# Test the application
log "Testing backend application..."
source venv/bin/activate
cd "$SCRIPT_DIR"
if python -c "import app" 2>> "$LOG_FILE"; then
log "Import test successful"
else
log "WARNING: Import test failed, check the log file for details"
fi
# Generate systemd service file
log "Creating systemd service..."
cat > /etc/systemd/system/myp-backend.service << EOF
[Unit]
Description=MYP Backend Service
After=network.target
[Service]
User=$CURRENT_USER
WorkingDirectory=$SCRIPT_DIR
ExecStart=$SCRIPT_DIR/venv/bin/gunicorn --bind 0.0.0.0:5000 app:app
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
# Reload systemd and enable the service
systemctl daemon-reload
systemctl enable myp-backend.service
log "Installation complete!"
log ""
log "To start the backend service, run: systemctl start myp-backend"
log "To check service status, run: systemctl status myp-backend"
log "To view logs, run: journalctl -u myp-backend -f"
log ""
log "Configuration file is at: $SCRIPT_DIR/.env"
log "Make sure to configure your Tapo smart plug credentials and printer mapping in this file"
log ""
log "For development mode, run: cd $SCRIPT_DIR && source venv/bin/activate && python app.py"
log ""
log "To run tests, use: cd $SCRIPT_DIR && source venv/bin/activate && python -m unittest development/tests/tests.py"
log ""
log "For issues, check the log file at: $LOG_FILE"

View File

@@ -1,14 +0,0 @@
[Unit]
Description=MYP Backend Service
After=network.target
[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi/Projektarbeit-MYP/backend
ExecStart=/home/pi/Projektarbeit-MYP/backend/venv/bin/python app.py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target