diff --git a/bin/u_agent/src/main.rs b/bin/u_agent/src/main.rs index b5a721e..e7ae405 100644 --- a/bin/u_agent/src/main.rs +++ b/bin/u_agent/src/main.rs @@ -16,6 +16,7 @@ use u_lib::{ api::ClientHandler, models::{gather}, build_jobs, + UID }; use tokio::{time::{Duration, sleep}}; @@ -25,9 +26,9 @@ macro_rules! retry_until_ok { loop { match $body { Ok(r) => break r, - Err(e) => eprintln!("{:?}", e) + Err(e) => error!("{:?}", e) }; - sleep(Duration::from_secs(2)).await; + sleep(Duration::from_secs(5)).await; } } } @@ -38,11 +39,11 @@ async fn main() { env_logger::init(); let arg_ip = env::args().nth(1); let instance = ClientHandler::new(arg_ip); - debug!("Gathering info"); + info!("Gathering info"); let cli_info = gather().await; - debug!("Connecting to the server"); + info!("Connecting to the server"); retry_until_ok!(instance.init(&cli_info).await); - debug!("Instanciated! Running main loop"); + info!("Instanciated! Running main loop"); loop {/* let jobs = retry_until_ok!(instance.get_jobs().await).unwrap(); if jobs.len() > 0 { @@ -53,9 +54,9 @@ async fn main() { result.into_iter().map(|r| r.unwrap()).collect() ).await) }*/ - let jobs = retry_until_ok!(instance.get_jobs().await); + let jobs = retry_until_ok!(instance.get_jobs(&*UID).await); println!("{:?}", jobs); - sleep(Duration::from_secs(2)).await; + sleep(Duration::from_secs(5)).await; } } diff --git a/bin/u_panel/Cargo.toml b/bin/u_panel/Cargo.toml index de289c5..029c533 100644 --- a/bin/u_panel/Cargo.toml +++ b/bin/u_panel/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" [dependencies] tokio = { version = "1.2.0", features = ["macros", "rt-multi-thread", "process"] } +structopt = "0.3.21" log = "^0.4" env_logger = "0.7.1" uuid = "0.8.1" diff --git a/bin/u_panel/src/main.rs b/bin/u_panel/src/main.rs index 414e1b3..824abd8 100644 --- a/bin/u_panel/src/main.rs +++ b/bin/u_panel/src/main.rs @@ -1,9 +1,10 @@ -use std::env::args; +use structopt::StructOpt; use u_lib::{ api::ClientHandler, - //client::* }; +use std::env::args; +struct Table; #[tokio::main] async fn main() -> Result<(), &'static str> { @@ -16,7 +17,7 @@ async fn main() -> Result<(), &'static str> { .password("123qwe".to_string()); match method.as_str() { "ls" => { - let result = cli_handler.ls().await; + let result = cli_handler.get_agents().await; for cli in result.iter() { println!("{:?}", cli) } diff --git a/bin/u_server/src/handlers.rs b/bin/u_server/src/handlers.rs index a70a599..96c09c0 100644 --- a/bin/u_server/src/handlers.rs +++ b/bin/u_server/src/handlers.rs @@ -93,7 +93,7 @@ pub async fn get_agents(db: Storage) -> Result { let result = db.lock().unwrap().get_agents(); match result { Ok(r) => Ok(warp::reply::json( - &r.into_message() + &r.as_message() )), Err(e) => { Err(warp::reject()) diff --git a/bin/u_server/src/main.rs b/bin/u_server/src/main.rs index 70ec83b..fb632fc 100644 --- a/bin/u_server/src/main.rs +++ b/bin/u_server/src/main.rs @@ -48,7 +48,7 @@ async fn main() { .and_then(handlers::add_agent); let get_agents = warp::get() - .and(warp::path(Paths::ls)) + .and(warp::path(Paths::get_agents)) .and(db.clone()) .and_then(handlers::get_agents); /* diff --git a/lib/u_lib/src/api.rs b/lib/u_lib/src/api.rs index fc6c6b0..55c04d1 100644 --- a/lib/u_lib/src/api.rs +++ b/lib/u_lib/src/api.rs @@ -21,20 +21,6 @@ use uuid::Uuid; pub struct Paths; -#[macro_export] -macro_rules! get_result { - ( () ) => ( |_| async Ok(()) ); - ( $result:ty ) => { - |response: Response| async { - response - .json::>() - .await - .map(|msg| msg.into_item()) - .map_err(|e| UError::from(e)) - } - }; -} - #[macro_export] macro_rules! build_url_by_method { ( @@ -80,22 +66,27 @@ macro_rules! build_url_by_method { macro_rules! build_handler { ( $method:tt - $path:tt( - $($param_name:literal:)? - $($param_type:ty)? - $(; $url_param:ty)? - ) -> $result:ty + $path:tt $( /$url_param:tt )? ( + $( $param_name:literal: )? + $( $param_type:ty )? + ) -> $result:ty ) => { impl ClientHandler { pub async fn $path( - &self $(, param: &$param_type)? $(, url_param: &$url_param)? + &self $(, param: &$param_type)? // $(, url_param: &$url_param)? ) -> UResult<$result> { let request = $crate::build_url_by_method!( $method $path, pname = $($param_name)?, ptype = $($param_type)?, urlparam = $($url_param)? )(self $(, param as &$param_type)? ); let response = request.send().await?; - ($crate::get_result!($result)(response)).await + match response.error_for_status() { + Ok(r) => r.json::>() + .await + .map_err(|e| UError::from(e)) + .map(|msg| msg.into_item()), + Err(e) => Err(UError::from(e)) + } } } @@ -148,19 +139,21 @@ impl ClientHandler { self.set_pwd(rb) } } - +////////////////// // method basic_path(json/query param; additional_url_param) -> return value // A - admin only +////////////////// // client listing (A) -build_handler!(GET ls() -> Vec); -// get jobs for client himself (A: id=client_id) -build_handler!(GET get_jobs() -> Vec); +build_handler!(GET get_agents() -> Vec); +// get jobs for client (agent_id=Uuid) +build_handler!(GET get_jobs("agent_id":Uuid) -> Vec); // add client to server's db build_handler!(POST init(IAgent) -> RawMsg); // create and upload job (A) -//build_handler!(POST upload_jobs) -// ??? -/*build_handler!(POST del() -> ()); +build_handler!(POST add_jobs(Vec) -> ()); +// delete something (A) +build_handler!(GET del/Uuid() -> ()); +/* // set jobs for client (A) // POST /set_jobs/Uuid json: JobMetaStorage build_handler!(POST set_jobs(JobMetaStorage; Uuid) -> ()); diff --git a/lib/u_lib/src/errors.rs b/lib/u_lib/src/errors.rs index 310b56a..ba4442f 100644 --- a/lib/u_lib/src/errors.rs +++ b/lib/u_lib/src/errors.rs @@ -14,7 +14,7 @@ pub enum UError { Raw(&'static str), #[error("Connection error: {0}")] - ConnectionError(String), + NetError(String), #[error("Parse error")] ParseError, @@ -34,6 +34,6 @@ pub enum UError { impl From for UError { fn from(e: ReqError) -> Self { - UError::ConnectionError(e.to_string()) + UError::NetError(e.to_string()) } } \ No newline at end of file diff --git a/lib/u_lib/src/messaging.rs b/lib/u_lib/src/messaging.rs index 673be6d..68d71e7 100644 --- a/lib/u_lib/src/messaging.rs +++ b/lib/u_lib/src/messaging.rs @@ -1,7 +1,6 @@ use serde::{ Serialize, Deserialize, - //de::DeserializeOwned, }; use std::{ borrow::Cow, @@ -9,39 +8,32 @@ use std::{ use uuid::Uuid; use crate::{UID}; -pub trait ToMsg: Clone { //+ Serialize + DeserializeOwned { +//this is only for impl From for Cow +pub struct Moo<'cow, T: Clone>(pub Cow<'cow, T>); + +pub trait ToMsg: Clone { fn as_message<'m>(&'m self) -> BaseMessage<'m, Self> - where Cow<'m, Self>: From<&'m Self> { + where Moo<'m, Self>: From<&'m Self> { BaseMessage::new(self) } - - fn into_message(self) -> BaseMessage<'static, Self> { - BaseMessage::new_cow(self) - } } #[derive(Serialize, Deserialize, Debug)] pub struct BaseMessage<'cow, I> where I: ToMsg { pub id: Uuid, - pub item: Cow<'cow, I> + item: Cow<'cow, I> } impl<'cow, I> BaseMessage<'cow, I> where I: ToMsg { pub fn new(item: C) -> Self - where C: Into> { - Self { - id: UID.clone(), - item: item.into() - } - } - - pub fn new_cow(item: I) -> Self { + where C: Into> { + let Moo(item) = item.into(); Self { id: UID.clone(), - item: Cow::Owned(item) + item } } @@ -53,7 +45,6 @@ impl<'cow, I> BaseMessage<'cow, I> #[derive(Serialize, Deserialize, Debug, Clone)] pub struct RawMsg(pub String); -impl ToMsg for Vec {} /* #[cfg(test)] mod tests { diff --git a/lib/u_lib/src/models/mod.rs b/lib/u_lib/src/models/mod.rs index 665e543..41a227e 100644 --- a/lib/u_lib/src/models/mod.rs +++ b/lib/u_lib/src/models/mod.rs @@ -19,20 +19,29 @@ macro_rules! to_message { impl ToMsg for $type {} - impl<'cow> From<$type> for Cow<'cow, $type> { + impl<'cow> From<$type> for Moo<'cow, $type> { #[inline] - fn from(obj: $type) -> Cow<'cow, $type> { - Cow::Owned(obj) + fn from(obj: $type) -> Moo<'cow, $type> { + Moo(Cow::Owned(obj)) } } - impl<'cow> From<&'cow $type> for Cow<'cow, $type> { + impl<'cow> From<&'cow $type> for Moo<'cow, $type> { #[inline] - fn from(obj: &'cow $type) -> Cow<'cow, $type> { - Cow::Borrowed(obj) + fn from(obj: &'cow $type) -> Moo<'cow, $type> { + Moo(Cow::Borrowed(obj)) } } )+ } } -to_message!(IAgent, Agent, RawMsg, JobMeta, JobResult); +to_message!( + Agent, + IAgent, + JobMeta, + JobResult, + RawMsg, + Vec, + Vec, + () +); diff --git a/cargo_musl.sh b/scripts/cargo_musl.sh similarity index 62% rename from cargo_musl.sh rename to scripts/cargo_musl.sh index f0171fa..49cf0cd 100755 --- a/cargo_musl.sh +++ b/scripts/cargo_musl.sh @@ -1,6 +1,8 @@ #!/bin/bash +set -e +source $(dirname $0)/rootdir.sh #set ROOTDIR docker run \ - -v $PWD:/volume \ + -v $ROOTDIR:/volume \ -v cargo-cache:/root/.cargo/registry \ -w /volume \ -it \ diff --git a/scripts/exec_bin.sh b/scripts/exec_bin.sh new file mode 100755 index 0000000..c324f6d --- /dev/null +++ b/scripts/exec_bin.sh @@ -0,0 +1,8 @@ +#!/bin/bash +set -e +source $(dirname $0)/rootdir.sh #set ROOTDIR +BIN=$1 +[[ $BIN == '' ]] && echo "Bin required" && exit 1 +shift +export RUST_LOG=info +$ROOTDIR/target/x86_64-unknown-linux-gnu/debug/u_$BIN $@ diff --git a/scripts/rootdir.sh b/scripts/rootdir.sh new file mode 100755 index 0000000..d55a827 --- /dev/null +++ b/scripts/rootdir.sh @@ -0,0 +1,7 @@ +#!/bin/bash +FILE=$(readlink -f $0) +export ROOTDIR=$(dirname $(dirname $FILE)) +if [[ $(basename $ROOTDIR) != "unki" ]]; then + echo "Wrong rootdir $ROOTDIR" + exit 1 +fi diff --git a/run_db.sh b/scripts/start_db.sh similarity index 52% rename from run_db.sh rename to scripts/start_db.sh index c10d80d..e8a9f14 100755 --- a/run_db.sh +++ b/scripts/start_db.sh @@ -1,8 +1,10 @@ #!/bin/bash +set -e +source $(dirname $0)/rootdir.sh #set ROOTDIR docker run \ -d \ --rm \ --name u_db \ -e POSTGRES_PASSWORD=12348756 \ - -v $(pwd)/data:/var/lib/postgresql/data \ + -v $ROOTDIR/data:/var/lib/postgresql/data \ postgres diff --git a/scripts/test_agent_docker.sh b/scripts/test_agent_docker.sh new file mode 100755 index 0000000..b4107ca --- /dev/null +++ b/scripts/test_agent_docker.sh @@ -0,0 +1,10 @@ +#!/bin/bash +set -e +source $(dirname $0)/rootdir.sh #set ROOTDIR +DOCKER_IF_IP=$(ip address show docker0 | grep "inet " | awk '{print $2}' | cut -d'/' -f1) +docker run \ + --rm \ + -d \ + -v $ROOTDIR/target/x86_64-unknown-linux-musl/debug/u_agent:/u_agent \ + centos:7 \ + /u_agent $DOCKER_IF_IP diff --git a/test_agent_docker.sh b/test_agent_docker.sh deleted file mode 100755 index 45779b5..0000000 --- a/test_agent_docker.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -DOCKER_IF_IP=$(ip address show docker0 | grep "inet " | awk '{print $2}' | cut -d'/' -f1) -docker run --rm -d -v $PWD/target/x86_64-unknown-linux-musl/debug/u_agent:/u_agent centos:7 /u_agent $DOCKER_IF_IP