diff --git a/install.sh b/install.sh index 27d5211..c5a00cb 100755 --- a/install.sh +++ b/install.sh @@ -49,6 +49,7 @@ NO_RESTART=false FRESH_ONBOARDING=false MIGRATE_ONLY=false MIGRATE_TO_VENV=false +MIGRATE_TO_OFFICIAL=false # Farben (nur wenn Terminal) if [ -t 1 ]; then @@ -242,6 +243,56 @@ print(Path(gateway.config.__file__).parent.parent / 'plugins' / 'platforms') return 1 } +# ────────────────────────────────────────────────────────────────────────── +# P-044: Detection-Helpers für offiziellen Hermes-Installer +# Der offizielle Installer (curl -fsSL https://hermes-agent.nousresearch.com/install.sh | bash) +# klont das Hermes-Repo nach ~/.hermes/hermes-agent/ und erstellt einen venv via uv. +# Erkennbar am .git-Verzeichnis im Installationspfad. +# ────────────────────────────────────────────────────────────────────────── + +# Prüft ob die aktuelle Hermes-Installation vom offiziellen Installer stammt. +# Kriterium: ~/.hermes/hermes-agent/.git existiert (git-clone, nicht pip). +is_official_installer() { + [ -d "$HOME/.hermes/hermes-agent/.git" ] +} + +# Prüft ob die aktuelle Hermes-Installation via pip/pipx erfolgt ist. +# Kriterium: venv existiert, aber KEIN .git (kein git-clone). +is_pip_install() { + [ -d "$HOME/.hermes/hermes-agent/venv" ] && \ + [ -x "$HOME/.hermes/hermes-agent/venv/bin/python" ] && \ + [ ! -d "$HOME/.hermes/hermes-agent/.git" ] +} + +# Löst den venv-Pfad über den Shebang des hermes-Kommandos auf. +# Liest die erste Zeile von ~/.local/bin/hermes (oder command -v hermes), +# extrahiert den Python-Interpreter-Pfad und leitet daraus den venv ab. +# Beispiel: #!/home/bernd/.hermes/hermes-agent/venv/bin/python +# → /home/bernd/.hermes/hermes-agent/venv +resolve_hermes_venv_from_shebang() { + local hermes_cmd + hermes_cmd="$(command -v hermes 2>/dev/null || true)" + if [ -z "$hermes_cmd" ]; then + hermes_cmd="$HOME/.local/bin/hermes" + fi + if [ -f "$hermes_cmd" ] && [ -r "$hermes_cmd" ]; then + local shebang + shebang="$(head -1 "$hermes_cmd" 2>/dev/null || true)" + # Shebang: #!/path/to/venv/bin/python + if [[ "$shebang" =~ ^#!.*/bin/python ]]; then + local python_path="${shebang#\#!}" + python_path="${python_path%% *}" # Entferne ggf. Argumente nach dem Pfad + local venv_path + venv_path="$(dirname "$(dirname "$python_path")")" + if [ -d "$venv_path" ] && [ -x "$venv_path/bin/python" ]; then + echo "$venv_path" + return 0 + fi + fi + fi + return 1 +} + # ────────────────────────────────────────────────────────────────────────── # 1. System-Dependencies installieren # ────────────────────────────────────────────────────────────────────────── @@ -637,6 +688,247 @@ migrate_to_venv() { echo "" } +# ────────────────────────────────────────────────────────────────────────── +# 3d. Migration: pip → offizieller Hermes-Installer (P-044) +# Aufruf: install.sh --migrate-to-official +# Andreas' Entscheidungen: +# A1: curl-bash direkt (nicht herunterladen+inspizieren) +# A2: --migrate Flag (nicht automatisch) +# A3: 7-Tage Auto-Backup (at-Befehl löscht Backup nach 7 Tagen) +# ────────────────────────────────────────────────────────────────────────── +migrate_to_official_installer() { + banner "Migration: pip → offizieller Hermes-Installer (P-044)" + + local hermes_agent_dir="$HOME/.hermes/hermes-agent" + local official_installer_url="https://hermes-agent.nousresearch.com/install.sh" + + # ── X1: Detection — ist die Migration überhaupt nötig? ── + if is_official_installer; then + ok "Offizieller Installer bereits aktiv: $hermes_agent_dir/.git vorhanden" + ok "Migration nicht nötig — Installation ist bereits offiziell." + return 0 + fi + + if ! is_pip_install; then + warn "Keine pip-basierte Installation gefunden unter $hermes_agent_dir/venv/" + warn "Migration nur für pip-Installationen unterstützt." + if [ -d "$hermes_agent_dir/venv" ]; then + warn "venv existiert, aber .git fehlt — möglicherweise beschädigte Installation." + fi + return 0 + fi + + log "pip-basierte Installation erkannt: $hermes_agent_dir/venv/ (kein .git)" + log "Ziel: Offizieller Installer via curl-bash (A1: direkt, kein Download+Inspect)" + + # ── X2: Pre-Flight-Checks ── + if ! command -v curl >/dev/null 2>&1; then + die "curl nicht verfügbar — wird für den offiziellen Installer benötigt" + fi + + # Gateway stoppen vor der Migration + local was_active=false + if gateway_is_active; then + was_active=true + log "Stoppe Gateway vor Migration..." + if ! $DRY_RUN; then + gateway_systemctl stop 2>/dev/null || true + local wait=0 + while gateway_is_active && [ $wait -lt 30 ]; do + sleep 1; wait=$((wait + 1)) + done + if gateway_is_active; then + die "Gateway konnte nach 30s nicht gestoppt werden" + fi + ok "Gateway gestoppt" + fi + fi + + # ── X3: Backup der alten pip-Installation (A3: 7-Tage Auto-Backup) ── + local today + today="$(date +%Y%m%d)" + local backup_dir="/tmp" + local backup_tar="$backup_dir/hermes-pip-backup-${today}.tar.gz" + + if $DRY_RUN; then + printf '%s[dry]%s tar czf %s %s/venv %s/.gitconfig 2>/dev/null\\n' \ + "$C_YELLOW" "$C_RESET" "$backup_tar" "$hermes_agent_dir" "$hermes_agent_dir" + printf '%s[dry]%s echo "rm -f %s" | at now + 7 days\\n' \ + "$C_YELLOW" "$C_RESET" "$backup_tar" + else + log "Backup: $backup_tar (A3: automatische Löschung in 7 Tagen)" + # Backup des alten venv + eventueller pip-Metadaten + # .gitconfig kann existieren wenn pip install im git-repo lief + tar czf "$backup_tar" \ + -C "$hermes_agent_dir" \ + venv/ \ + .gitconfig 2>/dev/null \ + || die "Backup-tar fehlgeschlagen: $backup_tar" + ok "Backup erstellt: $backup_tar ($(du -h "$backup_tar" | cut -f1))" + + # A3: 7-Tage Auto-Löschung via at + if command -v at >/dev/null 2>&1; then + echo "rm -f \"$backup_tar\"" | at now + 7 days 2>/dev/null && \ + ok "Auto-Löschung geplant: $backup_tar wird in 7 Tagen gelöscht (A3)" || \ + warn "at-Befehl fehlgeschlagen — Backup muss manuell gelöscht werden: $backup_tar" + else + warn "at nicht verfügbar — Backup muss manuell gelöscht werden: $backup_tar" + fi + fi + + # ── X4: Alten venv löschen (offizieller Installer legt neuen an) ── + if ! $DRY_RUN; then + log "Lösche alten pip-venv: $hermes_agent_dir/venv/" + rm -rf "$hermes_agent_dir/venv" + # Auch eventuelle pip-Reste entfernen + rm -f "$hermes_agent_dir/pyvenv.cfg" 2>/dev/null || true + ok "Alter pip-venv gelöscht" + fi + + # ── X5: Offiziellen Installer ausführen (A1: curl-bash direkt) ── + banner "Offizieller Hermes-Installer (curl-bash)" + log "Führe aus: curl -fsSL $official_installer_url | bash -s -- --skip-setup --non-interactive" + + if $DRY_RUN; then + printf '%s[dry]%s curl -fsSL %s | bash -s -- --skip-setup --non-interactive\\n' \ + "$C_YELLOW" "$C_RESET" "$official_installer_url" + else + # A1: Direkt via curl-bash, kein Download+Inspect + # --skip-setup: kein interaktiver Setup-Wizard + # --non-interactive: keine Prompts + if curl -fsSL "$official_installer_url" | bash -s -- --skip-setup --non-interactive; then + ok "Offizieller Installer erfolgreich ausgeführt" + else + err "Offizieller Installer fehlgeschlagen (Exit-Code: $?)" + echo "" + echo " ┌─────────────────────────────────────────────────────────────┐" + echo " │ ROLLBACK: Backup wiederherstellen │" + echo " │ │" + echo " │ tar xzf $backup_tar -C $hermes_agent_dir/ │" + echo " │ systemctl --user restart hermes-gateway │" + echo " └─────────────────────────────────────────────────────────────┘" + echo "" + die "Migration fehlgeschlagen — Backup liegt unter $backup_tar" + fi + fi + + # ── X6: Post-Migration: Plugin-Customizations wiederherstellen ── + banner "Post-Migration: Plugin-Customizations" + + # Venv-Pfad nach offizieller Installation + local new_venv="$hermes_agent_dir/venv" + if ! $DRY_RUN; then + if [ ! -x "$new_venv/bin/python" ]; then + die "Offizieller venv nicht gefunden: $new_venv/bin/python — Migration unvollständig" + fi + ok "Offizieller venv bereit: $new_venv" + fi + + local VENV_PY_SAVED="$VENV_PY" + VENV_PY="$new_venv/bin/python" + PIPX_VENV="$new_venv" + + # 6a: snowballstemmer in neuen venv injecten (P-029) + log "Injecte snowballstemmer in offiziellen venv (P-029)..." + if ! $DRY_RUN; then + "$VENV_PY" -m pip install --quiet snowballstemmer>=3.0.0 + if ! "$VENV_PY" -c 'import snowballstemmer' 2>/dev/null; then + die "snowballstemmer im offiziellen venv nicht importierbar — P-029-Regression!" + fi + ok "snowballstemmer in offiziellem venv (P-029)" + else + printf '%s[dry]%s %s -m pip install snowballstemmer\\n' "$C_YELLOW" "$C_RESET" "$VENV_PY" + fi + + # 6b: requirements.txt aus dem Plugin-Repo installieren + local script_dir + script_dir="$(cd "$(dirname "$0")" && pwd)" + local req="$script_dir/requirements.txt" + if [ -f "$req" ]; then + log "Installiere requirements.txt in offiziellen venv..." + if ! $DRY_RUN; then + "$VENV_PY" -m pip install --quiet -r "$req" + ok "requirements.txt installiert" + else + printf '%s[dry]%s %s -m pip install -r %s\\n' "$C_YELLOW" "$C_RESET" "$VENV_PY" "$req" + fi + fi + + # 6c: Plugin deployen (User-Space, Variante B) + deploy_plugin + + # 6d: Archivar-Symlink neu setzen + local user_dst="$HOME/.hermes/plugins/platforms/rocketchat" + if [ -d "$user_dst/archivar" ]; then + local site_packages + site_packages="$(find_site_packages "$new_venv")" + [ -n "$site_packages" ] || site_packages="$(ls -d "$new_venv/lib/python3."*"/site-packages" 2>/dev/null | head -1)" + if [ -n "$site_packages" ]; then + local archivar_link="$site_packages/archivar" + log "Archivar-Symlink: $archivar_link -> $user_dst/archivar" + if ! $DRY_RUN; then + ln -sfn "$user_dst/archivar" "$archivar_link" + ok "Archivar-Symlink aktualisiert" + fi + else + warn "site-packages nicht gefunden — Archivar-Symlink nicht gesetzt" + fi + fi + + # 6e: Toolset-Registrierung sicherstellen + ensure_toolset + + # 6f: Gateway Service aktualisieren (ExecStart auf neuen venv-Pfad) + if ! $DRY_RUN; then + local unit="$HOME/.config/systemd/user/hermes-gateway.service" + if [ -f "$unit" ]; then + log "Aktualisiere Gateway-Service ExecStart auf offiziellen venv..." + sed -i "s|^ExecStart=.*|ExecStart=$VENV_PY -m hermes_cli.main gateway run|" "$unit" + gateway_daemon_reload + ok "Gateway-Service ExecStart aktualisiert" + fi + fi + + # 6g: Gateway neustarten (falls vorher aktiv) + if $was_active && ! $DRY_RUN; then + log "Starte Gateway neu..." + gateway_systemctl start 2>/dev/null || true + local wait=0 + while ! gateway_is_active && [ $wait -lt 30 ]; do + sleep 1; wait=$((wait + 1)) + done + if gateway_is_active; then + ok "Gateway neugestartet und aktiv (nach ${wait}s)" + else + warn "Gateway nach 30s nicht aktiv — prüfe: gateway_journalctl -n 20" + fi + fi + + # Venv-Pfad zurücksetzen (falls andere Funktionen danach noch laufen) + VENV_PY="$VENV_PY_SAVED" + + # ── X7: Abschluss ── + banner "Migration abgeschlossen: pip → offizieller Installer" + echo "" + echo " ┌─────────────────────────────────────────────────────────────┐" + echo " │ MIGRATION ERFOLGREICH (P-044) │" + echo " │ │" + echo " │ ✓ Offizieller Hermes-Installer aktiv │" + echo " │ ✓ Venv: $new_venv │" + echo " │ ✓ Backup: $backup_tar (Auto-Löschung in 7 Tagen) │" + echo " │ ✓ Plugin-Customizations wiederhergestellt │" + echo " │ │" + echo " │ Verifikation: │" + echo " │ hermes --version │" + echo " │ gateway_journalctl -f │" + echo " │ │" + echo " │ Rollback (falls nötig): │" + echo " │ tar xzf $backup_tar -C $hermes_agent_dir/ │" + echo " │ systemctl --user restart hermes-gateway │" + echo " └─────────────────────────────────────────────────────────────┘" + echo "" +} + # ────────────────────────────────────────────────────────────────────────── # 4. config.yaml generieren (aus env-Variablen) # P2: auxiliary darf NICHT openrouter/Nous versuchen (payment errors) @@ -1350,6 +1642,7 @@ OPTIONEN: --fresh-onboarding room-map.json + archivar_state.json löschen (frisches Onboarding) --migrate Migration: Bundled → User-Space (Variante B) --migrate-to-venv Migration: pipx → User-Space-venv (P3, explizit) + --migrate-to-official Migration: pip → offizieller Hermes-Installer (P-044, explizit) --skip-system-deps apt-Install überspringen (für Container/CI) --skip-ollama-models ollama pull überspringen @@ -1402,6 +1695,7 @@ while [ $# -gt 0 ]; do --fresh-onboarding) FRESH_ONBOARDING=true; shift ;; --migrate) MIGRATE_ONLY=true; shift ;; --migrate-to-venv) MIGRATE_TO_VENV=true; shift ;; + --migrate-to-official) MIGRATE_TO_OFFICIAL=true; shift ;; --*) err "Unbekannte Option: $1"; print_help; exit 1 ;; *) err "Unbekanntes Argument: $1"; exit 1 ;; esac @@ -1483,8 +1777,8 @@ if $DRY_RUN; then warn "DRY-RUN — nichts wird geändert, nur angezeigt" fi -# Erforderliche Secrets prüfen (außer im --check und --update-only Modus) -if ! $CHECK_ONLY && ! $UPDATE_ONLY; then +# Erforderliche Secrets prüfen (außer im --check, --update-only und Migrations-Modus) +if ! $CHECK_ONLY && ! $UPDATE_ONLY && ! $MIGRATE_ONLY && ! $MIGRATE_TO_VENV && ! $MIGRATE_TO_OFFICIAL; then local_missing=0 [ -z "$OLLAMA_CLOUD_API_KEY" ] && { err "OLLAMA_CLOUD_API_KEY fehlt"; local_missing=1; } [ -z "$ROCKETCHAT_PASSWORD" ] && { err "ROCKETCHAT_PASSWORD fehlt"; local_missing=1; } @@ -1583,6 +1877,16 @@ if $MIGRATE_TO_VENV; then exit 0 fi +if $MIGRATE_TO_OFFICIAL; then + # P-044: Migration von pip → offizieller Hermes-Installer (explizit, nicht automatisch). + # A2: Nur mit --migrate-to-official Flag, nicht automatisch. + # Führt Backup, curl-bash, Plugin-Customizations und Gateway-Neustart durch. + banner "Migration-Only (pip → offizieller Hermes-Installer)" + read_env + migrate_to_official_installer + exit 0 +fi + if $UPDATE_ONLY; then # V8.6: Update-Only — Plugin-Code aktualisieren (git pull) + deployen # + __pycache__-Cleanup + Service-Restart + Post-Deploy-Verifikation.