From 57ae564fd7fd1fc97f429c1f0259641d2278ccf5 Mon Sep 17 00:00:00 2001 From: plazmoid Date: Sun, 2 Aug 2020 00:52:09 +0500 Subject: [PATCH] almost jobs --- bin/u-agent/src/main.rs | 5 --- bin/u-agent/src/network.rs | 17 +++++++--- bin/u-server/src/main.rs | 17 ++++++---- lib/u_lib/src/contracts/client.rs | 19 +++++++++-- lib/u_lib/src/contracts/jobs.rs | 55 +++++++++++++++++++++++++++++++ lib/u_lib/src/contracts/mod.rs | 4 ++- 6 files changed, 97 insertions(+), 20 deletions(-) create mode 100644 lib/u_lib/src/contracts/jobs.rs diff --git a/bin/u-agent/src/main.rs b/bin/u-agent/src/main.rs index a250c74..02385fb 100644 --- a/bin/u-agent/src/main.rs +++ b/bin/u-agent/src/main.rs @@ -9,7 +9,6 @@ //mod jobs; -//use std::process::Command; use std::thread::sleep; use std::time::Duration; @@ -24,9 +23,5 @@ async fn main() { loop { instance.list().await; sleep(Duration::from_secs(2)); - /*Command::new("touch") - .arg("/tmp/win2") - .output() - .unwrap();*/ } } diff --git a/bin/u-agent/src/network.rs b/bin/u-agent/src/network.rs index c1b28d7..3db3a35 100644 --- a/bin/u-agent/src/network.rs +++ b/bin/u-agent/src/network.rs @@ -6,7 +6,8 @@ use u_lib::{ use reqwest::{ Client, Url, - Response + Response, + RequestBuilder }; @@ -27,9 +28,16 @@ impl ClientHandler { } } + fn build_get(&self, url: &str) -> RequestBuilder { + self.client.get(self.base_url.join(url).unwrap()) + } + + fn build_post(&self, url: &str) -> RequestBuilder { + self.client.post(self.base_url.join(url).unwrap()) + } + pub async fn init(&self) { - let response: Response = self.client - .post(self.base_url.join("/new").unwrap()) + let response: Response = self.build_post("/new") .json(&self.cli_info.as_message()) .send() .await @@ -43,8 +51,7 @@ impl ClientHandler { } pub async fn list(&self) { - let response: Response = self.client - .get(self.base_url.join("/ls").unwrap()) + let response: Response = self.build_get("/ls") .send() .await .unwrap(); diff --git a/bin/u-server/src/main.rs b/bin/u-server/src/main.rs index ba6a8d7..27d2745 100644 --- a/bin/u-server/src/main.rs +++ b/bin/u-server/src/main.rs @@ -15,7 +15,8 @@ use uuid::Uuid; use u_lib::{ MASTER_SERVER, MASTER_PORT, - contracts::* + contracts::*, + Job }; type SharedStorage = Arc>; @@ -36,11 +37,13 @@ Result { &RawMsg("Already exist".to_string()).into_message() )) } else { - clients.insert(new_cli.id.clone(), new_cli.into_owned()); + clients.insert( + new_cli.id.clone(), + UClient::new(new_cli.into_owned()) + ); Ok(warp::reply::json( - &RawMsg("Added".to_string()).into_message() - ) - ) + &RawMsg("Added".to_string()).into_message() + )) } } @@ -49,7 +52,7 @@ Result { let clients = db.lock().await; let mut result: Vec = Vec::with_capacity(clients.len()); for cli in clients.values() { - result.push(cli.clone()); + result.push(cli.client_info.clone()); } Ok(warp::reply::json( &Message::new_owned(result) @@ -61,7 +64,7 @@ Result { async fn main() { env_logger::init(); let base_db: SharedStorage = Arc::new( - Mutex::new(HashMap::::new()) + Mutex::new(HashMap::::new()) ); let db = warp::any().map(move || Arc::clone(&base_db)); diff --git a/lib/u_lib/src/contracts/client.rs b/lib/u_lib/src/contracts/client.rs index 2d2bd03..9cf2b35 100644 --- a/lib/u_lib/src/contracts/client.rs +++ b/lib/u_lib/src/contracts/client.rs @@ -7,11 +7,26 @@ use std::{ }; use uuid::Uuid; use super::{ - ToMsg + ToMsg, + Job }; use crate::UID; -pub type CliStorage = HashMap; +pub type CliStorage = HashMap; + +pub struct UClient { + pub client_info: ClientInfo, + pub jobs: Vec> // TODO: to futures +} + +impl UClient { + pub fn new(client_info: ClientInfo) -> Self { + Self { + client_info, + jobs: Vec::new() + } + } +} #[derive(Serialize, Deserialize, Debug, Clone)] pub struct ClientInfo { diff --git a/lib/u_lib/src/contracts/jobs.rs b/lib/u_lib/src/contracts/jobs.rs new file mode 100644 index 0000000..b1ab1d6 --- /dev/null +++ b/lib/u_lib/src/contracts/jobs.rs @@ -0,0 +1,55 @@ +use std::process::{ + Command, + Output +}; + +pub type JobResult<'res> = Result<&'res [u8], &'res [u8]>; + +pub trait Job { + async fn run(&mut self); + fn result(&self) -> JobResult; +} + +pub struct ShellJob<'cmd> { + pub result: JobResult<'cmd>, + pub cmd: &'cmd str, + pub args: Vec<&'cmd str> +} + +impl ShellJob { + pub fn from_raw(raw_cmd: &str) -> Self { + let cmd_parts = raw_cmd.split(" "); + Self { + cmd: cmd_parts[0], + args: cmd_parts[1..], + result: Err("Did not run".as_bytes()) + } + } +} + +impl Job for ShellJob { + async 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) + } else { + Ok(&output.stdout) + } + } + Err(e) => e.to_string().as_bytes() + } + } + + fn result(&self) -> JobResult { + self.result + } +} + +/* +pub struct PyJob { + +}*/ \ 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 b96b2e8..3d29323 100644 --- a/lib/u_lib/src/contracts/mod.rs +++ b/lib/u_lib/src/contracts/mod.rs @@ -1,9 +1,11 @@ +pub mod jobs; pub mod client; pub mod messaging; pub use { client::*, - messaging::* + messaging::*, + jobs::* }; use std::{