Files
leleka/crates/deploy/src/layout.rs
T

322 lines
12 KiB
Rust

//! Where everything lives: install prefix, private state, model volume.
//!
//! ```text
//! PREFIX/bin/{leleka,strata-next,leleka-inference,leleka-daemon,leleka-cli,leleka-seal-exec}
//! PREFIX/lib/cublas/ CUDA 12 cuBLAS (only when the host has none)
//! STATE/leleka.json run configuration (no secrets) 0600
//! STATE/runtime.env tokens + LD_LIBRARY_PATH 0600
//! STATE/api-key bearer key of the public API 0600
//! STATE/daemon.json strata.json profile.json generated configs
//! STATE/logs/*.log STATE/run/status.json STATE/stamps/*.json STATE/mtp-staging/
//! ASSETS/model/*.gguf ASSETS/qwen-iq3-s-l2-v4.kar(+.provenance.json) ASSETS/data/mtp-rt/ ASSETS/tokenizer-assets/
//! ```
use crate::profile::{ARCHIVE_BYTES, SHARD_BYTES};
use std::path::{Path, PathBuf};
#[cfg(not(windows))]
pub const BINARIES: [&str; 6] = [
"leleka",
"strata-next",
"leleka-inference",
"leleka-daemon",
"leleka-cli",
"leleka-seal-exec",
];
/// Windows: five programs (the sealed-memfd shim `leleka-seal-exec` is Linux-only; the file-backed archive needs none).
#[cfg(windows)]
pub const BINARIES: [&str; 5] = [
"leleka.exe",
"strata-next.exe",
"leleka-inference.exe",
"leleka-daemon.exe",
"leleka-cli.exe",
];
pub const SHARD_NAMES: [&str; 2] = [
"Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S-00001-of-00002.gguf",
"Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S-00002-of-00002.gguf",
];
pub const KAR_NAME: &str = "qwen-iq3-s-l2-v4.kar";
pub const MTP_FILES: [&str; 4] = ["dense.bin", "dense.txt", "draft_vocab.bin", "experts.bin"];
pub const MANIFEST: &str = "qwen-iq3-s";
pub const MODEL_ID: &str = "qwen-iq3";
pub const MTP_PROFILE: &str = "qwen4exp-kar-ple-mtp-v1";
/// The typed load of `kar_source: none` (experimental): the same MTP stack with the PLE rows read from the GGUF shard.
pub const NATIVE_MTP_PROFILE: &str = "qwen4exp-native-mtp-v1";
/// Digest of the pinned IQ3_S shard pair (`crates/engine-qwen35-cuda/src/backend.rs` `MODEL_DIGEST`).
pub const MODEL_DIGEST: &str =
"sha256:80d5e3333801cedbca81a5c19c24ed6bb48fd56977acd190318f66b23a1766a3";
/// The file name of a program of this deployment on the current OS.
pub fn binary_file_name(name: &str) -> String {
if cfg!(windows) && !name.ends_with(".exe") {
format!("{name}.exe")
} else {
name.to_owned()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Dirs {
pub prefix: PathBuf,
pub state: PathBuf,
pub assets: PathBuf,
}
impl Dirs {
/// Defaults: root gets `/opt/leleka`, `/var/lib/leleka`; a user gets XDG-style directories under `$HOME`.
/// Environment overrides: `LELEKA_PREFIX`, `LELEKA_STATE`, `LELEKA_ASSETS`.
pub fn defaults(root: bool, home: Option<&Path>, env: &dyn Fn(&str) -> Option<String>) -> Dirs {
#[cfg(windows)]
{
let _ = root;
// Everything under the user profile (`%LOCALAPPDATA%\leleka`): the key and the tokens inherit the profile's ACL,
// which excludes other users. The model volume (140 GB) is better put on a big disk with `--assets D:\leleka`.
let pick =
|name: &str, fallback: PathBuf| env(name).map(PathBuf::from).unwrap_or(fallback);
let local = env("LOCALAPPDATA").map(PathBuf::from).unwrap_or_else(|| {
home.map(|h| h.join("AppData").join("Local"))
.unwrap_or_else(|| PathBuf::from("C:\\leleka-data"))
});
let base = local.join("leleka");
return Dirs {
prefix: pick("LELEKA_PREFIX", base.clone()),
state: pick("LELEKA_STATE", base.join("state")),
assets: pick("LELEKA_ASSETS", base.join("assets")),
};
}
#[cfg(not(windows))]
Self::unix_defaults(root, home, env)
}
#[cfg_attr(windows, allow(dead_code))]
fn unix_defaults(
root: bool,
home: Option<&Path>,
env: &dyn Fn(&str) -> Option<String>,
) -> Dirs {
let pick = |name: &str, fallback: PathBuf| env(name).map(PathBuf::from).unwrap_or(fallback);
if root {
Dirs {
prefix: pick("LELEKA_PREFIX", "/opt/leleka".into()),
state: pick("LELEKA_STATE", "/var/lib/leleka/state".into()),
assets: pick("LELEKA_ASSETS", "/var/lib/leleka/assets".into()),
}
} else {
let home = home.map(Path::to_path_buf).unwrap_or_else(|| "/tmp".into());
Dirs {
prefix: pick("LELEKA_PREFIX", home.join(".local/share/leleka")),
state: pick("LELEKA_STATE", home.join(".local/state/leleka")),
assets: pick("LELEKA_ASSETS", home.join(".local/share/leleka/assets")),
}
}
}
pub fn paths(&self) -> Paths {
Paths { dirs: self.clone() }
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Paths {
pub dirs: Dirs,
}
impl Paths {
pub fn bin_dir(&self) -> PathBuf {
self.dirs.prefix.join("bin")
}
/// `PREFIX/bin/NAME` (`NAME.exe` on Windows; the suffix is added when missing).
pub fn bin(&self, name: &str) -> PathBuf {
self.bin_dir().join(binary_file_name(name))
}
pub fn cublas_dir(&self) -> PathBuf {
self.dirs.prefix.join("lib/cublas")
}
pub fn run_config(&self) -> PathBuf {
self.dirs.state.join("leleka.json")
}
pub fn env_file(&self) -> PathBuf {
self.dirs.state.join("runtime.env")
}
pub fn api_key(&self) -> PathBuf {
self.dirs.state.join("api-key")
}
pub fn daemon_config(&self) -> PathBuf {
self.dirs.state.join("daemon.json")
}
pub fn strata_config(&self) -> PathBuf {
self.dirs.state.join("strata.json")
}
pub fn profile_file(&self) -> PathBuf {
self.dirs.state.join("profile.json")
}
pub fn logs(&self) -> PathBuf {
self.dirs.state.join("logs")
}
pub fn run_dir(&self) -> PathBuf {
self.dirs.state.join("run")
}
pub fn status_file(&self) -> PathBuf {
self.run_dir().join("status.json")
}
/// A pending `leleka reconfigure` for the supervisor (`reconfigure.rs`); consumed by it.
pub fn reconfigure_request(&self) -> PathBuf {
self.run_dir().join("reconfigure-request.json")
}
/// The supervisor's answer to the request with the same id.
pub fn reconfigure_result(&self) -> PathBuf {
self.run_dir().join("reconfigure-result.json")
}
pub fn stamps(&self) -> PathBuf {
self.dirs.state.join("stamps")
}
/// Verification stamps of the engine (`leleka-inference --verify-stamps`) and of the archive file
/// (`kar-source`, written by the supervisor). Private to the service account.
pub fn engine_stamps(&self) -> PathBuf {
self.stamps().join("engine")
}
pub fn mtp_staging(&self) -> PathBuf {
self.dirs.state.join("mtp-staging")
}
pub fn shard(&self, index: usize) -> PathBuf {
self.dirs.assets.join("model").join(SHARD_NAMES[index])
}
pub fn model_dir(&self) -> PathBuf {
self.dirs.assets.join("model")
}
pub fn kar(&self) -> PathBuf {
self.dirs.assets.join(KAR_NAME)
}
pub fn kar_provenance(&self) -> PathBuf {
self.dirs.assets.join(format!("{KAR_NAME}.provenance.json"))
}
pub fn mtp_pack(&self) -> PathBuf {
self.dirs.assets.join("data/mtp-rt")
}
pub fn tokenizer_root(&self) -> PathBuf {
self.dirs.assets.join("tokenizer-assets")
}
pub fn tokenizer_dir(&self) -> PathBuf {
self.tokenizer_root().join("tokenizer")
}
pub fn unit_name(&self) -> &'static str {
"leleka.service"
}
}
/// Bytes still missing on the model volume, from the current sizes of the pinned files. A file with the wrong size
/// counts as missing (fetch resumes or restarts it; the archive step refuses a wrong archive).
pub fn missing_asset_bytes(paths: &Paths) -> (u64, u64) {
let size = |path: PathBuf| {
std::fs::metadata(path)
.ok()
.filter(|m| m.is_file())
.map(|m| m.len())
};
let mut shards = 0u64;
for (index, expected) in SHARD_BYTES.iter().enumerate() {
let have = size(paths.shard(index)).unwrap_or(0);
let part = size(
paths
.model_dir()
.join(format!("{}.part", SHARD_NAMES[index])),
)
.unwrap_or(0);
if have != *expected {
shards += expected.saturating_sub(part.max(have.min(*expected)));
}
}
let kar = if size(paths.kar()) == Some(ARCHIVE_BYTES) {
0
} else {
// Sections, the final archive and slack: the guard in `ensure-sharded-streamed` is the exact bound.
ARCHIVE_BYTES + ARCHIVE_BYTES / 5
};
(shards, kar)
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(windows)]
#[test]
fn windows_defaults_live_under_the_profile() {
let none = |_: &str| None;
let dirs = Dirs::defaults(false, Some(Path::new("C:\\Users\\para")), &none);
assert_eq!(
dirs.state,
PathBuf::from("C:\\Users\\para\\AppData\\Local\\leleka\\state")
);
assert_eq!(
dirs.prefix,
PathBuf::from("C:\\Users\\para\\AppData\\Local\\leleka")
);
let overridden = Dirs::defaults(false, None, &|name| match name {
"LOCALAPPDATA" => Some("D:\\Local".to_owned()),
"LELEKA_ASSETS" => Some("E:\\models".to_owned()),
_ => None,
});
assert_eq!(overridden.assets, PathBuf::from("E:\\models"));
assert_eq!(overridden.state, PathBuf::from("D:\\Local\\leleka\\state"));
assert_eq!(binary_file_name("leleka"), "leleka.exe");
assert_eq!(binary_file_name("leleka.exe"), "leleka.exe");
}
#[cfg(unix)]
#[test]
fn defaults_follow_the_user() {
let none = |_: &str| None;
let root = Dirs::defaults(true, None, &none);
assert_eq!(root.prefix, PathBuf::from("/opt/leleka"));
let user = Dirs::defaults(false, Some(Path::new("/home/para")), &none);
assert_eq!(user.state, PathBuf::from("/home/para/.local/state/leleka"));
let overridden = Dirs::defaults(false, Some(Path::new("/home/para")), &|name| {
(name == "LELEKA_ASSETS").then(|| "/data/leleka".to_owned())
});
assert_eq!(overridden.assets, PathBuf::from("/data/leleka"));
assert_eq!(
overridden.prefix,
PathBuf::from("/home/para/.local/share/leleka")
);
}
#[test]
fn paths_are_derived_from_the_three_roots() {
let dirs = Dirs {
prefix: "/opt/leleka".into(),
state: "/var/lib/leleka/state".into(),
assets: "/data".into(),
};
let paths = dirs.paths();
assert_eq!(
paths.bin("leleka-inference"),
PathBuf::from("/opt/leleka/bin").join(binary_file_name("leleka-inference"))
);
assert_eq!(
paths.kar_provenance(),
PathBuf::from("/data/qwen-iq3-s-l2-v4.kar.provenance.json")
);
assert_eq!(
paths.tokenizer_dir(),
PathBuf::from("/data/tokenizer-assets/tokenizer")
);
}
#[test]
fn missing_bytes_of_an_empty_volume_are_the_whole_model() {
let dir = std::env::temp_dir().join(format!("leleka-layout-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let paths = Dirs {
prefix: dir.clone(),
state: dir.clone(),
assets: dir.join("assets"),
}
.paths();
let (shards, kar) = missing_asset_bytes(&paths);
assert_eq!(shards, SHARD_BYTES[0] + SHARD_BYTES[1]);
assert!(kar > ARCHIVE_BYTES);
std::fs::remove_dir_all(&dir).unwrap();
}
}