refactored db

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

2
.gitignore vendored

@ -0,0 +1,2 @@
/target
**/*.rs.bk

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

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

@ -10,9 +10,9 @@ use std::{
use crate::{
errors::USrvResult,
db::IAgent
db::*
};
use super::schema;
use u_db::schema;
pub type Storage = Arc<Mutex<UDB>>;
@ -39,4 +39,11 @@ impl UDB {
.execute(&self.conn)?;
Ok(())
}
pub fn get_agents(&self) -> USrvResult<Vec<QAgent>> {
use schema::agents;
let result = agents::table
.load::<QAgent>(&self.conn)?;
Ok(result)
}
}

@ -1,6 +0,0 @@
pub mod db;
mod schema;
mod models;
pub use db::*;
pub use models::*;

@ -86,17 +86,22 @@ pub async fn set_jobs(
Ok(())
}
*/
pub async fn ls(db: Storage) -> Result<impl Reply, Rejection> {
let clients = db.clients().await;
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());
}
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<impl Reply, Rejection> {
Ok(String::from("ok"))

@ -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::<IAgent>())
//.and(db.clone())
.and_then(handlers::dummy);
/*
.and(get_content::<IAgent>())
.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

@ -0,0 +1,17 @@
[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"

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

@ -0,0 +1,4 @@
DROP TABLE agents;
DROP TABLE ip_addrs;
DROP TABLE jobs;
DROP TABLE results;

@ -0,0 +1,7 @@
#[macro_use]
extern crate diesel;
mod models;
pub mod schema;
pub use models::*;

@ -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<String>,
@ -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<String>,
pub token: Option<String>,
pub username: String
}
#[derive(Insertable, Deserialize, Clone)]
#[derive(Clone, Debug, Serialize, Deserialize, Insertable)]
#[table_name = "agents"]
pub struct IAgent {
pub alias: Option<String>,
pub agent_id: Uid,
pub hostname: String,
pub is_root: bool,

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

@ -16,3 +16,7 @@ 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"
[dependencies.u_db]
path = "../u_db"
version = "*"

@ -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<QAgent>);
build_handler!(GET ls() -> ItemWrap<Vec<QAgent>>);
// 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)

@ -2,36 +2,17 @@ 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<String>,
pub token: Option<String>,
pub username: String
}
use guess_host_triple::guess_host_triple;
impl Agent {
pub async fn gather() -> Self {
pub async fn gather() -> IAgent {
async fn run_cmd_fast(cmd: String) -> String {
let job = exec_job(
@ -47,7 +28,8 @@ impl Agent {
}
#[cfg(unix)]
Agent {
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",
@ -59,8 +41,6 @@ impl Agent {
}
}
}
#[cfg(test)]
mod tests {
@ -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"

@ -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<C>(item: C) -> Self
where C: Into<Cow<'cow, I>> {
@ -44,31 +43,26 @@ impl<'cow, I> Message<'cow, I>
pub struct RawMsg(pub String);
// because can't impl From<ItemWrap<...>> for Cow
// because can't impl From<Vec<...>> for Cow
#[derive(Serialize, Deserialize, Debug, Clone)]
struct ItemWrap<T>(T);
pub struct ItemWrap<T: ToMsg>(pub T);
impl<T> ItemWrap<T> {
impl<T: ToMsg> ItemWrap<T> {
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> From<T> for ItemWrap<T> {
fn from(t: T) -> Self {
ItemWrap(t)
}
}
impl<T: Clone> ToMsg for ItemWrap<T> {}
impl<T: ToMsg> ToMsg for ItemWrap<T> {}
//impl<T: ToMsg> ToMsg for Vec<T> {}
impl<'cow, T: Clone> 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>> {
Cow::Owned(obj)
}
}
impl<'cow, T: Clone> From<&'cow ItemWrap<T>> for Cow<'cow, ItemWrap<T>> {
impl<'cow, T: ToMsg> From<&'cow ItemWrap<T>> for Cow<'cow, ItemWrap<T>> {
fn from(obj: &'cow ItemWrap<T>) -> Cow<'cow, ItemWrap<T>> {
Cow::Borrowed(obj)
}

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

@ -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::*,

Loading…
Cancel
Save