almost jobs

4-update-check
plazmoid 4 years ago
parent b711eff13d
commit 57ae564fd7
  1. 5
      bin/u-agent/src/main.rs
  2. 17
      bin/u-agent/src/network.rs
  3. 15
      bin/u-server/src/main.rs
  4. 19
      lib/u_lib/src/contracts/client.rs
  5. 55
      lib/u_lib/src/contracts/jobs.rs
  6. 4
      lib/u_lib/src/contracts/mod.rs

@ -9,7 +9,6 @@
//mod jobs; //mod jobs;
//use std::process::Command;
use std::thread::sleep; use std::thread::sleep;
use std::time::Duration; use std::time::Duration;
@ -24,9 +23,5 @@ async fn main() {
loop { loop {
instance.list().await; instance.list().await;
sleep(Duration::from_secs(2)); sleep(Duration::from_secs(2));
/*Command::new("touch")
.arg("/tmp/win2")
.output()
.unwrap();*/
} }
} }

@ -6,7 +6,8 @@ use u_lib::{
use reqwest::{ use reqwest::{
Client, Client,
Url, 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) { pub async fn init(&self) {
let response: Response = self.client let response: Response = self.build_post("/new")
.post(self.base_url.join("/new").unwrap())
.json(&self.cli_info.as_message()) .json(&self.cli_info.as_message())
.send() .send()
.await .await
@ -43,8 +51,7 @@ impl ClientHandler {
} }
pub async fn list(&self) { pub async fn list(&self) {
let response: Response = self.client let response: Response = self.build_get("/ls")
.get(self.base_url.join("/ls").unwrap())
.send() .send()
.await .await
.unwrap(); .unwrap();

@ -15,7 +15,8 @@ use uuid::Uuid;
use u_lib::{ use u_lib::{
MASTER_SERVER, MASTER_SERVER,
MASTER_PORT, MASTER_PORT,
contracts::* contracts::*,
Job
}; };
type SharedStorage = Arc<Mutex<CliStorage>>; type SharedStorage = Arc<Mutex<CliStorage>>;
@ -36,11 +37,13 @@ Result<impl Reply, Rejection> {
&RawMsg("Already exist".to_string()).into_message() &RawMsg("Already exist".to_string()).into_message()
)) ))
} else { } 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( Ok(warp::reply::json(
&RawMsg("Added".to_string()).into_message() &RawMsg("Added".to_string()).into_message()
) ))
)
} }
} }
@ -49,7 +52,7 @@ Result<impl Reply, Rejection> {
let clients = db.lock().await; let clients = db.lock().await;
let mut result: Vec<ClientInfo> = Vec::with_capacity(clients.len()); let mut result: Vec<ClientInfo> = Vec::with_capacity(clients.len());
for cli in clients.values() { for cli in clients.values() {
result.push(cli.clone()); result.push(cli.client_info.clone());
} }
Ok(warp::reply::json( Ok(warp::reply::json(
&Message::new_owned(result) &Message::new_owned(result)
@ -61,7 +64,7 @@ Result<impl Reply, Rejection> {
async fn main() { async fn main() {
env_logger::init(); env_logger::init();
let base_db: SharedStorage = Arc::new( let base_db: SharedStorage = Arc::new(
Mutex::new(HashMap::<Uuid, ClientInfo>::new()) Mutex::new(HashMap::<Uuid, UClient>::new())
); );
let db = warp::any().map(move || Arc::clone(&base_db)); let db = warp::any().map(move || Arc::clone(&base_db));

@ -7,11 +7,26 @@ use std::{
}; };
use uuid::Uuid; use uuid::Uuid;
use super::{ use super::{
ToMsg ToMsg,
Job
}; };
use crate::UID; use crate::UID;
pub type CliStorage = HashMap<Uuid, ClientInfo>; pub type CliStorage = HashMap<Uuid, UClient>;
pub struct UClient {
pub client_info: ClientInfo,
pub jobs: Vec<Box<Jobs>> // TODO: to futures
}
impl UClient {
pub fn new(client_info: ClientInfo) -> Self {
Self {
client_info,
jobs: Vec::new()
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ClientInfo { pub struct ClientInfo {

@ -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 {
}*/

@ -1,9 +1,11 @@
pub mod jobs;
pub mod client; pub mod client;
pub mod messaging; pub mod messaging;
pub use { pub use {
client::*, client::*,
messaging::* messaging::*,
jobs::*
}; };
use std::{ use std::{

Loading…
Cancel
Save