4-update-check
plazmoid 4 years ago
parent 3c538b44ac
commit 0908660fc9
  1. 1
      bin/u_panel/src/main.rs
  2. 37
      bin/u_server/src/handlers.rs
  3. 2
      lib/u_lib/src/client/network.rs
  4. 2
      lib/u_lib/src/contracts/datatypes.rs
  5. 71
      lib/u_lib/src/contracts/jobs.rs

@ -7,7 +7,6 @@ use u_lib::{
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), &'static str> { async fn main() -> Result<(), &'static str> {
//daemonize();
let mut raw_args = args(); let mut raw_args = args();
let method = match raw_args.nth(1) { let method = match raw_args.nth(1) {
Some(m) => m, Some(m) => m,

@ -0,0 +1,37 @@
use u_lib::contracts::*;
use warp::{
Rejection,
Reply,
};
pub async fn add_client(
msg: Message<'_, ClientInfo>,
db: Storage) -> Result<impl Reply, Rejection>
{
let new_cli = msg.item;
let mut clients = db.lock().await;
if clients.contains_key(&new_cli.id) {
Ok(warp::reply::json(
&RawMsg("Already exist".to_string()).into_message()
))
} else {
clients.insert(
new_cli.id.clone(),
UClient::new(new_cli.into_owned())
);
Ok(warp::reply::json(
&RawMsg("Added".to_string()).into_message()
))
}
}
pub async fn listing(db: Storage) -> Result<impl Reply, Rejection> {
let clients = db.lock().await;
let mut result: Vec<ClientInfo> = Vec::with_capacity(clients.len());
for cli in clients.values() {
result.push(cli.client_info.clone());
}
Ok(warp::reply::json(
&Message::new_owned(result)
))
}

@ -67,7 +67,7 @@ impl ClientHandler {
} }
pub async fn list(&self) -> Vec<ClientInfo> { pub async fn list(&self) -> Vec<ClientInfo> {
let response: Response = dbg!(self.build_get("/ls")) let response: Response = self.build_get("/ls")
.send() .send()
.await .await
.unwrap(); .unwrap();

@ -11,8 +11,6 @@ use {
//pub type JobPool = Vec<Box<dyn Job>>; //pub type JobPool = Vec<Box<dyn Job>>;
pub type JobPool = Vec<ShellJob>;
pub type JobResult = Result<Vec<u8>, Vec<u8>>;
pub type CliStorage = HashMap<Uuid, UClient>; pub type CliStorage = HashMap<Uuid, UClient>;

@ -1,13 +1,13 @@
use std::{ use std::{
process::Command process::Command,
time::SystemTime
}; };
use uuid::Uuid;
//use tokio::process::Command; //use tokio::process::Command;
use super::*; use super::*;
pub trait Job { pub type JobPool = Vec<ShellJob>;
fn run(&mut self); pub type JobResult = Result<Vec<u8>, Vec<u8>>;
fn get_result(&self) -> &JobResult;
}
pub struct ShellJob { pub struct ShellJob {
pub result: JobResult, pub result: JobResult,
@ -28,9 +28,7 @@ impl ShellJob {
result: Err(b"Did not run".to_vec()) result: Err(b"Did not run".to_vec())
} }
} }
}
impl Job for ShellJob {
fn run(&mut self) { fn run(&mut self) {
let result = Command::new(&self.cmd) let result = Command::new(&self.cmd)
.args(&self.args) .args(&self.args)
@ -52,7 +50,62 @@ impl Job for ShellJob {
} }
} }
pub enum ManageAction {
Ping,
UpdateAvailable,
JobsResultsRequest,
Terminate
}
pub enum JobSchedule {
Once,
Permanent,
//TODO: Scheduled
}
pub enum JobState {
Dummy,
Queued, // server created a job, but client didn't get it yet
Pending, // client got a job, but not running yet
Running, // client is currently running a job
Rerunning, // if job is cycled
Finished,
}
/* pub struct PyJob { pub enum JobType {
Manage(ManageAction),
Shell,
Python,
Binary
}
pub struct JobMeta {
id: Uuid,
name: String,
created: SystemTime,
updated: SystemTime,
state: JobState,
exec_type: JobType,
append_result: bool, //true: append, false: rewrite
payload: Option<Vec<u8>>,
}
impl JobMeta {
pub fn from_shell(shell_cmd: Vec<u8>) -> Self {
let uid = Uuid::new_v4();
let str_payload_name = shell_cmd.split(b" ").collect();
let job_name = format!("{} {}", uid.to_string()[..6], str_payload_name[0]);
Self {
id: uid,
name: job_name,
created: SystemTime::now(),
updated: SystemTime::now(),
state: JobState::Pending,
exec_type: JobType::Shell,
append_result: true,
payload: Some(shell_cmd)
}
}
}
} */ impl ToMsg for JobMeta {}
Loading…
Cancel
Save