diff --git a/.gitignore b/.gitignore index 40dd2ff..f45f0c7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /target .idea +/.env Cargo.lock hagent_test.sock \ No newline at end of file diff --git a/noxis-rs/Cargo.toml b/noxis-rs/Cargo.toml index 62c917f..0745074 100644 --- a/noxis-rs/Cargo.toml +++ b/noxis-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "noxis-rs" -version = "0.11.3" +version = "0.11.10" edition = "2021" [dependencies] @@ -17,3 +17,4 @@ serde_json = "1.0.118" sysinfo = "0.32.0" tokio = { version = "1.38.0", features = ["full", "time"] } noxis-cli = { path = "../noxis-cli" } +dotenv = "0.15.0" diff --git a/noxis-rs/src/options/preboot.rs b/noxis-rs/src/options/preboot.rs index 84a3f27..8293f8a 100644 --- a/noxis-rs/src/options/preboot.rs +++ b/noxis-rs/src/options/preboot.rs @@ -3,9 +3,91 @@ use anyhow::{Result, Ok, Error}; use clap::Parser; use std::path::PathBuf; +use std::env::var; +use dotenv::dotenv; const SOCKET_PATH: &str = "/var/run/enode/hostagent.sock"; +/// +enum EnvVars { + NoxisNoHagent, + NoxisNoLogs, + NoxisRefreshLogs, + NoxisNoRemoteConfig, + NoxisNoConfigSub, + NoxisSocketPath, + NoxisLogTo, + NoxisRemoteServerUrl, + NoxisConfig, + NoxisMetrics, +} + +/// +impl std::fmt::Display for EnvVars { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + EnvVars::NoxisNoHagent => write!(f, "NOXIS_NO_HAGENT"), + EnvVars::NoxisNoLogs => write!(f, "NOXIS_NO_LOGS"), + EnvVars::NoxisRefreshLogs => write!(f, "NOXIS_REFRESH_LOGS"), + EnvVars::NoxisNoRemoteConfig => write!(f, "NOXIS_NO_REMOTE_CONFIG"), + EnvVars::NoxisNoConfigSub => write!(f, "NOXIS_NO_CONFIG_SUB"), + EnvVars::NoxisSocketPath => write!(f, "NOXIS_SOCKET_PATH"), + EnvVars::NoxisLogTo => write!(f, "NOXIS_LOG_TO"), + EnvVars::NoxisRemoteServerUrl => write!(f, "NOXIS_REMOTE_SERVER_URL"), + EnvVars::NoxisConfig => write!(f, "NOXIS_CONFIG"), + EnvVars::NoxisMetrics => write!(f, "NOXIS_METRICS"), + } + } +} + +/// +impl<'a> EnvVars { + // Default trait func is not satisfying this issue + fn default(self) -> &'a str { + match self { + EnvVars::NoxisNoHagent => "false", + EnvVars::NoxisNoLogs => "false", + EnvVars::NoxisRefreshLogs => "false", + EnvVars::NoxisNoRemoteConfig => "false", + EnvVars::NoxisNoConfigSub => "false", + EnvVars::NoxisSocketPath => "/var/run/enode/hostagent.sock", + EnvVars::NoxisLogTo => "./", + EnvVars::NoxisRemoteServerUrl => "localhost", + EnvVars::NoxisConfig => "./settings.json", + EnvVars::NoxisMetrics => "full", + } + } + fn process_env_var(self, preboot_value: &str) { + // let default = self.default(); + match var(self.to_string()) { + std::result::Result::Ok(val) => { + if val != preboot_value { + std::env::set_var(self.to_string(), self.default()); + } + }, + Err(_) => { + std::env::set_var(self.to_string(), preboot_value); + }, + } + } + pub fn setup(preboot: &PrebootParams) { + // setup default if not exists + // check values and save preboot states in env vars if not equal + + Self::NoxisNoHagent.process_env_var(&preboot.no_hostagent.to_string()); + Self::NoxisNoLogs.process_env_var(&preboot.no_logs.to_string()); + Self::NoxisRefreshLogs.process_env_var(&preboot.refresh_logs.to_string()); + Self::NoxisNoRemoteConfig.process_env_var(&preboot.no_remote_config.to_string()); + Self::NoxisNoConfigSub.process_env_var(&preboot.no_sub.to_string()); + Self::NoxisSocketPath.process_env_var(preboot.socket_path.to_str().unwrap()); + Self::NoxisLogTo.process_env_var(preboot.log_to.to_str().unwrap()); + Self::NoxisRemoteServerUrl.process_env_var(&preboot.remote_server_url); + Self::NoxisConfig.process_env_var(preboot.config.to_str().unwrap()); + Self::NoxisMetrics.process_env_var(&preboot.metrics.to_string()); + + } +} + /// # Enum `MetricsPrebootParams` /// ## for setting up metrics mode as preboot param from command prompt /// @@ -187,6 +269,7 @@ pub struct PrebootParams { /// ## to enable validation mechanism impl PrebootParams { pub fn validate(mut self) -> Result { + dotenv().ok(); if !self.socket_path.exists() && !self.no_hostagent { if self.socket_path.to_string_lossy() == SOCKET_PATH { self.no_hostagent = true; @@ -211,10 +294,15 @@ impl PrebootParams { // existing sock file if !self.config.exists() { eprintln!("Error: Invalid character in config file. Config path was set to default"); + let config = PathBuf::from("/etc/settings.json"); + if !config.exists() && self.no_remote_config { + return Err(Error::msg("Noxis cannot run without config. Create local config or enable remote-config mechanism")); + } self.config = PathBuf::from("settings.json"); // return Err(Error::msg("Local Config Not Found or Noxis can't read it. Cannot start")); } // redis server check + EnvVars::setup(&self); Ok(self) } }