123 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			123 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
// 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 uuid: String = inspect_args.get_one::<String>("uuid").unwrap().clone();
 | 
						|
            if *inspect_args.get_one::<bool>("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."),
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
pub fn handle_vm_nodes(matches: &ArgMatches) {
 | 
						|
    match matches.subcommand() {
 | 
						|
        Some(("search", arguments)) => {
 | 
						|
            let location = arguments.get_one::<String>("location").unwrap().as_str();
 | 
						|
            cli_print(snp::search_nodes(location.into()).map_err(Into::into));
 | 
						|
        }
 | 
						|
        Some(("offers", arguments)) => {
 | 
						|
            let location = arguments.get_one::<String>("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::<String>("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::<String>("pubkey").unwrap().clone();
 | 
						|
            let contract_uuid: String =
 | 
						|
                path_subcommand.get_one::<String>("contract").unwrap().clone();
 | 
						|
            let reason: String = path_subcommand.get_one::<String>("reason").unwrap().clone();
 | 
						|
            cli_print(general::report_node(node_pubkey, contract_uuid, 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<snp::VmSshArgs, Box<dyn Error>> {
 | 
						|
    if let Some(path) = matches.get_one::<String>("yaml-path") {
 | 
						|
        return Ok(snp::deploy::Request::load_from_yaml(path)?);
 | 
						|
    }
 | 
						|
    let hostname = match matches.get_one::<String>("hostname") {
 | 
						|
        Some(hostname) => hostname.to_string(),
 | 
						|
        None => name_generator::random_vm_name(),
 | 
						|
    };
 | 
						|
    let location = matches.get_one::<String>("location").unwrap().as_str();
 | 
						|
    let ipv4: crate::snp::deploy::IPv4Config = match matches.get_one::<bool>("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::<String>("distribution").unwrap());
 | 
						|
    let vm_config = snp::deploy::Request {
 | 
						|
        hostname,
 | 
						|
        location: location.into(),
 | 
						|
        ipv4,
 | 
						|
        public_ipv6: false,
 | 
						|
        vcpus: *matches.get_one::<u32>("vcpus").unwrap(),
 | 
						|
        memory_gib: *matches.get_one::<u32>("memory").unwrap(),
 | 
						|
        disk_size_gib: *matches.get_one::<u32>("disk").unwrap(),
 | 
						|
        dtrfs: None,
 | 
						|
        hours: *matches.get_one::<u32>("hours").unwrap(),
 | 
						|
        price: *matches.get_one::<u64>("price").unwrap(),
 | 
						|
        distro: Some(distro),
 | 
						|
    };
 | 
						|
    Ok(vm_config.deploy()?)
 | 
						|
}
 | 
						|
 | 
						|
fn handle_vm_ssh(ssh_args: &ArgMatches) -> Result<snp::VmSshArgs, Box<dyn Error>> {
 | 
						|
    let uuid: String = ssh_args.get_one::<String>("uuid").unwrap().clone();
 | 
						|
    Ok(snp::ssh(&uuid, *ssh_args.get_one::<bool>("just-print").unwrap())?)
 | 
						|
}
 | 
						|
 | 
						|
fn handle_vm_list(update_vm_args: &ArgMatches) -> Result<Vec<snp::VmContract>, Box<dyn Error>> {
 | 
						|
    Ok(snp::list_contracts(*update_vm_args.get_one::<bool>("as-operator").unwrap())?)
 | 
						|
}
 | 
						|
 | 
						|
fn handle_vm_update(update_vm_args: &ArgMatches) -> Result<SimpleOutput, Box<dyn Error>> {
 | 
						|
    let uuid = update_vm_args.get_one::<String>("uuid").unwrap().clone();
 | 
						|
    let hostname = update_vm_args.get_one::<String>("hostname").unwrap().clone();
 | 
						|
    let memory = *update_vm_args.get_one::<u32>("memory").unwrap();
 | 
						|
    snp::update::Request::process_request(
 | 
						|
        hostname,
 | 
						|
        &uuid,
 | 
						|
        *update_vm_args.get_one::<u32>("vcpus").unwrap(),
 | 
						|
        memory,
 | 
						|
        *update_vm_args.get_one::<u32>("disk").unwrap(),
 | 
						|
        &update_vm_args.get_one::<String>("dtrfs").unwrap().to_string(),
 | 
						|
    )?;
 | 
						|
    let hours = *update_vm_args.get_one::<u32>("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<SimpleOutput, Box<dyn Error>> {
 | 
						|
    let uuid: String = delete_args.get_one::<String>("uuid").unwrap().clone();
 | 
						|
    snp::delete_contract(&uuid)?;
 | 
						|
    Ok(SimpleOutput::from("VM successfully deleted."))
 | 
						|
}
 | 
						|
 | 
						|
fn handle_vm_dtrfs(_matches: &ArgMatches) -> Result<Vec<snp::Dtrfs>, Box<dyn Error>> {
 | 
						|
    Ok(crate::snp::Dtrfs::print_dtrfs_list())
 | 
						|
}
 |