Merge pull request 'feature/envvars' (#21) from feature/envvars into rc
commit
ebff93698c
|
|
@ -1,4 +1,5 @@
|
|||
/target
|
||||
.idea
|
||||
/.env
|
||||
Cargo.lock
|
||||
hagent_test.sock
|
||||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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<Self> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue