Files
nexus/scripts/srv04-install.sh

136 lines
5.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Installiert Nexus auf srv04. Läuft AUF srv04 und braucht root (sudo).
#
# Was es tut (und nichts anderes):
# 1. legt /srv/apps/nexus/{releases/<version>,data} an (Besitzer svc-scotty)
# 2. sichert die vorhandene Datenbank nach data/backups/ (ab 0.1.4; die neue
# Version kann das Schema anheben, ältere öffnen es danach nicht mehr)
# 3. kopiert das Binary nach releases/<version>/nexus und schaltet den
# Symlink /srv/apps/nexus/current darauf um (die alte Version bleibt)
# 4. legt /srv/apps/nexus/config.toml an, NUR wenn es sie noch nicht gibt
# 5. schreibt /etc/systemd/system/nexus.service und lädt systemd neu
# Es startet und aktiviert den Dienst NICHT, außer mit --start bzw. --enable.
# Es ändert keine bestehende Engine-Installation und keine Modelle.
#
# Aufruf: sudo VERSION=0.1.3 bash install.sh [--start] [--enable]
# sudo bash install.sh --rollback (vorherige Version, Dienst neu starten)
set -euo pipefail
USER_NAME="svc-scotty"
BASE="/srv/apps/nexus"
UNIT="/etc/systemd/system/nexus.service"
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LISTEN="${LISTEN:-127.0.0.1:8080}"
[ "$(id -u)" -eq 0 ] || { echo "Bitte mit sudo ausführen." >&2; exit 1; }
id "$USER_NAME" >/dev/null 2>&1 || { echo "Benutzer $USER_NAME fehlt." >&2; exit 1; }
as_user() { sudo -u "$USER_NAME" "$@"; }
if [ "${1:-}" = "--rollback" ]; then
[ -d "$BASE/releases" ] || { echo "Keine Releases unter $BASE/releases." >&2; exit 1; }
current="$(basename "$(readlink -f "$BASE/current")")"
previous="$(ls -1t "$BASE/releases" | grep -vx "$current" | head -n 1 || true)"
[ -n "$previous" ] || { echo "Es gibt keine ältere Version für den Rückfall." >&2; exit 1; }
ln -sfn "$BASE/releases/$previous" "$BASE/current.new"
mv -T "$BASE/current.new" "$BASE/current"
echo "Rückfall: current zeigt jetzt auf $previous (vorher $current)."
backup="$(ls -1t "$BASE/data/backups"/nexus-vor-"$current"-*.db 2>/dev/null | head -n 1 || true)"
if [ -n "$backup" ]; then
echo "Hat $current das Datenbank-Schema angehoben, öffnet $previous die Datenbank nicht mehr."
echo "Die Sicherung von vor $current liegt unter: $backup"
echo "Zurückspielen (Daten seit dem Update gehen verloren):"
echo " sudo systemctl stop nexus && sudo -u $USER_NAME cp '$backup' '$BASE/data/nexus.db' && sudo systemctl start nexus"
fi
if systemctl is-active --quiet nexus; then systemctl restart nexus && echo "Dienst neu gestartet."; fi
exit 0
fi
VERSION="${VERSION:?VERSION fehlt (zum Beispiel VERSION=0.1.3)}"
[ -x "$HERE/nexus" ] || { echo "$HERE/nexus fehlt oder ist nicht ausführbar." >&2; exit 1; }
"$HERE/nexus" version | grep -q "$VERSION" || { echo "Das Binary meldet nicht die Version $VERSION." >&2; exit 1; }
install -d -o "$USER_NAME" -g "$USER_NAME" -m 0755 "$BASE" "$BASE/releases" "$BASE/releases/$VERSION"
install -d -o "$USER_NAME" -g "$USER_NAME" -m 0750 "$BASE/data"
install -o "$USER_NAME" -g "$USER_NAME" -m 0755 "$HERE/nexus" "$BASE/releases/$VERSION/nexus"
fresh=1
if [ -f "$BASE/data/nexus.db" ] && [ -f "$BASE/config.toml" ]; then
fresh=0
# Mit dem neuen Binary, das die Datenbank dabei nur liest (keine Migration).
backup="$BASE/data/backups/nexus-vor-$VERSION-$(date -u +%Y%m%dT%H%M%SZ).db"
as_user "$BASE/releases/$VERSION/nexus" backup --out "$backup" --config "$BASE/config.toml"
fi
ln -sfn "$BASE/releases/$VERSION" "$BASE/current.new"
mv -T "$BASE/current.new" "$BASE/current"
echo "Version $VERSION installiert, current zeigt darauf."
if [ -e "$BASE/config.toml" ]; then
echo "config.toml existiert und bleibt unverändert."
else
cat >"$BASE/config.toml" <<CONF
# Nexus auf srv04. Enthält keine Geheimnisse.
listen = "$LISTEN"
data_dir = "$BASE/data"
log_level = "info"
[llama_cpp]
binary = "/srv/apps/llama.cpp/build/bin/llama-server"
model_roots = ["/srv/models/gguf"]
port_from = 18080
port_to = 18180
ready_timeout_secs = 300
gpu_layers = 99
CONF
chown "$USER_NAME:$USER_NAME" "$BASE/config.toml"
chmod 0640 "$BASE/config.toml"
echo "config.toml angelegt (listen = $LISTEN)."
fi
cat >"$UNIT" <<UNITFILE
[Unit]
Description=Nexus AI Gateway
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=$USER_NAME
Group=$USER_NAME
WorkingDirectory=$BASE
ExecStart=$BASE/current/nexus serve --config $BASE/config.toml
Restart=on-failure
RestartSec=3
KillSignal=SIGTERM
TimeoutStopSec=60
NoNewPrivileges=true
PrivateTmp=true
[Install]
WantedBy=multi-user.target
UNITFILE
systemctl daemon-reload
echo "Dienst nexus.service geschrieben und systemd neu geladen."
for arg in "$@"; do
case "$arg" in
--enable) systemctl enable nexus && echo "Dienst aktiviert (startet beim Booten)." ;;
--start) systemctl restart nexus && echo "Dienst gestartet." ;;
esac
done
echo
if [ "$fresh" -eq 1 ]; then
# Neue Installation: noch keine Modelle und Keys.
echo "Nächste Schritte (als $USER_NAME):"
echo " sudo -u $USER_NAME $BASE/current/nexus model add --name bonsai --path /srv/models/gguf/Bonsai-1.7B-Q1_0.gguf --gpus 0 --config $BASE/config.toml"
echo " sudo -u $USER_NAME $BASE/current/nexus key create --name test --config $BASE/config.toml"
echo " sudo systemctl start nexus && journalctl -u nexus -f"
else
# Update: Modelle und Keys bleiben, nichts neu anlegen.
echo "Update: Datenbank, Modelle und Keys bleiben erhalten (Sicherung siehe oben)."
case " $* " in
*" --start "*) ;;
*) echo "Die neue Version läuft erst nach: sudo systemctl restart nexus" ;;
esac
fi