#!/bin/bash # AI Profile Router – Installation/Update auf dem Zielsystem. # Wird als root auf dem Zielsystem ausgeführt (per SSH, vgl. deploy.sh). # Erwartet ai_profile_router.py, image_worker.py und mike-ai-profile-router.service # im selben Verzeichnis wie dieses Skript. set -euo pipefail DIR="$(cd "$(dirname "$0")" && pwd)" INSTALL_DIR=/opt/mike-ai/ai-profile-router SERVICE=mike-ai-profile-router.service OLD_SERVICE=mike-ai-local-llm-router.service OLD_DIR=/opt/mike-ai/local-llm-router BACKUP_DIR=/opt/mike-ai/.backup-ai-profile-router-$(date +%Y%m%d-%H%M%S) VENV="$INSTALL_DIR/venv" MODEL_DIR=/opt/mike-ai/models/FLUX.2-klein-base-4B IMAGE_DIR="$INSTALL_DIR/images" echo "== AI Profile Router: Installation/Update ==" # --- 1. Bestehenden (alten) Router entfernen, mit Backup ------------------- if [ -f "/etc/systemd/system/${OLD_SERVICE}" ]; then echo "-- Entferne alten Router ${OLD_SERVICE} (Backup: ${BACKUP_DIR})" mkdir -p "$BACKUP_DIR" systemctl stop "$OLD_SERVICE" 2>/dev/null || true systemctl disable "$OLD_SERVICE" 2>/dev/null || true mv "/etc/systemd/system/${OLD_SERVICE}" "$BACKUP_DIR/" if [ -d "$OLD_DIR" ]; then mv "$OLD_DIR" "$BACKUP_DIR/local-llm-router" fi else echo "-- Kein alter Router gefunden" fi # --- 2. Neue Dateien installieren ------------------------------------------- mkdir -p "$INSTALL_DIR" "$IMAGE_DIR" install -m 0755 "$DIR/ai_profile_router.py" "$INSTALL_DIR/ai_profile_router.py" install -m 0755 "$DIR/image_worker.py" "$INSTALL_DIR/image_worker.py" install -m 0644 "$DIR/${SERVICE}" "/etc/systemd/system/${SERVICE}" # --- 3. Python-Venv mit Bild-Abhängigkeiten --------------------------------- if [ ! -x "$VENV/bin/python" ]; then echo "-- Erstelle Python-Venv in $VENV" python3 -m venv "$VENV" fi echo "-- Installiere/aktualisiere Bild-Abhängigkeiten (torch, diffusers, ...)" "$VENV/bin/pip" install --quiet --upgrade pip "$VENV/bin/pip" install --quiet \ torch \ diffusers \ transformers \ accelerate # --- 4. FLUX-Modell (nur wenn noch nicht vorhanden) -------------------------- if [ -f "$MODEL_DIR/model_index.json" ]; then echo "-- FLUX-Modell vorhanden: $MODEL_DIR" else echo "-- Lade FLUX.2-klein-base-4B nach $MODEL_DIR (kann dauern)" "$VENV/bin/python" - <<'PY' import os from huggingface_hub import snapshot_download snapshot_download( repo_id="black-forest-labs/FLUX.2-klein-base-4B", local_dir="/opt/mike-ai/models/FLUX.2-klein-base-4B", local_dir_use_symlinks=False, ) print("Modell-Download abgeschlossen") PY fi # --- 5. Service aktivieren und starten --------------------------------------- systemctl daemon-reload systemctl enable "$SERVICE" systemctl restart "$SERVICE" # --- 6. Verifikation ---------------------------------------------------------- sleep 1 if ! systemctl is-active --quiet "$SERVICE"; then echo "-- FEHLER: Service läuft nicht" >&2 journalctl -u "$SERVICE" -n 20 --no-pager >&2 exit 1 fi echo "-- Service läuft" curl -sf "http://127.0.0.1:8081/status" | python3 -m json.tool echo "== Fertig =="