Files
leleka/control-plane/src/preflight.rs
T

146 lines
5.7 KiB
Rust

//! Read-only deployment validation; never spawn a worker or acquire a listener.
use std::{env, ffi::OsStr, path::Path};
use crate::{config::Config, embedded_engine, tokenizer::Tokenizer};
const CONFIG_INVALID: &str = concat!(
"configuration or API key validation failed; ",
"check config fields, environment overrides and key-file permissions"
);
pub fn check(path: Option<&Path>) -> Result<serde_json::Value, &'static str> {
// Parser errors may contain user-supplied values. Keep diagnostics to stages
// so a malformed config/tokenizer cannot echo credentials into deployment logs.
let config = Config::load_with_path(path).map_err(|_| CONFIG_INVALID)?;
let engine = engine_kind(&config)?;
let tokenizer_pending =
config.native_model.is_some() && config.tokenizer_dir.as_ref().is_some_and(|p| !p.exists());
if let Some(path) = &config.tokenizer_dir
&& !tokenizer_pending
{
Tokenizer::from_assets(path).map_err(|_| "tokenizer asset validation failed")?;
}
Ok(report(&config, engine, tokenizer_pending))
}
/// Which engine the configuration names, after checking that it can run here.
fn engine_kind(config: &Config) -> Result<&'static str, &'static str> {
Ok(match &config.worker {
None => "unconfigured",
Some(worker) if worker.executable == OsStr::new("@embedded") => {
if !cfg!(target_os = "linux") {
return Err("embedded execution requires Linux");
}
if embedded_engine::info()["embedded"] != true {
return Err("this executable has no embedded engine");
}
"embedded"
}
Some(worker) => {
if !executable_available(&worker.executable) {
return Err(
"engine executable is not an accessible executable file; check its path or PATH",
);
}
"external"
}
})
}
/// The validation report: what the configuration says and which checks were not run.
fn report(config: &Config, engine: &str, tokenizer_pending: bool) -> serde_json::Value {
let mut report = serde_json::Map::new();
for part in [
service_fields(config, engine),
model_fields(config, tokenizer_pending),
unchecked_fields(config),
] {
if let serde_json::Value::Object(fields) = part {
report.extend(fields);
}
}
serde_json::Value::Object(report)
}
/// The fields about the service: platform, bind, limits, authentication, history and engine.
fn service_fields(config: &Config, engine: &str) -> serde_json::Value {
serde_json::json!({
"schema_version": 1,
"configuration_valid": true,
"platform": std::env::consts::OS,
"bind": config.bind.to_string(),
"max_in_flight_requests": config.max_in_flight_requests,
"queue_timeout_s": config.queue_timeout.map(|duration| duration.as_secs()),
"shutdown_grace_s": config.shutdown_grace.as_secs(),
"generation_timeout_s": config
.worker
.as_ref()
.and_then(|worker| worker.generation_timeout.map(|duration| duration.as_secs())),
"model": config.model,
"authentication_enabled": config.api_key.is_some(),
"response_history": if config.response_history_dir.is_some() { "durable_unchecked" } else { "memory" },
"engine": engine,
})
}
/// The fields about the model: tokenizer, native model, MTP pack and whether inference can be served.
fn model_fields(config: &Config, tokenizer_pending: bool) -> serde_json::Value {
let tokenizer_ready =
(config.tokenizer_dir.is_some() && !tokenizer_pending) || config.gptoss_gguf.is_some();
serde_json::json!({
"tokenizer_validated": tokenizer_ready,
"native_model_configured":config.native_model.is_some(),
"native_runtime_profile":config.native_model.as_ref().and_then(|native| native.runtime_profile),
"native_runtime_profile_checked":false,
"mtp_pack": if config.mtp_pack.is_some() { "pinned_structural_check_pending" } else { "unconfigured" },
"native_model_verified":false,
"inference_configured": config.worker.is_some() && tokenizer_ready,
})
}
/// The fields about assets, GPU, CUDA memory and the engine probe, none of which this check runs.
fn unchecked_fields(config: &Config) -> serde_json::Value {
serde_json::json!({
"readiness_checked": false,
"asset_bundles_configured": config.assets.len(),
"assets_verified": false,
"gpu_requirement": config.gpu_requirement,
"gpu_requirement_checked": false,
"cuda_memory": config.cuda_memory,
"cuda_memory_checked": false,
"engine_probe_configured": config.engine_probe,
"engine_probe_checked": false,
})
}
fn executable_available(executable: &OsStr) -> bool {
let path = Path::new(executable);
if executable.as_encoded_bytes().contains(&b'/') {
return executable_file(path);
}
// A missing PATH is deliberately not guessed. An explicit path is portable.
env::var_os("PATH").is_some_and(|paths| {
env::split_paths(&paths).any(|directory| executable_file(&directory.join(path)))
})
}
fn executable_file(path: &Path) -> bool {
if !path.is_file() {
return false;
}
#[cfg(unix)]
{
use std::{ffi::CString, os::unix::ffi::OsStrExt};
let Ok(path) = CString::new(path.as_os_str().as_bytes()) else {
return false;
};
// SAFETY: valid NUL-terminated path, no retained pointers. This is a
// point-in-time access check, not an execution or dependency probe.
unsafe { libc::access(path.as_ptr(), libc::X_OK) == 0 }
}
#[cfg(not(unix))]
{
false
}
}