krata/hypha/bin/controller.rs

91 lines
2.1 KiB
Rust
Raw Normal View History

use clap::{Parser, Subcommand};
2024-01-17 22:29:05 +00:00
use hypha::ctl::Controller;
2024-01-18 08:02:21 +00:00
use hypha::error::{HyphaError, Result};
2024-01-18 14:15:42 +00:00
use std::path::PathBuf;
2024-01-17 22:29:05 +00:00
#[derive(Parser, Debug)]
#[command(version, about)]
struct ControllerArgs {
2024-01-18 14:15:42 +00:00
#[arg(short, long, default_value = "auto")]
store: String,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand, Debug)]
enum Commands {
Launch {
2024-01-21 12:49:31 +00:00
#[arg(short, long)]
kernel: String,
#[arg(short = 'r', long)]
initrd: String,
#[arg(short, long)]
image: String,
#[arg(short, long, default_value_t = 1)]
cpus: u32,
#[arg(short, long, default_value_t = 512)]
mem: u64,
},
Destroy {
#[arg(short, long)]
domain: u32,
},
2024-01-21 12:49:31 +00:00
Console {
#[arg(short, long)]
domain: u32,
},
2024-01-17 22:29:05 +00:00
}
fn main() -> Result<()> {
env_logger::init();
let args = ControllerArgs::parse();
2024-01-18 14:15:42 +00:00
let store_path = if args.store == "auto" {
default_store_path()
.ok_or_else(|| HyphaError::new("unable to determine default store path"))
2024-01-18 08:02:21 +00:00
} else {
2024-01-18 14:15:42 +00:00
Ok(PathBuf::from(args.store))
2024-01-18 08:02:21 +00:00
}?;
2024-01-18 14:15:42 +00:00
let store_path = store_path
.to_str()
.map(|x| x.to_string())
.ok_or_else(|| HyphaError::new("unable to convert store path to string"))?;
2024-01-21 12:49:31 +00:00
let mut controller = Controller::new(store_path)?;
match args.command {
2024-01-21 12:49:31 +00:00
Commands::Launch {
kernel,
initrd,
image,
cpus,
mem,
} => {
let domid = controller.launch(&kernel, &initrd, &image, cpus, mem)?;
println!("launched domain: {}", domid);
}
2024-01-21 12:49:31 +00:00
Commands::Destroy { domain } => {
controller.destroy(domain)?;
}
2024-01-21 12:49:31 +00:00
Commands::Console { domain } => {
controller.console(domain)?;
}
}
2024-01-17 22:29:05 +00:00
Ok(())
}
2024-01-18 08:02:21 +00:00
2024-01-18 14:15:42 +00:00
fn default_store_path() -> Option<PathBuf> {
2024-01-18 08:02:21 +00:00
let user_dirs = directories::UserDirs::new()?;
let mut path = user_dirs.home_dir().to_path_buf();
2024-01-18 14:15:42 +00:00
if path == PathBuf::from("/root") {
path.push("/var/lib/hypha")
} else {
path.push(".hypha");
}
Some(path)
2024-01-18 08:02:21 +00:00
}