use anyhow::{Result}; // use std::str::FromStr; use crate::defs::*; use crate::clouds::defs::{ Cloud, MainResourcesConfig, }; use crate::tsksrvcs::{run_tsksrvc}; use crate::providers::defs::upcloud::{ ResourcesConfig, ConfigResources, }; pub async fn parse_resources_cfg(res_cfg: &mut ResourcesConfig) -> Result<()> { for i in 0..res_cfg.servers.len() { if res_cfg.servers[i].cluster.role.len() == 0 { res_cfg.servers[i].cluster.role = res_cfg.srvrcfg.role.to_owned(); } if res_cfg.servers[i].networks.len() == 0 { res_cfg.servers[i].networks.push(NetworkInterface::default()); res_cfg.servers[i].networks.push(NetworkInterface::upcloud_basic()); } if res_cfg.servers[i].storageDevices.len() == 0 { let size = res_cfg.srvrcfg.storageDeviceSize; let storage = StorageDevice::upcloud_storage_device_ubuntu20(size); res_cfg.servers[i].storageDevices = vec![storage]; } if res_cfg.servers[i].loginUser.sshKeys.len() == 0 { res_cfg.servers[i].loginUser.sshKeys = res_cfg.srvrcfg.sshKeys.to_owned(); } if res_cfg.servers[i].sshAccess.keyPath.len() == 0 { res_cfg.servers[i].sshAccess.keyPath = res_cfg.srvrcfg.sshKeyPath.to_owned(); } if res_cfg.servers[i].sshAccess.host.len() == 0 { res_cfg.servers[i].sshAccess.host = res_cfg.servers[i].hostname.to_owned(); } if res_cfg.servers[i].sshAccess.user.len() == 0 { res_cfg.servers[i].sshAccess.user = String::from("root"); } if res_cfg.servers[i].tags.len() == 0 { res_cfg.servers[i].tags = res_cfg.srvrcfg.tags.to_owned(); } res_cfg.servers[i].metadata = 1; } Ok(()) } pub async fn make_config_resources(res_cfg: &mut ResourcesConfig) -> Result { Ok(ConfigResources { floatIP: res_cfg.floatIP.to_owned(), hostPrefix: res_cfg.hostPrefix.to_owned(), mainName: res_cfg.mainName.to_owned(), group: res_cfg.group.to_owned(), group_path: res_cfg.group_path.to_owned(), provider: res_cfg.provider.to_owned(), domainName: res_cfg.domainName.to_owned(), cntrllrs: res_cfg.cntrllrs.to_owned(), servers: res_cfg.servers.to_owned(), }) } pub async fn run_tsksrvcs_list(cfg_data: &str, cloud: &Cloud, tsksrvc: &str, cmd: &str, nxt: &str, cfg: &MainResourcesConfig) -> Result<()> { let mut res_cfg: ResourcesConfig = serde_yaml::from_str(&cfg_data)?; // .with_context(|| format!("Failed to parse 'cfg_path' from {}", &cfg_path))?; // println!("{:#?}",&res_cfg); parse_resources_cfg(&mut res_cfg).await?; let config_res: ConfigResources = make_config_resources(&mut res_cfg).await?; // println!("{:#?}",&config_res); // let str_config_res = serde_json::to_string(&config_res).with_context(|| format!("Failed to stringify 'config_res' from {}", &cfg_path))?; // println!("{}",&str_config_res); for (_, elem) in config_res.servers.iter().enumerate() { // dbg!(&elem); if ! cloud.env.listhosts.is_empty() && ! cloud.env.listhosts.contains(&elem.hostname) { continue; } let mut use_next = false; match tsksrvc { "createserver" | "modifyip" | "startserver" => { println!("TskSrvc {} ",&tsksrvc); }, _ => { for (_, tsk) in elem.tsksrvcs.iter().enumerate() { let tsk_name = format!("{}",&tsk.name); if tsk_name.as_str() == tsksrvc && nxt == "next" { use_next=true; } if tsksrvc == "all" || tsk_name.as_str() == tsksrvc || use_next { let srvr_json = serde_yaml::to_string(elem)?; let ssh_access: SSHAccess = elem.sshAccess.to_owned(); run_tsksrvc(&cloud, &tsk, cmd, &elem.hostname, &cfg.domainName, srvr_json, ssh_access, &config_res.cntrllrs).await?; } } } }; }; Ok(()) }