glob-super-import 15 -> 0, env-read 41 -> 0 outside crates/deploy/src/env_settings.rs (typed accessors: home, PATH, tokens presence, route, distro; the LELEKA_* lookup is a fn pointer EnvLookup), dyn-trait 17 -> 0 (impl Fn / impl FnMut), instant-now 28 -> 0 (ServiceSupervisor holds an AnyClock, command flows use SystemClock of leleka-obs). Doc headers of the generated files are real sentences. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
138 lines
5.2 KiB
Rust
138 lines
5.2 KiB
Rust
//! The one place of `leleka-deploy` that reads the process environment.
|
|
//!
|
|
//! Every variable the deploy tool looks at is named here with the type it is parsed into (a path, a flag, a token
|
|
//! presence, a text), so the operator's surface is one list and no other module reaches for `std::env`. The reads are
|
|
//! live (not a snapshot taken at start): the commands that spawn children set the child's environment themselves with
|
|
//! `Command::env`, and the tests that need other values pass a lookup function (`EnvLookup`) to the code that takes
|
|
//! one.
|
|
//!
|
|
//! The variables:
|
|
//!
|
|
//! * `HOME`, `USERPROFILE`, `LOCALAPPDATA`, `SystemRoot`, `USERNAME`, `USERDOMAIN`: the default directories, the
|
|
//! `.wslconfig` and `wsl.exe` lookups and the account of the Windows task;
|
|
//! * `PATH`, `LD_LIBRARY_PATH`: tool and CUDA library discovery and the child environment of the engine;
|
|
//! * `HF_TOKEN`: the preflight (presence only; the value is never read) and the fake engine's leak check;
|
|
//! * `LELEKA_ROUTE`, `LELEKA_WSL_DISTRO`: the Windows front end's route and WSL distribution;
|
|
//! * `LELEKA_RUNTIME_TOKEN`, `LELEKA_GATEWAY_TOKEN`: the test doubles' check that the supervisor passed the credential;
|
|
//! * any name: [`lookup`] (the `LELEKA_*` overrides of `leleka up`) and [`operator_var`] (the extents pin).
|
|
|
|
use std::{ffi::OsString, path::PathBuf};
|
|
|
|
/// A lookup of one variable by name: the process environment in `main`, a fixed table in the tests.
|
|
pub type EnvLookup = fn(&str) -> Option<String>;
|
|
|
|
/// The value of `name`, `None` when it is unset, not Unicode or empty (the `LELEKA_*` overrides of `leleka up`).
|
|
pub fn lookup(name: &str) -> Option<String> {
|
|
std::env::var(name).ok().filter(|v| !v.is_empty())
|
|
}
|
|
|
|
/// The value of `name` as the operator set it (an empty value is a value), `None` when unset or not Unicode.
|
|
pub fn operator_var(name: &str) -> Option<String> {
|
|
std::env::var(name).ok()
|
|
}
|
|
|
|
/// The home directory of this platform: `USERPROFILE` on Windows, `HOME` elsewhere.
|
|
pub fn home_dir() -> Option<PathBuf> {
|
|
#[cfg(windows)]
|
|
{
|
|
user_profile()
|
|
}
|
|
#[cfg(not(windows))]
|
|
{
|
|
home()
|
|
}
|
|
}
|
|
|
|
/// `HOME` (the Unix home directory, the systemd user unit directory).
|
|
pub fn home() -> Option<PathBuf> {
|
|
std::env::var_os("HOME").map(PathBuf::from)
|
|
}
|
|
|
|
/// `USERPROFILE` (the Windows profile folder, where `.wslconfig` lives).
|
|
pub fn user_profile() -> Option<PathBuf> {
|
|
std::env::var_os("USERPROFILE").map(PathBuf::from)
|
|
}
|
|
|
|
/// The home directory as text, `HOME` first and `USERPROFILE` second (the report replaces it by `~`).
|
|
pub fn home_text() -> Option<String> {
|
|
std::env::var("HOME")
|
|
.or_else(|_| std::env::var("USERPROFILE"))
|
|
.ok()
|
|
}
|
|
|
|
/// `LOCALAPPDATA` (the Windows per-user data folder), `None` in a session that does not set it.
|
|
pub fn local_app_data() -> Option<PathBuf> {
|
|
std::env::var_os("LOCALAPPDATA").map(PathBuf::from)
|
|
}
|
|
|
|
/// `SystemRoot` (`C:\Windows`).
|
|
pub fn system_root() -> Option<PathBuf> {
|
|
std::env::var_os("SystemRoot").map(PathBuf::from)
|
|
}
|
|
|
|
/// `PATH` as the OS string, `None` when unset.
|
|
pub fn search_path() -> Option<OsString> {
|
|
std::env::var_os("PATH")
|
|
}
|
|
|
|
/// `PATH` as text, empty when unset or not Unicode (the prefix of a child's `PATH`).
|
|
pub fn search_path_text() -> String {
|
|
std::env::var("PATH").unwrap_or_default()
|
|
}
|
|
|
|
/// `LD_LIBRARY_PATH` as the OS string, `None` when unset.
|
|
pub fn library_path() -> Option<OsString> {
|
|
std::env::var_os("LD_LIBRARY_PATH")
|
|
}
|
|
|
|
/// `LD_LIBRARY_PATH` as text, empty when unset or not Unicode (the suffix of a child's loader path).
|
|
pub fn library_path_text() -> String {
|
|
std::env::var("LD_LIBRARY_PATH").unwrap_or_default()
|
|
}
|
|
|
|
/// Is `HF_TOKEN` set to something? Only the presence is ever read: the token itself is the model downloader's.
|
|
pub fn hf_token_set() -> bool {
|
|
std::env::var_os("HF_TOKEN").is_some_and(|v| !v.is_empty())
|
|
}
|
|
|
|
/// Is `HF_TOKEN` set at all (empty counts)? The fake engine uses it to prove the supervisor did not forward it.
|
|
pub fn hf_token_present() -> bool {
|
|
std::env::var_os("HF_TOKEN").is_some()
|
|
}
|
|
|
|
/// `LELEKA_ROUTE` (`wsl` or `native`), `None` when unset or not Unicode.
|
|
pub fn route() -> Option<String> {
|
|
std::env::var("LELEKA_ROUTE").ok()
|
|
}
|
|
|
|
/// `LELEKA_WSL_DISTRO` as set (an empty value is kept: the caller decides what empty means).
|
|
pub fn wsl_distro() -> Option<String> {
|
|
std::env::var("LELEKA_WSL_DISTRO").ok()
|
|
}
|
|
|
|
/// `USERNAME`, `"user"` when unset.
|
|
pub fn user_name() -> String {
|
|
std::env::var("USERNAME").unwrap_or_else(|_| "user".into())
|
|
}
|
|
|
|
/// `USERDOMAIN`, `None` when unset or not Unicode.
|
|
pub fn user_domain() -> Option<String> {
|
|
std::env::var("USERDOMAIN").ok()
|
|
}
|
|
|
|
/// The runtime credential the supervisor hands the engine and the API (`LELEKA_RUNTIME_TOKEN`), empty when unset.
|
|
pub fn runtime_token() -> String {
|
|
std::env::var("LELEKA_RUNTIME_TOKEN").unwrap_or_default()
|
|
}
|
|
|
|
/// The gateway credential (`LELEKA_GATEWAY_TOKEN`), empty when unset.
|
|
pub fn gateway_token() -> String {
|
|
std::env::var("LELEKA_GATEWAY_TOKEN").unwrap_or_default()
|
|
}
|
|
|
|
/// An environment with no variables: parsing that must not depend on the process (`leleka reconfigure` flag
|
|
/// reading, the unit tests).
|
|
pub fn nothing(_name: &str) -> Option<String> {
|
|
None
|
|
}
|