From 30fc217ac381e5722622f172d437c141dc238227 Mon Sep 17 00:00:00 2001 From: Noor Date: Thu, 20 Mar 2025 21:40:22 +0530 Subject: [PATCH] feat: implement VM command handling in snp module for enhanced CLI functionality --- src/bin/detee-cli.rs | 93 +--------------------------------------- src/snp/cli_handler.rs | 96 ++++++++++++++++++++++++++++++++++++++++++ src/snp/mod.rs | 1 + 3 files changed, 98 insertions(+), 92 deletions(-) create mode 100644 src/snp/cli_handler.rs diff --git a/src/bin/detee-cli.rs b/src/bin/detee-cli.rs index c35c5ef..517bd54 100644 --- a/src/bin/detee-cli.rs +++ b/src/bin/detee-cli.rs @@ -1,6 +1,7 @@ use clap::{builder::PossibleValue, Arg, ArgMatches, Command}; use clap_complete::{generate, Shell}; use detee_cli::sgx::cli_handler::handle_app; +use detee_cli::snp::cli_handler::handle_vm; use detee_cli::*; use std::error::Error; use std::io; @@ -598,98 +599,6 @@ fn handle_completion(matches: &ArgMatches, cmd: Command) { } } -fn handle_vm(matches: &ArgMatches) { - match matches.subcommand() { - Some(("deploy", args)) => cli_print(handle_vm_deploy(args)), - Some(("ssh", args)) => cli_print(handle_vm_ssh(args)), - Some(("list", args)) => cli_print(handle_vm_list(args)), - Some(("inspect", inspect_args)) => { - let uuid: String = inspect_args.get_one::("uuid").unwrap().clone(); - if *inspect_args.get_one::("show-node").unwrap() { - cli_print(snp::get_node_by_contract(&uuid).map_err(Into::into)); - } else { - cli_print(snp::get_one_contract(&uuid).map_err(Into::into)); - } - } - Some(("update", args)) => cli_print(handle_vm_update(args)), - Some(("delete", args)) => cli_print(handle_vm_delete(args)), - Some(("dtrfs", subcom_args)) => cli_print(handle_vm_dtrfs(subcom_args)), - Some(("distro", _)) => cli_print(Ok(crate::snp::Distro::get_template_list())), - _ => println!("No valid VM subcommand provided."), - } -} - -fn handle_vm_deploy(matches: &ArgMatches) -> Result> { - if let Some(path) = matches.get_one::("yaml-path") { - return Ok(snp::deploy::Request::load_from_yaml(path)?); - } - let hostname = match matches.get_one::("hostname") { - Some(hostname) => hostname.to_string(), - None => name_generator::random_vm_name(), - }; - let location = matches.get_one::("location").unwrap().as_str(); - let ipv4: crate::snp::deploy::IPv4Config = match matches.get_one::("public-ip").unwrap() { - true => crate::snp::deploy::IPv4Config::PublicIPv4, - false => crate::snp::deploy::IPv4Config::PublishPorts(Vec::new()), - }; - let distro = - crate::snp::Distro::from_string(matches.get_one::("distribution").unwrap()); - let vm_config = snp::deploy::Request { - hostname, - location: location.into(), - ipv4, - public_ipv6: false, - vcpus: *matches.get_one::("vcpus").unwrap(), - memory_mb: *matches.get_one::("memory").unwrap(), - disk_size_gb: *matches.get_one::("disk").unwrap(), - dtrfs: None, - hours: *matches.get_one::("hours").unwrap(), - price: *matches.get_one::("price").unwrap(), - distro: Some(distro), - }; - Ok(vm_config.deploy()?) -} - -fn handle_vm_update(update_vm_args: &ArgMatches) -> Result> { - let uuid = update_vm_args.get_one::("uuid").unwrap().clone(); - let memory = *update_vm_args.get_one::("memory").unwrap(); - if memory > 0 && memory < 800 { - log::error!("At least 800MB of memory must be assgined to the VM"); - return Ok(SimpleOutput::from("")); - } - snp::update::Request::process_request( - &uuid, - *update_vm_args.get_one::("vcpus").unwrap(), - memory, - *update_vm_args.get_one::("disk").unwrap(), - &update_vm_args.get_one::("dtrfs").unwrap().to_string(), - )?; - let hours = *update_vm_args.get_one::("hours").unwrap(); - if hours != 0 { - snp::update::expand_vm_hours(&uuid, hours)?; - } - Ok(SimpleOutput::from("VM successfully updated.")) -} - -fn handle_vm_list(update_vm_args: &ArgMatches) -> Result, Box> { - Ok(snp::list_contracts(*update_vm_args.get_one::("as-operator").unwrap())?) -} - -fn handle_vm_ssh(ssh_args: &ArgMatches) -> Result> { - let uuid: String = ssh_args.get_one::("uuid").unwrap().clone(); - Ok(snp::ssh(&uuid, *ssh_args.get_one::("just-print").unwrap())?) -} - -fn handle_vm_delete(delete_args: &ArgMatches) -> Result> { - let uuid: String = delete_args.get_one::("uuid").unwrap().clone(); - snp::delete_contract(&uuid)?; - Ok(SimpleOutput::from("VM successfully deleted.")) -} - -fn handle_vm_dtrfs(_matches: &ArgMatches) -> Result, Box> { - Ok(crate::snp::Dtrfs::print_dtrfs_list()) -} - fn handle_vm_nodes(matches: &ArgMatches) { match matches.subcommand() { Some(("search", _)) => cli_print(snp::print_nodes().map_err(Into::into)), diff --git a/src/snp/cli_handler.rs b/src/snp/cli_handler.rs new file mode 100644 index 0000000..4602228 --- /dev/null +++ b/src/snp/cli_handler.rs @@ -0,0 +1,96 @@ +use crate::name_generator; +use crate::snp; +use crate::{cli_print, SimpleOutput}; +use clap::ArgMatches; +use std::error::Error; + +pub fn handle_vm(matches: &ArgMatches) { + match matches.subcommand() { + Some(("deploy", args)) => cli_print(handle_vm_deploy(args)), + Some(("ssh", args)) => cli_print(handle_vm_ssh(args)), + Some(("list", args)) => cli_print(handle_vm_list(args)), + Some(("inspect", inspect_args)) => { + let uuid: String = inspect_args.get_one::("uuid").unwrap().clone(); + if *inspect_args.get_one::("show-node").unwrap() { + cli_print(snp::get_node_by_contract(&uuid).map_err(Into::into)); + } else { + cli_print(snp::get_one_contract(&uuid).map_err(Into::into)); + } + } + Some(("update", args)) => cli_print(handle_vm_update(args)), + Some(("delete", args)) => cli_print(handle_vm_delete(args)), + Some(("dtrfs", subcom_args)) => cli_print(handle_vm_dtrfs(subcom_args)), + Some(("distro", _)) => cli_print(Ok(crate::snp::Distro::get_template_list())), + _ => println!("No valid VM subcommand provided."), + } +} +fn handle_vm_deploy(matches: &ArgMatches) -> Result> { + if let Some(path) = matches.get_one::("yaml-path") { + return Ok(snp::deploy::Request::load_from_yaml(path)?); + } + let hostname = match matches.get_one::("hostname") { + Some(hostname) => hostname.to_string(), + None => name_generator::random_vm_name(), + }; + let location = matches.get_one::("location").unwrap().as_str(); + let ipv4: crate::snp::deploy::IPv4Config = match matches.get_one::("public-ip").unwrap() { + true => crate::snp::deploy::IPv4Config::PublicIPv4, + false => crate::snp::deploy::IPv4Config::PublishPorts(Vec::new()), + }; + let distro = + crate::snp::Distro::from_string(matches.get_one::("distribution").unwrap()); + let vm_config = snp::deploy::Request { + hostname, + location: location.into(), + ipv4, + public_ipv6: false, + vcpus: *matches.get_one::("vcpus").unwrap(), + memory_mb: *matches.get_one::("memory").unwrap(), + disk_size_gb: *matches.get_one::("disk").unwrap(), + dtrfs: None, + hours: *matches.get_one::("hours").unwrap(), + price: *matches.get_one::("price").unwrap(), + distro: Some(distro), + }; + Ok(vm_config.deploy()?) +} + +fn handle_vm_ssh(ssh_args: &ArgMatches) -> Result> { + let uuid: String = ssh_args.get_one::("uuid").unwrap().clone(); + Ok(snp::ssh(&uuid, *ssh_args.get_one::("just-print").unwrap())?) +} + +fn handle_vm_list(update_vm_args: &ArgMatches) -> Result, Box> { + Ok(snp::list_contracts(*update_vm_args.get_one::("as-operator").unwrap())?) +} + +fn handle_vm_update(update_vm_args: &ArgMatches) -> Result> { + let uuid = update_vm_args.get_one::("uuid").unwrap().clone(); + let memory = *update_vm_args.get_one::("memory").unwrap(); + if memory > 0 && memory < 800 { + log::error!("At least 800MB of memory must be assgined to the VM"); + return Ok(SimpleOutput::from("")); + } + snp::update::Request::process_request( + &uuid, + *update_vm_args.get_one::("vcpus").unwrap(), + memory, + *update_vm_args.get_one::("disk").unwrap(), + &update_vm_args.get_one::("dtrfs").unwrap().to_string(), + )?; + let hours = *update_vm_args.get_one::("hours").unwrap(); + if hours != 0 { + snp::update::expand_vm_hours(&uuid, hours)?; + } + Ok(SimpleOutput::from("VM successfully updated.")) +} + +fn handle_vm_delete(delete_args: &ArgMatches) -> Result> { + let uuid: String = delete_args.get_one::("uuid").unwrap().clone(); + snp::delete_contract(&uuid)?; + Ok(SimpleOutput::from("VM successfully deleted.")) +} + +fn handle_vm_dtrfs(_matches: &ArgMatches) -> Result, Box> { + Ok(crate::snp::Dtrfs::print_dtrfs_list()) +} diff --git a/src/snp/mod.rs b/src/snp/mod.rs index 8e9f964..7c1f7a6 100644 --- a/src/snp/mod.rs +++ b/src/snp/mod.rs @@ -1,3 +1,4 @@ +pub mod cli_handler; pub mod deploy; pub mod grpc; mod injector;