fucked up with itemwrap but finished db

4-update-check
plazmoid 4 years ago
parent 0b667882f1
commit 3fc7da93ec
  1. 1
      .env
  2. 1
      Cargo.toml
  3. 13
      bin/u_agent/src/main.rs
  4. 2
      bin/u_panel/src/main.rs
  5. 3
      bin/u_server/Cargo.toml
  6. 47
      bin/u_server/src/db.rs
  7. 21
      bin/u_server/src/handlers.rs
  8. 27
      bin/u_server/src/main.rs
  9. 2
      diesel.toml
  10. 17
      lib/u_db/Cargo.toml
  11. 3
      lib/u_db/src/models/mod.rs
  12. 5
      lib/u_lib/Cargo.toml
  13. 7
      lib/u_lib/src/api.rs
  14. 6
      lib/u_lib/src/contracts/agent.rs
  15. 2
      lib/u_lib/src/contracts/jobs.rs
  16. 22
      lib/u_lib/src/contracts/messaging.rs
  17. 4
      lib/u_lib/src/contracts/mod.rs
  18. 2
      lib/u_lib/src/lib.rs
  19. 4
      lib/u_lib/src/models/agent.rs
  20. 11
      lib/u_lib/src/models/mod.rs
  21. 0
      lib/u_lib/src/models/schema.rs
  22. 0
      migrations/2020-10-24-111622_create_all/down.sql
  23. 0
      migrations/2020-10-24-111622_create_all/up.sql

@ -0,0 +1 @@
DATABASE_URL=./bin/u_server/u_server.db

@ -5,7 +5,6 @@ members = [
"bin/u_run", "bin/u_run",
"bin/u_server", "bin/u_server",
"lib/u_lib", "lib/u_lib",
"lib/u_db"
] ]
[profile.release] [profile.release]

@ -12,10 +12,7 @@ use {
std::time::Duration, std::time::Duration,
std::env, std::env,
u_lib::{ u_lib::{
client::{ api::ClientHandler,
Agent,
network::ClientHandler
},
contracts::*, contracts::*,
send_jobs_to_executor, send_jobs_to_executor,
}, },
@ -26,9 +23,9 @@ async fn main() {
//daemonize(); //daemonize();
let arg_ip = env::args().nth(1); let arg_ip = env::args().nth(1);
let instance = ClientHandler::new(arg_ip); 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); retry_until_ok!(instance.init(&cli_info).await);
loop { loop {/*
let jobs = retry_until_ok!(instance.get_jobs().await); let jobs = retry_until_ok!(instance.get_jobs().await);
if jobs.0.len() > 0 { if jobs.0.len() > 0 {
let result = send_jobs_to_executor(jobs let result = send_jobs_to_executor(jobs
@ -39,7 +36,9 @@ async fn main() {
retry_until_ok!(instance.report( retry_until_ok!(instance.report(
ItemWrap(result.into_iter().map(|r| r.unwrap()).collect()) ItemWrap(result.into_iter().map(|r| r.unwrap()).collect())
).await) ).await)
} }*/
//let jobs = retry_until_ok!(instance.get_jobs().await);
//println!("{:?}", jobs);
sleep(Duration::from_secs(2)); sleep(Duration::from_secs(2));
} }
} }

@ -1,6 +1,6 @@
use std::env::args; use std::env::args;
use u_lib::{ use u_lib::{
client::network::ClientHandler, api::ClientHandler,
//client::* //client::*
}; };

@ -27,6 +27,3 @@ version = "0.2.22"
path = "../../lib/u_lib" path = "../../lib/u_lib"
version = "*" version = "*"
[dependencies.u_db]
path = "../../lib/u_db"
version = "*"

@ -9,10 +9,9 @@ use std::{
}; };
use crate::{ use crate::{
errors::USrvResult, errors::USrvResult
db::*
}; };
use u_db::schema; use u_lib::models::*;
pub type Storage = Arc<Mutex<UDB>>; pub type Storage = Arc<Mutex<UDB>>;
@ -21,9 +20,12 @@ pub struct UDB {
} }
impl UDB { impl UDB {
pub fn new() -> USrvResult<Storage> { pub fn new(path: Option<String>) -> USrvResult<Storage> {
dotenv()?; 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)?; let conn = SqliteConnection::establish(&db_path)?;
conn.execute("PRAGMA foreign_keys = ON;")?; conn.execute("PRAGMA foreign_keys = ON;")?;
let instance = UDB { let instance = UDB {
@ -40,10 +42,41 @@ impl UDB {
Ok(()) Ok(())
} }
pub fn get_agents(&self) -> USrvResult<Vec<QAgent>> { pub fn get_agents(&self) -> USrvResult<Vec<Agent>> {
use schema::agents; use schema::agents;
let result = agents::table let result = agents::table
.load::<QAgent>(&self.conn)?; .load::<Agent>(&self.conn)?;
Ok(result) Ok(result)
} }
} }
#[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()
)
}
}

@ -1,4 +1,7 @@
use u_lib::contracts::*; use u_lib::{
contracts::*,
models::*
};
use warp::{ use warp::{
Rejection, Rejection,
Reply, Reply,
@ -6,8 +9,7 @@ use warp::{
http::StatusCode http::StatusCode
}; };
use crate::db::{ use crate::db::{
Storage, Storage
IAgent
}; };
pub async fn add_agent( pub async fn add_agent(
@ -87,19 +89,16 @@ pub async fn set_jobs(
} }
*/ */
pub async fn ls(db: Storage) -> Result<impl Reply, Rejection> { pub async fn get_agents(db: Storage) -> Result<impl Reply, Rejection> {
let result = db.lock().unwrap().get_agents(); let result = db.lock().unwrap().get_agents();
/*
let mut result: Vec<Agent> = Vec::with_capacity(clients.len());
for cli in clients.values() {
result.push(cli.client_info.clone());
}
*/
match result { match result {
Ok(r) => Ok(warp::reply::json( Ok(r) => Ok(warp::reply::json(
&ItemWrap(r).as_message() &ItemWrap(r).as_message()
)), )),
Err(e) => error!(e) Err(e) => {
error!("{}", &e);
Err(warp::reject())
}
} }
} }

@ -16,22 +16,20 @@ use env_logger;
use u_lib::{ use u_lib::{
MASTER_PORT, MASTER_PORT,
contracts::*, contracts::*,
api::Paths api::Paths,
models::*
}; };
use db::*; use db::*;
use serde::{ use serde::{
de::DeserializeOwned de::DeserializeOwned
}; };
#[macro_use]
extern crate diesel;
fn get_content<M>() fn get_content<M>()
-> impl Filter<Extract = (Message<'static, M>,), -> impl Filter<Extract = (Message<'static, M>,),
Error = Rejection> + Clone Error = Rejection> + Clone
where where
M: Clone + Sync + Send + DeserializeOwned + 'static M: ToMsg + Sync + Send + DeserializeOwned + 'static
{ {
body::content_length_limit(1024*64) body::content_length_limit(1024*64)
.and(body::json::<Message<M>>()) .and(body::json::<Message<M>>())
@ -42,7 +40,7 @@ where
async fn main() { async fn main() {
env_logger::init(); 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 db = warp::any().map(move || base_db.clone());
let new_client = warp::post() let new_client = warp::post()
@ -51,10 +49,10 @@ async fn main() {
.and(db.clone()) .and(db.clone())
.and_then(handlers::add_agent); .and_then(handlers::add_agent);
let ls = warp::get() let get_agents = warp::get()
.and(warp::path(Paths::ls)) .and(warp::path(Paths::ls))
.and(db.clone()) .and(db.clone())
.and_then(handlers::ls); .and_then(handlers::get_agents);
/* /*
let get_jobs = warp::get() let get_jobs = warp::get()
.and(warp::path(Paths::get_jobs)) .and(warp::path(Paths::get_jobs))
@ -88,7 +86,7 @@ async fn main() {
; ;
let auth_zone = auth_token let auth_zone = auth_token
.and(ls .and(get_agents
// .or(set_jobs) // .or(set_jobs)
// .or(get_job_results) // .or(get_job_results)
) )
@ -100,3 +98,14 @@ async fn main() {
warp::serve(routes) warp::serve(routes)
.run(([0,0,0,0], MASTER_PORT)).await; .run(([0,0,0,0], MASTER_PORT)).await;
} }
#[cfg(test)]
mod tests {
use super::*;
/*
#[tokio::test]
async fn test_gather() {
}
*/
}

@ -2,4 +2,4 @@
# see diesel.rs/guides/configuring-diesel-cli # see diesel.rs/guides/configuring-diesel-cli
[print_schema] [print_schema]
file = "src/schema.rs" file = "lib/u_lib/src/models/schema.rs"

@ -1,17 +0,0 @@
[package]
name = "u_db"
version = "0.1.0"
authors = ["plazmoid <kronos44@mail.ru>"]
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"

@ -1,3 +0,0 @@
mod agent;
pub use agent::*;

@ -17,6 +17,5 @@ reqwest = { version = "0.10.7", features = ["json"] }
futures = "0.3.5" futures = "0.3.5"
guess_host_triple = "0.1.2" guess_host_triple = "0.1.2"
[dependencies.u_db] [dependencies.diesel]
path = "../u_db" version = "1.4.5"
version = "*"

@ -4,6 +4,7 @@ use crate::{
MASTER_SERVER, MASTER_SERVER,
MASTER_PORT, MASTER_PORT,
contracts::*, contracts::*,
models::*,
UResult, UResult,
UError UError
}; };
@ -29,7 +30,7 @@ macro_rules! get_result {
response response
.json::<Message<$result>>() .json::<Message<$result>>()
.await .await
.map(|msg| msg.into_item().into_owned()) .map(|msg| msg.into_item())
.map_err(|e| UError::from(e)) .map_err(|e| UError::from(e))
} }
}; };
@ -152,9 +153,9 @@ impl ClientHandler {
// method basic_path(json/query param; additional_url_param) -> return value // method basic_path(json/query param; additional_url_param) -> return value
// A - admin only // A - admin only
// client listing (A) // client listing (A)
build_handler!(GET ls() -> ItemWrap<Vec<QAgent>>); build_handler!(GET ls() -> ItemWrap<Vec<Agent>>);
// get jobs for client himself (A: id=client_id) // get jobs for client himself (A: id=client_id)
//build_handler!(GET get_jobs() -> JobMetaStorage); build_handler!(GET get_jobs() -> ItemWrap<Vec<JobMeta>>);
// add client to server's db // add client to server's db
build_handler!(POST init(IAgent) -> RawMsg); build_handler!(POST init(IAgent) -> RawMsg);
// ??? // ???

@ -6,9 +6,9 @@ use crate::{
contracts::*, contracts::*,
UID, UID,
exec_job, exec_job,
utils::vec_to_string utils::vec_to_string,
models::*
}; };
use u_db::IAgent;
use guess_host_triple::guess_host_triple; use guess_host_triple::guess_host_triple;
@ -51,7 +51,7 @@ mod tests {
let cli_info = gather().await; let cli_info = gather().await;
assert_eq!( assert_eq!(
&cli_info.username, &cli_info.username,
"root" "plazmoid"
) )
} }

@ -343,7 +343,7 @@ mod tests {
let job_result = exec_job(job.clone()).await.unwrap(); let job_result = exec_job(job.clone()).await.unwrap();
assert_eq!( assert_eq!(
vec_to_string(&job_result.data.unwrap()?.stdout).trim(), vec_to_string(&job_result.data.unwrap()?.stdout).trim(),
"root" "plazmoid"
); );
Ok(()) Ok(())
} }

@ -1,21 +1,23 @@
use uuid::Uuid;
use serde::{ use serde::{
Serialize, Serialize,
Deserialize, Deserialize,
de::DeserializeOwned, de::DeserializeOwned,
}; };
use std::{ use std::{
borrow::Cow borrow::Cow,
}; };
use crate::{UID, Uid}; 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> fn as_message<'m>(&'m self) -> Message<'m, Self>
where Cow<'m, Self>: From<&'m Self> { where Cow<'m, Self>: From<&'m Self> {
Message::new(self) Message::new(self)
} }
} }
// 1. Cow<'_, ItemWrap<I>> - failed, Message::new needs val or ref
// 2. ItemWrap<Cow<'_, I>> - can't impl From<Vec<...>> for Cow
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct Message<'cow, I> pub struct Message<'cow, I>
where I: ToMsg { where I: ToMsg {
@ -34,27 +36,21 @@ impl<'cow, I> Message<'cow, I>
} }
} }
pub fn into_item(self) -> Cow<'cow, I> { pub fn into_item(self) -> I {
self.item self.item.into_owned()
} }
} }
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RawMsg(pub String); pub struct RawMsg(pub String);
impl<T: ToMsg> ToMsg for Vec<T> {}
// because can't impl From<Vec<...>> for Cow
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ItemWrap<T: ToMsg>(pub T); pub struct ItemWrap<T: ToMsg>(pub T);
impl<T: ToMsg> ItemWrap<T> {
pub fn into_inner(self) -> T {
self.0
}
}
impl<T: ToMsg> ToMsg for ItemWrap<T> {} impl<T: ToMsg> ToMsg for ItemWrap<T> {}
//impl<T: ToMsg> ToMsg for Vec<T> {}
impl<'cow, T: ToMsg> From<ItemWrap<T>> for Cow<'cow, ItemWrap<T>> { impl<'cow, T: ToMsg> From<ItemWrap<T>> for Cow<'cow, ItemWrap<T>> {
fn from(obj: ItemWrap<T>) -> Cow<'cow, ItemWrap<T>> { fn from(obj: ItemWrap<T>) -> Cow<'cow, ItemWrap<T>> {

@ -10,7 +10,7 @@ pub use {
use std::{ use std::{
borrow::Cow borrow::Cow
}; };
use u_db::*; use crate::models::*;
macro_rules! to_message { macro_rules! to_message {
($($type:ty),+) => { $( ($($type:ty),+) => { $(
@ -33,4 +33,4 @@ macro_rules! to_message {
} }
} }
to_message!(IAgent, QAgent, RawMsg, JobMeta, JobResult); to_message!(IAgent, Agent, RawMsg, JobMeta, JobResult);

@ -4,12 +4,14 @@ pub mod utils;
pub mod errors; pub mod errors;
pub mod contracts; pub mod contracts;
pub mod api; pub mod api;
pub mod models;
pub use { pub use {
utils::*, utils::*,
config::*, config::*,
executor::*, executor::*,
errors::*, errors::*,
models::*
}; };
#[macro_use] #[macro_use]

@ -7,14 +7,14 @@ use diesel::{
Identifiable, Identifiable,
Insertable Insertable
}; };
use crate::schema::*; use crate::models::schema::*;
type Uid = String; type Uid = String;
//belongs_to //belongs_to
#[derive(Clone, Debug, Serialize, Deserialize, Identifiable, Queryable)] #[derive(Clone, Debug, Serialize, Deserialize, Identifiable, Queryable)]
#[table_name = "agents"] #[table_name = "agents"]
pub struct QAgent { pub struct Agent {
pub alias: Option<String>, pub alias: Option<String>,
pub agent_id: Uid, pub agent_id: Uid,
pub hostname: String, pub hostname: String,

@ -1,7 +1,8 @@
#[macro_use] mod agent;
extern crate diesel;
mod models;
pub mod schema; pub mod schema;
pub use models::*;
pub use agent::*;
#[macro_use]
extern crate diesel;
Loading…
Cancel
Save