config preboot adj

pull/14/head
prplV 2024-12-11 13:38:29 +03:00
parent e7817a97b6
commit da3d8cd129
3 changed files with 46 additions and 35 deletions

View File

@ -19,11 +19,12 @@ use options::preboot::PrebootParams;
#[tokio::main(flavor = "multi_thread")] #[tokio::main(flavor = "multi_thread")]
async fn main() -> anyhow::Result<()>{ async fn main() -> anyhow::Result<()>{
let preboot = PrebootParams::parse().validate()?; let preboot = Arc::new(PrebootParams::parse().validate()?);
// if let Err(_) = preboot { // if let Err(_) = preboot {
// return; // return;
// } // }
// let preboot = Arc::new(preboot);
let _ = setup_logger(); let _ = setup_logger();
@ -31,7 +32,7 @@ async fn main() -> anyhow::Result<()>{
// setting up redis connection \ // setting up redis connection \
// then conf checks to choose the most actual \ // 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..."); error!("No actual configuration for runner. Stopping...");
std::process::exit(1); std::process::exit(1);
}); });
@ -87,7 +88,7 @@ async fn main() -> anyhow::Result<()>{
// remote config update subscription // remote config update subscription
handler.push(tokio::spawn(async move { 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 // cli pipeline

View File

@ -8,6 +8,7 @@ use std::process::Command;
use std::sync::Arc; use std::sync::Arc;
use std::{env, fs}; use std::{env, fs};
use tokio::time::Duration; use tokio::time::Duration;
use super::preboot::PrebootParams;
const CONFIG_PATH: &str = "settings.json"; const CONFIG_PATH: &str = "settings.json";
@ -46,43 +47,49 @@ fn load_processes(json_filename: &str) -> Option<Processes> {
/// ///
/// *depends on* : struct `Processes` /// *depends on* : struct `Processes`
/// ///
pub async fn get_actual_config() -> Option<Processes> { pub async fn get_actual_config(params : Arc<PrebootParams>) -> Option<Processes> {
// * if no local conf -> loop and +inf getting conf from redis server // * if no local conf -> loop and +inf getting conf from redis server
// * if local conf -> once 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) => { Some(local_conf) => {
info!( info!(
"Found local configuration, version - {}", "Found local configuration, version - {}",
&local_conf.date_of_creation &local_conf.date_of_creation
); );
if let Some(remote_conf) = if !params.no_remote_config {
// TODO : rework with pubsub mech if let Some(remote_conf) =
once_get_remote_configuration(&format!("redis://{}/", local_conf.config_server)) // TODO : rework with pubsub mech
{ once_get_remote_configuration(&format!("redis://{}/", &params.remote_server_url))
return match config_comparing(&local_conf, &remote_conf) { {
ConfigActuality::Local => { return match config_comparing(&local_conf, &remote_conf) {
info!("Local config is actual"); ConfigActuality::Local => {
Some(local_conf) 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...")
} }
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) Some(local_conf)
} }
None => { None => {
warn!("No local valid conf was found. Trying to pull remote one..."); warn!("No local valid conf was found. Trying to pull remote one...");
let mut conn = get_connection_watcher(&open_watcher("redis://localhost/")); if !params.no_remote_config {
let remote_config = get_remote_conf_watcher(&mut conn).await; let mut conn = get_connection_watcher(&open_watcher("redis://localhost/"));
if let Some(conf) = remote_config { let remote_config = get_remote_conf_watcher(&mut conn).await;
info!("Config {} was pulled from Redis-Server. Starting...", &conf.date_of_creation); if let Some(conf) = remote_config {
let _ = save_new_config(&conf, CONFIG_PATH); info!("Config {} was pulled from Redis-Server. Starting...", &conf.date_of_creation);
return Some(conf); let _ = save_new_config(&conf, CONFIG_PATH);
return Some(conf);
}
} }
None None
} }
@ -311,7 +318,10 @@ fn restart_main_thread() -> std::io::Result<()> {
/// ///
/// *depends on* : `Processes` /// *depends on* : `Processes`
/// ///
pub async fn subscribe_config_stream(actual_prcs: Arc<Processes>) -> Result<(), CustomError> { pub async fn subscribe_config_stream(actual_prcs: Arc<Processes>, params: Arc<PrebootParams>) -> 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(client) = Client::open(format!("redis://{}/", &actual_prcs.config_server)) {
if let Ok(mut conn) = client.get_connection() { if let Ok(mut conn) = client.get_connection() {
match crate::utils::get_container_id() { match crate::utils::get_container_id() {

View File

@ -4,7 +4,7 @@ use clap::Parser;
use std::path::PathBuf; use std::path::PathBuf;
#[derive(clap::ValueEnum, Debug, Clone)] #[derive(clap::ValueEnum, Debug, Clone)]
enum MetricsPrebootParams { pub enum MetricsPrebootParams {
Full, Full,
System, System,
Processes, Processes,
@ -68,28 +68,28 @@ pub struct PrebootParams {
conflicts_with="no_hostagent", conflicts_with="no_hostagent",
help="To set .sock file's path used in communication with host-agent" help="To set .sock file's path used in communication with host-agent"
)] )]
socket_path : PathBuf, pub socket_path : PathBuf,
#[arg( #[arg(
long = "log-to", long = "log-to",
default_value="./", default_value="./",
conflicts_with="no_logs", conflicts_with="no_logs",
help="To set a path to logs directory" help="To set a path to logs directory"
)] )]
log_to : PathBuf, pub log_to : PathBuf,
#[arg( #[arg(
long = "remote-server-url", long = "remote-server-url",
default_value="redis://localhost", default_value="localhost",
conflicts_with="no_remote_config", conflicts_with="no_remote_config",
help = "To set url of remote config server using in remote config pulling mechanism" help = "To set url of remote config server using in remote config pulling mechanism"
)] )]
remote_server_url : String, pub remote_server_url : String,
#[arg( #[arg(
long = "config", long = "config",
short, short,
default_value="settings.json", default_value="settings.json",
help="To set local config file path" help="To set local config file path"
)] )]
config : PathBuf, pub config : PathBuf,
// value enum params (metrics) // value enum params (metrics)
#[arg( #[arg(
@ -98,7 +98,7 @@ pub struct PrebootParams {
default_value_t=MetricsPrebootParams::Full, default_value_t=MetricsPrebootParams::Full,
help="To set metrics grubbing mode" help="To set metrics grubbing mode"
)] )]
metrics: MetricsPrebootParams, pub metrics: MetricsPrebootParams,
} }
impl PrebootParams { impl PrebootParams {