started full refactoring because this is big shit it's easier to start from the very beginning4-update-check
parent
b0ca782fcb
commit
c94912252a
16 changed files with 177 additions and 345 deletions
@ -1,58 +0,0 @@ |
||||
use std::{ |
||||
collections::HashMap, |
||||
time::SystemTime |
||||
}; |
||||
use crate::{ |
||||
contracts::*, |
||||
UID, |
||||
exec_job, |
||||
utils::vec_to_string, |
||||
models::* |
||||
}; |
||||
|
||||
use guess_host_triple::guess_host_triple; |
||||
|
||||
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, |
||||
id: UID.clone(), |
||||
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, |
||||
} |
||||
} |
||||
|
||||
|
||||
#[cfg(test)] |
||||
mod tests { |
||||
use super::*; |
||||
|
||||
#[tokio::test] |
||||
async fn test_gather() { |
||||
let cli_info = gather().await; |
||||
assert_eq!( |
||||
&cli_info.username, |
||||
"plazmoid" |
||||
) |
||||
} |
||||
|
||||
} |
@ -1,36 +0,0 @@ |
||||
pub mod jobs; |
||||
pub mod messaging; |
||||
pub mod agent; |
||||
|
||||
pub use { |
||||
messaging::*, |
||||
jobs::*, |
||||
}; |
||||
|
||||
use std::{ |
||||
borrow::Cow |
||||
}; |
||||
use crate::models::*; |
||||
|
||||
macro_rules! to_message { |
||||
($($type:ty),+) => { $( |
||||
|
||||
impl ToMsg for $type {} |
||||
|
||||
impl<'cow> From<$type> for Cow<'cow, $type> { |
||||
#[inline] |
||||
fn from(obj: $type) -> Cow<'cow, $type> { |
||||
Cow::Owned(obj) |
||||
} |
||||
} |
||||
|
||||
impl<'cow> From<&'cow $type> for Cow<'cow, $type> { |
||||
#[inline] |
||||
fn from(obj: &'cow $type) -> Cow<'cow, $type> { |
||||
Cow::Borrowed(obj) |
||||
} |
||||
} )+ |
||||
} |
||||
} |
||||
|
||||
to_message!(IAgent, Agent, RawMsg, JobMeta, JobResult); |
@ -1,5 +1,38 @@ |
||||
mod agent; |
||||
pub mod schema; |
||||
pub mod jobs; |
||||
|
||||
pub use crate::{ |
||||
models::{ |
||||
agent::*, |
||||
jobs::*, |
||||
}, |
||||
messaging::*, |
||||
}; |
||||
|
||||
pub use agent::*; |
||||
use std::{ |
||||
borrow::Cow |
||||
}; |
||||
|
||||
macro_rules! to_message { |
||||
($($type:ty),+) => { $( |
||||
|
||||
impl ToMsg for $type {} |
||||
|
||||
impl<'cow> From<$type> for Cow<'cow, $type> { |
||||
#[inline] |
||||
fn from(obj: $type) -> Cow<'cow, $type> { |
||||
Cow::Owned(obj) |
||||
} |
||||
} |
||||
|
||||
impl<'cow> From<&'cow $type> for Cow<'cow, $type> { |
||||
#[inline] |
||||
fn from(obj: &'cow $type) -> Cow<'cow, $type> { |
||||
Cow::Borrowed(obj) |
||||
} |
||||
} )+ |
||||
} |
||||
} |
||||
|
||||
to_message!(IAgent, Agent, RawMsg, JobMeta, JobResult); |
||||
|
@ -1,73 +0,0 @@ |
||||
table! { |
||||
agents (id) { |
||||
alias -> Nullable<Text>, |
||||
agent_id -> Text, |
||||
hostname -> Text, |
||||
id -> Integer, |
||||
is_root -> Bool, |
||||
is_root_allowed -> Bool, |
||||
last_active -> Timestamp, |
||||
platform -> Text, |
||||
regtime -> Timestamp, |
||||
status -> Nullable<Text>, |
||||
token -> Nullable<Text>, |
||||
username -> Text, |
||||
} |
||||
} |
||||
|
||||
table! { |
||||
certificates (id) { |
||||
agent_id -> Integer, |
||||
id -> Integer, |
||||
is_revoked -> Bool, |
||||
} |
||||
} |
||||
|
||||
table! { |
||||
ip_addrs (id) { |
||||
agent_id -> Integer, |
||||
check_ts -> Timestamp, |
||||
gateway -> Nullable<Text>, |
||||
id -> Integer, |
||||
iface -> Text, |
||||
ip_addr -> Text, |
||||
is_gray -> Bool, |
||||
netmask -> Text, |
||||
} |
||||
} |
||||
|
||||
table! { |
||||
jobs (id) { |
||||
alias -> Nullable<Text>, |
||||
id -> Integer, |
||||
job_type -> Text, |
||||
exec_type -> Text, |
||||
platform -> Nullable<Text>, |
||||
data -> Binary, |
||||
} |
||||
} |
||||
|
||||
table! { |
||||
results (id) { |
||||
agent_id -> Integer, |
||||
created -> Timestamp, |
||||
id -> Integer, |
||||
job_id -> Integer, |
||||
result -> Nullable<Binary>, |
||||
status -> Nullable<Text>, |
||||
ts -> Timestamp, |
||||
} |
||||
} |
||||
|
||||
joinable!(certificates -> agents (agent_id)); |
||||
joinable!(ip_addrs -> agents (agent_id)); |
||||
joinable!(results -> agents (agent_id)); |
||||
joinable!(results -> jobs (job_id)); |
||||
|
||||
allow_tables_to_appear_in_same_query!( |
||||
agents, |
||||
certificates, |
||||
ip_addrs, |
||||
jobs, |
||||
results, |
||||
); |
@ -1,4 +1 @@ |
||||
DROP TABLE agents; |
||||
DROP TABLE ip_addrs; |
||||
DROP TABLE jobs; |
||||
DROP TABLE results; |
||||
DROP DATABASE u_db; |
||||
|
Loading…
Reference in new issue