as_message is now functional for all types

improved client handlers builder
added some scripts
4-update-check
plazmoid 4 years ago
parent bb5e7aa04c
commit 2fc52aafb6
  1. 15
      bin/u_agent/src/main.rs
  2. 1
      bin/u_panel/Cargo.toml
  3. 7
      bin/u_panel/src/main.rs
  4. 2
      bin/u_server/src/handlers.rs
  5. 2
      bin/u_server/src/main.rs
  6. 47
      lib/u_lib/src/api.rs
  7. 4
      lib/u_lib/src/errors.rs
  8. 27
      lib/u_lib/src/messaging.rs
  9. 23
      lib/u_lib/src/models/mod.rs
  10. 4
      scripts/cargo_musl.sh
  11. 8
      scripts/exec_bin.sh
  12. 7
      scripts/rootdir.sh
  13. 4
      scripts/start_db.sh
  14. 10
      scripts/test_agent_docker.sh
  15. 3
      test_agent_docker.sh

@ -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;
}
}

@ -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"

@ -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)
}

@ -93,7 +93,7 @@ pub async fn get_agents(db: Storage) -> Result<impl Reply, Rejection> {
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())

@ -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);
/*

@ -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::<BaseMessage<$result>>()
.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)?
$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::<BaseMessage<$result>>()
.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<Agent>);
// get jobs for client himself (A: id=client_id)
build_handler!(GET get_jobs() -> Vec<JobMeta>);
build_handler!(GET get_agents() -> Vec<Agent>);
// get jobs for client (agent_id=Uuid)
build_handler!(GET get_jobs("agent_id":Uuid) -> Vec<JobMeta>);
// 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<JobMeta>) -> ());
// 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) -> ());

@ -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<ReqError> for UError {
fn from(e: ReqError) -> Self {
UError::ConnectionError(e.to_string())
UError::NetError(e.to_string())
}
}

@ -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<T> for Cow<T>
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<C>(item: C) -> Self
where C: Into<Cow<'cow, I>> {
Self {
id: UID.clone(),
item: item.into()
}
}
pub fn new_cow(item: I) -> Self {
where C: Into<Moo<'cow, I>> {
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<T: ToMsg> ToMsg for Vec<T> {}
/*
#[cfg(test)]
mod tests {

@ -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<Agent>,
Vec<JobMeta>,
()
);

@ -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 \

@ -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 $@

@ -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

@ -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

@ -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

@ -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
Loading…
Cancel
Save