From 325aa8a72887f438920aabbaa68fb08d79648d88 Mon Sep 17 00:00:00 2001 From: JesusPerez Date: Thu, 14 Oct 2021 15:08:12 +0100 Subject: [PATCH] chore: liveness & check return SrvcsHostInfo. on_cloud_name_req_[info/check] --- src/clouds/on_clouds.rs | 87 +++++++++++++++++++++++++++++------------ src/clouds/upcloud.rs | 37 +++++++++++++----- 2 files changed, 89 insertions(+), 35 deletions(-) diff --git a/src/clouds/on_clouds.rs b/src/clouds/on_clouds.rs index 5df9879..7558875 100644 --- a/src/clouds/on_clouds.rs +++ b/src/clouds/on_clouds.rs @@ -29,9 +29,9 @@ use crate::defs::{ CloudItem, CloudCheckItem }; -use crate::clouds::defs::{TsksrvcInfo,AppsrvcInfo}; +use crate::clouds::defs::{TsksrvcInfo,AppsrvcInfo,SrvcsHostInfo}; -use crate::clouds::upcloud::{get_upcloud_info,run_on_upcloud}; +use crate::clouds::upcloud::{get_upcloud_info,run_on_upcloud_info,run_on_upcloud_check}; /// On_cloud /// load __`item`__ form `envmnt` with `dflt` @@ -160,11 +160,12 @@ pub async fn load_cloud_env(cloud: &mut Cloud, source: &str) -> Result<()> { Ok(()) } pub async fn load_cloud_config(cloud: &mut Cloud, source: &str) -> Result<(KloudHome,Provider,String), Error> { - // dbg!(&cloud.env); + // dbg!(&cloud); let cfg_data = load_config_data(&cloud,source).await?; let cfg: KloudHome = serde_yaml::from_str(&cfg_data)?; let cfg_provider = format!("{}",cfg.provider[0]); - let provider = cloud.providers.get(&cfg_provider).with_context(|| format!("Provider '{}'' not defined", &cfg_provider))?; + let provider = cloud.providers.get(&cfg_provider).with_context(|| + format!("Provider '{}'' not defined", &cfg_provider))?; Ok((cfg, provider.to_owned(), cfg_data)) } pub async fn load_cloud_check_config(cloud: &mut Cloud, source: &str) -> Result<(KloudCheckHome,Provider,String), Error> { @@ -225,7 +226,7 @@ pub async fn get_cloud_home_list(cloud: &Cloud) -> Result> { Ok(kloud_files.to_owned()) } -pub fn run_ssh_on_srvr(hostname: &str,tsksrvc_name: &str, tsksrvc_cmd: &str, ssh_access: &SSHAccess) -> Result { +pub fn run_ssh_on_srvr(hostname: &str,tsksrvc_name: &str, tsksrvc_cmd: &str, ssh_access: &SSHAccess) -> Result { let debug = envmnt::get_isize("DEBUG",0); if debug > 0 { println!("Checking connection to {}@{} on {} for {} ",ssh_access.user,ssh_access.host,ssh_access.port,&tsksrvc_name); @@ -249,10 +250,10 @@ pub fn run_ssh_on_srvr(hostname: &str,tsksrvc_name: &str, tsksrvc_cmd: &str, ssh return Err(anyhow!("Connection to '{}' for tsksrvc '{}' failed: {}",&hostname,&tsksrvc_name,&output.status)); } let res = str::from_utf8(&output.stdout).unwrap_or_else(|_| ""); - let info: serde_yaml::Value = serde_yaml::from_str(&res) + let info: String = serde_yaml::from_str(&res) .unwrap_or_else(|e| { eprintln!("serde_yaml: {}",e); - serde_yaml::Value::default() + String::from("") }); Ok(info.to_owned()) } @@ -267,7 +268,7 @@ pub async fn parse_srvr_tsksrvcs(hostname: &str, sshaccess: &SSHAccess, tsksrvcs info: run_ssh_on_srvr(&hostname, &os_name, "", &sshaccess) .unwrap_or_else(|e| { eprintln!("run_ssh_on_srvr os: {}",e); - serde_yaml::Value::default() + String::from("") }), srvc: TskSrvc::default(), }); @@ -279,7 +280,7 @@ pub async fn parse_srvr_tsksrvcs(hostname: &str, sshaccess: &SSHAccess, tsksrvcs info: run_ssh_on_srvr(&hostname, &floatip_name, "", &sshaccess) .unwrap_or_else(|e| { eprintln!("run_ssh_on_srvr floatip: {}",e); - serde_yaml::Value::default() + String::from("") }), srvc: TskSrvc::default(), }); @@ -290,8 +291,8 @@ pub async fn parse_srvr_tsksrvcs(hostname: &str, sshaccess: &SSHAccess, tsksrvcs name: format!("{}_pods",&k8_name), info: run_ssh_on_srvr(&hostname, &k8_name, "pods", &sshaccess) .unwrap_or_else(|e| { - eprintln!("run_ssh_on_srvr kubernetes_pods: {}",e); - serde_yaml::Value::default() + eprintln!("run_ssh_on_srvr kubernetes_pods: {}",e); + String::from("") }), srvc: TskSrvc::default(), }); @@ -315,7 +316,7 @@ pub async fn parse_srvr_tsksrvcs(hostname: &str, sshaccess: &SSHAccess, tsksrvcs info: run_ssh_on_srvr(&hostname, &name, "", &sshaccess) .unwrap_or_else(|e| { eprintln!("run_ssh_on_srvr for {}: {}",&tsksrvc.name,e); - serde_yaml::Value::default() + String::from("") }), srvc: tsksrvc.to_owned(), }); @@ -360,7 +361,7 @@ pub async fn liveness_srvr_tsksrvcs(source: &str, cntrllrs: &Vec, tsksr name: format!("{}",&tsksrvc.name), info: serde_yaml::from_str(res_info).unwrap_or_else(|e| { eprintln!("Serde liveness Error: {} {} -> {}",&source,&serverstring,e); - serde_yaml::Value::default() + String::from("") }), srvc: tsksrvc.to_owned(), }); @@ -381,7 +382,7 @@ pub async fn parse_srvr_appsrvcs(hostname: &str, sshaccess: &SSHAccess, appsrvcs info: run_ssh_on_srvr(&hostname, &name, "", &sshaccess) .unwrap_or_else(|e| { eprintln!("run_ssh_on_srvr for {}: {}",&appsrvc.name,e); - serde_yaml::Value::default() + String::from("") }), srvc: appsrvc.to_owned(), }); @@ -425,7 +426,7 @@ pub async fn liveness_srvr_appsrvcs(source: &str, cntrllrs: &Vec, appsr name: format!("{}",&appsrvc.name), info: serde_yaml::from_str(res_info).unwrap_or_else(|e| { eprintln!("Serde liveness Error: {} {} -> {}",&source,&serverstring,e); - serde_yaml::Value::default() + String::from("") }), srvc: appsrvc.to_owned(), }); @@ -443,12 +444,12 @@ pub async fn get_provider_info(provider: &str, hostname: &str, cmd: &str , cfg_p } } -pub async fn run_on_provider(reqname: &str, req_tsksrvc: &str, req_srvrs: &str, provider: Provider, source: &str, cfg_data: String, env_cloud: &Cloud) -> String { +pub async fn run_on_provider_info(reqname: &str, req_tsksrvc: &str, req_srvrs: &str, provider: Provider, source: &str, cfg_data: String, env_cloud: &Cloud) -> String { match provider.name.as_str() { "upcloud" => { // TODO clean SSH keys or encrypt content // dbg!(&cloud_config); - run_on_upcloud(reqname,req_tsksrvc,req_srvrs, source, cfg_data, env_cloud).await + run_on_upcloud_info(reqname,req_tsksrvc,req_srvrs, source, cfg_data, env_cloud).await }, _ => { let result = format!("Errors on {} provider {} not found",&source,&provider.name); @@ -459,7 +460,22 @@ pub async fn run_on_provider(reqname: &str, req_tsksrvc: &str, req_srvrs: &str, } } } -pub async fn on_cloud_name_req(reqname: &str,env_cloud: &Cloud,_reqenv: &ReqEnv,req_tsksrvc: &str, req_srvrs: &str, source: &str) -> String { +pub async fn run_on_provider_check(reqname: &str, req_tsksrvc: &str, req_srvrs: &str, provider: Provider, source: &str, cfg_data: String, env_cloud: &Cloud) -> Vec { + match provider.name.as_str() { + "upcloud" => { + // TODO clean SSH keys or encrypt content + // dbg!(&cloud_config); + run_on_upcloud_check(reqname,req_tsksrvc,req_srvrs, source, cfg_data, env_cloud).await + }, + _ => { + if envmnt::get_isize("DEBUG",0) > 1 { + println!("Errors on {} provider {} not found",&source,&provider.name); + } + Vec::new() + } + } +} +pub async fn on_cloud_name_req_info(reqname: &str,env_cloud: &Cloud,_reqenv: &ReqEnv,req_tsksrvc: &str, req_srvrs: &str, source: &str) -> String { let mut cloud = env_cloud.to_owned(); load_cloud_env(&mut cloud, &source).await .unwrap_or_else(|e| { @@ -477,7 +493,26 @@ pub async fn on_cloud_name_req(reqname: &str,env_cloud: &Cloud,_reqenv: &ReqEnv, } return result; } - run_on_provider(&reqname,&req_tsksrvc,&req_srvrs,provider,&source,cfg_data,&cloud).await + run_on_provider_info(&reqname,&req_tsksrvc,&req_srvrs,provider,&source,cfg_data,&cloud).await +} +pub async fn on_cloud_name_req_check(reqname: &str,env_cloud: &Cloud,_reqenv: &ReqEnv,req_tsksrvc: &str, req_srvrs: &str, source: &str) -> Vec { + let mut cloud = env_cloud.to_owned(); + load_cloud_env(&mut cloud, &source).await + .unwrap_or_else(|e| { + eprintln!("load_cloud_env: {}",e); + }); + let (cfg,provider,cfg_data) = load_cloud_name_config(&mut cloud, &source).await + .unwrap_or_else(|e| { + eprintln!("load_cloud_name_config: {}",e); + (MainResourcesConfig::default(),Provider::default(),String::from("")) + }); + if cfg.mainName.is_empty() || cfg_data.is_empty() { + if envmnt::get_isize("DEBUG",0) > 1 { + println!("Errors loading {}",&source); + } + return Vec::new(); + } + run_on_provider_check(&reqname,&req_tsksrvc,&req_srvrs,provider,&source,cfg_data,&cloud).await } pub async fn create_cloud_config(reqname: &str,req_tsksrvcs: &str,reqenv: &ReqEnv, entries: Vec,mut cloud: Cloud) -> String { let config = reqenv.config(); @@ -537,21 +572,21 @@ pub async fn create_cloud_config(reqname: &str,req_tsksrvcs: &str,reqenv: &ReqEn let mut items: Vec = Vec::new(); for (itm_idx,itm) in grp.items.iter().enumerate() { let resources: Option; - let liveness: Option; + let liveness: Vec; let provision: Option; if req_tsksrvcs.contains("liveness") { if no_check_entries { - liveness = Some(on_cloud_name_req("liveness",&cloud,&reqenv,"","",&itm.path).await); + liveness = on_cloud_name_req_check("liveness",&cloud,&reqenv,"","",&itm.path).await; } else if check_entries.len() > 0 && check_entries.len() < idx { liveness = check_entries[idx].groups[grp_idx].items[itm_idx].liveness.to_owned(); } else { - liveness = Some(String::from("")); + liveness = Vec::new(); } } else { liveness = itm.liveness.to_owned(); } if reqname.contains("provision") || req_tsksrvcs.contains("provision") { - provision = Some(on_cloud_name_req("provision",&cloud,&reqenv,"","",&itm.path).await); + provision = Some(on_cloud_name_req_info("provision",&cloud,&reqenv,"","",&itm.path).await); } else { provision = itm.provision.to_owned(); } @@ -623,11 +658,11 @@ pub async fn create_cloud_check(req_tsksrvcs: &str,reqenv: &ReqEnv,entries: Vec< for grp in cfg.groups.iter() { let mut items: Vec = Vec::new(); for itm in grp.items.iter() { - let liveness: Option; + let liveness: Vec; if req_tsksrvcs.contains("liveness") { - liveness = Some(on_cloud_name_req("liveness",&cloud,&reqenv,"","",&itm.path).await); + liveness = on_cloud_name_req_check("liveness",&cloud,&reqenv,"","",&itm.path).await; } else if req_tsksrvcs.contains("apps") { - liveness = Some(on_cloud_name_req("apps",&cloud,&reqenv,"","",&itm.path).await); + liveness = on_cloud_name_req_check("apps",&cloud,&reqenv,"","",&itm.path).await; } else { liveness = itm.liveness.to_owned(); } diff --git a/src/clouds/upcloud.rs b/src/clouds/upcloud.rs index e51e770..5d284c2 100644 --- a/src/clouds/upcloud.rs +++ b/src/clouds/upcloud.rs @@ -64,7 +64,7 @@ pub async fn get_upcloud_info(hostname: &str, cmd: &str,cfg_path: &str) -> Stri _ => { String::from("")} } } -pub async fn run_on_upcloud(reqname: &str,req_tsksrvc: &str, req_srvrs: &str, source: &str, cfg_data: String, env_cloud: &Cloud) -> String { +pub async fn run_on_upcloud_info(reqname: &str,req_tsksrvc: &str, req_srvrs: &str, source: &str, cfg_data: String, env_cloud: &Cloud) -> String { let cloud_config = load_upcloud_config(cfg_data).await .unwrap_or_else(|e| { eprintln!("load_upcloud_config: {}",e); @@ -99,6 +99,20 @@ pub async fn run_on_upcloud(reqname: &str,req_tsksrvc: &str, req_srvrs: &str, so serde_json::to_string(&hosts_info) .unwrap_or_else(|e| { eprintln!("{}",e); String::from("")}) }, + _ => { + serde_json::to_string(&cloud_config) + .unwrap_or_else(|e| { eprintln!("{}",e); String::from("")}) + }, + } +} + +pub async fn run_on_upcloud_check(reqname: &str,req_tsksrvc: &str, req_srvrs: &str, source: &str, cfg_data: String, _env_cloud: &Cloud) -> Vec { + let cloud_config = load_upcloud_config(cfg_data).await + .unwrap_or_else(|e| { + eprintln!("load_upcloud_config: {}",e); + ConfigResources::default() + }); + match reqname { "status" => { match req_tsksrvc { _ => { @@ -125,8 +139,10 @@ pub async fn run_on_upcloud(reqname: &str,req_tsksrvc: &str, req_srvrs: &str, so // println!("{}",&res); // let tsks_yaml: Vec = serde_yaml::from_str(&res) // .unwrap_or_else(|e| { eprintln!("{}",e); Vec::new() }); - serde_json::to_string(&srvcs_hosts_info) - .unwrap_or_else(|e| { eprintln!("{}",e); String::from("")}) + + // serde_json::to_string(&srvcs_hosts_info) + // .unwrap_or_else(|e| { eprintln!("{}",e); String::from("")}) + srvcs_hosts_info } } } @@ -143,8 +159,9 @@ pub async fn run_on_upcloud(reqname: &str,req_tsksrvc: &str, req_srvrs: &str, so }); } }; - serde_json::to_string(&srvcs_hosts_info) - .unwrap_or_else(|e| { eprintln!("serde liveness: {}",e); String::from("")}) + // serde_json::to_string(&srvcs_hosts_info) + // .unwrap_or_else(|e| { eprintln!("serde liveness: {}",e); String::from("")}) + srvcs_hosts_info } } }, @@ -161,14 +178,16 @@ pub async fn run_on_upcloud(reqname: &str,req_tsksrvc: &str, req_srvrs: &str, so }); } }; - serde_json::to_string(&srvcs_hosts_info) - .unwrap_or_else(|e| { eprintln!("serde liveness: {}",e); String::from("")}) + // serde_json::to_string(&srvcs_hosts_info) + // .unwrap_or_else(|e| { eprintln!("serde liveness: {}",e); String::from("")}) + srvcs_hosts_info } } }, _ => { - serde_json::to_string(&cloud_config) - .unwrap_or_else(|e| { eprintln!("{}",e); String::from("")}) + // serde_json::to_string(&cloud_config) + // .unwrap_or_else(|e| { eprintln!("{}",e); String::from("")}) + Vec::new() }, } }