166 lines
6.3 KiB
Rust
166 lines
6.3 KiB
Rust
//! Nexus: AI Gateway für srv04.
|
||
//!
|
||
//! Befehle: `version`, `selftest`, `serve`, `key ...`, `model ...`, `diag`.
|
||
|
||
mod admin;
|
||
mod bootstrap;
|
||
mod cli;
|
||
mod diag;
|
||
mod logcap;
|
||
mod logging;
|
||
mod selftest;
|
||
mod serve;
|
||
|
||
use std::path::PathBuf;
|
||
use std::process::ExitCode;
|
||
|
||
const HILFE: &str = "\
|
||
Nexus – AI Gateway für srv04
|
||
|
||
Aufruf:
|
||
nexus version Version anzeigen
|
||
nexus selftest [--config PFAD] Konfiguration, Datenverzeichnis und Datenbank prüfen
|
||
nexus serve [--config PFAD] Gateway starten
|
||
nexus key create --name N [--max-priority low|normal|high] [--default-priority ...] [--models a,b|*] [--metrics] [--ui] [--admin]
|
||
nexus key list | enable --name N | disable --name N | remove --name N (nur gesperrte Keys)
|
||
nexus user list | create --name N --role admin|nutzer | reset-password --name N (Passwort zufällig, wird einmal angezeigt)
|
||
nexus model add --name N --path PFAD [--gpus 0,1] [--embeddings --pooling cls] [--context N] [--vram-mb N] [--ram-mb N] [--idle-min N] [--quant Q] [--disabled]
|
||
nexus model add --name N --engine ollama --tag MODELLNAME [--gpus 0] [--disabled]
|
||
nexus model add --name N --engine maya --maya-config PFAD.json [--gpus 0,1] [--disabled]
|
||
nexus model add --name N --engine strata --strata-config PFAD.json [--gpus 0,1] [--disabled]
|
||
nexus model add --name N --engine tts --tts-model QWEN-MODELL [--gpus 0] [--vram-mb N] [--disabled] (Sprachausgabe, laufendes Studio)
|
||
nexus model set --name N [--vram-mb N] [--idle-min MINUTEN (0 = nie) | --idle-default]
|
||
nexus model list | enable --name N | disable --name N | remove --name N (nur gesperrte Modelle) | discover [--engine llama.cpp|ollama|maya|strata|tts]
|
||
nexus diag [--out DATEI.tar.gz] [--ohne-engine-logs] Diagnose-Archiv ohne Prompts und Keys
|
||
nexus backup [--out DATEI.db] Kopie der Datenbank (ohne sie zu migrieren; auch bei laufendem Gateway)
|
||
nexus schema [--config PFAD] Schema-Version von Programm und Datenbank ausgeben (nur lesend, ohne Migration)
|
||
nexus help Diese Hilfe
|
||
|
||
Die Konfiguration kommt aus --config, sonst NEXUS_CONFIG, sonst ./config.toml, sonst gelten Standardwerte.
|
||
";
|
||
|
||
#[tokio::main]
|
||
async fn main() -> ExitCode {
|
||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||
match args.first().map(String::as_str) {
|
||
Some("version") | Some("--version") => {
|
||
println!("nexus {}", nexus_core::VERSION);
|
||
ExitCode::SUCCESS
|
||
}
|
||
Some("selftest") => run_selftest(&args[1..]),
|
||
Some("serve") => serve::command(&args[1..]).await,
|
||
Some("key") => admin::key(&args[1..]),
|
||
Some("user") => admin::user(&args[1..]),
|
||
Some("model") => admin::model(&args[1..]).await,
|
||
Some("diag") => diag::command(&args[1..]),
|
||
Some("backup") => backup(&args[1..]),
|
||
Some("schema") => schema(&args[1..]),
|
||
Some("help") | Some("--help") | None => {
|
||
print!("{HILFE}");
|
||
ExitCode::SUCCESS
|
||
}
|
||
Some(other) => {
|
||
eprintln!("Unbekannter Befehl: {other}\n");
|
||
eprint!("{HILFE}");
|
||
ExitCode::from(2)
|
||
}
|
||
}
|
||
}
|
||
|
||
fn run_selftest(args: &[String]) -> ExitCode {
|
||
let config = match config_path(args) {
|
||
Ok(path) => path,
|
||
Err(message) => {
|
||
eprintln!("{message}");
|
||
return ExitCode::from(2);
|
||
}
|
||
};
|
||
let checks = selftest::run(config.as_deref());
|
||
let mut failed = 0;
|
||
for c in &checks {
|
||
match &c.result {
|
||
Ok(detail) => println!("ok {}: {detail}", c.name),
|
||
Err(detail) => {
|
||
failed += 1;
|
||
println!("FEHLER {}: {detail}", c.name);
|
||
}
|
||
}
|
||
}
|
||
if failed == 0 {
|
||
println!("Selbsttest bestanden ({} Prüfungen).", checks.len());
|
||
ExitCode::SUCCESS
|
||
} else {
|
||
println!(
|
||
"Selbsttest fehlgeschlagen ({failed} von {} Prüfungen).",
|
||
checks.len()
|
||
);
|
||
ExitCode::FAILURE
|
||
}
|
||
}
|
||
|
||
/// Ermittelt den Konfigurationspfad: `--config`, sonst `NEXUS_CONFIG`.
|
||
fn config_path(args: &[String]) -> Result<Option<PathBuf>, String> {
|
||
match args {
|
||
[] => Ok(std::env::var_os("NEXUS_CONFIG").map(PathBuf::from)),
|
||
[flag, path] if flag == "--config" => Ok(Some(PathBuf::from(path))),
|
||
_ => Err("Aufruf: nexus selftest [--config PFAD]".to_string()),
|
||
}
|
||
}
|
||
|
||
/// `nexus backup`: Kopie der Datenbank, Standard `data_dir/backups/nexus-<zeit>.db`.
|
||
/// Gibt die Schema-Version des Programms und der Datenbank aus, **nur lesend und ohne Migration** (→ #1176). Das
|
||
/// Installationsskript prüft damit vor einem Rückfall, ob die Datenbank zum Zielstand passt.
|
||
fn schema(args: &[String]) -> ExitCode {
|
||
println!("programm {}", nexus_store::LATEST_SCHEMA_VERSION);
|
||
let Ok(flags) = cli::Flags::parse(args, &["config"], &[]) else {
|
||
println!("datenbank unbekannt");
|
||
return ExitCode::SUCCESS;
|
||
};
|
||
let Ok(config) = cli::load_config(&flags) else {
|
||
println!("datenbank unbekannt");
|
||
return ExitCode::SUCCESS;
|
||
};
|
||
let db = config.db_path();
|
||
if !db.is_file() {
|
||
println!("datenbank fehlt");
|
||
return ExitCode::SUCCESS;
|
||
}
|
||
match nexus_store::Store::open_readonly(&db).and_then(|s| s.schema_version()) {
|
||
Ok(v) => println!("datenbank {v}"),
|
||
Err(e) => {
|
||
println!("datenbank unlesbar");
|
||
eprintln!("{e}");
|
||
}
|
||
}
|
||
ExitCode::SUCCESS
|
||
}
|
||
|
||
fn backup(args: &[String]) -> ExitCode {
|
||
let run = || -> Result<PathBuf, String> {
|
||
let flags = cli::Flags::parse(args, &["out", "config"], &[])?;
|
||
let config = cli::load_config(&flags)?;
|
||
let db = config.db_path();
|
||
if !db.is_file() {
|
||
return Err(format!("Es gibt keine Datenbank unter {}.", db.display()));
|
||
}
|
||
let out = flags.get("out").map(PathBuf::from).unwrap_or_else(|| {
|
||
config.data_dir.join("backups").join(format!(
|
||
"nexus-{}.db",
|
||
nexus_core::now_iso8601().replace([':', '-'], "")
|
||
))
|
||
});
|
||
nexus_store::backup_file(&db, &out).map_err(|e| e.to_string())?;
|
||
Ok(out)
|
||
};
|
||
match run() {
|
||
Ok(out) => {
|
||
println!("Sicherung geschrieben: {}", out.display());
|
||
ExitCode::SUCCESS
|
||
}
|
||
Err(message) => {
|
||
eprintln!("{message}");
|
||
ExitCode::FAILURE
|
||
}
|
||
}
|
||
}
|