From d2f23d589a18faaf6d091ceb3be3dbf64170cb57 Mon Sep 17 00:00:00 2001 From: Till Tomczak Date: Fri, 23 May 2025 11:12:35 +0200 Subject: [PATCH] "feat: Enhanced backend scripts documentation in README --- backend/README_BACKEND_SCRIPTS.md | 1 + backend/install.sh | 39 +++-- backend/start-backend-server.sh | 270 ++++++++++-------------------- backend/start-debug-server.sh | 112 ++++++++----- backend/start-production.sh | 45 +++-- 5 files changed, 224 insertions(+), 243 deletions(-) create mode 100644 backend/README_BACKEND_SCRIPTS.md diff --git a/backend/README_BACKEND_SCRIPTS.md b/backend/README_BACKEND_SCRIPTS.md new file mode 100644 index 00000000..0519ecba --- /dev/null +++ b/backend/README_BACKEND_SCRIPTS.md @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/backend/install.sh b/backend/install.sh index 0955ce09..f096eb3c 100644 --- a/backend/install.sh +++ b/backend/install.sh @@ -247,10 +247,8 @@ initialize_database() { # Aktiviere virtuelle Umgebung source venv/bin/activate - # Lade Umgebungsvariablen - if [ -f "env.backend" ]; then - export $(cat env.backend | grep -v '^#' | grep -v '^$' | xargs) - fi + # Umgebungsvariablen sind bereits in prepare_configuration() gesetzt + log "Verwende hardgecodete Konfiguration..." # Setze Flask-App export FLASK_APP=app.py @@ -286,7 +284,24 @@ User=$USER Group=$USER WorkingDirectory=$SCRIPT_DIR Environment=PATH=$SCRIPT_DIR/venv/bin -EnvironmentFile=$SCRIPT_DIR/env.backend +Environment=FLASK_APP=app.py +Environment=FLASK_ENV=production +Environment=SECRET_KEY=7445630171969DFAC92C53CEC92E67A9CB2E00B3CB2F +Environment=DATABASE_PATH=instance/myp.db +Environment=LOG_LEVEL=INFO +Environment=JOB_CHECK_INTERVAL=60 +Environment=SOCKET_CHECK_INTERVAL=120 +Environment=PRINTERS={"Drucker 1": {"ip": "192.168.0.100"}, "Drucker 2": {"ip": "192.168.0.101"}, "Drucker 3": {"ip": "192.168.0.102"}, "Drucker 4": {"ip": "192.168.0.103"}, "Drucker 5": {"ip": "192.168.0.104"}, "Drucker 6": {"ip": "192.168.0.106"}} +Environment=TAPO_USERNAME=till.tomczak@mercedes-benz.com +Environment=TAPO_PASSWORD=744563017196A +Environment=HOST=0.0.0.0 +Environment=PORT=5000 +Environment=BACKEND_URL=http://localhost:5000 +Environment=UPLOAD_FOLDER=uploads +Environment=MAX_CONTENT_LENGTH=16777216 +Environment=DEBUG=false +Environment=TESTING=false +Environment=DEVELOPMENT=false ExecStart=$SCRIPT_DIR/venv/bin/gunicorn --bind 0.0.0.0:5000 --workers 4 wsgi:application ExecReload=/bin/kill -s HUP \$MAINPID Restart=always @@ -326,15 +341,15 @@ main() { if [ "$INSTALL_MODE" = "production" ]; then echo "📋 Produktionsbetrieb:" - echo " 1. Konfiguration prüfen: nano env.backend" - echo " 2. Service starten: sudo systemctl start myp-backend" - echo " 3. Service prüfen: sudo systemctl status myp-backend" - echo " 4. Oder manuell: ./start-production.sh" + echo " 1. Service starten: sudo systemctl start myp-backend" + echo " 2. Service prüfen: sudo systemctl status myp-backend" + echo " 3. Oder manuell: ./start-production.sh" + echo " 4. Logs anzeigen: sudo journalctl -u myp-backend -f" else echo "🔧 Entwicklungsbetrieb:" - echo " 1. Konfiguration prüfen: nano env.backend" - echo " 2. Server starten: ./start-backend-server.sh" - echo " 3. Development-Server: ./start-backend-server.sh --development" + echo " 1. Server starten: ./start-backend-server.sh" + echo " 2. Development-Server: ./start-backend-server.sh --development" + echo " 3. Debug-Modus: ./start-debug-server.sh" fi echo "" diff --git a/backend/start-backend-server.sh b/backend/start-backend-server.sh index 133a5f6c..81bc7a31 100644 --- a/backend/start-backend-server.sh +++ b/backend/start-backend-server.sh @@ -1,195 +1,103 @@ #!/bin/bash -# 🏭 MYP Backend - Standalone Server Start -# Startet den Backend-Server vollständig unabhängig vom Frontend +# 🚀 MYP Backend - Entwicklungs-Startskript +# Startet den Backend-Server mit hardgecodeter Konfiguration set -e -echo "🏭 MYP Backend - Standalone Server wird gestartet..." +echo "=== MYP Backend - Entwicklungsstart ===" -# Farben für Terminal-Ausgabe -RED='\033[0;31m' -GREEN='\033[0;32m' -YELLOW='\033[1;33m' -BLUE='\033[0;34m' -NC='\033[0m' # No Color - -# Funktionen -log_info() { - echo -e "${BLUE}[INFO]${NC} $1" -} - -log_success() { - echo -e "${GREEN}[SUCCESS]${NC} $1" -} - -log_warning() { - echo -e "${YELLOW}[WARNING]${NC} $1" -} - -log_error() { - echo -e "${RED}[ERROR]${NC} $1" -} - -# Arbeitsverzeichnis setzen -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# Wechsel ins Backend-Verzeichnis +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" cd "$SCRIPT_DIR" -log_info "Arbeitsverzeichnis: $SCRIPT_DIR" - -# Umgebungsmodus bestimmen -if [ "$1" = "--production" ] || [ "$FLASK_ENV" = "production" ]; then - RUN_MODE="production" - log_info "Produktionsmodus aktiviert" -elif [ "$1" = "--development" ] || [ "$FLASK_ENV" = "development" ]; then - RUN_MODE="development" - log_info "Entwicklungsmodus aktiviert" -else - RUN_MODE="development" - log_info "Standard-Entwicklungsmodus aktiviert" -fi - -# Umgebungsvariablen laden -if [ -f "env.backend" ]; then - log_info "Lade Backend-Umgebungsvariablen..." - export $(cat env.backend | grep -v '^#' | grep -v '^$' | xargs) - export FLASK_ENV="$RUN_MODE" -fi - -# Prüfe Python-Installation -if ! command -v python3 &> /dev/null; then - log_error "Python3 ist nicht installiert!" - exit 1 -fi - -# Prüfe pip-Installation -if ! command -v pip3 &> /dev/null; then - log_error "pip3 ist nicht installiert!" - exit 1 -fi - -log_success "Python-Installation verifiziert" - -# Erstelle virtuelle Umgebung falls nicht vorhanden -if [ ! -d "venv" ]; then - log_info "Erstelle virtuelle Python-Umgebung..." - python3 -m venv venv -fi - -# Aktiviere virtuelle Umgebung -log_info "Aktiviere virtuelle Umgebung..." -source venv/bin/activate - -# Installiere Dependencies -log_info "Installiere Python-Dependencies..." -pip install --upgrade pip -pip install -r requirements.txt - -# Notwendige Verzeichnisse erstellen -log_info "Erstelle notwendige Verzeichnisse..." -mkdir -p instance logs migrations/versions - -# Datenbank initialisieren -log_info "Initialisiere Datenbank..." +# Hardgecodete Umgebungsvariablen setzen export FLASK_APP=app.py -python3 -c " -from app import create_app, init_db -app = create_app('$RUN_MODE') -with app.app_context(): - init_db() - print('Datenbank initialisiert') -" +export FLASK_ENV=development +export SECRET_KEY=7445630171969DFAC92C53CEC92E67A9CB2E00B3CB2F +export DATABASE_PATH=instance/myp.db +export LOG_LEVEL=INFO +export JOB_CHECK_INTERVAL=60 +export SOCKET_CHECK_INTERVAL=120 +export PRINTERS='{"Drucker 1": {"ip": "192.168.0.100"}, "Drucker 2": {"ip": "192.168.0.101"}, "Drucker 3": {"ip": "192.168.0.102"}, "Drucker 4": {"ip": "192.168.0.103"}, "Drucker 5": {"ip": "192.168.0.104"}, "Drucker 6": {"ip": "192.168.0.106"}}' +export TAPO_USERNAME=till.tomczak@mercedes-benz.com +export TAPO_PASSWORD=744563017196A +export HOST=0.0.0.0 +export PORT=5000 +export BACKEND_URL=http://localhost:5000 +export UPLOAD_FOLDER=uploads +export MAX_CONTENT_LENGTH=16777216 +export DEBUG=true +export TESTING=false +export DEVELOPMENT=true +export PYTHONPATH=${PYTHONPATH}:$(pwd) -# Drucker initialisieren -if [ ! -z "$PRINTERS" ]; then - log_info "Initialisiere Drucker-Konfiguration..." - python3 -c " -from app import create_app, init_printers -app = create_app('$RUN_MODE') -with app.app_context(): - init_printers() - print('Drucker initialisiert') -" +# Prüfe Entwicklungsmodus Parameter +if [ "$1" = "--production" ]; then + export FLASK_ENV=production + export DEBUG=false + export DEVELOPMENT=false + echo "🚀 Produktions-Modus aktiviert" +elif [ "$1" = "--debug" ]; then + export FLASK_ENV=development + export DEBUG=true + export DEVELOPMENT=true + echo "🔧 Debug-Modus aktiviert" +else + echo "🔧 Standard-Entwicklungsmodus" fi -# Server starten basierend auf Modus -if [ "$RUN_MODE" = "production" ]; then - log_info "Starte Backend-Server im Produktionsmodus..." - log_info "Verwende Gunicorn für Produktionsbetrieb" - - # Prüfe ob Gunicorn installiert ist - if ! command -v gunicorn &> /dev/null; then - log_error "Gunicorn ist nicht installiert! Installiere mit: pip install gunicorn" - exit 1 - fi - - # Starte mit Produktions-Skript - exec ./start-production.sh - +echo "Konfiguration:" +echo " - Flask Environment: $FLASK_ENV" +echo " - Debug Modus: $DEBUG" +echo " - Host: $HOST" +echo " - Port: $PORT" +echo " - Database: $DATABASE_PATH" +echo "" + +# Erstelle notwendige Verzeichnisse +mkdir -p instance logs uploads + +# Aktiviere virtuelle Umgebung falls vorhanden +if [ -d "venv" ]; then + echo "Aktiviere virtuelle Umgebung..." + source venv/bin/activate else - log_info "Starte Backend-Server im Entwicklungsmodus..." - - # Flask-Entwicklungsserver - export FLASK_APP=app.py - export FLASK_ENV=development - export FLASK_DEBUG=1 - - # Port prüfen - PORT=${PORT:-5000} - if netstat -tuln | grep -q ":$PORT "; then - log_warning "Port $PORT ist bereits belegt!" - log_info "Verwende alternativen Port 5001..." - PORT=5001 - fi - - log_info "Backend-Server startet auf Port $PORT..." - - # Flask-Server starten - python3 -m flask run --host=0.0.0.0 --port=$PORT & - FLASK_PID=$! - - # Warten auf Server-Start - log_info "Warte auf Backend-Service..." - timeout=60 - counter=0 - - while [ $counter -lt $timeout ]; do - if curl -f http://localhost:$PORT/health >/dev/null 2>&1; then - log_success "Backend-Server ist bereit!" - break - fi - - if [ $((counter % 10)) -eq 0 ]; then - log_info "Warte auf Backend-Service... ($counter/$timeout Sekunden)" - fi - - sleep 1 - counter=$((counter + 1)) - done - - if [ $counter -eq $timeout ]; then - log_error "Backend-Service konnte nicht gestartet werden!" - kill $FLASK_PID 2>/dev/null || true - exit 1 - fi - - # URLs anzeigen - echo "" - log_success "🎉 Backend-Server erfolgreich gestartet!" - echo "" - echo "📡 Backend-API: http://localhost:$PORT" - echo "🔧 Backend-Health: http://localhost:$PORT/health" - echo "📋 Backend-Test: http://localhost:$PORT/api/test" - echo "" - - # Logs anzeigen (optional) - if [ "$2" = "--logs" ] || [ "$3" = "--logs" ]; then - log_info "Zeige Backend-Logs (Strg+C zum Beenden):" - tail -f logs/myp.log 2>/dev/null || log_warning "Keine Log-Datei gefunden" - fi - - log_info "Verwende Strg+C um den Server zu stoppen" - - # Warte auf Signal - wait $FLASK_PID + echo "WARNUNG: Keine virtuelle Umgebung gefunden. Verwende System-Python." +fi + +# Prüfe ob App-Datei existiert +if [ ! -f "app.py" ]; then + echo "FEHLER: app.py nicht gefunden!" + exit 1 +fi + +# Starte Flask Development Server +echo "Starte Flask Development Server..." +echo "Backend verfügbar unter: http://localhost:$PORT" +echo "Health-Check: http://localhost:$PORT/health" +echo "API-Test: http://localhost:$PORT/api/test" +echo "" +echo "Zum Stoppen: Ctrl+C" +echo "" + +# Starte den Server +if [ "$FLASK_ENV" = "production" ]; then + # Produktionsmodus mit Gunicorn + echo "Starte mit Gunicorn (Produktionsmodus)..." + exec gunicorn \ + --bind=$HOST:$PORT \ + --workers=2 \ + --worker-class=sync \ + --timeout=30 \ + --keep-alive=5 \ + --reload \ + --access-logfile=logs/access.log \ + --error-logfile=logs/error.log \ + --log-level=info \ + wsgi:application +else + # Entwicklungsmodus mit Flask + echo "Starte mit Flask (Entwicklungsmodus)..." + exec python3 -m flask run --host=$HOST --port=$PORT --debug fi \ No newline at end of file diff --git a/backend/start-debug-server.sh b/backend/start-debug-server.sh index 04015db6..a6435713 100644 --- a/backend/start-debug-server.sh +++ b/backend/start-debug-server.sh @@ -1,52 +1,84 @@ #!/bin/bash -# Farbcodes für Ausgabe -RED='\033[0;31m' -GREEN='\033[0;32m' -YELLOW='\033[0;33m' -BLUE='\033[0;34m' -NC='\033[0m' # No Color +# 🔧 MYP Backend - Debug-Startskript +# Startet den Backend-Server im Debug-Modus mit maximaler Verbosity -# Funktion zur Ausgabe mit Zeitstempel -log() { - echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" -} +set -e -error_log() { - echo -e "${RED}[$(date +'%Y-%m-%d %H:%M:%S')] FEHLER:${NC} $1" >&2 -} +echo "=== MYP Backend - Debug-Modus ===" -# Pfad zum Debug-Server +# Wechsel ins Backend-Verzeichnis SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -DEBUG_SERVER_DIR="$SCRIPT_DIR/debug-server" +cd "$SCRIPT_DIR" -# Prüfe, ob das Debug-Server-Verzeichnis existiert -if [ ! -d "$DEBUG_SERVER_DIR" ]; then - error_log "Debug-Server-Verzeichnis nicht gefunden: $DEBUG_SERVER_DIR" - exit 1 +# Hardgecodete Debug-Umgebungsvariablen +export FLASK_APP=app.py +export FLASK_ENV=development +export FLASK_DEBUG=1 +export SECRET_KEY=7445630171969DFAC92C53CEC92E67A9CB2E00B3CB2F +export DATABASE_PATH=instance/myp.db +export LOG_LEVEL=DEBUG +export JOB_CHECK_INTERVAL=30 +export SOCKET_CHECK_INTERVAL=60 +export PRINTERS='{"Drucker 1": {"ip": "192.168.0.100"}, "Drucker 2": {"ip": "192.168.0.101"}, "Drucker 3": {"ip": "192.168.0.102"}, "Drucker 4": {"ip": "192.168.0.103"}, "Drucker 5": {"ip": "192.168.0.104"}, "Drucker 6": {"ip": "192.168.0.106"}}' +export TAPO_USERNAME=till.tomczak@mercedes-benz.com +export TAPO_PASSWORD=744563017196A +export HOST=0.0.0.0 +export PORT=5000 +export BACKEND_URL=http://localhost:5000 +export UPLOAD_FOLDER=uploads +export MAX_CONTENT_LENGTH=16777216 +export DEBUG=true +export TESTING=false +export DEVELOPMENT=true +export PYTHONPATH=${PYTHONPATH}:$(pwd) + +echo "🔧 Debug-Konfiguration:" +echo " - Flask Environment: $FLASK_ENV" +echo " - Debug Modus: $FLASK_DEBUG" +echo " - Log Level: $LOG_LEVEL" +echo " - Host: $HOST" +echo " - Port: $PORT" +echo " - Database: $DATABASE_PATH" +echo " - Job Check Interval: $JOB_CHECK_INTERVAL Sekunden" +echo "" + +# Erstelle notwendige Verzeichnisse +mkdir -p instance logs uploads + +# Aktiviere virtuelle Umgebung falls vorhanden +if [ -d "venv" ]; then + echo "Aktiviere virtuelle Umgebung..." + source venv/bin/activate +else + echo "WARNUNG: Keine virtuelle Umgebung gefunden. Verwende System-Python." fi -# Prüfe, ob Python installiert ist -if ! command -v python &> /dev/null; then - error_log "Python nicht gefunden. Bitte installieren Sie Python." - exit 1 +# Prüfe ob App-Datei existiert +if [ ! -f "app.py" ]; then + echo "FEHLER: app.py nicht gefunden!" + exit 1 fi -# Prüfe, ob pip installiert ist -if ! command -v pip &> /dev/null; then - error_log "pip nicht gefunden. Bitte installieren Sie pip." - exit 1 -fi +# Starte Flask im Debug-Modus +echo "🚀 Starte Flask Debug-Server..." +echo "Backend verfügbar unter: http://localhost:$PORT" +echo "Health-Check: http://localhost:$PORT/health" +echo "API-Test: http://localhost:$PORT/api/test" +echo "" +echo "🔧 Debug-Features aktiviert:" +echo " - Auto-Reload bei Code-Änderungen" +echo " - Detaillierte Error-Pages" +echo " - Erweiterte Logging-Ausgabe" +echo " - Schnellere Job-Check-Intervalle" +echo "" +echo "Zum Stoppen: Ctrl+C" +echo "" -# Wechsle ins Debug-Server-Verzeichnis -cd "$DEBUG_SERVER_DIR" || exit 1 - -# Installiere Abhängigkeiten, falls nötig -if [ -f "requirements.txt" ]; then - log "Installiere Abhängigkeiten..." - pip install -r requirements.txt -fi - -# Starte den Debug-Server -log "Starte Backend-Debug-Server..." -python app.py \ No newline at end of file +# Starte Flask mit Debug-Optionen +exec python3 -m flask run \ + --host=$HOST \ + --port=$PORT \ + --debug \ + --reload \ + --debugger \ No newline at end of file diff --git a/backend/start-production.sh b/backend/start-production.sh index 5c1bc996..8f5cc0c0 100644 --- a/backend/start-production.sh +++ b/backend/start-production.sh @@ -7,6 +7,31 @@ set -e echo "=== MYP Backend - Produktionsstart ===" +# Wechsel ins Backend-Verzeichnis +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +cd "$SCRIPT_DIR" + +# Hardgecodete Produktions-Umgebungsvariablen +export FLASK_APP=app.py +export FLASK_ENV=production +export SECRET_KEY=7445630171969DFAC92C53CEC92E67A9CB2E00B3CB2F +export DATABASE_PATH=instance/myp.db +export LOG_LEVEL=INFO +export JOB_CHECK_INTERVAL=60 +export SOCKET_CHECK_INTERVAL=120 +export PRINTERS='{"Drucker 1": {"ip": "192.168.0.100"}, "Drucker 2": {"ip": "192.168.0.101"}, "Drucker 3": {"ip": "192.168.0.102"}, "Drucker 4": {"ip": "192.168.0.103"}, "Drucker 5": {"ip": "192.168.0.104"}, "Drucker 6": {"ip": "192.168.0.106"}}' +export TAPO_USERNAME=till.tomczak@mercedes-benz.com +export TAPO_PASSWORD=744563017196A +export HOST=0.0.0.0 +export PORT=5000 +export BACKEND_URL=http://localhost:5000 +export UPLOAD_FOLDER=uploads +export MAX_CONTENT_LENGTH=16777216 +export DEBUG=false +export TESTING=false +export DEVELOPMENT=false +export PYTHONPATH=${PYTHONPATH}:$(pwd) + # Konfiguration WORKERS=${WORKERS:-4} BIND_ADDRESS=${BIND_ADDRESS:-"0.0.0.0:5000"} @@ -15,19 +40,9 @@ KEEP_ALIVE=${KEEP_ALIVE:-5} MAX_REQUESTS=${MAX_REQUESTS:-1000} MAX_REQUESTS_JITTER=${MAX_REQUESTS_JITTER:-100} -# Umgebungsvariablen -export FLASK_ENV=production -export PYTHONPATH=${PYTHONPATH}:$(pwd) - # Log-Verzeichnis erstellen mkdir -p logs -# Prüfe, ob alle erforderlichen Umgebungsvariablen gesetzt sind -if [ -z "$SECRET_KEY" ]; then - echo "WARNUNG: SECRET_KEY ist nicht gesetzt. Verwende einen festgelegten Schlüssel." - export SECRET_KEY=7445630171969DFAC92C53CEC92E67A9CB2E00B3CB2F -fi - # Produktionsparameter ausgeben echo "Konfiguration:" echo " - Workers: $WORKERS" @@ -35,8 +50,18 @@ echo " - Bind: $BIND_ADDRESS" echo " - Timeout: $TIMEOUT Sekunden" echo " - Max Requests: $MAX_REQUESTS" echo " - Environment: $FLASK_ENV" +echo " - Database: $DATABASE_PATH" +echo " - Log Level: $LOG_LEVEL" echo "" +# Aktiviere virtuelle Umgebung falls vorhanden +if [ -d "venv" ]; then + echo "Aktiviere virtuelle Umgebung..." + source venv/bin/activate +else + echo "WARNUNG: Keine virtuelle Umgebung gefunden. Verwende System-Python." +fi + # Gunicorn starten echo "Starte Gunicorn-Server..." exec gunicorn \