🐛 Backend Cleanup & Enhancements:
This commit is contained in:
193
backend/start_production.py
Normal file
193
backend/start_production.py
Normal file
@@ -0,0 +1,193 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Production-Startskript für Mercedes-Benz TBA Marienfelde
|
||||
MYP (Mercedes-Benz Your Printer) System - Air-Gapped Production Environment
|
||||
|
||||
Dieses Skript startet das System im Production-Modus mit allen
|
||||
erforderlichen Sicherheits- und Performance-Optimierungen.
|
||||
|
||||
Verwendung:
|
||||
python start_production.py
|
||||
|
||||
Umgebungsvariablen:
|
||||
FLASK_ENV=production
|
||||
USE_PRODUCTION_CONFIG=true
|
||||
MERCEDES_ENVIRONMENT=production
|
||||
AIR_GAPPED_MODE=true
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import logging
|
||||
from datetime import datetime
|
||||
|
||||
# Production-Environment setzen
|
||||
os.environ['FLASK_ENV'] = 'production'
|
||||
os.environ['USE_PRODUCTION_CONFIG'] = 'true'
|
||||
os.environ['MERCEDES_ENVIRONMENT'] = 'production'
|
||||
os.environ['AIR_GAPPED_MODE'] = 'true'
|
||||
|
||||
# SSL für Production
|
||||
os.environ['FLASK_SSL_REQUIRED'] = 'true'
|
||||
|
||||
# Logging-Level
|
||||
os.environ['LOG_LEVEL'] = 'INFO'
|
||||
|
||||
# Performance-Optimierungen
|
||||
os.environ['PYTHONOPTIMIZE'] = '1'
|
||||
os.environ['PYTHONDONTWRITEBYTECODE'] = '1'
|
||||
|
||||
def print_production_banner():
|
||||
"""Zeigt den Production-Start-Banner"""
|
||||
banner = f"""
|
||||
{'='*80}
|
||||
🏢 MERCEDES-BENZ TBA MARIENFELDE - MYP PRODUCTION SYSTEM
|
||||
{'='*80}
|
||||
|
||||
🚀 Environment: Production Air-Gapped
|
||||
🔒 Security: Maximum (SSL + Security Headers)
|
||||
🌐 Network: Air-Gapped (Offline-Mode)
|
||||
⚡ Performance: Optimized for Industrial Environment
|
||||
📊 Monitoring: Enabled
|
||||
🔍 Audit-Logging: Enabled
|
||||
📅 Start-Zeit: {datetime.now().strftime('%d.%m.%Y %H:%M:%S')}
|
||||
|
||||
{'='*80}
|
||||
"""
|
||||
print(banner)
|
||||
|
||||
def check_production_requirements():
|
||||
"""Prüft Production-Voraussetzungen"""
|
||||
print("🔍 Prüfe Production-Voraussetzungen...")
|
||||
|
||||
requirements = []
|
||||
|
||||
# Python-Version prüfen
|
||||
if sys.version_info < (3, 8):
|
||||
requirements.append("❌ Python 3.8+ erforderlich")
|
||||
else:
|
||||
requirements.append("✅ Python-Version OK")
|
||||
|
||||
# Erforderliche Dateien prüfen
|
||||
required_files = [
|
||||
'app.py',
|
||||
'models.py',
|
||||
'utils/settings.py',
|
||||
'requirements.txt'
|
||||
]
|
||||
|
||||
for file in required_files:
|
||||
if os.path.exists(file):
|
||||
requirements.append(f"✅ {file}")
|
||||
else:
|
||||
requirements.append(f"❌ {file} fehlt")
|
||||
|
||||
# SSL-Zertifikate prüfen (optional)
|
||||
ssl_files = [
|
||||
'ssl/server.crt',
|
||||
'ssl/server.key',
|
||||
'certs/mercedes/cert.pem'
|
||||
]
|
||||
|
||||
ssl_available = any(os.path.exists(f) for f in ssl_files)
|
||||
if ssl_available:
|
||||
requirements.append("✅ SSL-Zertifikate verfügbar")
|
||||
else:
|
||||
requirements.append("⚠️ SSL-Zertifikate nicht gefunden (HTTP-Mode)")
|
||||
|
||||
# Datenbank-Verzeichnis prüfen
|
||||
if os.path.exists('instance'):
|
||||
requirements.append("✅ Datenbank-Verzeichnis")
|
||||
else:
|
||||
requirements.append("❌ Instance-Verzeichnis fehlt")
|
||||
os.makedirs('instance', exist_ok=True)
|
||||
requirements.append("✅ Instance-Verzeichnis erstellt")
|
||||
|
||||
for req in requirements:
|
||||
print(f" {req}")
|
||||
|
||||
# Kritische Fehler prüfen
|
||||
critical_errors = [r for r in requirements if r.startswith("❌")]
|
||||
if critical_errors:
|
||||
print("\n❌ KRITISCHE FEHLER GEFUNDEN:")
|
||||
for error in critical_errors:
|
||||
print(f" {error}")
|
||||
print("\n🛑 Production-Start abgebrochen!")
|
||||
sys.exit(1)
|
||||
|
||||
print("✅ Alle Voraussetzungen erfüllt\n")
|
||||
|
||||
def set_production_optimizations():
|
||||
"""Setzt Production-Optimierungen"""
|
||||
print("⚡ Aktiviere Production-Optimierungen...")
|
||||
|
||||
# Memory-Optimierungen
|
||||
os.environ['MALLOC_TRIM_THRESHOLD'] = '100000'
|
||||
|
||||
# Flask-Optimierungen
|
||||
os.environ['FLASK_SKIP_DOTENV'] = '1'
|
||||
|
||||
# SQLite-Optimierungen für Air-Gapped
|
||||
os.environ['SQLITE_SYNCHRONOUS'] = 'NORMAL'
|
||||
os.environ['SQLITE_CACHE_SIZE'] = '10000'
|
||||
|
||||
print(" ✅ Memory-Optimierungen aktiviert")
|
||||
print(" ✅ Flask-Optimierungen aktiviert")
|
||||
print(" ✅ Datenbank-Optimierungen aktiviert")
|
||||
print()
|
||||
|
||||
def setup_security():
|
||||
"""Konfiguriert Production-Sicherheit"""
|
||||
print("🔒 Konfiguriere Production-Sicherheit...")
|
||||
|
||||
# Security Headers
|
||||
os.environ['FORCE_HTTPS'] = 'true'
|
||||
os.environ['HSTS_MAX_AGE'] = '31536000'
|
||||
|
||||
# Session-Sicherheit
|
||||
os.environ['SESSION_SECURE'] = 'true'
|
||||
os.environ['SESSION_HTTPONLY'] = 'true'
|
||||
os.environ['SESSION_SAMESITE'] = 'Strict'
|
||||
|
||||
# CSRF-Schutz
|
||||
os.environ['CSRF_TIME_LIMIT'] = '3600'
|
||||
|
||||
print(" ✅ Security Headers konfiguriert")
|
||||
print(" ✅ Session-Sicherheit aktiviert")
|
||||
print(" ✅ CSRF-Schutz aktiviert")
|
||||
print()
|
||||
|
||||
def start_application():
|
||||
"""Startet die Hauptanwendung"""
|
||||
print("🚀 Starte MYP Production System...\n")
|
||||
|
||||
try:
|
||||
# app.py importieren und starten
|
||||
from app import main
|
||||
main()
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("\n🛑 Production-System durch Benutzer gestoppt")
|
||||
except Exception as e:
|
||||
print(f"\n❌ KRITISCHER FEHLER: {str(e)}")
|
||||
sys.exit(1)
|
||||
|
||||
def main():
|
||||
"""Haupt-Production-Start-Funktion"""
|
||||
# Banner anzeigen
|
||||
print_production_banner()
|
||||
|
||||
# Voraussetzungen prüfen
|
||||
check_production_requirements()
|
||||
|
||||
# Optimierungen setzen
|
||||
set_production_optimizations()
|
||||
|
||||
# Sicherheit konfigurieren
|
||||
setup_security()
|
||||
|
||||
# Anwendung starten
|
||||
start_application()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user