144 lines
6.3 KiB
Rust
144 lines
6.3 KiB
Rust
//! The only place `leleka-control-plane` reads the process environment, and what each variable is parsed into.
|
|
//!
|
|
//! One reader means the operator's surface is one list and a test can see where the environment enters. The readers
|
|
//! are live (each call asks the process, nothing is snapshotted at start), which keeps the order of the checks and
|
|
//! the messages of the run configuration exactly as they were when every module asked for itself. Each function
|
|
//! returns the parsed type (a number, a path, a flag, a mode), not the raw string, and says what an unset, empty or
|
|
//! non-Unicode value means.
|
|
//!
|
|
//! The variables, and the function that reads each:
|
|
//!
|
|
//! * the `STRATA_*` of the run configuration (config path, bind address, limits, model id, key file, tokenizer,
|
|
//! engine arguments, ...): [`text`], [`os_string`], [`number`]; they override the file's value and the config
|
|
//! loader checks the range;
|
|
//! * the name the configuration gives (`runtime.token_env`): [`variable`], the runtime credential;
|
|
//! * `LELEKA_STATUS_FILE`: [`supervisor_status_file`], the supervisor's status document;
|
|
//! * `LELEKA_PROFILE`: [`serving_profile`], the profile name shown in the request log;
|
|
//! * `CUDA_VISIBLE_DEVICES`: [`cuda_visible_devices`]; `HF_TOKEN`: [`hub_token`] (the model downloader's credential);
|
|
//! * `STRATA_MTP_REVISION`: [`mtp_revision`], the checkpoint revision of the MTP pack;
|
|
//! * `STRATA_DEBUG`, `STRATA_DEBUG_FAILED_OUTPUT`: [`mcp_stderr_debug`], [`debug_failed_output`], debugging switches;
|
|
//! * `STRATA_STAGE_TRACE`: [`stage_trace`], the per-stage timing of the host side of a request;
|
|
//! * `PATH`: [`search_path`], the executable lookup of the preflight;
|
|
//! * `XDG_CONFIG_HOME`, `HOME`: [`config_home`], where the user systemd unit goes;
|
|
//! * every `STRATA_*`: [`strata_variable_names`], scrubbed from the environment of a provisioning child.
|
|
|
|
use std::{
|
|
env::VarError,
|
|
ffi::OsString,
|
|
path::PathBuf,
|
|
};
|
|
|
|
/// The value of the variable; `Ok(None)` when it is unset, an error when it is set but not valid Unicode.
|
|
pub fn text(name: &str) -> Result<Option<String>, VarError> {
|
|
match std::env::var(name) {
|
|
Ok(value) => Ok(Some(value)),
|
|
Err(VarError::NotPresent) => Ok(None),
|
|
Err(error) => Err(error),
|
|
}
|
|
}
|
|
|
|
/// The value of the variable as an OS string (a path); `None` when it is unset.
|
|
pub fn os_string(name: &str) -> Option<OsString> {
|
|
std::env::var_os(name)
|
|
}
|
|
|
|
/// The variable parsed as `T` (a number); `Ok(None)` when unset, an error for a value that is not valid Unicode or
|
|
/// does not parse.
|
|
pub fn number<T: std::str::FromStr>(name: &str) -> Result<Option<T>, crate::BoxError>
|
|
where
|
|
T::Err: std::error::Error + 'static,
|
|
{
|
|
Ok(text(name)?.map(|value| value.parse::<T>()).transpose()?)
|
|
}
|
|
|
|
/// A variable named by the configuration (the runtime credential), `None` when unset or not Unicode.
|
|
pub fn variable(name: &str) -> Option<String> {
|
|
std::env::var(name).ok()
|
|
}
|
|
|
|
/// The status document of the supervisor that started this process (`LELEKA_STATUS_FILE`).
|
|
pub fn supervisor_status_file() -> Option<PathBuf> {
|
|
std::env::var_os("LELEKA_STATUS_FILE").map(PathBuf::from)
|
|
}
|
|
|
|
/// The serving profile named by `LELEKA_PROFILE`, when it is set and not empty.
|
|
pub fn serving_profile() -> Option<String> {
|
|
std::env::var("LELEKA_PROFILE").ok().filter(|profile| !profile.is_empty())
|
|
}
|
|
|
|
/// `CUDA_VISIBLE_DEVICES`: `Ok(None)` when unset, an error message when it is not UTF-8.
|
|
pub fn cuda_visible_devices() -> Result<Option<String>, String> {
|
|
text("CUDA_VISIBLE_DEVICES").map_err(|_| "CUDA_VISIBLE_DEVICES must be UTF-8".to_owned())
|
|
}
|
|
|
|
/// The `HF_TOKEN` variable: unset or empty is no token; whitespace or non-UTF-8 is an error.
|
|
pub fn hub_token() -> Result<Option<String>, crate::BoxError> {
|
|
match std::env::var("HF_TOKEN") {
|
|
Ok(token) if token.is_empty() => Ok(None),
|
|
Ok(token) if token.chars().any(char::is_whitespace) => {
|
|
Err("HF_TOKEN must not contain whitespace".into())
|
|
}
|
|
Ok(token) => Ok(Some(token)),
|
|
Err(VarError::NotPresent) => Ok(None),
|
|
Err(VarError::NotUnicode(_)) => Err("HF_TOKEN must contain valid UTF-8".into()),
|
|
}
|
|
}
|
|
|
|
/// The checkpoint revision of the MTP pack (`STRATA_MTP_REVISION`), `None` when unset or empty.
|
|
pub fn mtp_revision() -> Option<String> {
|
|
std::env::var("STRATA_MTP_REVISION").ok().filter(|revision| !revision.is_empty())
|
|
}
|
|
|
|
/// Is `STRATA_DEBUG` set (to anything)? Then the stderr of an MCP server child is copied into the log.
|
|
pub fn mcp_stderr_debug() -> bool {
|
|
std::env::var_os("STRATA_DEBUG").is_some()
|
|
}
|
|
|
|
/// Is `STRATA_DEBUG_FAILED_OUTPUT` exactly `1`? Then the text of a failed streaming tool turn is logged (model output).
|
|
pub fn debug_failed_output() -> bool {
|
|
std::env::var_os("STRATA_DEBUG_FAILED_OUTPUT").is_some_and(|value| value == "1")
|
|
}
|
|
|
|
/// What `STRATA_STAGE_TRACE` asks of the stage timer.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum StageTrace {
|
|
/// Unset or any other value: no clock reads.
|
|
Off,
|
|
/// `1`: spans add their duration to the per-stage table.
|
|
Spans,
|
|
/// `log`: spans also write one line each to stderr.
|
|
SpansAndLog,
|
|
}
|
|
|
|
/// The mode `STRATA_STAGE_TRACE` selects.
|
|
pub fn stage_trace() -> StageTrace {
|
|
match std::env::var("STRATA_STAGE_TRACE").as_deref() {
|
|
Ok("1") => StageTrace::Spans,
|
|
Ok("log") => StageTrace::SpansAndLog,
|
|
_ => StageTrace::Off,
|
|
}
|
|
}
|
|
|
|
/// The directories of `PATH`, `None` when `PATH` is unset (a missing `PATH` is deliberately not guessed).
|
|
pub fn search_path() -> Option<Vec<PathBuf>> {
|
|
std::env::var_os("PATH").map(|paths| std::env::split_paths(&paths).collect())
|
|
}
|
|
|
|
/// The base of the user configuration directory: `XDG_CONFIG_HOME` when it is set and not empty, else `HOME/.config`.
|
|
/// `Err` when neither is available.
|
|
#[cfg(unix)]
|
|
pub fn config_home() -> Result<PathBuf, String> {
|
|
match std::env::var_os("XDG_CONFIG_HOME") {
|
|
Some(value) if !value.is_empty() => Ok(PathBuf::from(value)),
|
|
_ => Ok(PathBuf::from(std::env::var_os("HOME").ok_or("HOME is missing")?).join(".config")),
|
|
}
|
|
}
|
|
|
|
/// The names of the variables that start with `STRATA_` (a child that must not inherit the operator's overrides).
|
|
pub fn strata_variable_names() -> Vec<OsString> {
|
|
std::env::vars_os()
|
|
.map(|(name, _)| name)
|
|
.filter(|name| name.to_string_lossy().starts_with("STRATA_"))
|
|
.collect()
|
|
}
|