|
|
|
@ -3,9 +3,91 @@
|
|
|
|
use anyhow::{Result, Ok, Error};
|
|
|
|
use anyhow::{Result, Ok, Error};
|
|
|
|
use clap::Parser;
|
|
|
|
use clap::Parser;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use std::env::var;
|
|
|
|
|
|
|
|
use dotenv::dotenv;
|
|
|
|
|
|
|
|
|
|
|
|
const SOCKET_PATH: &str = "/var/run/enode/hostagent.sock";
|
|
|
|
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`
|
|
|
|
/// # Enum `MetricsPrebootParams`
|
|
|
|
/// ## for setting up metrics mode as preboot param from command prompt
|
|
|
|
/// ## for setting up metrics mode as preboot param from command prompt
|
|
|
|
///
|
|
|
|
///
|
|
|
|
@ -187,6 +269,7 @@ pub struct PrebootParams {
|
|
|
|
/// ## to enable validation mechanism
|
|
|
|
/// ## to enable validation mechanism
|
|
|
|
impl PrebootParams {
|
|
|
|
impl PrebootParams {
|
|
|
|
pub fn validate(mut self) -> Result<Self> {
|
|
|
|
pub fn validate(mut self) -> Result<Self> {
|
|
|
|
|
|
|
|
dotenv().ok();
|
|
|
|
if !self.socket_path.exists() && !self.no_hostagent {
|
|
|
|
if !self.socket_path.exists() && !self.no_hostagent {
|
|
|
|
if self.socket_path.to_string_lossy() == SOCKET_PATH {
|
|
|
|
if self.socket_path.to_string_lossy() == SOCKET_PATH {
|
|
|
|
self.no_hostagent = true;
|
|
|
|
self.no_hostagent = true;
|
|
|
|
@ -211,10 +294,15 @@ impl PrebootParams {
|
|
|
|
// existing sock file
|
|
|
|
// existing sock file
|
|
|
|
if !self.config.exists() {
|
|
|
|
if !self.config.exists() {
|
|
|
|
eprintln!("Error: Invalid character in config file. Config path was set to default");
|
|
|
|
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");
|
|
|
|
self.config = PathBuf::from("settings.json");
|
|
|
|
// return Err(Error::msg("Local Config Not Found or Noxis can't read it. Cannot start"));
|
|
|
|
// return Err(Error::msg("Local Config Not Found or Noxis can't read it. Cannot start"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// redis server check
|
|
|
|
// redis server check
|
|
|
|
|
|
|
|
EnvVars::setup(&self);
|
|
|
|
Ok(self)
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|