diff --git a/noxis-cli/.env.example b/noxis-cli/.env.example new file mode 100644 index 0000000..b0d53a3 --- /dev/null +++ b/noxis-cli/.env.example @@ -0,0 +1 @@ +NOXIS_SOCKET_PATH = "/home/vladislavd/diplom_code/noxis-rs/noxis.sock" \ No newline at end of file diff --git a/noxis-cli/Cargo.toml b/noxis-cli/Cargo.toml index e02d5f8..712b2b6 100644 --- a/noxis-cli/Cargo.toml +++ b/noxis-cli/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [dependencies] anyhow = "1.0.94" clap = { version = "4.5.22", features = ["derive"] } +dotenv = "0.15.0" serde = { version = "1.0.215", features = ["derive"] } serde_json = "1.0.133" thiserror = "2.0.11" diff --git a/noxis-cli/src/cli.rs b/noxis-cli/src/cli.rs index 5a82b64..f5e7672 100644 --- a/noxis-cli/src/cli.rs +++ b/noxis-cli/src/cli.rs @@ -79,7 +79,23 @@ pub enum ConfigAction { about = "To reset all config settings", )] Reset, + #[command( + about = "To get current Noxis configuration", + name = "ls" + )] + Show(EnvConfig), } +#[derive(Debug, Parser, serde::Serialize, serde::Deserialize)] +pub struct EnvConfig { + // flag + #[arg( + long = "env", + action, + help = "to read environment vars configuration", + )] + pub is_env : bool, +} + #[derive(Debug, Parser, serde::Serialize, serde::Deserialize)] pub struct LocalConfig { @@ -148,4 +164,28 @@ pub enum ProcessAction { about = "To get info about current process's services-dependencies", )] Services, +} + +pub mod metrics_models { + pub enum MetricsMode { + Full, + // system + Cpu, + Memory, + Ram, + Rom, + Network, + // processes + Processes + // Config + } +} + +impl Cli { + pub fn validate_socket(mut self) -> Self { + if let Ok(path) = std::env::var("NOXIS_SOCKET_PATH") { + self.socket = path; + } + self + } } \ No newline at end of file diff --git a/noxis-cli/src/cli_error.rs b/noxis-cli/src/cli_error.rs index d5bae9b..ef9f802 100644 --- a/noxis-cli/src/cli_error.rs +++ b/noxis-cli/src/cli_error.rs @@ -2,7 +2,7 @@ use thiserror::Error; #[derive(Debug, Error)] pub enum NoxisCliError { - #[error("Can't find socket `{0}`. Error : {1}")] + #[error("Can't find socket `{0}`. {1}")] NoxisDaemonMissing(String, String), #[error("Noxis CLI can't write any data to the Noxis-rs port. Check daemon and it's runtime!")] PortIsNotWritable, diff --git a/noxis-cli/src/cli_net.rs b/noxis-cli/src/cli_net.rs index a3300ed..6d31d12 100644 --- a/noxis-cli/src/cli_net.rs +++ b/noxis-cli/src/cli_net.rs @@ -25,6 +25,6 @@ pub async fn try_send(cli: Cli) -> Result<()> { .await .map_err(|er| NoxisCliError::CliResponseReadError(er.to_string()))?; - println!("Received response: {}", String::from_utf8_lossy(&response)); + println!("{}", String::from_utf8_lossy(&response)); Ok(()) } \ No newline at end of file diff --git a/noxis-cli/src/main.rs b/noxis-cli/src/main.rs index 7961b75..2ad8ead 100644 --- a/noxis-cli/src/main.rs +++ b/noxis-cli/src/main.rs @@ -9,7 +9,8 @@ use anyhow::Result; #[tokio::main] async fn main() -> Result<()>{ - let cli = Cli::parse(); + dotenv::dotenv().ok(); + let cli = Cli::parse().validate_socket(); try_send(cli).await?; Ok(()) } diff --git a/noxis-rs/src/options/cli_pipeline.rs b/noxis-rs/src/options/cli_pipeline.rs index 9464465..451b6bb 100644 --- a/noxis-rs/src/options/cli_pipeline.rs +++ b/noxis-rs/src/options/cli_pipeline.rs @@ -1,4 +1,4 @@ -use log::{error, info}; +use log::{error, info, warn}; use tokio::net::{ UnixStream, UnixListener }; use tokio::time::{sleep, Duration}; use std::fs; @@ -34,7 +34,7 @@ pub async fn init_cli_pipeline(params: Arc) -> anyhow::Result<()> match list.accept().await { Ok((socket, _)) => { // tokio::spawn(); - process_connection(socket).await; + process_connection(socket, params.clone()).await; }, Err(er) => { error!("Cannot poll connection to CLI due to {}", er); @@ -64,7 +64,7 @@ pub async fn init_cli_pipeline(params: Arc) -> anyhow::Result<()> /// /// *depends on* : `tokio::net::TcpStream` /// -async fn process_connection(mut stream: UnixStream) { +async fn process_connection(mut stream: UnixStream, params: Arc) { let mut buf = vec![0; 1024]; match stream.read(&mut buf).await { Ok(0) => { @@ -76,9 +76,15 @@ async fn process_connection(mut stream: UnixStream) { match serde_json::from_slice::(&buf) { Ok(cli) => { info!("Received CLI request: {:?}", cli); - let response = "OK"; - if let Err(e) = stream.write_all(response.as_bytes()).await { - error!("Failed to send response: {}", e); + match process_cli_cmd(cli, params.clone()).await { + Ok(response) => { + if let Err(e) = stream.write_all(response.as_bytes()).await { + error!("Failed to send response: {}", e); + } + }, + Err(er) => { + error!("Can't send response from cli_pipeline: {}", er); + }, } } Err(e) => { @@ -90,3 +96,27 @@ async fn process_connection(mut stream: UnixStream) { } let _ = stream.shutdown().await; } + + +async fn process_cli_cmd(cli : Cli, params: Arc) -> anyhow::Result { + use noxis_cli::{Commands, ConfigAction}; + return match cli.command { + Commands::Config(config) => { + match config.action { + ConfigAction::Show(env ) => { + if env.is_env { + Ok(serde_json::to_string_pretty(params.as_ref())?) + } else { + /* */ + Ok(String::from("Ok")) + } + }, + /* */ + _ => Ok(String::from("Ok")) + } + }, + /* */ + Commands::Status => Ok(String::from("Ok")), + _ => Ok(String::from("Ok")) + } +} diff --git a/noxis-rs/src/options/preboot.rs b/noxis-rs/src/options/preboot.rs index 78463c7..21c523e 100644 --- a/noxis-rs/src/options/preboot.rs +++ b/noxis-rs/src/options/preboot.rs @@ -20,9 +20,9 @@ use dotenv::dotenv; /// noxis-rs ... --metrics none /// ``` /// -#[derive(clap::ValueEnum, Debug, Clone)] +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub enum MetricsPrebootParams { - Full, + Full, System, Processes, Net, @@ -177,7 +177,7 @@ impl std::fmt::Display for MetricsPrebootParams { /// export NOXIS_METRICS_MODE "full" /// ``` /// -#[derive(Debug)] +#[derive(Debug, serde::Serialize, serde::Deserialize)] pub struct PrebootParams { pub no_hostagent : bool, pub no_logs: bool,