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

99 lines
3.4 KiB
Rust

//! The Windows front end of the WSL2 route: `leleka.exe` runs the Linux `leleka` inside a WSL2 distribution
//! (`wsl.exe -d NAME --exec ...`), translates paths, and talks to the engine over the loopback HTTP that WSL2 forwards.
//!
//! This file is pure logic (parsers, path translation, advice) plus the thin `wsl.exe` process wrapper. The parsers
//! compile and are unit-tested on every OS; only the Windows entry point (`app.rs`) uses the process wrapper.
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
const GIB: u64 = 1 << 30;
/// Distributions that exist for other products and cannot run the engine.
const FOREIGN_DISTROS: [&str; 5] = [
"docker-desktop",
"docker-desktop-data",
"rancher-desktop",
"rancher-desktop-data",
"podman-machine-default",
];
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Distro {
pub name: String,
pub state: String,
pub version: u8,
pub default: bool,
}
/// Flags of `leleka` whose value is a path in the distribution; their values are translated before forwarding.
pub const PATH_FLAGS: [&str; 8] = [
"--assets",
"--state-dir",
"--prefix",
"--bin-src",
"--cublas-dir",
"--mtp-pack",
"--expert-profile-file",
"--config",
];
/// Split the bridge's own flags out of `args`, translating the path-valued ones. `--profile FILE` is translated when it
/// names an existing-looking path (contains a path separator or ends in `.json`).
#[derive(Debug, Default, PartialEq, Eq)]
pub struct Forwarded {
pub args: Vec<String>,
pub distro: Option<String>,
pub wsl_bin: Option<String>,
/// `--assets` after translation (checked for the slow Windows mount).
pub assets: Option<String>,
pub state_dir: Option<String>,
pub api_port: Option<u16>,
}
// ---------------------------------------------------------------------------------------------------------------
// .wslconfig
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct WslConfig {
pub memory_bytes: Option<u64>,
pub swap_bytes: Option<u64>,
pub processors: Option<u32>,
}
// ---------------------------------------------------------------------------------------------------------------
// bridge state kept on the Windows side
/// `%LOCALAPPDATA%\leleka\wsl-bridge.json`: what the last `leleka up` through WSL used, so that `status`, `reconfigure`
/// and `down` find the same engine without repeating the flags. Holds no secret.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct BridgeState {
pub distro: String,
pub leleka_bin: String,
pub state_dir: String,
pub api: String,
}
#[derive(Clone, Debug)]
pub struct Bridge {
pub wsl: PathBuf,
pub distro: String,
}
/// Where a build of the Linux `leleka` is searched for inside the distribution (after an explicit `--wsl-bin`).
const FIND_LELEKA: &str = concat!(
r#"for c in "$1" "$HOME/.local/share/leleka/bin/leleka" "#,
r#""$HOME/leleka/target/release/leleka" "$HOME/leleka-bin/leleka" "#,
r#""$HOME/.cargo/bin/leleka"; do if [ "$c" != "-" ] && [ -x "$c" ]; then echo "$c"; exit "#,
r#"0; fi; done; command -v leleka; true"#
);
#[cfg(test)]
mod tests;
mod bridge;
mod wsl_translation;
pub use bridge::{effective_wsl_memory, list_distros, parse_wslconfig, wsl_exe, wslconfig_advice};
pub use wsl_translation::{
decode_console_text, is_drvfs, linux_to_unc, parse_distro_list, parse_size, pick_distro,
to_wsl_path, translate_args,
};