hypha: implement arg cli interface and dynamic container lookup

This commit is contained in:
Alex Zenla 2024-01-31 09:08:19 -08:00
parent 86c512474a
commit def4306a04
No known key found for this signature in database
GPG Key ID: 067B238899B51269
2 changed files with 31 additions and 12 deletions

View File

@ -22,8 +22,6 @@ enum Commands {
kernel: String, kernel: String,
#[arg(short = 'r', long, default_value = "auto")] #[arg(short = 'r', long, default_value = "auto")]
initrd: String, initrd: String,
#[arg(short, long)]
image: String,
#[arg(short, long, default_value_t = 1)] #[arg(short, long, default_value_t = 1)]
cpus: u32, cpus: u32,
#[arg(short, long, default_value_t = 512)] #[arg(short, long, default_value_t = 512)]
@ -32,16 +30,18 @@ enum Commands {
config_bundle: Option<String>, config_bundle: Option<String>,
#[arg[short, long]] #[arg[short, long]]
env: Option<Vec<String>>, env: Option<Vec<String>>,
#[arg()]
image: String,
#[arg(allow_hyphen_values = true, trailing_var_arg = true)] #[arg(allow_hyphen_values = true, trailing_var_arg = true)]
run: Vec<String>, run: Vec<String>,
}, },
Destroy { Destroy {
#[arg(short, long)] #[arg()]
domain: u32, container: String,
}, },
Console { Console {
#[arg(short, long)] #[arg()]
domain: u32, container: String,
}, },
} }
@ -88,12 +88,12 @@ fn main() -> Result<()> {
println!("launched container: {}", uuid); println!("launched container: {}", uuid);
} }
Commands::Destroy { domain } => { Commands::Destroy { container } => {
controller.destroy(domain)?; controller.destroy(&container)?;
} }
Commands::Console { domain } => { Commands::Console { container } => {
controller.console(domain)?; controller.console(&container)?;
} }
Commands::List { .. } => { Commands::List { .. } => {

View File

@ -145,7 +145,11 @@ impl Controller {
} }
} }
pub fn destroy(&mut self, domid: u32) -> Result<Uuid> { pub fn destroy(&mut self, id: &str) -> Result<Uuid> {
let info = self
.resolve(id)?
.ok_or_else(|| anyhow!("unable to resolve container: {}", id))?;
let domid = info.domid;
let mut store = XsdClient::open()?; let mut store = XsdClient::open()?;
let dom_path = store.get_domain_path(domid)?; let dom_path = store.get_domain_path(domid)?;
let uuid = match store.read_string_optional(format!("{}/hypha/uuid", dom_path).as_str())? { let uuid = match store.read_string_optional(format!("{}/hypha/uuid", dom_path).as_str())? {
@ -181,7 +185,11 @@ impl Controller {
Ok(uuid) Ok(uuid)
} }
pub fn console(&mut self, domid: u32) -> Result<()> { pub fn console(&mut self, id: &str) -> Result<()> {
let info = self
.resolve(id)?
.ok_or_else(|| anyhow!("unable to resolve container: {}", id))?;
let domid = info.domid;
let (mut read, mut write) = self.client.open_console(domid)?; let (mut read, mut write) = self.client.open_console(domid)?;
let mut stdin = io::stdin(); let mut stdin = io::stdin();
let is_tty = termion::is_tty(&stdin); let is_tty = termion::is_tty(&stdin);
@ -258,6 +266,17 @@ impl Controller {
Ok(containers) Ok(containers)
} }
pub fn resolve(&mut self, id: &str) -> Result<Option<ContainerInfo>> {
for container in self.list()? {
let uuid_string = container.uuid.to_string();
let domid_string = container.domid.to_string();
if uuid_string == id || domid_string == id || id == format!("hypha-{}", uuid_string) {
return Ok(Some(container));
}
}
Ok(None)
}
fn parse_loop_set(input: &str) -> Vec<ContainerLoopInfo> { fn parse_loop_set(input: &str) -> Vec<ContainerLoopInfo> {
let sets = input let sets = input
.split(',') .split(',')