"feat: Update service installer scripts for better maintainability"

This commit is contained in:
Till Tomczak 2025-05-27 06:52:21 +02:00
parent 320f56f32a
commit 32ec001b62
3 changed files with 161 additions and 122 deletions

View File

@ -1,12 +1,12 @@
[Unit]
Description=MYP Flask Backend
Description=MYP Flask Backend (Python 3.11)
After=network-online.target
Wants=network-online.target
[Service]
User=pi
WorkingDirectory=/opt/myp
ExecStart=/opt/myp/.venv/bin/python /opt/myp/app.py
ExecStart=/opt/myp/.venv/bin/python3.11 /opt/myp/app.py
Restart=always
Environment=PYTHONUNBUFFERED=1

View File

@ -1,5 +1,5 @@
[Unit]
Description=MYP Reservation Platform Backend
Description=MYP Reservation Platform Backend (Python 3.11)
After=network.target
Wants=network.target
@ -11,7 +11,8 @@ WorkingDirectory=/home/user/Projektarbeit-MYP/backend/app
Environment=PYTHONPATH=/home/user/Projektarbeit-MYP/backend/app
Environment=FLASK_ENV=production
Environment=FLASK_APP=app.py
ExecStart=/home/user/Projektarbeit-MYP/backend/venv/bin/python3 app.py --host 0.0.0.0 --port 443 --cert certs/backend.crt --key certs/backend.key
Environment=PYTHONUNBUFFERED=1
ExecStart=/home/user/Projektarbeit-MYP/backend/venv/bin/python3.11 app.py --host 0.0.0.0 --port 443 --cert certs/backend.crt --key certs/backend.key
Restart=always
RestartSec=10
StandardOutput=journal

View File

@ -26,6 +26,132 @@ if [ "$EUID" -eq 0 ]; then
fi
# Funktionen
# Alte Services entfernen
remove_old_services() {
local service_type="$1" # "backend" oder "kiosk" oder "all"
if [ $is_root -eq 1 ]; then
echo -e "${BLUE}Entferne alte MYP Services...${NC}"
# Backend Services
if [ "$service_type" = "backend" ] || [ "$service_type" = "all" ]; then
local backend_services=("myp.service" "myp-platform.service" "myp-backend.service" "myp-reservation.service")
for service in "${backend_services[@]}"; do
if systemctl is-enabled "$service" >/dev/null 2>&1; then
echo -e "${YELLOW}Stoppe und deaktiviere $service...${NC}"
systemctl stop "$service" 2>/dev/null || true
systemctl disable "$service" 2>/dev/null || true
echo -e "${GREEN}$service entfernt${NC}"
fi
done
fi
# Kiosk Services
if [ "$service_type" = "kiosk" ] || [ "$service_type" = "all" ]; then
local kiosk_services=("myp-kiosk-browser.service" "myp-kiosk.service" "kiosk.service" "chromium-kiosk.service")
for service in "${kiosk_services[@]}"; do
if systemctl is-enabled "$service" >/dev/null 2>&1; then
echo -e "${YELLOW}Stoppe und deaktiviere $service...${NC}"
systemctl stop "$service" 2>/dev/null || true
systemctl disable "$service" 2>/dev/null || true
echo -e "${GREEN}$service entfernt${NC}"
fi
done
fi
systemctl daemon-reload
echo -e "${GREEN}✓ Alte Services erfolgreich entfernt${NC}"
else
echo -e "${YELLOW}⚠ Service-Entfernung übersprungen (keine Root-Rechte)${NC}"
echo -e "${BLUE}Manuell ausführen: sudo systemctl stop <service> && sudo systemctl disable <service>${NC}"
fi
}
# Backend Service mit Python 3.11 erstellen
create_backend_service() {
if [ $is_root -eq 1 ]; then
echo -e "${BLUE}Erstelle Backend-Service mit Python 3.11...${NC}"
cat > "/etc/systemd/system/myp.service" << EOF
[Unit]
Description=MYP Reservation Platform Backend (Python 3.11)
After=network.target
Wants=network.target
[Service]
Type=simple
User=$USER
Group=$USER
WorkingDirectory=$PROJECT_DIR/backend/app
Environment=PYTHONPATH=$PROJECT_DIR/backend/app
Environment=FLASK_ENV=production
Environment=FLASK_APP=app.py
Environment=PYTHONUNBUFFERED=1
ExecStart=$PROJECT_DIR/backend/venv/bin/python3.11 app.py --host 0.0.0.0 --port 443 --cert certs/backend.crt --key certs/backend.key
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
SyslogIdentifier=myp-backend
# Security settings
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=$PROJECT_DIR/backend/app/logs
ReadWritePaths=$PROJECT_DIR/backend/app/database
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable myp.service
echo -e "${GREEN}✓ Backend-Service mit Python 3.11 erstellt und aktiviert${NC}"
else
echo -e "${YELLOW}⚠ Service-Erstellung übersprungen (keine Root-Rechte)${NC}"
echo -e "${BLUE}Manuell erstellen: Service-Datei mit Python 3.11 Pfad${NC}"
fi
}
# Kiosk-Browser Service erstellen
create_kiosk_service() {
if [ $is_root -eq 1 ]; then
echo -e "${BLUE}Erstelle Kiosk-Browser-Service...${NC}"
cat > "/etc/systemd/system/myp-kiosk-browser.service" << EOF
[Unit]
Description=MYP Kiosk Browser - 3D Printer Management Kiosk Interface (Python 3.11 Backend)
After=network.target graphical-session.target myp.service
Requires=myp.service
PartOf=myp.service
[Service]
Type=simple
User=$USER
Group=$USER
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/$USER/.Xauthority
ExecStartPre=/bin/bash -c 'until curl -k -s https://localhost:443/ > /dev/null; do sleep 2; done'
ExecStart=/usr/bin/chromium-browser --kiosk --disable-infobars --disable-session-crashed-bubble --disable-translate --no-first-run --disable-features=VizDisplayCompositor --start-fullscreen --autoplay-policy=no-user-gesture-required https://localhost:443/
Restart=always
RestartSec=10
KillMode=mixed
TimeoutStopSec=30
[Install]
WantedBy=graphical-session.target
EOF
systemctl daemon-reload
echo -e "${GREEN}✓ Kiosk-Browser-Service erstellt${NC}"
else
echo -e "${YELLOW}⚠ Kiosk-Service-Erstellung übersprungen (keine Root-Rechte)${NC}"
fi
}
show_header() {
local title="$1"
clear
@ -140,62 +266,8 @@ install_production_backend() {
chmod 644 app/certs/backend.crt
# Alte Services entfernen und neue systemd Services installieren
if [ $is_root -eq 1 ]; then
prod_log_info "Entferne alte Services..."
# Alte Services stoppen und deaktivieren
for old_service in "myp.service" "myp-platform.service" "myp-backend.service"; do
if systemctl is-enabled "$old_service" >/dev/null 2>&1; then
systemctl stop "$old_service" 2>/dev/null || true
systemctl disable "$old_service" 2>/dev/null || true
prod_log_info "Alter Service $old_service entfernt"
fi
done
prod_log_info "Installiere neuen systemd Service mit Python 3.11..."
# Neuen Service mit Python 3.11 erstellen
cat > "/etc/systemd/system/myp.service" << EOF
[Unit]
Description=MYP Reservation Platform Backend (Python 3.11)
After=network.target
Wants=network.target
[Service]
Type=simple
User=$USER
Group=$USER
WorkingDirectory=$PROJECT_DIR/backend/app
Environment=PYTHONPATH=$PROJECT_DIR/backend/app
Environment=FLASK_ENV=production
Environment=FLASK_APP=app.py
Environment=PYTHONUNBUFFERED=1
ExecStart=$PROJECT_DIR/backend/venv/bin/python3.11 app.py --host 0.0.0.0 --port 443 --cert certs/backend.crt --key certs/backend.key
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
SyslogIdentifier=myp-backend
# Security settings
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=$PROJECT_DIR/backend/app/logs
ReadWritePaths=$PROJECT_DIR/backend/app/database
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable myp.service
prod_log_success "Neuer systemd Service mit Python 3.11 installiert"
else
prod_log_warning "systemd Service-Installation übersprungen (keine Root-Rechte)"
prod_log_info "Manuell ausführen: sudo systemctl stop myp.service && sudo systemctl disable myp.service"
prod_log_info "Dann neuen Service erstellen mit Python 3.11 Pfad"
fi
remove_old_services "backend"
create_backend_service
# Datenbank initialisieren
prod_log_info "Initialisiere Datenbank..."
@ -235,35 +307,8 @@ EOF
fi
# Kiosk-Browser-Service für Produktion
if [ $is_root -eq 1 ]; then
prod_log_info "Erstelle Kiosk-Browser-Service für Produktion..."
cat > "/etc/systemd/system/myp-kiosk-browser.service" << EOF
[Unit]
Description=MYP Production Kiosk Browser
After=network.target graphical-session.target myp.service
Requires=myp.service
PartOf=myp.service
[Service]
Type=simple
User=$USER
Group=$USER
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/$USER/.Xauthority
ExecStartPre=/bin/bash -c 'until curl -k -s https://localhost:443/ > /dev/null; do sleep 2; done'
ExecStart=/usr/bin/chromium-browser --kiosk --disable-infobars --disable-session-crashed-bubble --disable-translate --no-first-run --disable-features=VizDisplayCompositor --start-fullscreen --autoplay-policy=no-user-gesture-required https://localhost:443/
Restart=always
RestartSec=10
KillMode=mixed
TimeoutStopSec=30
[Install]
WantedBy=graphical-session.target
EOF
systemctl daemon-reload
prod_log_success "Kiosk-Browser-Service erstellt"
remove_old_services "kiosk"
create_kiosk_service
# Optional: Service automatisch aktivieren
read -p "Soll der Kiosk-Browser beim Boot automatisch starten? (j/n, Standard: j): " auto_kiosk
@ -719,12 +764,21 @@ install_backend() {
return 1
fi
# Virtual Environment erstellen
echo -e "${BLUE}1. Virtual Environment erstellen...${NC}"
# Virtual Environment mit Python 3.11 erstellen
echo -e "${BLUE}1. Virtual Environment mit Python 3.11 erstellen...${NC}"
if [ ! -d "$VENV_DIR" ]; then
exec_command "$python_cmd -m venv $VENV_DIR" "Erstelle Virtual Environment"
# Explizit Python 3.11 verwenden
exec_command "python3.11 -m venv $VENV_DIR" "Erstelle Virtual Environment mit Python 3.11"
else
echo -e "${YELLOW}Virtual Environment existiert bereits${NC}"
# Prüfen ob es Python 3.11 verwendet
if [ -f "$VENV_DIR/bin/python3.11" ]; then
echo -e "${GREEN}✓ Virtual Environment verwendet bereits Python 3.11${NC}"
else
echo -e "${YELLOW}⚠ Virtual Environment verwendet nicht Python 3.11. Wird neu erstellt...${NC}"
rm -rf "$VENV_DIR"
exec_command "python3.11 -m venv $VENV_DIR" "Erstelle neues Virtual Environment mit Python 3.11"
fi
fi
# Virtual Environment aktivieren
@ -761,7 +815,8 @@ install_backend() {
echo -e "${BLUE}6. Datenbank initialisieren...${NC}"
cd "$APP_DIR"
if [ ! -f "database/myp.db" ]; then
exec_command "$python_cmd -c 'from models import init_database, create_initial_admin; init_database(); create_initial_admin()'" "Datenbank initialisieren"
# Explizit Python 3.11 für Datenbank-Initialisierung verwenden
exec_command "python3.11 -c 'from models import init_database, create_initial_admin; init_database(); create_initial_admin()'" "Datenbank mit Python 3.11 initialisieren"
else
echo -e "${YELLOW}Datenbank existiert bereits${NC}"
fi
@ -773,8 +828,16 @@ install_backend() {
create_ssl_certificates
fi
# Alte Services entfernen
echo -e "${BLUE}8. Alte Services entfernen...${NC}"
remove_old_services "all"
# Backend-Service erstellen
echo -e "${BLUE}9. Backend-Service erstellen...${NC}"
create_backend_service
# Kiosk-Konfiguration fragen
echo -e "${BLUE}8. Kiosk-Modus konfigurieren...${NC}"
echo -e "${BLUE}10. Kiosk-Modus konfigurieren...${NC}"
read -p "Soll das Backend auch als Kiosk-Web-Interface konfiguriert werden? (j/n, Standard: j): " enable_kiosk
if [ "$enable_kiosk" != "n" ]; then
configure_backend_for_kiosk
@ -842,35 +905,10 @@ EOF
fi
# Systemd Service für Kiosk-Browser erstellen (falls Root)
remove_old_services "kiosk"
create_kiosk_service
if [ $is_root -eq 1 ]; then
echo -e "${BLUE}Erstelle Kiosk-Browser-Service...${NC}"
cat > "/etc/systemd/system/myp-kiosk-browser.service" << EOF
[Unit]
Description=MYP Kiosk Browser - 3D Printer Management Kiosk Interface
After=network.target graphical-session.target myp.service
Requires=myp.service
PartOf=myp.service
[Service]
Type=simple
User=$USER
Group=$USER
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/$USER/.Xauthority
ExecStartPre=/bin/bash -c 'until curl -k -s https://localhost:443/ > /dev/null; do sleep 2; done'
ExecStart=/usr/bin/chromium-browser --kiosk --disable-infobars --disable-session-crashed-bubble --disable-translate --no-first-run --disable-features=VizDisplayCompositor --start-fullscreen https://localhost:443/
Restart=always
RestartSec=10
KillMode=mixed
TimeoutStopSec=30
[Install]
WantedBy=graphical-session.target
EOF
systemctl daemon-reload
echo -e "${GREEN}✓ Kiosk-Browser-Service erstellt${NC}"
echo -e "${BLUE}Service wird nicht automatisch gestartet. Verwenden Sie: sudo systemctl enable myp-kiosk-browser${NC}"
else
echo -e "${YELLOW}⚠ Kiosk-Service übersprungen (keine Root-Rechte)${NC}"