From 5d54c5b97c13bf3e4ffb5fe36c30ff6177980fb5 Mon Sep 17 00:00:00 2001 From: prplV Date: Wed, 15 Jan 2025 12:51:22 +0300 Subject: [PATCH 1/5] env vars enum + display-default impl --- noxis-rs/src/options/preboot.rs | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/noxis-rs/src/options/preboot.rs b/noxis-rs/src/options/preboot.rs index 84a3f27..fd22143 100644 --- a/noxis-rs/src/options/preboot.rs +++ b/noxis-rs/src/options/preboot.rs @@ -6,6 +6,51 @@ use std::path::PathBuf; 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 Default for EnvVars { + fn default() { + todo!() + // setting default env vars values if not exists + } +} + +impl EnvVars { + fn setup() { + + } +} + /// # Enum `MetricsPrebootParams` /// ## for setting up metrics mode as preboot param from command prompt /// From eed9fa881a202329ed2087d6a880da1394fe9321 Mon Sep 17 00:00:00 2001 From: prplV Date: Wed, 15 Jan 2025 13:17:14 +0300 Subject: [PATCH 2/5] default impl self-written + config preboot param check fixed --- noxis-rs/src/options/preboot.rs | 36 ++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/noxis-rs/src/options/preboot.rs b/noxis-rs/src/options/preboot.rs index fd22143..cf3e55d 100644 --- a/noxis-rs/src/options/preboot.rs +++ b/noxis-rs/src/options/preboot.rs @@ -38,16 +38,34 @@ impl std::fmt::Display for EnvVars { } } -impl Default for EnvVars { - fn default() { - todo!() - // setting default env vars values if not exists +// impl<'a> Default for EnvVars { +// fn default() -> &'a str { +// todo!() +// // setting default env vars values if not exists +// } +// } + +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 => "NOXIS_METRICS", + } } -} - -impl EnvVars { fn setup() { + // setup default if not exists + // check values and save preboot states in env vars if not equal + todo!() } } @@ -256,6 +274,10 @@ 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")); } From 011206641819333a4ae109dbb7eef1f8c3f6727a Mon Sep 17 00:00:00 2001 From: prplV Date: Wed, 15 Jan 2025 16:25:53 +0300 Subject: [PATCH 3/5] setting up env vars + comaping with preboot values --- noxis-rs/src/options/preboot.rs | 42 ++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/noxis-rs/src/options/preboot.rs b/noxis-rs/src/options/preboot.rs index cf3e55d..d5162d5 100644 --- a/noxis-rs/src/options/preboot.rs +++ b/noxis-rs/src/options/preboot.rs @@ -3,6 +3,7 @@ use anyhow::{Result, Ok, Error}; use clap::Parser; use std::path::PathBuf; +use std::env::{var, vars}; const SOCKET_PATH: &str = "/var/run/enode/hostagent.sock"; @@ -38,13 +39,7 @@ impl std::fmt::Display for EnvVars { } } -// impl<'a> Default for EnvVars { -// fn default() -> &'a str { -// todo!() -// // setting default env vars values if not exists -// } -// } - +/// impl<'a> EnvVars { // Default trait func is not satisfying this issue fn default(self) -> &'a str { @@ -58,14 +53,38 @@ impl<'a> EnvVars { EnvVars::NoxisLogTo => "./", EnvVars::NoxisRemoteServerUrl => "localhost", EnvVars::NoxisConfig => "./settings.json", - EnvVars::NoxisMetrics => "NOXIS_METRICS", + EnvVars::NoxisMetrics => "full", } } - fn setup() { + 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); + println!("{:?}", vars()); + }, + } + } + pub fn setup(preboot: &PrebootParams) { // setup default if not exists - // check values and save preboot states in env vars if not equal - todo!() + + 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()); + } } @@ -282,6 +301,7 @@ impl PrebootParams { // return Err(Error::msg("Local Config Not Found or Noxis can't read it. Cannot start")); } // redis server check + EnvVars::setup(&self); Ok(self) } } From b67feb8ce5c2806dfbec5608a73c4eb7d97a4839 Mon Sep 17 00:00:00 2001 From: prplV Date: Wed, 15 Jan 2025 16:30:17 +0300 Subject: [PATCH 4/5] dotenv support + gitignore update --- .gitignore | 1 + noxis-rs/Cargo.toml | 1 + noxis-rs/src/options/preboot.rs | 5 +++-- 3 files changed, 5 insertions(+), 2 deletions(-) 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..9040d12 100644 --- a/noxis-rs/Cargo.toml +++ b/noxis-rs/Cargo.toml @@ -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 d5162d5..8293f8a 100644 --- a/noxis-rs/src/options/preboot.rs +++ b/noxis-rs/src/options/preboot.rs @@ -3,7 +3,8 @@ use anyhow::{Result, Ok, Error}; use clap::Parser; use std::path::PathBuf; -use std::env::{var, vars}; +use std::env::var; +use dotenv::dotenv; const SOCKET_PATH: &str = "/var/run/enode/hostagent.sock"; @@ -66,7 +67,6 @@ impl<'a> EnvVars { }, Err(_) => { std::env::set_var(self.to_string(), preboot_value); - println!("{:?}", vars()); }, } } @@ -269,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; From d400318aadb4fc812ccaa8378390b0cbf5afa229 Mon Sep 17 00:00:00 2001 From: prplV Date: Wed, 15 Jan 2025 16:31:16 +0300 Subject: [PATCH 5/5] version update --- noxis-rs/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/noxis-rs/Cargo.toml b/noxis-rs/Cargo.toml index 9040d12..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]