Files
leleka/crates/deploy/src/app/entry.rs
T

157 lines
4.9 KiB
Rust

//! The process entry: environment, version text, root check and the command dispatch of `main`.
use super::*;
pub fn env_var(name: &str) -> Option<String> {
std::env::var(name).ok().filter(|v| !v.is_empty())
}
/// What `leleka version` prints: the first line stays `leleka VERSION` (scripts parse it); the rest is the build identity embedded by
/// `build.rs` (docs/release/5090-v0.1-checklist.md, section 3).
pub fn version_text() -> String {
format!(
"leleka {}
commit {} ({})
strata pin {} (upstream commit of docs/upstream.md at build time)
built for {}, {} profile",
env!("CARGO_PKG_VERSION"),
env!("LELEKA_BUILD_COMMIT"),
env!("LELEKA_BUILD_TREE"),
env!("LELEKA_BUILD_STRATA_PIN"),
env!("LELEKA_BUILD_TARGET"),
env!("LELEKA_BUILD_PROFILE"),
)
}
pub(super) fn is_root() -> bool {
#[cfg(target_os = "linux")]
{
rustix::process::geteuid().is_root()
}
#[cfg(not(target_os = "linux"))]
{
false
}
}
/// The command line of `leleka`. On Windows the WSL2 route (`winfront`) takes the deployment commands first.
pub async fn main_with(args: Vec<String>) -> i32 {
#[cfg(windows)]
if let Some(code) = crate::winfront::dispatch(&args).await {
return code;
}
main_native(args).await
}
pub(super) fn home_dir() -> Option<PathBuf> {
#[cfg(windows)]
{
std::env::var_os("USERPROFILE").map(PathBuf::from)
}
#[cfg(not(windows))]
{
std::env::var_os("HOME").map(PathBuf::from)
}
}
/// The commands on this host itself: systemd or foreground on Linux, foreground on Windows (`--native`).
pub async fn main_native(mut args: Vec<String>) -> i32 {
// `--hidden` (the logon task): hide the console window (the console itself stays, children stop through it).
if let Some(index) = args.iter().position(|a| a == "--hidden") {
args.remove(index);
crate::supervisor::HIDDEN.store(true, std::sync::atomic::Ordering::Relaxed);
#[cfg(windows)]
crate::winsys::hide_console();
}
let var = env_var;
let environment = Environment {
root: is_root(),
home: home_dir(),
var: &var,
};
match parse(&args, &environment) {
Ok(command) => run_command(command).await,
Err(message) => {
eprintln!("leleka: {message}");
2
}
}
}
/// The commands that work on the installation; the others are the small ones of [`run_local_command`].
async fn run_command(command: Command) -> i32 {
match command {
Command::Preflight(opts) => preflight(opts).await,
Command::Up(opts) => up(opts).await,
Command::Status(opts) => status(opts).await,
Command::Down(opts) => down(opts).await,
Command::Reconfigure(opts, args) => reconfigure(opts, args).await,
Command::Verify(opts) => verify(opts).await,
Command::Logs(opts, args) => crate::logview::run_logs(&opts, &args),
Command::Report(opts, args) => crate::logview::run_report(&opts, &args),
Command::Service(action, opts) => crate::winservice::run_native(action, opts).await,
other => run_local_command(other).await,
}
}
async fn run_local_command(command: Command) -> i32 {
match command {
Command::Help => {
print!("{USAGE}");
0
}
Command::Version => {
println!("{}", version_text());
0
}
Command::Profiles(None) => {
for (name, _) in BUILTIN {
let profile = Profile::builtin(name).expect("built-in");
println!("{name}\n {}", profile.description);
}
0
}
Command::Profiles(Some(name)) => show_profile(&name),
Command::Run { config, kar_fd } => {
supervisor::run(supervisor::Options { config, kar_fd }).await
}
Command::SealProbe(file) => seal_probe(&file),
Command::ExecInherit(rest) => exec_inherit_command(&rest),
Command::SealExec(rest) => seal_exec_command(&rest),
_ => unreachable!("run_command takes the commands of the installation"),
}
}
fn show_profile(name: &str) -> i32 {
match Profile::builtin(name) {
Some(profile) => {
print!("{}", crate::config::to_file_text(&profile));
0
}
None => {
eprintln!("leleka: unknown profile {name:?}");
2
}
}
}
fn exec_inherit_command(rest: &[String]) -> i32 {
match crate::sealed::exec_inherit(rest) {
Ok(never) => match never {},
Err(message) => {
eprintln!("leleka exec-inherit: {message}");
2
}
}
}
fn seal_exec_command(rest: &[String]) -> i32 {
match crate::sealed::seal_exec(rest) {
Ok(never) => match never {},
Err(message) => {
eprintln!("leleka seal-exec: {message}");
2
}
}
}