diff --git a/src/config.rs b/src/config.rs index 7b1d68b..1354ae3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -104,7 +104,7 @@ impl Config { pub fn verify_and_install_artefacts(file_hash: &str) -> Result<(), Error> { use std::fs; let artefacts_dir = Config::artefacts_dir()? + "/"; - let file_path = Path::new(&artefacts_dir).join(&file_hash); + let file_path = Path::new(&artefacts_dir).join(file_hash); if file_path.exists() { log::debug!("Artefact '{}' already exists", file_hash); @@ -158,7 +158,7 @@ impl Config { if !Path::new(&dir).exists() { std::fs::create_dir_all(dir.clone())?; } - Ok(String::from(dir + "/uuid_list")) + Ok(dir + "/uuid_list") } pub fn path_dir() -> Result { @@ -191,11 +191,13 @@ impl Config { Err(e) => { debug!("Could not load config due to error: {e}"); eprintln!("Config file not found. Creating new config file!"); - let mut config = Self::default(); - config.hratls_pubkey = Self::get_hratls_pubkey_hex(); - config.hratls_path = Self::hratls_private_key_path(); - config.mrsigner = Self::get_mr_signer(); - config.signing_key_path = Self::signing_key_path(); + let config = Self { + hratls_pubkey: Self::get_hratls_pubkey_hex(), + hratls_path: Self::hratls_private_key_path(), + mrsigner: Self::get_mr_signer(), + signing_key_path: Self::signing_key_path(), + ..Default::default() + }; if let Err(e) = config.save_to_disk() { log::error!("Could not save config to disk: {e}"); panic!("Could not initialize config."); diff --git a/src/general/operators.rs b/src/general/operators.rs index 7b2794f..c33a2eb 100644 --- a/src/general/operators.rs +++ b/src/general/operators.rs @@ -33,18 +33,15 @@ pub fn register(escrow: u64, email: String) -> Result { - println!("The operator {} supplies {} nanoLP as escrow,", op.pubkey, op.escrow,); - println!( - "has {} app servers, {} VM servers, and {} total reports for all servers.", - op.app_nodes, op.vm_nodes, op.reports - ); - println!("He can be contacted at {}", op.email); - } - None => (), + if let Some(op) = &self.operator { + println!("The operator {} supplies {} nanoLP as escrow,", op.pubkey, op.escrow,); + println!( + "has {} app servers, {} VM servers, and {} total reports for all servers.", + op.app_nodes, op.vm_nodes, op.reports + ); + println!("He can be contacted at {}", op.email); } - if self.vm_nodes.len() == 0 { + if self.vm_nodes.is_empty() { return; } println!("\n-- VM NODES --"); @@ -77,7 +74,7 @@ impl crate::HumanOutput for Vec { } pub fn print_operators() -> Result, grpc::Error> { - Ok(block_on(grpc::list_operators())?) + block_on(grpc::list_operators()) } pub fn kick(contract_uuid: String, reason: String) -> Result { diff --git a/src/name_generator.rs b/src/name_generator.rs index fdf5fb3..5bd24fc 100644 --- a/src/name_generator.rs +++ b/src/name_generator.rs @@ -7,8 +7,8 @@ pub fn random_app_name() -> String { let adjective = ADJECTIVES[random_index].to_string(); let random_index = rng.gen_range(0..97); let substantive = SUBSTANTIVES[random_index].to_string(); - let app_name = adjective + "-" + &substantive; - app_name + + adjective + "-" + &substantive } pub fn random_vm_name() -> String { let mut rng = rand::thread_rng(); diff --git a/src/sgx/cli_handler.rs b/src/sgx/cli_handler.rs index 9fa0de7..a458aa3 100644 --- a/src/sgx/cli_handler.rs +++ b/src/sgx/cli_handler.rs @@ -51,18 +51,18 @@ fn handle_deploy( { AppDeployConfig::from_path(file_path).unwrap() } else { - let vcpu = deploy_match.get_one::("vcpus").unwrap().clone(); - let memory_mb = deploy_match.get_one::("memory").unwrap().clone(); - let disk_mb = deploy_match.get_one::("disk").unwrap().clone(); + let vcpu = *deploy_match.get_one::("vcpus").unwrap(); + let memory_mb = *deploy_match.get_one::("memory").unwrap(); + let disk_mb = *deploy_match.get_one::("disk").unwrap(); let port = deploy_match.get_many::("port").unwrap_or_default().cloned().collect::>(); let package_url = deploy_match.get_one::("package-url").unwrap().clone(); // let package_type = deploy_match.get_one::("package-type").unwrap().clone(); - let hours = deploy_match.get_one::("hours").unwrap().clone(); - let node_unit_price = deploy_match.get_one::("price").unwrap().clone(); + let hours = *deploy_match.get_one::("hours").unwrap(); + let node_unit_price = *deploy_match.get_one::("price").unwrap(); let location = deploy_match.get_one::("location").unwrap().as_str(); let app_name = - deploy_match.get_one::("name").cloned().unwrap_or_else(|| random_app_name()); + deploy_match.get_one::("name").cloned().unwrap_or_else(random_app_name); // let private_package = package_type == "private"; let private_package = false; @@ -88,7 +88,7 @@ fn handle_deploy( } }; - if app_deploy_config.app_name == "" { + if app_deploy_config.app_name.is_empty() { app_deploy_config.app_name = random_app_name(); } @@ -104,7 +104,7 @@ fn handle_deploy( override_envs_and_args_launch_config(&mut launch_config, envs, args); match block_on(deploy_new_app_and_update_config(app_deploy_config, launch_config)) { - Ok(new_app_res) if new_app_res.error == "" => Ok(new_app_res.into()), + Ok(new_app_res) if new_app_res.error.is_empty() => Ok(new_app_res.into()), Ok(new_app_res) => Err(Box::new(std::io::Error::other(new_app_res.error))), Err(e) => Err(Box::new(e)), } @@ -154,7 +154,7 @@ fn handle_config_sub_validate( check_matche: &ArgMatches, ) -> Result> { if let Some(file_path) = check_matche.get_one::("config") { - let _config = validate_yaml(&file_path); + let _config = validate_yaml(file_path); Ok(SimpleOutput::from("The YAML file is valid!")) } else { Err(Box::new(std::io::Error::other("config file path argument required"))) @@ -167,7 +167,7 @@ fn handle_config_sub_update( if let (Some(file_path), Some(uuid)) = (update_matche.get_one::("config"), update_matche.get_one::("uuid")) { - let loaded_config = validate_yaml(&file_path).unwrap(); + let loaded_config = validate_yaml(file_path).unwrap(); match block_on(attest_and_send_config(loaded_config, uuid)) { Ok(_) => Ok(SimpleOutput::from("App launch config updated successfully")), Err(e) => Err(Box::new(std::io::Error::other(format!( diff --git a/src/sgx/config.rs b/src/sgx/config.rs index 9633a36..02e3105 100644 --- a/src/sgx/config.rs +++ b/src/sgx/config.rs @@ -14,13 +14,13 @@ type Result = std::result::Result; #[allow(dead_code)] pub trait DeteeCliExt { - fn from_path(path: &String) -> Result + fn from_path(path: &str) -> Result where Self: std::marker::Sized; } impl DeteeCliExt for AppDeployConfig { - fn from_path(path: &String) -> Result { + fn from_path(path: &str) -> Result { let config_str = std::fs::read_to_string(path)?; Ok(serde_yaml::from_str(&config_str)?) } diff --git a/src/sgx/mod.rs b/src/sgx/mod.rs index 127bcaf..f2401e4 100644 --- a/src/sgx/mod.rs +++ b/src/sgx/mod.rs @@ -48,7 +48,7 @@ fn display_mins(minutes: &u64) -> String { format!("{hours}h {mins}m") } -fn display_ports(ports: &Vec<(u32, u32)>) -> String { +fn display_ports(ports: &[(u32, u32)]) -> String { ports.iter().map(|port| format!("({}:{})", port.0, port.1,)).collect::>().join(", ") } diff --git a/src/sgx/utils.rs b/src/sgx/utils.rs index 75a901e..b78aa38 100644 --- a/src/sgx/utils.rs +++ b/src/sgx/utils.rs @@ -119,19 +119,16 @@ pub fn override_envs_and_args_launch_config( let value = env.next().expect("environment variable pair must be in the format 'key=value'"); - if launch_config.environments.iter().find(|env| env.name == key).is_some() { + if launch_config.environments.iter().any(|env| env.name == key) { let existing_env = launch_config.environments.iter_mut().find(|env| env.name == key).unwrap(); existing_env.name = key.to_string(); existing_env.value = value.to_string(); } else { - let mut new_env = EnvironmentEntry::default(); - - new_env.name = key.to_string(); - new_env.value = value.to_string(); - - launch_config.environments.push(new_env); + launch_config + .environments + .push(EnvironmentEntry { name: key.to_string(), value: value.to_string() }); } } @@ -145,7 +142,7 @@ pub async fn deploy_new_app_and_update_config( launch_config: DtpmConfig, ) -> Result { let new_app_res = new_app(app_deploy_config).await?; - if new_app_res.error == "" { + if new_app_res.error.is_empty() { println!("Deploying..."); tokio::time::sleep(tokio::time::Duration::from_millis(3100)).await; Retry::spawn(FixedInterval::from_millis(500).take(5), || { @@ -153,7 +150,7 @@ pub async fn deploy_new_app_and_update_config( attest_and_send_config(launch_config.clone(), &new_app_res.uuid) }) .await?; - Ok(new_app_res.into()) + Ok(new_app_res) } else { Err(Error::Deployment(new_app_res.error)) } diff --git a/src/snp/mod.rs b/src/snp/mod.rs index 7c1f7a6..0e146d2 100644 --- a/src/snp/mod.rs +++ b/src/snp/mod.rs @@ -63,7 +63,7 @@ impl TryFrom for VmSshArgs { args.user = "root".to_string(); args.key_path = Config::init_config().get_ssh_pubkey()?.trim_end_matches(".pub").to_string(); - if contract.public_ipv4 != "" { + if !contract.public_ipv4.is_empty() { args.ip = contract.public_ipv4; args.port = "22".to_string(); } else { @@ -96,11 +96,13 @@ pub struct Dtrfs { impl Dtrfs { pub fn print_dtrfs_list() -> Vec { - let mut dtrfs_vec = Vec::new(); - dtrfs_vec.push(DEFAULT_DTRFS.clone()); - dtrfs_vec.push(ALTERNATIVE_INIT[0].clone()); - dtrfs_vec.push(ALTERNATIVE_INIT[1].clone()); - dtrfs_vec + // let mut dtrfs_vec = Vec::new(); + // dtrfs_vec.push(DEFAULT_DTRFS.clone()); + // dtrfs_vec.push(ALTERNATIVE_INIT[0].clone()); + // dtrfs_vec.push(ALTERNATIVE_INIT[1].clone()); + // dtrfs_vec + + vec![DEFAULT_DTRFS.clone(), ALTERNATIVE_INIT[0].clone(), ALTERNATIVE_INIT[1].clone()] } fn load_from_file(config_path: &str) -> Result { @@ -131,14 +133,23 @@ impl super::HumanOutput for Vec { impl Distro { pub fn get_template_list() -> Vec { - let mut distro_vec = Vec::new(); - distro_vec.push(DEFAULT_ARCHLINUX.clone()); - distro_vec.push(DEFAULT_UBUNTU.clone()); - distro_vec.push(DEFAULT_FEDORA.clone()); - distro_vec.push(ALTERNATIVE_DISTROS[0].clone()); - distro_vec.push(ALTERNATIVE_DISTROS[1].clone()); - distro_vec.push(ALTERNATIVE_DISTROS[2].clone()); - distro_vec + // let mut distro_vec = Vec::new(); + // distro_vec.push(DEFAULT_ARCHLINUX.clone()); + // distro_vec.push(DEFAULT_UBUNTU.clone()); + // distro_vec.push(DEFAULT_FEDORA.clone()); + // distro_vec.push(ALTERNATIVE_DISTROS[0].clone()); + // distro_vec.push(ALTERNATIVE_DISTROS[1].clone()); + // distro_vec.push(ALTERNATIVE_DISTROS[2].clone()); + // distro_vec + + vec![ + DEFAULT_ARCHLINUX.clone(), + DEFAULT_UBUNTU.clone(), + DEFAULT_FEDORA.clone(), + ALTERNATIVE_DISTROS[0].clone(), + ALTERNATIVE_DISTROS[1].clone(), + ALTERNATIVE_DISTROS[2].clone(), + ] } pub fn from_string(distro: &str) -> Distro { @@ -250,7 +261,7 @@ pub fn ssh(uuid: &str, just_print: bool) -> Result { ..Default::default() }; let contracts = block_on(snp::grpc::list_contracts(req))?; - if contracts.len() == 0 { + if contracts.is_empty() { return Err(Error::VmContractNotFound(uuid.to_string())); } let args: VmSshArgs = contracts[0].clone().try_into()?; @@ -315,7 +326,7 @@ pub fn list_contracts(as_operator: bool) -> Result, Error> { Ok(contracts) } -fn write_uuid_list(contracts: &Vec) -> Result<(), Error> { +fn write_uuid_list(contracts: &[VmContract]) -> Result<(), Error> { let vm_uuid_list_path = Config::vm_uuid_list_path()?; let mut file = std::fs::File::create(vm_uuid_list_path)?; let output: String = contracts @@ -330,7 +341,7 @@ fn write_uuid_list(contracts: &Vec) -> Result<(), Error> { pub fn append_uuid_list(uuid: &str, hostname: &str) -> Result<(), Error> { use std::{fs::OpenOptions, io::prelude::*}; - let mut file = OpenOptions::new().write(true).append(true).open(Config::vm_uuid_list_path()?)?; + let mut file = OpenOptions::new().append(true).open(Config::vm_uuid_list_path()?)?; writeln!(file, "{uuid}\t{hostname}")?; Ok(()) } @@ -384,8 +395,8 @@ pub fn calculate_nanolp( node_price as f64 / 1_000_000_000.0, total_units, ); - eprint!( - "Locking {} LP (offering the VM for {} hours).\n", + eprintln!( + "Locking {} LP (offering the VM for {} hours).", locked_nano as f64 / 1_000_000_000.0, hours );