From 8c5e048b7cda429c8d194f6c11a4bcf074a488fc Mon Sep 17 00:00:00 2001 From: plazmoid Date: Sun, 16 Aug 2020 19:21:43 +0500 Subject: [PATCH] panel added --- bin/{u-agent => u_agent}/Cargo.toml | 7 +-- bin/{u-agent => u_agent}/src/main.rs | 10 +++- bin/u_panel/Cargo.toml | 15 +++++ bin/u_panel/src/main.rs | 27 +++++++++ bin/{u-run => u_run}/Cargo.toml | 2 +- bin/{u-run => u_run}/src/main.rs | 2 +- bin/{u-server => u_server}/Cargo.toml | 2 +- bin/{u-server => u_server}/src/main.rs | 19 +++--- lib/u_lib/Cargo.toml | 2 + lib/u_lib/src/{contracts => client}/client.rs | 14 ++--- lib/u_lib/src/client/mod.rs | 4 ++ .../src => lib/u_lib/src/client}/network.rs | 5 +- lib/u_lib/src/contracts/datatypes.rs | 15 +++++ lib/u_lib/src/contracts/jobs.rs | 59 ++++++++++--------- lib/u_lib/src/contracts/messaging.rs | 19 +++++- lib/u_lib/src/contracts/mod.rs | 7 ++- lib/u_lib/src/lib.rs | 7 ++- 17 files changed, 150 insertions(+), 66 deletions(-) rename bin/{u-agent => u_agent}/Cargo.toml (67%) rename bin/{u-agent => u_agent}/src/main.rs (88%) create mode 100644 bin/u_panel/Cargo.toml create mode 100644 bin/u_panel/src/main.rs rename bin/{u-run => u_run}/Cargo.toml (93%) rename bin/{u-run => u_run}/src/main.rs (97%) rename bin/{u-server => u_server}/Cargo.toml (83%) rename bin/{u-server => u_server}/src/main.rs (82%) rename lib/u_lib/src/{contracts => client}/client.rs (80%) create mode 100644 lib/u_lib/src/client/mod.rs rename {bin/u-agent/src => lib/u_lib/src/client}/network.rs (97%) create mode 100644 lib/u_lib/src/contracts/datatypes.rs diff --git a/bin/u-agent/Cargo.toml b/bin/u_agent/Cargo.toml similarity index 67% rename from bin/u-agent/Cargo.toml rename to bin/u_agent/Cargo.toml index f092dcb..a1cc023 100644 --- a/bin/u-agent/Cargo.toml +++ b/bin/u_agent/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "u-agent" +name = "u_agent" version = "0.1.0" authors = ["plazmoid "] edition = "2018" @@ -7,13 +7,10 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -tokio = { version = "*", features = ["macros"] } +tokio = { version = "*", features = ["macros", "rt-core", "process", "blocking"] } sysinfo = "0.10.5" -#lazy_static = "1.4.0" log = "^0.4" env_logger = "0.7.1" -cron = "0.6.0" uuid = "0.8.1" - reqwest = { version = "0.10.7", features = ["json"] } u_lib = { version = "*", path = "../../lib/u_lib" } \ No newline at end of file diff --git a/bin/u-agent/src/main.rs b/bin/u_agent/src/main.rs similarity index 88% rename from bin/u-agent/src/main.rs rename to bin/u_agent/src/main.rs index 02385fb..031de83 100644 --- a/bin/u-agent/src/main.rs +++ b/bin/u_agent/src/main.rs @@ -9,10 +9,14 @@ //mod jobs; -use std::thread::sleep; -use std::time::Duration; +use { + std::thread::sleep, + std::time::Duration, + u_lib::client::* +}; + +mod executor; -mod network; use network::ClientHandler; #[tokio::main] diff --git a/bin/u_panel/Cargo.toml b/bin/u_panel/Cargo.toml new file mode 100644 index 0000000..c28bc03 --- /dev/null +++ b/bin/u_panel/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "u_panel" +version = "0.1.0" +authors = ["plazmoid "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +tokio = { version = "*", features = ["macros", "rt-core", "process", "blocking"] } +log = "^0.4" +env_logger = "0.7.1" +uuid = "0.8.1" +reqwest = { version = "0.10.7", features = ["json"] } +u_lib = { version = "*", path = "../../lib/u_lib" } diff --git a/bin/u_panel/src/main.rs b/bin/u_panel/src/main.rs new file mode 100644 index 0000000..9a05098 --- /dev/null +++ b/bin/u_panel/src/main.rs @@ -0,0 +1,27 @@ +use std::env::args; +use std::future::Future; +use u_lib::client::network::ClientHandler; + +fn get_cmd_handler(cli_handler: &ClientHandler, method: &str) -> Option { + match method { + "ls" => Some(cli_handler.list()), + _ => None + } +} + +#[tokio::main] +async fn main() -> Result<(), &'static str> { + //daemonize(); + let mut raw_args = args(); + let method = match raw_args.nth(1) { + Some(m) => m, + None => return Err("Method required") + }; + let instance = ClientHandler::new(); + let future = match get_cmd_handler(&instance, &method) { + Some(c) => c, + None => return Err("Unknown method") + }; + future.await; + Ok(()) +} diff --git a/bin/u-run/Cargo.toml b/bin/u_run/Cargo.toml similarity index 93% rename from bin/u-run/Cargo.toml rename to bin/u_run/Cargo.toml index fe58777..9756ea2 100644 --- a/bin/u-run/Cargo.toml +++ b/bin/u_run/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "u-run" +name = "u_run" version = "0.1.0" authors = ["plazmoid "] edition = "2018" diff --git a/bin/u-run/src/main.rs b/bin/u_run/src/main.rs similarity index 97% rename from bin/u-run/src/main.rs rename to bin/u_run/src/main.rs index 88d2b1e..de6c406 100644 --- a/bin/u-run/src/main.rs +++ b/bin/u_run/src/main.rs @@ -19,7 +19,7 @@ fn get_uagent_data() -> Option> { let ls = read_dir("./").unwrap(); for dirfile in ls { let filename = dirfile.unwrap().path(); - if filename.to_str().unwrap() == "u-agent" { + if filename.to_str().unwrap() == "u_agent" { let mut file = File::open(filename).unwrap(); let mut data: Vec = vec![]; file.read_to_end(&mut data).unwrap(); diff --git a/bin/u-server/Cargo.toml b/bin/u_server/Cargo.toml similarity index 83% rename from bin/u-server/Cargo.toml rename to bin/u_server/Cargo.toml index 98b6946..74e56ef 100644 --- a/bin/u-server/Cargo.toml +++ b/bin/u_server/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "u-server" +name = "u_server" version = "0.1.0" authors = ["plazmoid "] edition = "2018" diff --git a/bin/u-server/src/main.rs b/bin/u_server/src/main.rs similarity index 82% rename from bin/u-server/src/main.rs rename to bin/u_server/src/main.rs index 27d2745..bf8b2e6 100644 --- a/bin/u-server/src/main.rs +++ b/bin/u_server/src/main.rs @@ -11,25 +11,21 @@ use std::{ use env_logger; use tokio::sync::Mutex; use uuid::Uuid; - +use tokio::join; use u_lib::{ - MASTER_SERVER, MASTER_PORT, contracts::*, - Job + client::* }; -type SharedStorage = Arc>; -fn get_content() -> impl Filter< - Extract = (Message<'static, ClientInfo>,), - Error = Rejection -> + Clone { +fn get_content() -> impl Filter,), + Error = Rejection> + Clone { body::content_length_limit(1024*64).and(body::json::>()) } -async fn add_client(msg: Message<'_, ClientInfo>, db: SharedStorage) -> -Result { +async fn add_client(msg: Message<'_, ClientInfo>, + db: SharedStorage) -> Result { let new_cli = msg.item; let mut clients = db.lock().await; if clients.contains_key(&new_cli.id) { @@ -47,8 +43,7 @@ Result { } } -async fn listing(db: SharedStorage) -> -Result { +async fn listing(db: SharedStorage) -> Result { let clients = db.lock().await; let mut result: Vec = Vec::with_capacity(clients.len()); for cli in clients.values() { diff --git a/lib/u_lib/Cargo.toml b/lib/u_lib/Cargo.toml index 2ebea39..580c247 100644 --- a/lib/u_lib/Cargo.toml +++ b/lib/u_lib/Cargo.toml @@ -12,3 +12,5 @@ uuid = { version = "^0.8.1", features = ["serde", "v4"] } nix = "0.17" libc = "^0.2" lazy_static = "1.4.0" +tokio = { version = "*", features = ["macros", "process"] } +reqwest = { version = "0.10.7", features = ["json"] } diff --git a/lib/u_lib/src/contracts/client.rs b/lib/u_lib/src/client/client.rs similarity index 80% rename from lib/u_lib/src/contracts/client.rs rename to lib/u_lib/src/client/client.rs index 9cf2b35..b62b74f 100644 --- a/lib/u_lib/src/contracts/client.rs +++ b/lib/u_lib/src/client/client.rs @@ -6,24 +6,24 @@ use std::{ collections::HashMap }; use uuid::Uuid; -use super::{ - ToMsg, - Job +use crate::{ + contracts::*, + UID }; -use crate::UID; -pub type CliStorage = HashMap; pub struct UClient { pub client_info: ClientInfo, - pub jobs: Vec> // TODO: to futures + pub jobs: JobPool, // TODO: to futures + pub is_admin: bool, } impl UClient { pub fn new(client_info: ClientInfo) -> Self { Self { client_info, - jobs: Vec::new() + jobs: Vec::new(), + is_admin: false } } } diff --git a/lib/u_lib/src/client/mod.rs b/lib/u_lib/src/client/mod.rs new file mode 100644 index 0000000..679b350 --- /dev/null +++ b/lib/u_lib/src/client/mod.rs @@ -0,0 +1,4 @@ +pub mod network; +pub mod client; + +pub use client::*; diff --git a/bin/u-agent/src/network.rs b/lib/u_lib/src/client/network.rs similarity index 97% rename from bin/u-agent/src/network.rs rename to lib/u_lib/src/client/network.rs index 3db3a35..ce85107 100644 --- a/bin/u-agent/src/network.rs +++ b/lib/u_lib/src/client/network.rs @@ -1,7 +1,8 @@ -use u_lib::{ +use crate::{ MASTER_SERVER, MASTER_PORT, - contracts::* + contracts::*, + client::* }; use reqwest::{ Client, diff --git a/lib/u_lib/src/contracts/datatypes.rs b/lib/u_lib/src/contracts/datatypes.rs new file mode 100644 index 0000000..316bb78 --- /dev/null +++ b/lib/u_lib/src/contracts/datatypes.rs @@ -0,0 +1,15 @@ +use { + super::*, + tokio::sync::Mutex, + std::sync::Arc, + std::collections::HashMap, + uuid::Uuid, + crate::client::* +}; + + +pub type JobPool = Vec>; +pub type JobResult = Result, Vec>; + +pub type CliStorage = HashMap; +pub type SharedStorage = Arc>; \ No newline at end of file diff --git a/lib/u_lib/src/contracts/jobs.rs b/lib/u_lib/src/contracts/jobs.rs index b1ab1d6..e1d064a 100644 --- a/lib/u_lib/src/contracts/jobs.rs +++ b/lib/u_lib/src/contracts/jobs.rs @@ -1,55 +1,58 @@ -use std::process::{ - Command, - Output +use std::{ + process::Command }; - -pub type JobResult<'res> = Result<&'res [u8], &'res [u8]>; +//use tokio::process::Command; +use super::*; pub trait Job { - async fn run(&mut self); - fn result(&self) -> JobResult; + fn run(&mut self); + fn get_result(&self) -> &JobResult; } -pub struct ShellJob<'cmd> { - pub result: JobResult<'cmd>, - pub cmd: &'cmd str, - pub args: Vec<&'cmd str> +pub struct ShellJob { + pub result: JobResult, + pub cmd: String, + pub args: Vec } impl ShellJob { - pub fn from_raw(raw_cmd: &str) -> Self { - let cmd_parts = raw_cmd.split(" "); + pub fn from_raw(raw_cmd: String) -> Self { + let mut cmd_parts = raw_cmd + .split(" ") + .map(String::from) + .collect::>(); + let args: Vec<_> = cmd_parts.drain(1..).collect(); Self { - cmd: cmd_parts[0], - args: cmd_parts[1..], - result: Err("Did not run".as_bytes()) + cmd: cmd_parts.into_iter().nth(1).unwrap(), + args, + result: Err(b"Did not run".to_vec()) } } } -impl Job for ShellJob { - async fn run(&mut self) { - let result = Command::new(self.cmd) +impl Job for ShellJob{ + fn run(&mut self) { + let result = Command::new(&self.cmd) .args(&self.args) .output(); self.result = match result { Ok(output) => { - if output.status != 0 { - Err(&output.stderr) + if output.status.success() { + Ok(output.stdout.to_vec()) } else { - Ok(&output.stdout) + Err(output.stderr.to_vec()) } } - Err(e) => e.to_string().as_bytes() + Err(e) => Err(e.to_string().into_bytes()) } } - fn result(&self) -> JobResult { - self.result + fn get_result(&self) -> &JobResult { + &self.result } } -/* -pub struct PyJob { -}*/ \ No newline at end of file +/* pub struct PyJob { + +} */ \ No newline at end of file diff --git a/lib/u_lib/src/contracts/messaging.rs b/lib/u_lib/src/contracts/messaging.rs index 64c9a20..332096e 100644 --- a/lib/u_lib/src/contracts/messaging.rs +++ b/lib/u_lib/src/contracts/messaging.rs @@ -50,4 +50,21 @@ where I: Clone { #[derive(Serialize, Deserialize, Debug, Clone)] pub struct RawMsg(pub String); -impl ToMsg for RawMsg {} \ No newline at end of file +impl ToMsg for RawMsg {} + +/* +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_create_message_owned() { + let item = String::from("QWEDSA"); + let msg_raw = Message { + id: *UID, + item: Cow::Owned(item.clone()) + }; + let msg = Message::new(item); + assert_eq!(msg_raw.item, msg.item); + } +}*/ \ No newline at end of file diff --git a/lib/u_lib/src/contracts/mod.rs b/lib/u_lib/src/contracts/mod.rs index 3d29323..833f09f 100644 --- a/lib/u_lib/src/contracts/mod.rs +++ b/lib/u_lib/src/contracts/mod.rs @@ -1,11 +1,12 @@ pub mod jobs; -pub mod client; pub mod messaging; +pub mod datatypes; pub use { - client::*, + crate::client::client::*, messaging::*, - jobs::* + jobs::*, + datatypes::* }; use std::{ diff --git a/lib/u_lib/src/lib.rs b/lib/u_lib/src/lib.rs index dcb2823..669a574 100644 --- a/lib/u_lib/src/lib.rs +++ b/lib/u_lib/src/lib.rs @@ -1,9 +1,12 @@ pub mod config; pub mod contracts; pub mod utils; +pub mod client; -pub use utils::*; -pub use config::*; +pub use { + utils::*, + config::*, +}; #[macro_use] extern crate lazy_static;