From 3fc7da93ec2bc2ff57db6423d283d8696c9d4097 Mon Sep 17 00:00:00 2001 From: plazmoid Date: Sun, 22 Nov 2020 05:10:37 +0500 Subject: [PATCH] fucked up with itemwrap but finished db --- .env | 1 + Cargo.toml | 1 - bin/u_agent/src/main.rs | 13 +++-- bin/u_panel/src/main.rs | 2 +- bin/u_server/Cargo.toml | 3 -- bin/u_server/src/db.rs | 49 ++++++++++++++++--- bin/u_server/src/handlers.rs | 23 +++++---- bin/u_server/src/main.rs | 35 ++++++++----- lib/u_db/diesel.toml => diesel.toml | 2 +- lib/u_db/Cargo.toml | 17 ------- lib/u_db/src/models/mod.rs | 3 -- lib/u_lib/Cargo.toml | 5 +- lib/u_lib/src/api.rs | 7 +-- lib/u_lib/src/contracts/agent.rs | 6 +-- lib/u_lib/src/contracts/jobs.rs | 2 +- lib/u_lib/src/contracts/messaging.rs | 22 ++++----- lib/u_lib/src/contracts/mod.rs | 4 +- lib/u_lib/src/lib.rs | 4 +- lib/{u_db => u_lib}/src/models/agent.rs | 4 +- .../src/lib.rs => u_lib/src/models/mod.rs} | 11 +++-- lib/{u_db/src => u_lib/src/models}/schema.rs | 0 .../2020-10-24-111622_create_all/down.sql | 0 .../2020-10-24-111622_create_all/up.sql | 0 23 files changed, 115 insertions(+), 99 deletions(-) create mode 100644 .env rename lib/u_db/diesel.toml => diesel.toml (73%) delete mode 100644 lib/u_db/Cargo.toml delete mode 100644 lib/u_db/src/models/mod.rs rename lib/{u_db => u_lib}/src/models/agent.rs (94%) rename lib/{u_db/src/lib.rs => u_lib/src/models/mod.rs} (63%) rename lib/{u_db/src => u_lib/src/models}/schema.rs (100%) rename {lib/u_db/migrations => migrations}/2020-10-24-111622_create_all/down.sql (100%) rename {lib/u_db/migrations => migrations}/2020-10-24-111622_create_all/up.sql (100%) diff --git a/.env b/.env new file mode 100644 index 0000000..5d66ce7 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +DATABASE_URL=./bin/u_server/u_server.db diff --git a/Cargo.toml b/Cargo.toml index a90044d..fd7382c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,6 @@ members = [ "bin/u_run", "bin/u_server", "lib/u_lib", - "lib/u_db" ] [profile.release] diff --git a/bin/u_agent/src/main.rs b/bin/u_agent/src/main.rs index 07ba8ab..3acc6de 100644 --- a/bin/u_agent/src/main.rs +++ b/bin/u_agent/src/main.rs @@ -12,10 +12,7 @@ use { std::time::Duration, std::env, u_lib::{ - client::{ - Agent, - network::ClientHandler - }, + api::ClientHandler, contracts::*, send_jobs_to_executor, }, @@ -26,9 +23,9 @@ async fn main() { //daemonize(); let arg_ip = env::args().nth(1); let instance = ClientHandler::new(arg_ip); - let cli_info = Agent::gather(); + let cli_info = agent::gather().await; retry_until_ok!(instance.init(&cli_info).await); - loop { + loop {/* let jobs = retry_until_ok!(instance.get_jobs().await); if jobs.0.len() > 0 { let result = send_jobs_to_executor(jobs @@ -39,7 +36,9 @@ async fn main() { retry_until_ok!(instance.report( ItemWrap(result.into_iter().map(|r| r.unwrap()).collect()) ).await) - } + }*/ + //let jobs = retry_until_ok!(instance.get_jobs().await); + //println!("{:?}", jobs); sleep(Duration::from_secs(2)); } } diff --git a/bin/u_panel/src/main.rs b/bin/u_panel/src/main.rs index 3001a8c..8801333 100644 --- a/bin/u_panel/src/main.rs +++ b/bin/u_panel/src/main.rs @@ -1,6 +1,6 @@ use std::env::args; use u_lib::{ - client::network::ClientHandler, + api::ClientHandler, //client::* }; diff --git a/bin/u_server/Cargo.toml b/bin/u_server/Cargo.toml index 8f8e5e3..fbf2af4 100644 --- a/bin/u_server/Cargo.toml +++ b/bin/u_server/Cargo.toml @@ -27,6 +27,3 @@ version = "0.2.22" path = "../../lib/u_lib" version = "*" -[dependencies.u_db] -path = "../../lib/u_db" -version = "*" diff --git a/bin/u_server/src/db.rs b/bin/u_server/src/db.rs index 6a56621..aa07d0c 100644 --- a/bin/u_server/src/db.rs +++ b/bin/u_server/src/db.rs @@ -9,10 +9,9 @@ use std::{ }; use crate::{ - errors::USrvResult, - db::* + errors::USrvResult }; -use u_db::schema; +use u_lib::models::*; pub type Storage = Arc>; @@ -21,9 +20,12 @@ pub struct UDB { } impl UDB { - pub fn new() -> USrvResult { + pub fn new(path: Option) -> USrvResult { dotenv()?; - let db_path = env::var("DATABASE_URL")?; + let db_path = match path { + Some(p) => p, + None => env::var("DATABASE_URL").unwrap_or(":memory:".to_string()) + }; let conn = SqliteConnection::establish(&db_path)?; conn.execute("PRAGMA foreign_keys = ON;")?; let instance = UDB { @@ -40,10 +42,41 @@ impl UDB { Ok(()) } - pub fn get_agents(&self) -> USrvResult> { + pub fn get_agents(&self) -> USrvResult> { use schema::agents; let result = agents::table - .load::(&self.conn)?; + .load::(&self.conn)?; Ok(result) } -} \ No newline at end of file +} + +#[cfg(test)] +mod tests { + use super::*; + + fn setup_db() -> Storage { + return UDB::new(Some(":memory:".to_string())).unwrap(); + } + + #[tokio::test] + async fn test_add_agent() { + let db = setup_db(); + let agent = IAgent { + alias: None, + agent_id: "000-000".to_string(), + hostname: "test".to_string(), + is_root: false, + is_root_allowed: false, + platform: "linux".to_string(), + status: None, + token: None, + username: "test".to_string() + }; + db.lock().unwrap().new_agent(agent).unwrap(); + let result = db.lock().unwrap().get_agents().unwrap(); + assert_eq!( + result[0].username, + "test".to_string() + ) + } +} diff --git a/bin/u_server/src/handlers.rs b/bin/u_server/src/handlers.rs index c8abed0..67cdf5f 100644 --- a/bin/u_server/src/handlers.rs +++ b/bin/u_server/src/handlers.rs @@ -1,4 +1,7 @@ -use u_lib::contracts::*; +use u_lib::{ + contracts::*, + models::* +}; use warp::{ Rejection, Reply, @@ -6,8 +9,7 @@ use warp::{ http::StatusCode }; use crate::db::{ - Storage, - IAgent + Storage }; pub async fn add_agent( @@ -87,22 +89,19 @@ pub async fn set_jobs( } */ -pub async fn ls(db: Storage) -> Result { +pub async fn get_agents(db: Storage) -> Result { let result = db.lock().unwrap().get_agents(); - /* - let mut result: Vec = Vec::with_capacity(clients.len()); - for cli in clients.values() { - result.push(cli.client_info.clone()); - } - */ match result { Ok(r) => Ok(warp::reply::json( &ItemWrap(r).as_message() )), - Err(e) => error!(e) + Err(e) => { + error!("{}", &e); + Err(warp::reject()) + } } } pub async fn dummy() -> Result { Ok(String::from("ok")) -} \ No newline at end of file +} diff --git a/bin/u_server/src/main.rs b/bin/u_server/src/main.rs index 9793645..cee28c9 100644 --- a/bin/u_server/src/main.rs +++ b/bin/u_server/src/main.rs @@ -16,22 +16,20 @@ use env_logger; use u_lib::{ MASTER_PORT, contracts::*, - api::Paths + api::Paths, + models::* }; use db::*; use serde::{ de::DeserializeOwned }; -#[macro_use] -extern crate diesel; - fn get_content() -> impl Filter,), Error = Rejection> + Clone where - M: Clone + Sync + Send + DeserializeOwned + 'static + M: ToMsg + Sync + Send + DeserializeOwned + 'static { body::content_length_limit(1024*64) .and(body::json::>()) @@ -42,7 +40,7 @@ where async fn main() { env_logger::init(); - let base_db = UDB::new().unwrap(); + let base_db = UDB::new(None).unwrap(); let db = warp::any().map(move || base_db.clone()); let new_client = warp::post() @@ -51,15 +49,15 @@ async fn main() { .and(db.clone()) .and_then(handlers::add_agent); - let ls = warp::get() + let get_agents = warp::get() .and(warp::path(Paths::ls)) .and(db.clone()) - .and_then(handlers::ls); + .and_then(handlers::get_agents); /* - let get_jobs = warp::get() - .and(warp::path(Paths::get_jobs)) - .and(db.clone()) - .and_then(handlers::get_jobs); + let get_jobs = warp::get() + .and(warp::path(Paths::get_jobs)) + .and(db.clone()) + .and_then(handlers::get_jobs); let set_jobs = warp::post() .and(warp::path(Paths::set_jobs)) @@ -88,7 +86,7 @@ async fn main() { ; let auth_zone = auth_token - .and(ls + .and(get_agents // .or(set_jobs) // .or(get_job_results) ) @@ -100,3 +98,14 @@ async fn main() { warp::serve(routes) .run(([0,0,0,0], MASTER_PORT)).await; } + + +#[cfg(test)] +mod tests { + use super::*; +/* + #[tokio::test] + async fn test_gather() { + } +*/ +} diff --git a/lib/u_db/diesel.toml b/diesel.toml similarity index 73% rename from lib/u_db/diesel.toml rename to diesel.toml index 92267c8..09a260f 100644 --- a/lib/u_db/diesel.toml +++ b/diesel.toml @@ -2,4 +2,4 @@ # see diesel.rs/guides/configuring-diesel-cli [print_schema] -file = "src/schema.rs" +file = "lib/u_lib/src/models/schema.rs" diff --git a/lib/u_db/Cargo.toml b/lib/u_db/Cargo.toml deleted file mode 100644 index 3d2f64c..0000000 --- a/lib/u_db/Cargo.toml +++ /dev/null @@ -1,17 +0,0 @@ -[package] -name = "u_db" -version = "0.1.0" -authors = ["plazmoid "] -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] - -[dependencies.diesel] -features = ["sqlite"] -version = "1.4.5" - -[dependencies.serde] -features = ["derive"] -version = "1.0.114" diff --git a/lib/u_db/src/models/mod.rs b/lib/u_db/src/models/mod.rs deleted file mode 100644 index f9d3c9e..0000000 --- a/lib/u_db/src/models/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod agent; - -pub use agent::*; \ No newline at end of file diff --git a/lib/u_lib/Cargo.toml b/lib/u_lib/Cargo.toml index 3a528a6..6bfe371 100644 --- a/lib/u_lib/Cargo.toml +++ b/lib/u_lib/Cargo.toml @@ -17,6 +17,5 @@ reqwest = { version = "0.10.7", features = ["json"] } futures = "0.3.5" guess_host_triple = "0.1.2" -[dependencies.u_db] -path = "../u_db" -version = "*" \ No newline at end of file +[dependencies.diesel] +version = "1.4.5" \ No newline at end of file diff --git a/lib/u_lib/src/api.rs b/lib/u_lib/src/api.rs index 734ce54..58c71c5 100644 --- a/lib/u_lib/src/api.rs +++ b/lib/u_lib/src/api.rs @@ -4,6 +4,7 @@ use crate::{ MASTER_SERVER, MASTER_PORT, contracts::*, + models::*, UResult, UError }; @@ -29,7 +30,7 @@ macro_rules! get_result { response .json::>() .await - .map(|msg| msg.into_item().into_owned()) + .map(|msg| msg.into_item()) .map_err(|e| UError::from(e)) } }; @@ -152,9 +153,9 @@ impl ClientHandler { // method basic_path(json/query param; additional_url_param) -> return value // A - admin only // client listing (A) -build_handler!(GET ls() -> ItemWrap>); +build_handler!(GET ls() -> ItemWrap>); // get jobs for client himself (A: id=client_id) -//build_handler!(GET get_jobs() -> JobMetaStorage); +build_handler!(GET get_jobs() -> ItemWrap>); // add client to server's db build_handler!(POST init(IAgent) -> RawMsg); // ??? diff --git a/lib/u_lib/src/contracts/agent.rs b/lib/u_lib/src/contracts/agent.rs index 707e204..3fd681c 100644 --- a/lib/u_lib/src/contracts/agent.rs +++ b/lib/u_lib/src/contracts/agent.rs @@ -6,9 +6,9 @@ use crate::{ contracts::*, UID, exec_job, - utils::vec_to_string + utils::vec_to_string, + models::* }; -use u_db::IAgent; use guess_host_triple::guess_host_triple; @@ -51,7 +51,7 @@ mod tests { let cli_info = gather().await; assert_eq!( &cli_info.username, - "root" + "plazmoid" ) } diff --git a/lib/u_lib/src/contracts/jobs.rs b/lib/u_lib/src/contracts/jobs.rs index 08994b5..240dc6d 100644 --- a/lib/u_lib/src/contracts/jobs.rs +++ b/lib/u_lib/src/contracts/jobs.rs @@ -343,7 +343,7 @@ mod tests { let job_result = exec_job(job.clone()).await.unwrap(); assert_eq!( vec_to_string(&job_result.data.unwrap()?.stdout).trim(), - "root" + "plazmoid" ); Ok(()) } diff --git a/lib/u_lib/src/contracts/messaging.rs b/lib/u_lib/src/contracts/messaging.rs index 72db16e..cd5b44b 100644 --- a/lib/u_lib/src/contracts/messaging.rs +++ b/lib/u_lib/src/contracts/messaging.rs @@ -1,21 +1,23 @@ -use uuid::Uuid; use serde::{ Serialize, Deserialize, de::DeserializeOwned, }; use std::{ - borrow::Cow + borrow::Cow, }; use crate::{UID, Uid}; -pub trait ToMsg: Clone + Serialize + DeserializeOwned { +pub trait ToMsg: Clone { //+ Serialize + DeserializeOwned { fn as_message<'m>(&'m self) -> Message<'m, Self> where Cow<'m, Self>: From<&'m Self> { Message::new(self) } } +// 1. Cow<'_, ItemWrap> - failed, Message::new needs val or ref +// 2. ItemWrap> - can't impl From> for Cow + #[derive(Serialize, Deserialize, Debug)] pub struct Message<'cow, I> where I: ToMsg { @@ -34,27 +36,21 @@ impl<'cow, I> Message<'cow, I> } } - pub fn into_item(self) -> Cow<'cow, I> { - self.item + pub fn into_item(self) -> I { + self.item.into_owned() } } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct RawMsg(pub String); +impl ToMsg for Vec {} + -// because can't impl From> for Cow #[derive(Serialize, Deserialize, Debug, Clone)] pub struct ItemWrap(pub T); -impl ItemWrap { - pub fn into_inner(self) -> T { - self.0 - } -} - impl ToMsg for ItemWrap {} -//impl ToMsg for Vec {} impl<'cow, T: ToMsg> From> for Cow<'cow, ItemWrap> { fn from(obj: ItemWrap) -> Cow<'cow, ItemWrap> { diff --git a/lib/u_lib/src/contracts/mod.rs b/lib/u_lib/src/contracts/mod.rs index 1cbe2bc..4399f6e 100644 --- a/lib/u_lib/src/contracts/mod.rs +++ b/lib/u_lib/src/contracts/mod.rs @@ -10,7 +10,7 @@ pub use { use std::{ borrow::Cow }; -use u_db::*; +use crate::models::*; macro_rules! to_message { ($($type:ty),+) => { $( @@ -33,4 +33,4 @@ macro_rules! to_message { } } -to_message!(IAgent, QAgent, RawMsg, JobMeta, JobResult); +to_message!(IAgent, Agent, RawMsg, JobMeta, JobResult); diff --git a/lib/u_lib/src/lib.rs b/lib/u_lib/src/lib.rs index 2811811..dba6417 100644 --- a/lib/u_lib/src/lib.rs +++ b/lib/u_lib/src/lib.rs @@ -4,13 +4,15 @@ pub mod utils; pub mod errors; pub mod contracts; pub mod api; +pub mod models; pub use { utils::*, config::*, executor::*, errors::*, + models::* }; #[macro_use] -extern crate lazy_static; +extern crate lazy_static; \ No newline at end of file diff --git a/lib/u_db/src/models/agent.rs b/lib/u_lib/src/models/agent.rs similarity index 94% rename from lib/u_db/src/models/agent.rs rename to lib/u_lib/src/models/agent.rs index ac9cbcf..2a8fe37 100644 --- a/lib/u_db/src/models/agent.rs +++ b/lib/u_lib/src/models/agent.rs @@ -7,14 +7,14 @@ use diesel::{ Identifiable, Insertable }; -use crate::schema::*; +use crate::models::schema::*; type Uid = String; //belongs_to #[derive(Clone, Debug, Serialize, Deserialize, Identifiable, Queryable)] #[table_name = "agents"] -pub struct QAgent { +pub struct Agent { pub alias: Option, pub agent_id: Uid, pub hostname: String, diff --git a/lib/u_db/src/lib.rs b/lib/u_lib/src/models/mod.rs similarity index 63% rename from lib/u_db/src/lib.rs rename to lib/u_lib/src/models/mod.rs index 3597cc3..9119f8a 100644 --- a/lib/u_db/src/lib.rs +++ b/lib/u_lib/src/models/mod.rs @@ -1,7 +1,8 @@ -#[macro_use] -extern crate diesel; - -mod models; +mod agent; pub mod schema; -pub use models::*; \ No newline at end of file + +pub use agent::*; + +#[macro_use] +extern crate diesel; diff --git a/lib/u_db/src/schema.rs b/lib/u_lib/src/models/schema.rs similarity index 100% rename from lib/u_db/src/schema.rs rename to lib/u_lib/src/models/schema.rs diff --git a/lib/u_db/migrations/2020-10-24-111622_create_all/down.sql b/migrations/2020-10-24-111622_create_all/down.sql similarity index 100% rename from lib/u_db/migrations/2020-10-24-111622_create_all/down.sql rename to migrations/2020-10-24-111622_create_all/down.sql diff --git a/lib/u_db/migrations/2020-10-24-111622_create_all/up.sql b/migrations/2020-10-24-111622_create_all/up.sql similarity index 100% rename from lib/u_db/migrations/2020-10-24-111622_create_all/up.sql rename to migrations/2020-10-24-111622_create_all/up.sql