Compare commits

..

No commits in common. "da3d8cd12912020d76ab07920dd9c79ab2092094" and "2d225f4c09d28a78b3fdfbbba36fa60b6b82f543" have entirely different histories.

3 changed files with 48 additions and 57 deletions

View File

@ -1,7 +1,6 @@
mod options;
mod utils;
use anyhow::Error;
use clap::Parser;
use log::{error, info};
use options::config::*;
@ -18,13 +17,12 @@ use utils::*;
use options::preboot::PrebootParams;
#[tokio::main(flavor = "multi_thread")]
async fn main() -> anyhow::Result<()>{
let preboot = Arc::new(PrebootParams::parse().validate()?);
async fn main() {
let preboot = PrebootParams::parse().validate();
// if let Err(_) = preboot {
// return;
// }
// let preboot = Arc::new(preboot);
if let Err(_) = preboot {
return;
}
let _ = setup_logger();
@ -32,9 +30,9 @@ async fn main() -> anyhow::Result<()>{
// setting up redis connection \
// then conf checks to choose the most actual \
let processes: Processes = get_actual_config(preboot.clone()).await.unwrap_or_else(|| {
let processes: Processes = get_actual_config().await.unwrap_or_else(|| {
error!("No actual configuration for runner. Stopping...");
std::process::exit(1);
std::process::exit(101);
});
info!(
@ -45,7 +43,7 @@ async fn main() -> anyhow::Result<()>{
if processes.processes.is_empty() {
error!("Processes list is null, runner-rs initialization is stopped");
return Err(Error::msg("Empty processes segment in config"));
return;
}
let mut handler: Vec<tokio::task::JoinHandle<()>> = vec![];
// is in need to send to the signals handler thread
@ -88,7 +86,7 @@ async fn main() -> anyhow::Result<()>{
// remote config update subscription
handler.push(tokio::spawn(async move {
let _ = subscribe_config_stream(Arc::new(processes), preboot.clone()).await;
let _ = subscribe_config_stream(Arc::new(processes)).await;
}));
// cli pipeline
@ -99,7 +97,7 @@ async fn main() -> anyhow::Result<()>{
for i in handler {
let _ = i.await;
}
Ok(())
return;
}
// todo: integration tests

View File

@ -8,7 +8,6 @@ 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";
@ -47,49 +46,43 @@ fn load_processes(json_filename: &str) -> Option<Processes> {
///
/// *depends on* : struct `Processes`
///
pub async fn get_actual_config(params : Arc<PrebootParams>) -> Option<Processes> {
pub async fn get_actual_config() -> Option<Processes> {
// * if no local conf -> loop and +inf getting conf from redis server
// * if local conf -> once getting conf from redis server
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) {
match load_processes(CONFIG_PATH) {
Some(local_conf) => {
info!(
"Found local configuration, version - {}",
&local_conf.date_of_creation
);
if !params.no_remote_config {
if let Some(remote_conf) =
// 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 => {
info!("Local config is actual");
Some(local_conf)
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...")
}
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(remote_conf)
}
};
}
Some(local_conf)
}
None => {
warn!("No local valid conf was found. Trying to pull remote one...");
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);
}
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
}
@ -318,10 +311,7 @@ fn restart_main_thread() -> std::io::Result<()> {
///
/// *depends on* : `Processes`
///
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);
}
pub async fn subscribe_config_stream(actual_prcs: Arc<Processes>) -> Result<(), CustomError> {
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() {

View File

@ -4,7 +4,7 @@ use clap::Parser;
use std::path::PathBuf;
#[derive(clap::ValueEnum, Debug, Clone)]
pub enum MetricsPrebootParams {
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"
)]
pub socket_path : PathBuf,
socket_path : PathBuf,
#[arg(
long = "log-to",
default_value="./",
conflicts_with="no_logs",
help="To set a path to logs directory"
)]
pub log_to : PathBuf,
log_to : PathBuf,
#[arg(
long = "remote-server-url",
default_value="localhost",
default_value="redis://localhost",
conflicts_with="no_remote_config",
help = "To set url of remote config server using in remote config pulling mechanism"
)]
pub remote_server_url : String,
remote_server_url : String,
#[arg(
long = "config",
short,
default_value="settings.json",
help="To set local config file path"
)]
pub config : PathBuf,
config : PathBuf,
// value enum params (metrics)
#[arg(
@ -98,21 +98,24 @@ pub struct PrebootParams {
default_value_t=MetricsPrebootParams::Full,
help="To set metrics grubbing mode"
)]
pub metrics: MetricsPrebootParams,
metrics: MetricsPrebootParams,
}
impl PrebootParams {
pub fn validate(self) -> Result<Self> {
if !self.socket_path.exists() {
return Err(Error::msg("Socket-file not found or Noxis can't read it. Cannot start"));
eprintln!("Socket-file {} doesn't exist. Cannot start", &self.socket_path.display());
return Err(Error::msg("Socket-file Not Found"));
}
// existing log dir
if !self.log_to.exists() {
return Err(Error::msg("Log Directory Not Found or Noxis can't read it. Cannot start"));
eprintln!("Log directory {} doesn't exist", &self.log_to.display());
return Err(Error::msg("Log Directory Not Found. Cannot start"));
}
// existing sock file
if !self.config.exists() {
return Err(Error::msg("Local Config Not Found or Noxis can't read it. Cannot start"));
eprintln!("Local config file {} doesn't exist", &self.config.display());
return Err(Error::msg("Local Config Not Found. Cannot start"));
}
// redis server check
Ok(self)