From 0b667882f1917c7f8c72e8725644d4507eb1d4ac Mon Sep 17 00:00:00 2001 From: plazmoid Date: Sun, 22 Nov 2020 00:08:20 +0500 Subject: [PATCH] refactored db --- .gitignore | 2 + Cargo.toml | 3 +- bin/u_server/Cargo.toml | 10 +-- bin/u_server/src/{db => }/db.rs | 11 ++- bin/u_server/src/db/mod.rs | 6 -- bin/u_server/src/handlers.rs | 17 +++-- bin/u_server/src/main.rs | 26 ++++--- lib/u_db/Cargo.toml | 17 +++++ {bin/u_server => lib/u_db}/diesel.toml | 2 +- .../2020-10-24-111622_create_all/down.sql | 4 + .../2020-10-24-111622_create_all/up.sql | 0 lib/u_db/src/lib.rs | 7 ++ .../models.rs => lib/u_db/src/models/agent.rs | 27 +++---- lib/u_db/src/models/mod.rs | 3 + .../src/db => lib/u_db/src}/schema.rs | 0 lib/u_lib/Cargo.toml | 6 +- lib/u_lib/src/{network.rs => api.rs} | 4 +- lib/u_lib/src/contracts/agent.rs | 76 +++++++------------ lib/u_lib/src/contracts/messaging.rs | 30 +++----- lib/u_lib/src/contracts/mod.rs | 4 +- lib/u_lib/src/lib.rs | 2 +- 21 files changed, 140 insertions(+), 117 deletions(-) create mode 100644 .gitignore rename bin/u_server/src/{db => }/db.rs (78%) delete mode 100644 bin/u_server/src/db/mod.rs create mode 100644 lib/u_db/Cargo.toml rename {bin/u_server => lib/u_db}/diesel.toml (81%) create mode 100644 lib/u_db/migrations/2020-10-24-111622_create_all/down.sql rename {bin/u_server => lib/u_db}/migrations/2020-10-24-111622_create_all/up.sql (100%) create mode 100644 lib/u_db/src/lib.rs rename bin/u_server/src/db/models.rs => lib/u_db/src/models/agent.rs (66%) create mode 100644 lib/u_db/src/models/mod.rs rename {bin/u_server/src/db => lib/u_db/src}/schema.rs (100%) rename lib/u_lib/src/{network.rs => api.rs} (97%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/Cargo.toml b/Cargo.toml index 42d4a66..a90044d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,8 @@ members = [ "bin/u_panel", "bin/u_run", "bin/u_server", - "lib/u_lib" + "lib/u_lib", + "lib/u_db" ] [profile.release] diff --git a/bin/u_server/Cargo.toml b/bin/u_server/Cargo.toml index 53fa5a4..8f8e5e3 100644 --- a/bin/u_server/Cargo.toml +++ b/bin/u_server/Cargo.toml @@ -12,13 +12,9 @@ anyhow = "*" warp = "0.2.4" [dependencies.diesel] -features = ["sqlite", "uuid"] +features = ["sqlite"] version = "1.4.5" -[dependencies.uuid] -features = ["serde", "v4"] -version = "*" - [dependencies.serde] features = ["derive"] version = "1.0.114" @@ -30,3 +26,7 @@ version = "0.2.22" [dependencies.u_lib] path = "../../lib/u_lib" version = "*" + +[dependencies.u_db] +path = "../../lib/u_db" +version = "*" diff --git a/bin/u_server/src/db/db.rs b/bin/u_server/src/db.rs similarity index 78% rename from bin/u_server/src/db/db.rs rename to bin/u_server/src/db.rs index cc55886..6a56621 100644 --- a/bin/u_server/src/db/db.rs +++ b/bin/u_server/src/db.rs @@ -10,9 +10,9 @@ use std::{ use crate::{ errors::USrvResult, - db::IAgent + db::* }; -use super::schema; +use u_db::schema; pub type Storage = Arc>; @@ -39,4 +39,11 @@ impl UDB { .execute(&self.conn)?; Ok(()) } + + pub fn get_agents(&self) -> USrvResult> { + use schema::agents; + let result = agents::table + .load::(&self.conn)?; + Ok(result) + } } \ No newline at end of file diff --git a/bin/u_server/src/db/mod.rs b/bin/u_server/src/db/mod.rs deleted file mode 100644 index c761827..0000000 --- a/bin/u_server/src/db/mod.rs +++ /dev/null @@ -1,6 +0,0 @@ -pub mod db; -mod schema; -mod models; - -pub use db::*; -pub use models::*; \ No newline at end of file diff --git a/bin/u_server/src/handlers.rs b/bin/u_server/src/handlers.rs index 227f14e..c8abed0 100644 --- a/bin/u_server/src/handlers.rs +++ b/bin/u_server/src/handlers.rs @@ -86,17 +86,22 @@ pub async fn set_jobs( Ok(()) } - +*/ pub async fn ls(db: Storage) -> Result { - let clients = db.clients().await; + 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()); } - Ok(warp::reply::json( - &ItemWrap(result).as_message() - )) -}*/ + */ + match result { + Ok(r) => Ok(warp::reply::json( + &ItemWrap(r).as_message() + )), + Err(e) => error!(e) + } +} pub async fn dummy() -> Result { Ok(String::from("ok")) diff --git a/bin/u_server/src/main.rs b/bin/u_server/src/main.rs index affa74b..9793645 100644 --- a/bin/u_server/src/main.rs +++ b/bin/u_server/src/main.rs @@ -8,11 +8,15 @@ use warp::{ Reply, body }; + +#[macro_use] +extern crate log; use env_logger; + use u_lib::{ MASTER_PORT, contracts::*, - network::Paths + api::Paths }; use db::*; use serde::{ @@ -43,15 +47,15 @@ async fn main() { let new_client = warp::post() .and(warp::path(Paths::init)) - //.and(get_content::()) - //.and(db.clone()) - .and_then(handlers::dummy); - /* - let ls = warp::get() - .and(warp::path(Paths::ls)) - .and(db.clone()) - .and_then(handlers::ls); + .and(get_content::()) + .and(db.clone()) + .and_then(handlers::add_agent); + let ls = warp::get() + .and(warp::path(Paths::ls)) + .and(db.clone()) + .and_then(handlers::ls); +/* let get_jobs = warp::get() .and(warp::path(Paths::get_jobs)) .and(db.clone()) @@ -84,10 +88,10 @@ async fn main() { ; let auth_zone = auth_token -// .and(ls + .and(ls // .or(set_jobs) // .or(get_job_results) -// ) + ) ; let routes = auth_zone diff --git a/lib/u_db/Cargo.toml b/lib/u_db/Cargo.toml new file mode 100644 index 0000000..3d2f64c --- /dev/null +++ b/lib/u_db/Cargo.toml @@ -0,0 +1,17 @@ +[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/bin/u_server/diesel.toml b/lib/u_db/diesel.toml similarity index 81% rename from bin/u_server/diesel.toml rename to lib/u_db/diesel.toml index 71215db..92267c8 100644 --- a/bin/u_server/diesel.toml +++ b/lib/u_db/diesel.toml @@ -2,4 +2,4 @@ # see diesel.rs/guides/configuring-diesel-cli [print_schema] -file = "src/db/schema.rs" +file = "src/schema.rs" diff --git a/lib/u_db/migrations/2020-10-24-111622_create_all/down.sql b/lib/u_db/migrations/2020-10-24-111622_create_all/down.sql new file mode 100644 index 0000000..7d59dc2 --- /dev/null +++ b/lib/u_db/migrations/2020-10-24-111622_create_all/down.sql @@ -0,0 +1,4 @@ +DROP TABLE agents; +DROP TABLE ip_addrs; +DROP TABLE jobs; +DROP TABLE results; \ No newline at end of file diff --git a/bin/u_server/migrations/2020-10-24-111622_create_all/up.sql b/lib/u_db/migrations/2020-10-24-111622_create_all/up.sql similarity index 100% rename from bin/u_server/migrations/2020-10-24-111622_create_all/up.sql rename to lib/u_db/migrations/2020-10-24-111622_create_all/up.sql diff --git a/lib/u_db/src/lib.rs b/lib/u_db/src/lib.rs new file mode 100644 index 0000000..3597cc3 --- /dev/null +++ b/lib/u_db/src/lib.rs @@ -0,0 +1,7 @@ +#[macro_use] +extern crate diesel; + +mod models; +pub mod schema; + +pub use models::*; \ No newline at end of file diff --git a/bin/u_server/src/db/models.rs b/lib/u_db/src/models/agent.rs similarity index 66% rename from bin/u_server/src/db/models.rs rename to lib/u_db/src/models/agent.rs index a6d273a..ac9cbcf 100644 --- a/bin/u_server/src/db/models.rs +++ b/lib/u_db/src/models/agent.rs @@ -1,18 +1,18 @@ +use serde::{ + Serialize, + Deserialize +}; use diesel::{ - Insertable, Queryable, - Identifiable -}; -use serde::{ - Deserialize, - Serialize + Identifiable, + Insertable }; -use u_lib::Uid; -use std::time::SystemTime; -use crate::db::schema::*; +use crate::schema::*; + +type Uid = String; //belongs_to -#[derive(Identifiable, Queryable, Serialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Identifiable, Queryable)] #[table_name = "agents"] pub struct QAgent { pub alias: Option, @@ -21,17 +21,18 @@ pub struct QAgent { pub id: i32, pub is_root: bool, pub is_root_allowed: bool, - pub last_active: SystemTime, + pub last_active: String, pub platform: String, - pub regtime: SystemTime, + pub regtime: String, pub status: Option, pub token: Option, pub username: String } -#[derive(Insertable, Deserialize, Clone)] +#[derive(Clone, Debug, Serialize, Deserialize, Insertable)] #[table_name = "agents"] pub struct IAgent { + pub alias: Option, pub agent_id: Uid, pub hostname: String, pub is_root: bool, diff --git a/lib/u_db/src/models/mod.rs b/lib/u_db/src/models/mod.rs new file mode 100644 index 0000000..f9d3c9e --- /dev/null +++ b/lib/u_db/src/models/mod.rs @@ -0,0 +1,3 @@ +mod agent; + +pub use agent::*; \ No newline at end of file diff --git a/bin/u_server/src/db/schema.rs b/lib/u_db/src/schema.rs similarity index 100% rename from bin/u_server/src/db/schema.rs rename to lib/u_db/src/schema.rs diff --git a/lib/u_lib/Cargo.toml b/lib/u_lib/Cargo.toml index 2b0dbce..3a528a6 100644 --- a/lib/u_lib/Cargo.toml +++ b/lib/u_lib/Cargo.toml @@ -15,4 +15,8 @@ lazy_static = "1.4.0" tokio = { version = "0.2.22", features = ["macros", "process"] } reqwest = { version = "0.10.7", features = ["json"] } futures = "0.3.5" -guess_host_triple = "0.1.2" \ No newline at end of file +guess_host_triple = "0.1.2" + +[dependencies.u_db] +path = "../u_db" +version = "*" \ No newline at end of file diff --git a/lib/u_lib/src/network.rs b/lib/u_lib/src/api.rs similarity index 97% rename from lib/u_lib/src/network.rs rename to lib/u_lib/src/api.rs index 9217e19..734ce54 100644 --- a/lib/u_lib/src/network.rs +++ b/lib/u_lib/src/api.rs @@ -152,11 +152,11 @@ impl ClientHandler { // method basic_path(json/query param; additional_url_param) -> return value // A - admin only // client listing (A) -// build_handler!(GET ls() -> Vec); +build_handler!(GET ls() -> ItemWrap>); // get jobs for client himself (A: id=client_id) //build_handler!(GET get_jobs() -> JobMetaStorage); // add client to server's db -build_handler!(POST init(Agent) -> RawMsg); +build_handler!(POST init(IAgent) -> RawMsg); // ??? /*build_handler!(POST del() -> ()); // set jobs for client (A) diff --git a/lib/u_lib/src/contracts/agent.rs b/lib/u_lib/src/contracts/agent.rs index fd30166..707e204 100644 --- a/lib/u_lib/src/contracts/agent.rs +++ b/lib/u_lib/src/contracts/agent.rs @@ -2,63 +2,43 @@ use std::{ collections::HashMap, time::SystemTime }; - -use serde::{ - Deserialize, - Serialize -}; - -use guess_host_triple::guess_host_triple; - use crate::{ contracts::*, UID, - Uid, exec_job, utils::vec_to_string }; +use u_db::IAgent; -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct Agent { - pub agent_id: Uid, - pub hostname: String, - pub is_root: bool, - pub is_root_allowed: bool, - pub platform: String, - pub status: Option, - pub token: Option, - pub username: String -} - -impl Agent { - pub async fn gather() -> Self { - - async fn run_cmd_fast(cmd: String) -> String { - let job = exec_job( - JobMeta::from_shell_arc(cmd) - ).await; - let job_result = match job.unwrap().data.unwrap() { - Ok(output) => output.multiline(), - Err(e) => e.to_string() - }; - JobOutput::from_multiline(&job_result) - .map(|o| vec_to_string(&o.into_appropriate())) - .unwrap_or(job_result) - } +use guess_host_triple::guess_host_triple; - #[cfg(unix)] - Agent { - agent_id: UID.clone().to_string(), - hostname: run_cmd_fast("hostname".to_string()).await, - is_root: &run_cmd_fast("id -u".to_string()).await == "0", - is_root_allowed: false, //TODO - platform: guess_host_triple().unwrap_or("Error").to_string(), - status: None, //TODO - token: None, //TODO - username: run_cmd_fast("id -un".to_string()).await, - } +pub async fn gather() -> IAgent { + + async fn run_cmd_fast(cmd: String) -> String { + let job = exec_job( + JobMeta::from_shell_arc(cmd) + ).await; + let job_result = match job.unwrap().data.unwrap() { + Ok(output) => output.multiline(), + Err(e) => e.to_string() + }; + JobOutput::from_multiline(&job_result) + .map(|o| vec_to_string(&o.into_appropriate())) + .unwrap_or(job_result) } + #[cfg(unix)] + IAgent { + alias: None, + agent_id: UID.clone().to_string(), + hostname: run_cmd_fast("hostname".to_string()).await, + is_root: &run_cmd_fast("id -u".to_string()).await == "0", + is_root_allowed: false, //TODO + platform: guess_host_triple().unwrap_or("Error").to_string(), + status: None, //TODO + token: None, //TODO + username: run_cmd_fast("id -un".to_string()).await, + } } @@ -68,7 +48,7 @@ mod tests { #[tokio::test] async fn test_gather() { - let cli_info = Agent::gather().await; + let cli_info = gather().await; assert_eq!( &cli_info.username, "root" diff --git a/lib/u_lib/src/contracts/messaging.rs b/lib/u_lib/src/contracts/messaging.rs index ac7ce24..72db16e 100644 --- a/lib/u_lib/src/contracts/messaging.rs +++ b/lib/u_lib/src/contracts/messaging.rs @@ -1,16 +1,15 @@ use uuid::Uuid; use serde::{ Serialize, - Deserialize + Deserialize, + de::DeserializeOwned, }; use std::{ borrow::Cow }; use crate::{UID, Uid}; - -pub trait ToMsg -where Self: Clone { +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) @@ -19,13 +18,13 @@ where Self: Clone { #[derive(Serialize, Deserialize, Debug)] pub struct Message<'cow, I> -where I: Clone { +where I: ToMsg { pub id: Uid, pub item: Cow<'cow, I> } impl<'cow, I> Message<'cow, I> - where I: Clone + where I: ToMsg { pub fn new(item: C) -> Self where C: Into> { @@ -44,31 +43,26 @@ impl<'cow, I> Message<'cow, I> pub struct RawMsg(pub String); -// because can't impl From> for Cow +// because can't impl From> for Cow #[derive(Serialize, Deserialize, Debug, Clone)] -struct ItemWrap(T); +pub struct ItemWrap(pub T); -impl ItemWrap { +impl ItemWrap { pub fn into_inner(self) -> T { self.0 } } -impl From for ItemWrap { - fn from(t: T) -> Self { - ItemWrap(t) - } -} - -impl ToMsg for ItemWrap {} +impl ToMsg for ItemWrap {} +//impl ToMsg for Vec {} -impl<'cow, T: Clone> From> for Cow<'cow, ItemWrap> { +impl<'cow, T: ToMsg> From> for Cow<'cow, ItemWrap> { fn from(obj: ItemWrap) -> Cow<'cow, ItemWrap> { Cow::Owned(obj) } } -impl<'cow, T: Clone> From<&'cow ItemWrap> for Cow<'cow, ItemWrap> { +impl<'cow, T: ToMsg> From<&'cow ItemWrap> for Cow<'cow, ItemWrap> { fn from(obj: &'cow ItemWrap) -> Cow<'cow, ItemWrap> { Cow::Borrowed(obj) } diff --git a/lib/u_lib/src/contracts/mod.rs b/lib/u_lib/src/contracts/mod.rs index 5bfffd8..1cbe2bc 100644 --- a/lib/u_lib/src/contracts/mod.rs +++ b/lib/u_lib/src/contracts/mod.rs @@ -5,12 +5,12 @@ pub mod agent; pub use { messaging::*, jobs::*, - agent::* }; use std::{ borrow::Cow }; +use u_db::*; macro_rules! to_message { ($($type:ty),+) => { $( @@ -33,4 +33,4 @@ macro_rules! to_message { } } -to_message!(Agent, RawMsg, JobMeta, JobResult); +to_message!(IAgent, QAgent, RawMsg, JobMeta, JobResult); diff --git a/lib/u_lib/src/lib.rs b/lib/u_lib/src/lib.rs index 907e650..2811811 100644 --- a/lib/u_lib/src/lib.rs +++ b/lib/u_lib/src/lib.rs @@ -3,7 +3,7 @@ pub mod config; pub mod utils; pub mod errors; pub mod contracts; -pub mod network; +pub mod api; pub use { utils::*,