chore: debug > 0 for println! and verbose > 0

This commit is contained in:
Jesús Pérez Lorenzo 2021-09-17 17:09:35 +01:00
parent a364d270ed
commit 15c109d0c8
2 changed files with 92 additions and 48 deletions

View File

@ -69,7 +69,6 @@ pub async fn get_env_path(item: &str, dflt: &str, source: &str, root: &str, is_t
}
// println!("Path: {} src {} root {} item {}", &base, &source, &root, item_path);
Ok(base)
// Ok(item_path)
}
/// env_cloud
/// Scanning environment from __CLOUD_PATH__ overloaded with __CLOUD_HOME__ `source` CliOpts argument (-s) in case
@ -117,17 +116,13 @@ pub async fn env_cloud(source: &str, cloud_env: &mut CloudEnv) -> Result<()> {
if ! Path::new(&cloud_env.provision).exists() {
let dir_path_buf = Path::new(&cloud_env.provision).to_path_buf();
let dirs: Vec<&std::path::PathBuf> = vec![&dir_path_buf];
mkdir(&dirs)
.with_context(|| format!("\nFailed to create dir path {}", &cloud_env.provision))?;
//fs::create_dir(&cloud_env.provision)?;
println!("{} created", &cloud_env.provision);
mkdir(&dirs).with_context(|| format!("\nFailed to create dir path {}", &cloud_env.provision))?;
if envmnt::get_isize("DEBUG",0) > 1 {
println!("{} created", &cloud_env.provision);
}
}
cloud_env.wk_path = envmnt::get_or("KLDS_WKDIR", "/tmp");
// cloud_env.root_tsksrvcs=get_env_path("CLOUDS_ROOT_TSKSRVCS","tsksrvcs", &cloud_env.source_path,&cloud_env.path,false).await?;
// cloud_env.tsksrvcs_path=get_env_path("KLDS_TSKSRVCS","tsksrvcs", &cloud_env.source_path,&cloud_env.path,false).await?;
// cloud_env.tpls_path=get_env_path("KLDS_TPLS","tpls", &cloud_env.source_path,&cloud_env.path,false).await?;
cloud_env.tsksrvcs_path=get_env_path("KLDS_TSKSRVCS","tsksrvcs", &cloud_env.source_path,&cloud_env.path,false).await?;
// cloud_env.pkgs_list=get_env_path("KLDS_PKGS_LIST","pkgs_list.yaml", &cloud_env.source_path,&cloud_env.path,false).await?;
cloud_env.versions=get_env_path("KLDS_VERSIONS","versions.yaml", &cloud_env.source_path,&cloud_env.path,false).await?;
Ok(())
}
@ -141,8 +136,10 @@ pub async fn clear_specs(source: &str) -> Result<()> {
let env_provision=format!("{}/{}",&env_source,envmnt::get_or("KLDS_PROVISION","provision"));
let env_specs=format!("{}/specs",env_provision);
if Path::new(&env_specs).exists() {
println!("Delete {}/specs",env_provision);
fs::remove_dir_all(&env_specs)?;
if envmnt::get_isize("DEBUG",0) > 1 {
println!("Delete {}/specs",env_provision);
}
fs::remove_dir_all(&env_specs)?;
}
Ok(())
}
@ -227,9 +224,12 @@ pub async fn get_cloud_home_list(cloud: &Cloud) -> Result<Vec<String>> {
Ok(kloud_files.to_owned())
}
pub async fn run_ssh_on_srvr(hostname: &str,tsksrvc_name: &str, tsksrvc_cmd: &str, ssh_access: &SSHAccess) -> Result<serde_yaml::Value> {
println!("Checking connection to {}@{} on {} for {} ",ssh_access.user,ssh_access.host,ssh_access.port,&tsksrvc_name);
println!("ssh {} /var/lib/klouds/bin/{}_info.sh yaml",&hostname,&tsksrvc_name);
pub fn run_ssh_on_srvr(hostname: &str,tsksrvc_name: &str, tsksrvc_cmd: &str, ssh_access: &SSHAccess) -> Result<serde_yaml::Value> {
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);
println!("ssh {} /var/lib/klouds/bin/{}_info.sh yaml",&hostname,&tsksrvc_name);
}
let output = Command::new("ssh")
.arg("-q")
.arg("-o")
@ -263,7 +263,7 @@ pub async fn parse_srvr_tsksrvcs(hostname: &str, sshaccess: &SSHAccess, tsksrvcs
let os_name = String::from("os");
tsksrvcs_info.push( TsksrvcInfo {
name: format!("{}",&os_name),
info: run_ssh_on_srvr(&hostname, &os_name, "", &sshaccess).await
info: run_ssh_on_srvr(&hostname, &os_name, "", &sshaccess)
.unwrap_or_else(|e| {
eprintln!("run_ssh_on_srvr os: {}",e);
serde_yaml::Value::default()
@ -274,7 +274,7 @@ pub async fn parse_srvr_tsksrvcs(hostname: &str, sshaccess: &SSHAccess, tsksrvcs
let floatip_name = String::from("floatip");
tsksrvcs_info.push( TsksrvcInfo {
name: format!("{}",&floatip_name),
info: run_ssh_on_srvr(&hostname, &floatip_name, "", &sshaccess).await
info: run_ssh_on_srvr(&hostname, &floatip_name, "", &sshaccess)
.unwrap_or_else(|e| {
eprintln!("run_ssh_on_srvr floatip: {}",e);
serde_yaml::Value::default()
@ -285,7 +285,7 @@ pub async fn parse_srvr_tsksrvcs(hostname: &str, sshaccess: &SSHAccess, tsksrvcs
let k8_name = String::from("kubernetes");
tsksrvcs_info.push(TsksrvcInfo {
name: format!("{}_pods",&k8_name),
info: run_ssh_on_srvr(&hostname, &k8_name, "pods", &sshaccess).await
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()
@ -308,7 +308,7 @@ pub async fn parse_srvr_tsksrvcs(hostname: &str, sshaccess: &SSHAccess, tsksrvcs
//println!("{} {} {}",&hostname,sshaccess.user,&tksrvc.name);
tsksrvcs_info.push(TsksrvcInfo {
name: format!("{}",&tsksrvc.name),
info: run_ssh_on_srvr(&hostname, &name, "", &sshaccess).await
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()
@ -342,9 +342,11 @@ pub async fn liveness_srvr_tsksrvcs(source: &str, cntrllrs: &Vec<Cntrllr>, tsksr
Err(e) => {
if tsksrvc.critical == IsCritical::yes {
let monitor_name = "WUJI_MONITOR";
println!("{} : critical livenes: {} -> {}",envmnt::get_or(&monitor_name,""),&tsksrvc.name,&serverstring);
println!("{} critical livenes: {} -> {}",envmnt::get_or(&monitor_name,""),&tsksrvc.name,&serverstring);
}
if debug > 0 {
eprint!("liveness_check error: {}",e);
}
eprint!("liveness_check error: {}",e);
"err"
},
};
@ -379,7 +381,9 @@ pub async fn run_on_provider(reqname: &str, req_tsksrvc: &str, req_srvrs: &str,
},
_ => {
let result = format!("Errors on {} provider {} not found",&source,&provider.name);
println!("{}",&result);
if envmnt::get_isize("DEBUG",0) > 1 {
println!("{}",&result);
}
result
}
}
@ -397,13 +401,16 @@ pub async fn on_cloud_name_req(reqname: &str,env_cloud: &Cloud,_reqenv: &ReqEnv,
});
if cfg.mainName.is_empty() || cfg_data.is_empty() {
let result = format!("Errors loading {}",&source);
println!("{}",&result);
if envmnt::get_isize("DEBUG",0) > 1 {
println!("{}",&result);
}
return result;
}
run_on_provider(&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<String>,mut cloud: Cloud) -> String {
let config = reqenv.config();
let debug = envmnt::get_isize("DEBUG",0);
let check_path = format!("{}/clouds.json",&config.check_path);
let mut check_entries: Vec<KloudCheckHome> = Vec::new();
let mut no_check_entries = true;
@ -411,16 +418,18 @@ pub async fn create_cloud_config(reqname: &str,req_tsksrvcs: &str,reqenv: &ReqEn
if Path::new(&check_path).exists() {
// Load & Parse reuse liveness and monitor
let check_data = fs::read_to_string(&check_path).unwrap_or_else(|e|{
println!("Failed to read 'check_path' from {}: {}", &check_path,e);
eprintln!("Failed to read 'check_path' from {}: {}", &check_path,e);
String::from("")
});
if !check_data.is_empty() {
check_entries = serde_json::from_str(&check_data).unwrap_or_else(|e| {
println!("Error loading check_entries ({}): {}",&check_path,e);
eprintln!("Error loading check_entries ({}): {}",&check_path,e);
Vec::new()
});
no_check_entries=false;
println!("Using check_entries from {}",&check_path);
if debug> 0 {
println!("Using check_entries from {}",&check_path);
}
// dbg!("{:#?}",&check_entries);
}
}
@ -434,7 +443,9 @@ pub async fn create_cloud_config(reqname: &str,req_tsksrvcs: &str,reqenv: &ReqEn
});
if cfg.name.is_empty() || cfg_data.is_empty() {
let result = format!("Errors loading {}",&entry);
println!("{}",&result);
if debug > 0 {
println!("{}",&result);
}
continue;
}
if req_tsksrvcs.contains("monitor") {
@ -523,7 +534,9 @@ pub async fn create_cloud_check(req_tsksrvcs: &str,reqenv: &ReqEnv,entries: Vec<
});
if cfg.name.is_empty() || cfg_data.is_empty() {
let result = format!("Errors loading {}",&entry);
println!("{}",&result);
if envmnt::get_isize("DEBUG",0) > 0 {
println!("{}",&result);
}
continue;
}
if req_tsksrvcs.contains("monitor") {
@ -576,7 +589,9 @@ pub async fn on_cloud_req(reqname: &str,env_cloud: &Cloud,reqenv: &ReqEnv,req_ts
if ! reqname.ends_with("_job") {
let output_path = format!("{}/{}_{}.json",&config.cache_path,&reqname,&req_tsksrvcs.replace(",","_"));
if Path::new(&output_path).exists() {
println!("Using cache: {} at {}",&output_path,envmnt::get_or(format!("LAST_CACHE_{}",&output_path), ""));
if envmnt::get_isize("DEBUG",0) > 0 {
println!("Using cache: {} at {}",&output_path,envmnt::get_or(format!("LAST_CACHE_{}",&output_path), ""));
}
let output_data = fs::read_to_string(&output_path).with_context(|| format!("Failed to read json cache 'outut_path' from {}", &output_path))
.unwrap_or_else(|e| {
eprintln!("read file {}: {}",&output_path,e);
@ -607,13 +622,18 @@ pub async fn on_cloud_req(reqname: &str,env_cloud: &Cloud,reqenv: &ReqEnv,req_ts
}
}
pub async fn get_cloud_cache_req(reqenv: &ReqEnv,cloud: &Cloud, reqname: &str, reqname_job: &str, tsksrvcs: &str) -> Result<()> {
println!("cloud cache {} ... {:?} ",reqname,chrono::Utc::now());
let debug = envmnt::get_isize("DEBUG",0);
if debug > 0 {
println!("cloud cache {} ... {:?} ",reqname,chrono::Utc::now());
}
let config = reqenv.config();
let lock_path = format!("{}/{}_{}.{}",&config.cache_lock_path,&reqname,&tsksrvcs.replace(",","_"),&config.cache_lock_ext);
let output_path = format!("{}/{}_{}.json",&config.cache_path,&reqname,&tsksrvcs.replace(",","_"));
if Path::new(&lock_path).exists() {
if envmnt::get_or(format!("LAST_CACHE_{}",&output_path),"") != "" {
println!("Lock found {} ",&lock_path);
if debug > 0 {
println!("Lock found {} ",&lock_path);
}
// return Err(anyhow!("Lock found {} ",&lock_path));
return Ok(())
} else {
@ -629,18 +649,23 @@ pub async fn get_cloud_cache_req(reqenv: &ReqEnv,cloud: &Cloud, reqname: &str, r
}
let mut file = OpenOptions::new().write(true).create(true).open(&output_path)?;
file.write_all(result.as_bytes())?;
let out = format!("{}: [cloud config] -> {}\n",&now,&output_path);
println!("{}",&out);
if debug > 0 {
println!("{}: [cloud config] -> {}\n",&now,&output_path);
}
Ok(())
}
pub async fn make_cloud_cache(reqenv: &ReqEnv,cloud: &Cloud) -> Result<()> {
println!("Making cloud cache {:?} ... ",chrono::Utc::now());
if envmnt::get_isize("DEBUG",0) > 0 {
println!("Making cloud cache {:?} ... ",chrono::Utc::now());
}
get_cloud_cache_req(reqenv,cloud, "config", "config_job", "monitor,resources,liveness,provision").await.unwrap_or_else(|e| println!("Error cache config: {}",e));
//get_cloud_cache_req(reqenv,cloud, "provision", "provision_job", "").await.unwrap_or_else(|e| println!("Error cache provision: {}",e));
Ok(())
}
pub async fn run_clouds_check(reqenv: &ReqEnv,cloud: &Cloud) -> Result<()> {
println!("cloud check ... {:?} ",chrono::Utc::now());
let debug = envmnt::get_isize("DEBUG",0);
if debug > 0 {
println!("cloud check ... {:?} ",chrono::Utc::now());
}
let config = reqenv.config();
let output_path = format!("{}/clouds.json",&config.check_path);
let now = chrono::Utc::now().timestamp();
@ -652,8 +677,8 @@ pub async fn run_clouds_check(reqenv: &ReqEnv,cloud: &Cloud) -> Result<()> {
}
let mut file = OpenOptions::new().write(true).create(true).open(&output_path)?;
file.write_all(result.as_bytes())?;
let out = format!("{}: [cloud check] -> {}\n",&now,&output_path);
println!("{}",&out);
if debug > 0 {
println!("{}: [cloud check] -> {}\n",&now,&output_path);
}
Ok(())
}
// let debug=envmnt::get_isize("DEBUG",0);
}

View File

@ -121,16 +121,21 @@ impl MonitorAction {
Ok(())
}
pub async fn on_server(&self, task: &str, srvr: String, provider: &str, args: &str, _rule: &MonitorRule) -> Result<()> {
let debug = envmnt::get_isize("DEBUG",0);
let provider_name = ProviderName::set_provider(provider.to_owned());
match provider_name {
ProviderName::upcloud => {
let cmd = format!("upclapi -c {}server -id {} {}",&task,&srvr,&args);
println!("action {}: {} on {} {}",&task,&srvr,&provider_name,&args);
if debug > 1 {
println!("action {}: {} on {} {}",&task,&srvr,&provider_name,&args);
}
let res = run_command(&cmd).unwrap_or_else(|e|{
eprintln!("Error {}: {}",&cmd,e);
String::from("")
});
println!("{}",&res);
if debug > 0 {
println!("{}",&res);
}
Ok(())
},
_ => Err(anyhow!("Provider {} not defined", &provider_name))
@ -385,7 +390,10 @@ impl MonitorRule {
Ok(())
}
pub async fn on_service(&self, srvc: &str, out_cmd: &str) -> Result<()> {
println!("Service: {} -> {}",&srvc,&out_cmd);
let debug = envmnt::get_isize("DEBUG",0);
if debug > 1 {
println!("Service: {} -> {}",&srvc,&out_cmd);
}
if !out_cmd.is_empty() {
let mut targets = out_cmd.split_whitespace();
while let Some(target) = targets.next() {
@ -434,7 +442,8 @@ impl MonitorRule {
}
}
pub async fn run(&self,monitor_rules: &MonitorRules, idx: usize, app_env: &AppEnv) -> Result<()> {
if envmnt::get_isize("DEBUG",0) > 0 {
let debug = envmnt::get_isize("DEBUG",0);
if debug > 0 {
println!("{} [{}] {}: {}",&idx,&self.id,&self.name,&self.description);
}
let out_cmd: String;
@ -459,7 +468,9 @@ impl MonitorRule {
if ! &self.operator.on(&out_cmd, &self.value) {
if env_state > 0 {
let msg = format!("{} Ok in state: {}",&env_name,&env_state);
println!("{}",&msg);
if debug > 0 {
println!("{}",&msg);
}
envmnt::set_isize(&env_name, 0);
self.log_state(&app_env, &env_name, env_state, &msg, &out_cmd).await?;
self.notify(format!("Ok state from -> {}",&env_state)).await;
@ -471,14 +482,18 @@ impl MonitorRule {
}
if ! monitor_name_value.is_empty() {
let msg = format!("Monitor {}: rule {} is active, waiting for {}",&monitor_rules.name,&monitor_name_value,&self.id);
println!("{}",&msg);
if debug > 0 {
println!("{}",&msg);
}
self.log_state(&app_env, &env_name, env_state, &msg, &out_cmd).await?;
return Ok(());
}
if env_state > 0 && env_state < self.wait_checks {
let msg = format!("{} is already set: {} increment counter",&env_name,&env_state);
envmnt::increment(&env_name);
println!("{}",&msg);
if debug > 0 {
println!("{}",&msg);
}
self.log_state(&app_env, &env_name, env_state, &msg, &out_cmd).await?;
return Ok(());
}
@ -498,7 +513,9 @@ impl MonitorRule {
envmnt::set(&monitor_name,format!("{}",&self.id));
envmnt::set_isize(&env_name,1);
}
println!("{}",&msg);
if debug > 0 {
println!("{}",&msg);
}
self.log_state(&app_env, &env_name, env_state, &msg, &out_cmd).await?;
Ok(())
}
@ -552,7 +569,9 @@ impl MonitorRules {
_ => String::from(""),
};
if ! data_str.is_empty() {
println!("Dump loading MonitorRules to {}/{}.{}",&root,&path,&format);
if envmnt::get_isize("DEBUG",0) > 0 {
println!("Dump loading MonitorRules to {}/{}.{}",&root,&path,&format);
}
let output_path = format!("{}/{}.{}",&root,&path,&format);
write_str_data(output_path, data_str, "Monitor rules dump".to_string())?;
}