Projektarbeit-MYP/install.sh

218 lines
5.3 KiB
Bash
Executable File

#!/bin/bash
# MYP Reservation Platform - Zentraler Installer
# Unterstützt Backend und Frontend Installation
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$SCRIPT_DIR"
# Farben für Output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging
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"
}
# Hilfsfunktionen
check_root() {
if [[ $EUID -eq 0 ]]; then
log_error "Dieses Skript sollte nicht als root ausgeführt werden!"
exit 1
fi
}
check_dependencies() {
local deps=("python3.11" "curl" "openssl")
for dep in "${deps[@]}"; do
if ! command -v "$dep" &> /dev/null; then
log_error "Abhängigkeit '$dep' nicht gefunden!"
exit 1
fi
done
}
install_backend() {
log_info "=== Backend Installation ==="
cd "$PROJECT_ROOT/backend"
# Python Virtual Environment erstellen
log_info "Erstelle Python Virtual Environment..."
python3.11 -m venv venv
source venv/bin/activate
# Requirements installieren
log_info "Installiere Python-Abhängigkeiten..."
pip install --upgrade pip
pip install -r requirements.txt
# Zertifikate kopieren
log_info "Kopiere TLS-Zertifikate..."
mkdir -p app/certs
cp "$PROJECT_ROOT/backend/app/certs/backend.crt" app/certs/
cp "$PROJECT_ROOT/backend/app/certs/backend.key" app/certs/
chmod 600 app/certs/backend.key
chmod 644 app/certs/backend.crt
# Systemd Service installieren
log_info "Installiere systemd Service..."
sudo cp myp.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable myp.service
# Datenbank initialisieren
log_info "Initialisiere Datenbank..."
cd app
python3.11 init_db.py
log_success "Backend Installation abgeschlossen!"
log_info "Service starten mit: sudo systemctl start myp.service"
}
install_frontend() {
log_info "=== Frontend Installation ==="
cd "$PROJECT_ROOT/frontend"
# Docker prüfen
if ! command -v docker &> /dev/null; then
log_error "Docker ist nicht installiert!"
exit 1
fi
if ! command -v docker-compose &> /dev/null; then
log_error "Docker Compose ist nicht installiert!"
exit 1
fi
# Zertifikate kopieren
log_info "Kopiere TLS-Zertifikate..."
mkdir -p certs
cp "$PROJECT_ROOT/frontend/certs/frontend.crt" certs/
cp "$PROJECT_ROOT/frontend/certs/frontend.key" certs/
chmod 600 certs/frontend.key
chmod 644 certs/frontend.crt
# Caddyfile Symlink erstellen
log_info "Erstelle Caddyfile Symlink..."
mkdir -p docker/caddy
if [[ ! -L docker/caddy/Caddyfile ]]; then
ln -sf "$PROJECT_ROOT/frontend/docker/caddy/Caddyfile" docker/caddy/Caddyfile
fi
# Docker Images bauen
log_info "Baue Docker Images..."
docker-compose build
# Services starten
log_info "Starte Frontend Services..."
docker-compose up -d
log_success "Frontend Installation abgeschlossen!"
log_info "Frontend verfügbar unter: https://m040tbaraspi001.de040.corpintra.net"
}
health_check_backend() {
log_info "=== Backend Health Check ==="
local max_attempts=30
local attempt=1
while [[ $attempt -le $max_attempts ]]; do
log_info "Versuche Backend-Verbindung (Versuch $attempt/$max_attempts)..."
if curl -k -s --max-time 5 https://raspberrypi/api/test > /dev/null 2>&1; then
log_success "Backend ist erreichbar!"
return 0
fi
sleep 2
((attempt++))
done
log_error "Backend Health Check fehlgeschlagen!"
return 1
}
health_check_frontend() {
log_info "=== Frontend Health Check ==="
local max_attempts=30
local attempt=1
while [[ $attempt -le $max_attempts ]]; do
log_info "Versuche Frontend-Verbindung (Versuch $attempt/$max_attempts)..."
if curl -k -s --max-time 5 https://m040tbaraspi001.de040.corpintra.net/ > /dev/null 2>&1; then
log_success "Frontend ist erreichbar!"
return 0
fi
sleep 2
((attempt++))
done
log_error "Frontend Health Check fehlgeschlagen!"
return 1
}
show_usage() {
echo "Usage: $0 [backend|frontend]"
echo ""
echo "Optionen:"
echo " backend - Installiert das Backend (Python/Flask)"
echo " frontend - Installiert das Frontend (Docker/Next.js)"
echo ""
echo "Beispiele:"
echo " $0 backend # Backend installieren"
echo " $0 frontend # Frontend installieren"
}
# Main
main() {
log_info "MYP Reservation Platform - Installer v3.2"
log_info "Projektpfad: $PROJECT_ROOT"
check_root
check_dependencies
case "${1:-}" in
"backend")
install_backend
health_check_backend
;;
"frontend")
install_frontend
health_check_frontend
;;
*)
show_usage
exit 1
;;
esac
log_success "Installation erfolgreich abgeschlossen!"
}
# Skript ausführen
main "$@"