From da3d8cd12912020d76ab07920dd9c79ab2092094 Mon Sep 17 00:00:00 2001 From: prplV Date: Wed, 11 Dec 2024 13:38:29 +0300 Subject: [PATCH] config preboot adj --- noxis-rs/src/main.rs | 7 ++-- noxis-rs/src/options/config.rs | 60 +++++++++++++++++++-------------- noxis-rs/src/options/preboot.rs | 14 ++++---- 3 files changed, 46 insertions(+), 35 deletions(-) diff --git a/noxis-rs/src/main.rs b/noxis-rs/src/main.rs index 616802b..7ca43bc 100644 --- a/noxis-rs/src/main.rs +++ b/noxis-rs/src/main.rs @@ -19,11 +19,12 @@ use options::preboot::PrebootParams; #[tokio::main(flavor = "multi_thread")] async fn main() -> anyhow::Result<()>{ - let preboot = PrebootParams::parse().validate()?; + let preboot = Arc::new(PrebootParams::parse().validate()?); // if let Err(_) = preboot { // return; // } + // let preboot = Arc::new(preboot); let _ = setup_logger(); @@ -31,7 +32,7 @@ async fn main() -> anyhow::Result<()>{ // setting up redis connection \ // then conf checks to choose the most actual \ - let processes: Processes = get_actual_config().await.unwrap_or_else(|| { + let processes: Processes = get_actual_config(preboot.clone()).await.unwrap_or_else(|| { error!("No actual configuration for runner. Stopping..."); std::process::exit(1); }); @@ -87,7 +88,7 @@ async fn main() -> anyhow::Result<()>{ // remote config update subscription handler.push(tokio::spawn(async move { - let _ = subscribe_config_stream(Arc::new(processes)).await; + let _ = subscribe_config_stream(Arc::new(processes), preboot.clone()).await; })); // cli pipeline diff --git a/noxis-rs/src/options/config.rs b/noxis-rs/src/options/config.rs index 5f865ea..2088729 100644 --- a/noxis-rs/src/options/config.rs +++ b/noxis-rs/src/options/config.rs @@ -8,6 +8,7 @@ use std::process::Command; use std::sync::Arc; use std::{env, fs}; use tokio::time::Duration; +use super::preboot::PrebootParams; const CONFIG_PATH: &str = "settings.json"; @@ -46,43 +47,49 @@ fn load_processes(json_filename: &str) -> Option { /// /// *depends on* : struct `Processes` /// -pub async fn get_actual_config() -> Option { +pub async fn get_actual_config(params : Arc) -> Option { // * if no local conf -> loop and +inf getting conf from redis server // * if local conf -> once getting conf from redis server - match load_processes(CONFIG_PATH) { + let config_path = params.config.to_str()?; + info!("Configurating config module with params: no-remote-config={}, no-sub={}, local config path={:?}, remote server={}", params.no_remote_config, params.no_sub, params.config, params.remote_server_url); + match load_processes(config_path) { Some(local_conf) => { info!( "Found local configuration, version - {}", &local_conf.date_of_creation ); - if let Some(remote_conf) = - // TODO : rework with pubsub mech - once_get_remote_configuration(&format!("redis://{}/", local_conf.config_server)) - { - return match config_comparing(&local_conf, &remote_conf) { - ConfigActuality::Local => { - info!("Local config is actual"); - Some(local_conf) - } - ConfigActuality::Remote => { - info!("Pulled config is more actual. Saving changes!"); - if save_new_config(&remote_conf, CONFIG_PATH).is_err() { - error!("Saving changes process failed due to unexpected error...") + if !params.no_remote_config { + if let Some(remote_conf) = + // TODO : rework with pubsub mech + once_get_remote_configuration(&format!("redis://{}/", ¶ms.remote_server_url)) + { + return match config_comparing(&local_conf, &remote_conf) { + ConfigActuality::Local => { + info!("Local config is actual"); + Some(local_conf) } - Some(remote_conf) - } - }; + ConfigActuality::Remote => { + info!("Pulled config is more actual. Saving changes!"); + if save_new_config(&remote_conf, CONFIG_PATH).is_err() { + error!("Saving changes process failed due to unexpected error...") + } + Some(remote_conf) + } + }; + } } Some(local_conf) } None => { warn!("No local valid conf was found. Trying to pull remote one..."); - let mut conn = get_connection_watcher(&open_watcher("redis://localhost/")); - let remote_config = get_remote_conf_watcher(&mut conn).await; - if let Some(conf) = remote_config { - info!("Config {} was pulled from Redis-Server. Starting...", &conf.date_of_creation); - let _ = save_new_config(&conf, CONFIG_PATH); - return Some(conf); + if !params.no_remote_config { + let mut conn = get_connection_watcher(&open_watcher("redis://localhost/")); + let remote_config = get_remote_conf_watcher(&mut conn).await; + if let Some(conf) = remote_config { + info!("Config {} was pulled from Redis-Server. Starting...", &conf.date_of_creation); + let _ = save_new_config(&conf, CONFIG_PATH); + return Some(conf); + } } None } @@ -311,7 +318,10 @@ fn restart_main_thread() -> std::io::Result<()> { /// /// *depends on* : `Processes` /// -pub async fn subscribe_config_stream(actual_prcs: Arc) -> Result<(), CustomError> { +pub async fn subscribe_config_stream(actual_prcs: Arc, params: Arc) -> Result<(), CustomError> { + if !(params.no_sub && params.no_remote_config) { + return Err(CustomError::Fatal); + } if let Ok(client) = Client::open(format!("redis://{}/", &actual_prcs.config_server)) { if let Ok(mut conn) = client.get_connection() { match crate::utils::get_container_id() { diff --git a/noxis-rs/src/options/preboot.rs b/noxis-rs/src/options/preboot.rs index c2241e0..7c9f687 100644 --- a/noxis-rs/src/options/preboot.rs +++ b/noxis-rs/src/options/preboot.rs @@ -4,7 +4,7 @@ use clap::Parser; use std::path::PathBuf; #[derive(clap::ValueEnum, Debug, Clone)] -enum MetricsPrebootParams { +pub enum MetricsPrebootParams { Full, System, Processes, @@ -68,28 +68,28 @@ pub struct PrebootParams { conflicts_with="no_hostagent", help="To set .sock file's path used in communication with host-agent" )] - socket_path : PathBuf, + pub socket_path : PathBuf, #[arg( long = "log-to", default_value="./", conflicts_with="no_logs", help="To set a path to logs directory" )] - log_to : PathBuf, + pub log_to : PathBuf, #[arg( long = "remote-server-url", - default_value="redis://localhost", + default_value="localhost", conflicts_with="no_remote_config", help = "To set url of remote config server using in remote config pulling mechanism" )] - remote_server_url : String, + pub remote_server_url : String, #[arg( long = "config", short, default_value="settings.json", help="To set local config file path" )] - config : PathBuf, + pub config : PathBuf, // value enum params (metrics) #[arg( @@ -98,7 +98,7 @@ pub struct PrebootParams { default_value_t=MetricsPrebootParams::Full, help="To set metrics grubbing mode" )] - metrics: MetricsPrebootParams, + pub metrics: MetricsPrebootParams, } impl PrebootParams {