// SPDX-License-Identifier: Apache-2.0 use crate::{cli_print, general, name_generator, snp, 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 vm_id: String = inspect_args.get_one::("id").unwrap().clone(); if *inspect_args.get_one::("show-node").unwrap() { cli_print(snp::get_node_by_contract(&vm_id).map_err(Into::into)); } else { cli_print(snp::get_one_contract(&vm_id).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."), } } pub fn handle_vm_nodes(matches: &ArgMatches) { match matches.subcommand() { Some(("search", arguments)) => { let location = arguments.get_one::("location").unwrap().as_str(); cli_print(snp::search_nodes(location.into()).map_err(Into::into)); } Some(("offers", arguments)) => { let location = arguments.get_one::("location").unwrap().as_str(); cli_print(snp::print_node_offers(location.into()).map_err(Into::into)); } Some(("inspect", path_subcommand)) => { let ip: String = path_subcommand.get_one::("ip").unwrap().clone(); cli_print(snp::inspect_node(ip).map_err(Into::into)); } Some(("report", path_subcommand)) => { let node_pubkey: String = path_subcommand.get_one::("pubkey").unwrap().clone(); let contract_id: String = path_subcommand.get_one::("contract").unwrap().clone(); let reason: String = path_subcommand.get_one::("reason").unwrap().clone(); cli_print(general::report_node(node_pubkey, contract_id, reason).map_err(Into::into)) } _ => { println!("Available commands are search, inspect and report. Use --help for more information.") } } } 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_gib: *matches.get_one::("memory").unwrap(), disk_size_gib: *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 vm_id: String = ssh_args.get_one::("id").unwrap().clone(); Ok(snp::ssh(&vm_id, *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 vm_id = update_vm_args.get_one::("id").unwrap().clone(); let hostname = update_vm_args.get_one::("hostname").unwrap().clone(); let memory = *update_vm_args.get_one::("memory").unwrap(); snp::update::Request::process_request( hostname, &vm_id, *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(&vm_id, hours)?; } Ok(SimpleOutput::from("VM successfully updated.")) } fn handle_vm_delete(delete_args: &ArgMatches) -> Result> { let vm_id: String = delete_args.get_one::("id").unwrap().clone(); snp::delete_contract(&vm_id)?; Ok(SimpleOutput::from("VM successfully deleted.")) } fn handle_vm_dtrfs(_matches: &ArgMatches) -> Result, Box> { Ok(crate::snp::Dtrfs::print_dtrfs_list()) }