diff --git a/Cargo.toml b/Cargo.toml index 4f6b3d7..a2a0f2c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runner-rs" -version = "0.9.25" +version = "0.10.9" edition = "2021" [profile.dev] @@ -9,7 +9,6 @@ debug = true [profile.test] debug = false - [dependencies] anyhow = "1.0.93" chrono = "0.4.38" diff --git a/settings.json b/settings.json index d9ed0ab..8fb8d10 100644 --- a/settings.json +++ b/settings.json @@ -4,12 +4,12 @@ "processes": [ { "name": "temp-process", - "path": "/usr/src/kii/temp-process", + "path": "./temp-process", "dependencies": { "files": [ { "filename": "dep-file", - "src": "/usr/src/kii/tests/examples/", + "src": "./tests/examples/", "triggers": { "onDelete": "stop", "onChange": "stay" diff --git a/src/main.rs b/src/main.rs index 4590956..00b7460 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,20 +6,18 @@ use log::{error, info}; use options::config::*; use options::logger::setup_logger; use options::signals::set_valid_destructor; -// use options::structs::*; -use options::structs::{PrebootParams, Processes}; +use options::structs::Processes; use std::sync::Arc; use std::time::Duration; use tokio::sync::mpsc; use utils::*; #[allow(unused_imports)] -use options::preboot::validate_preboot_params; +use options::preboot::PrebootParams; #[tokio::main(flavor = "multi_thread")] async fn main() { - let _preboot = PrebootParams::parse(); + let preboot = PrebootParams::parse().validate(); - let _ = setup_logger(); info!("Runner is configurating..."); diff --git a/src/options/preboot.rs b/src/options/preboot.rs index b50baf6..fe312bb 100644 --- a/src/options/preboot.rs +++ b/src/options/preboot.rs @@ -1,14 +1,142 @@ // module to handle pre-boot params of the monitor -use super::structs::PrebootParams; use anyhow::{Result, Ok}; +use clap::Parser; -#[allow(dead_code)] -pub fn validate_preboot_params(_cli: &PrebootParams) -> Result<()> { - Ok(()) + + +#[derive(clap::ValueEnum, Debug, Clone)] +enum MetricsPrebootParams { + Full, + System, + Processes, + Net, + None, +} + +impl std::fmt::Display for MetricsPrebootParams { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + MetricsPrebootParams::Full => write!(f, "full"), + MetricsPrebootParams::System => write!(f, "system"), + MetricsPrebootParams::Processes => write!(f, "processes"), + MetricsPrebootParams::Net => write!(f, "net"), + MetricsPrebootParams::None => write!(f, "none"), + } + } +} + +#[derive(Debug, Parser)] +pub struct PrebootParams { + // actions + #[arg(long = "no-hagent", action, conflicts_with="socket_path", help="To disable work with host-agent")] + pub no_hostagent : bool, + #[arg(long = "no-logs", action, conflicts_with="log_to", help="To disable logs")] + pub no_logs: bool, + #[arg(long = "refresh-logs", action, conflicts_with="no_logs", help="To clear logs directory")] + pub refresh_logs : bool, + #[arg(long = "no-remote-config", action, help="To disable work with remote config server", conflicts_with="no_sub")] + pub no_remote_config : bool, + #[arg(long = "no-sub", action, help="To disable subscription mechanism", conflicts_with="no_remote_config")] + pub no_sub : bool, + + // params (socket_path, log_to, remote_server_url, config) + #[arg(long = "socket-path", default_value=None, conflicts_with="no_hostagent", help="To set .sock file's path used in communication with host-agent")] + socket_path : Option, + #[arg(long = "log-to", default_value=None, conflicts_with="no_logs", help="To set a path to logs directory")] + log_to : Option, + #[arg(long = "remote-server-url", default_value="redis://localhost", conflicts_with="no_remote_config", help = "To set url of remote config server using in remote config pulling mechanism")] + remote_server_url : String, + #[arg(long = "config", short, default_value="settings.json", help="To set local config file path")] + config : String, + + // value enum params (metrics) + #[arg(long = "metrics", short, default_value_t=MetricsPrebootParams::Full, help="To set metrics grubbing mode")] + metrics: MetricsPrebootParams, +} + +impl PrebootParams { + pub fn validate(self) -> Result { + // existing sock file + + // existing log dir + + // existing sock file + + // existing sock file + Ok(self) + } } +// unit tests of preboot params parsing mech +#[cfg(test)] +mod preboot_unitests{ + use super::*; - - -// unit tests of prebot params parsing mech \ No newline at end of file + #[test] + fn parsing_zero_args() { + assert!(PrebootParams::try_parse_from(vec!["runner-rs"]).is_ok()) + } + #[test] + fn parsing_hagent_valid_args() { + assert!(PrebootParams::try_parse_from(vec![ + "runner-rs", + "--socket-path", "/path/to/socket" + ]).is_ok()) +} + #[test] + fn parsing_hagent_invalid_args() { + assert!(PrebootParams::try_parse_from(vec![ + "runner-rs", + "--socket-path", "/path/to/socket", + "--no-hagent" + ]).is_err()) + } + #[test] + fn parsing_log_valid_args() { + assert!(PrebootParams::try_parse_from(vec![ + "runner-rs", + "--log-to", "/path/to/log/dir" + ]).is_ok()) + } + #[test] + fn parsing_log_invalid_args() { + assert!(PrebootParams::try_parse_from(vec![ + "runner-rs", + "--log-to /path/to/log/dir", + "--no-logs" + ]).is_err()) + } + #[test] + fn parsing_config_valid_args() { + assert!(PrebootParams::try_parse_from(vec![ + "runner-rs", + "--no-sub", + "--remote-server-url", "redis://127.0.0.1" + ]).is_ok()) + } + #[test] + fn parsing_config_invalid_args_noremote_nosub() { + assert!(PrebootParams::try_parse_from(vec![ + "runner-rs", + "--no-remote-config", "--no-sub" + ]).is_err()) + } + #[test] + fn parsing_config_invalid_args_noremote_remoteurl() { + assert!(PrebootParams::try_parse_from(vec![ + "runner-rs", + "--no-remote-config", + "--remote-server-url", "redis://127.0.0.1" + ]).is_err()) + } + #[test] + fn parsing_metrics_args_using_value_enum() { + assert!(PrebootParams::try_parse_from(vec!["runner-rs", "--metrics", "full"]).is_ok()); + assert!(PrebootParams::try_parse_from(vec!["runner-rs", "--metrics", "system"]).is_ok()); + assert!(PrebootParams::try_parse_from(vec!["runner-rs", "--metrics", "processes"]).is_ok()); + assert!(PrebootParams::try_parse_from(vec!["runner-rs", "--metrics", "net"]).is_ok()); + assert!(PrebootParams::try_parse_from(vec!["runner-rs", "--metrics", "none"]).is_ok()); + assert!(PrebootParams::try_parse_from(vec!["runner-rs", "--metrics", "unusual_value"]).is_err()); + } +} \ No newline at end of file diff --git a/src/options/structs.rs b/src/options/structs.rs index 405d7d2..65c1a19 100644 --- a/src/options/structs.rs +++ b/src/options/structs.rs @@ -2,63 +2,12 @@ use std::net::Ipv4Addr; use serde::{Deserialize, Serialize}; -use clap::Parser; /// # an Error enum (next will be deleted and replaced) pub enum CustomError { Fatal, } -#[derive(clap::ValueEnum, Debug, Clone)] -enum MetricsPrebootParams { - Full, - System, - Processes, - Net, - None, -} - -impl std::fmt::Display for MetricsPrebootParams { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - match self { - MetricsPrebootParams::Full => write!(f, "full"), - MetricsPrebootParams::System => write!(f, "system"), - MetricsPrebootParams::Processes => write!(f, "processes"), - MetricsPrebootParams::Net => write!(f, "net"), - MetricsPrebootParams::None => write!(f, "none"), - } - } -} - -#[derive(Debug, Parser)] -pub struct PrebootParams { - // actions - #[arg(long = "no-hagent", action, conflicts_with="socket_path", help="To disable work with host-agent")] - no_hostagent : bool, - #[arg(long = "no-logs", action, conflicts_with="log_to", help="To disable logs")] - no_logs: bool, - #[arg(long = "refresh-logs", action, conflicts_with="no_logs", help="To clear logs directory")] - refresh_logs : bool, - #[arg(long = "no-remote-config", action, help="To disable work with remote config server", conflicts_with="no_sub")] - no_remote_config : bool, - #[arg(long = "no-sub", action, help="To disable subscription mechanism", conflicts_with="no_remote_config")] - no_sub : bool, - - // params (socket_path, log_to, remote_server_url, config) - #[arg(long = "socket-path", default_value=None, conflicts_with="no_hostagent", help="To set .sock file's path used in communication with host-agent")] - socket_path : Option, - #[arg(long = "log-to", default_value=None, conflicts_with="no_logs", help="To set a path to logs directory")] - log_to : Option, - #[arg(long = "remote-server-url", default_value="redis://localhost", conflicts_with="no_remote_config", help = "To set url of remote config server using in remote config pulling mechanism")] - remote_server_url : String, - #[arg(long = "config", short, default_value="settings.json", help="To set local config file path")] - config : String, - - // value enum params (metrics) - #[arg(long = "metrics", short, default_value_t=MetricsPrebootParams::Full, help="To set metrics grubbing mode")] - metrics: MetricsPrebootParams, -} - #[derive(Debug, PartialEq)] pub enum ConfigActuality { Local,