wasm initial

pull/1/head
plazmoid 3 years ago
parent b247c8640d
commit f840865597
  1. 2
      Cargo.toml
  2. 3
      Makefile.toml
  3. 5
      bin/u_agent/src/lib.rs
  4. 6
      bin/u_panel/Cargo.toml
  5. 10
      bin/u_panel/be/Cargo.toml
  6. 0
      bin/u_panel/be/src/lib.rs
  7. 12
      bin/u_panel/fe/Cargo.toml
  8. 9
      bin/u_panel/fe/index.html
  9. 51
      bin/u_panel/fe/src/main.rs
  10. 2
      bin/u_panel/src/argparse.rs
  11. 1
      bin/u_panel/src/main.rs
  12. 3
      bin/u_panel/src/server/mod.rs
  13. 10
      integration/tests/tests.rs
  14. 13
      lib/u_lib/Cargo.toml
  15. 2
      lib/u_lib/src/config.rs
  16. 3
      lib/u_lib/src/errors/variants.rs
  17. 35
      lib/u_lib/src/lib.rs
  18. 15
      lib/u_lib/src/models/agent.rs
  19. 8
      lib/u_lib/src/models/jobs/assigned.rs
  20. 8
      lib/u_lib/src/models/jobs/meta.rs
  21. 38
      lib/u_lib/src/utils/misc.rs
  22. 7
      lib/u_lib/src/utils/mod.rs
  23. 37
      lib/u_lib/src/utils/unix.rs
  24. 3
      scripts/deploy.sh

@ -2,6 +2,8 @@
members = [ members = [
"bin/u_agent", "bin/u_agent",
"bin/u_panel", "bin/u_panel",
"bin/u_panel/be",
"bin/u_panel/fe",
"bin/u_run", "bin/u_run",
"bin/u_server", "bin/u_server",
"lib/u_lib", "lib/u_lib",

@ -66,3 +66,6 @@ docker run --rm \
[tasks.test] [tasks.test]
dependencies = ["unit", "integration"] dependencies = ["unit", "integration"]
[tasks.deploy]
script = './scripts/deploy.sh'

@ -60,7 +60,7 @@ pub async fn process_request(job_requests: Vec<AssignedJob>, client: &ClientHand
} }
} }
async fn error_reporting(client: Arc<ClientHandler>) { async fn error_reporting(client: Arc<ClientHandler>) -> ! {
loop { loop {
let err = ErrChan::recv(); let err = ErrChan::recv();
debug!("Error encountered: {:?}", err); debug!("Error encountered: {:?}", err);
@ -100,10 +100,9 @@ pub async fn run_forever() {
env_logger::init(); env_logger::init();
let arg_ip = env::args().nth(1); let arg_ip = env::args().nth(1);
let client = Arc::new(ClientHandler::new(arg_ip.as_deref())); let client = Arc::new(ClientHandler::new(arg_ip.as_deref()));
let _cli = client.clone();
panic::set_hook(Box::new(|panic_info| { panic::set_hook(Box::new(|panic_info| {
ErrChan::send(UError::Panic(panic_info.to_string())) ErrChan::send(UError::Panic(panic_info.to_string()))
})); }));
tokio::spawn(error_reporting(_cli)); tokio::spawn(error_reporting(client.clone()));
do_stuff(client).await; do_stuff(client).await;
} }

@ -11,10 +11,8 @@ structopt = "0.3.21"
log = "^0.4" log = "^0.4"
env_logger = "0.7.1" env_logger = "0.7.1"
uuid = "0.6.5" uuid = "0.6.5"
reqwest = { version = "0.11", features = ["json"] }
openssl = "*"
u_lib = { version = "*", path = "../../lib/u_lib" }
serde_json = "1.0.4" serde_json = "1.0.4"
serde = { version = "1.0.114", features = ["derive"] } serde = { version = "1.0.114", features = ["derive"] }
actix-web = "3.3.2"
tokio = "1.11.0" tokio = "1.11.0"
be = { version = "*", path = "./be" }
u_lib = { version = "*", path = "../../lib/u_lib" }

@ -0,0 +1,10 @@
[package]
name = "be"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "3.3.2"
u_lib = { version = "*", path = "../../../lib/u_lib" }

@ -0,0 +1,12 @@
[package]
name = "fe"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
u_lib = { version = "*", path = "../../../lib/u_lib" }
wasm-bindgen = "0.2.78"
yew = "0.18.0"
yew-router = "0.15.0"

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Yew App</title>
</head>
</html>

@ -0,0 +1,51 @@
use wasm_bindgen::prelude::*;
use yew::prelude::*;
enum Msg {
AddOne,
}
struct Model {
// `ComponentLink` is like a reference to a component.
// It can be used to send messages to the component
link: ComponentLink<Self>,
value: i64,
}
impl Component for Model {
type Message = Msg;
type Properties = ();
fn create(_props: Self::Properties, link: ComponentLink<Self>) -> Self {
Self { link, value: 0 }
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::AddOne => {
self.value += 1;
// the value has changed so we need to
// re-render for it to appear on the page
true
}
}
}
fn change(&mut self, _props: Self::Properties) -> ShouldRender {
// Should only return "true" if new properties are different to
// previously received properties.
// This component has no properties so we will always return "false".
false
}
fn view(&self) -> Html {
html! {
<div>
<button onclick=self.link.callback(|_| Msg::AddOne)>{ "+1" }</button>
<p>{ self.value }</p>
</div>
}
}
}
pub fn main() {
yew::start_app::<Model>();
}

@ -131,7 +131,7 @@ pub async fn process_cmd(args: Args) -> UResult<()> {
JobMapALD::List { uid } => printer.print(cli_handler.get_agent_jobs(uid).await), JobMapALD::List { uid } => printer.print(cli_handler.get_agent_jobs(uid).await),
JobMapALD::Delete { uid } => printer.print(cli_handler.del(Some(uid)).await), JobMapALD::Delete { uid } => printer.print(cli_handler.del(Some(uid)).await),
}, },
Cmd::Server => crate::server::serve().unwrap(), Cmd::Server => be::serve().unwrap(),
} }
Ok(()) Ok(())
} }

@ -1,5 +1,4 @@
mod argparse; mod argparse;
mod server;
use argparse::{process_cmd, Args}; use argparse::{process_cmd, Args};
use std::process; use std::process;

@ -1,3 +0,0 @@
mod app;
pub use app::serve;

@ -3,19 +3,19 @@ mod fixtures;
mod helpers; mod helpers;
use std::env; use std::env;
use u_lib::config::MASTER_PORT;
#[macro_use] #[macro_use]
extern crate rstest; extern crate rstest;
// TODO: huita
#[ignore]
#[tokio::test] #[tokio::test]
async fn test_non_auth_connection_dropped() { async fn test_non_auth_connection_dropped() {
let env_server = env::var("U_SERVER").unwrap(); let env_server = env::var("U_SERVER").unwrap();
match reqwest::get(format!("https://{}", env_server)).await { match reqwest::get(format!("https://{}:{}", env_server, MASTER_PORT)).await {
Err(e) => { Err(e) => {
dbg!(e.to_string()); assert!(e
assert!(e.is_request()) .to_string()
.contains("unable to get local issuer certificate"))
} }
_ => panic!("no error occured on foreign client connection"), _ => panic!("no error occured on foreign client connection"),
} }

@ -13,11 +13,7 @@ uuid = { version = "0.6.5", features = ["serde", "v4"] }
nix = "0.17" nix = "0.17"
libc = "^0.2" libc = "^0.2"
lazy_static = "1.4.0" lazy_static = "1.4.0"
tokio = { version = "1.2.0", features = ["rt-multi-thread", "sync", "macros", "process", "time"] }
reqwest = { version = "0.11", features = ["json", "native-tls"] }
openssl = "*"
futures = "0.3.5" futures = "0.3.5"
guess_host_triple = "0.1.2"
thiserror = "*" thiserror = "*"
log = "*" log = "*"
mockall = "0.9.1" mockall = "0.9.1"
@ -30,10 +26,13 @@ shlex = "1.0.0"
u_api_proc_macro = { version = "*", path = "../u_api_proc_macro" } u_api_proc_macro = { version = "*", path = "../u_api_proc_macro" }
crossbeam = "0.8.1" crossbeam = "0.8.1"
backtrace = "0.3.61" backtrace = "0.3.61"
diesel = { version = "1.4.5", features = ["postgres", "uuid"] }
[dependencies.diesel] [target.'cfg(not(target_arch = "wasm32"))'.dependencies]
version = "1.4.5" reqwest = { version = "0.11", features = ["json", "native-tls"] }
features = ["postgres", "uuid"] tokio = { version = "1.2.0", features = ["rt-multi-thread", "sync", "macros", "process", "time"] }
guess_host_triple = "0.1.2"
openssl = "*"
[dev-dependencies] [dev-dependencies]
test-case = "1.1.0" test-case = "1.1.0"

@ -1,7 +1,7 @@
use lazy_static::lazy_static; use lazy_static::lazy_static;
use uuid::Uuid; use uuid::Uuid;
pub const MASTER_SERVER: &str = "127.0.0.1"; //Ipv4Addr::new(3,9,16,40) pub const MASTER_SERVER: &str = "ortem.xyz"; //Ipv4Addr::new(3,9,16,40)
pub const MASTER_PORT: u16 = 63714; pub const MASTER_PORT: u16 = 63714;
lazy_static! { lazy_static! {

@ -1,5 +1,6 @@
use backtrace::Backtrace as CrateBacktrace; use backtrace::Backtrace as CrateBacktrace;
use diesel::result::Error as DslError; use diesel::result::Error as DslError;
#[cfg(not(target_arch = "wasm32"))]
use reqwest::Error as ReqError; use reqwest::Error as ReqError;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
@ -72,12 +73,14 @@ impl UError {
} }
} }
#[cfg(not(target_arch = "wasm32"))]
impl From<ReqError> for UErrorBt { impl From<ReqError> for UErrorBt {
fn from(e: ReqError) -> Self { fn from(e: ReqError) -> Self {
UError::from(e).into_bt() UError::from(e).into_bt()
} }
} }
#[cfg(not(target_arch = "wasm32"))]
impl From<ReqError> for UError { impl From<ReqError> for UError {
fn from(e: ReqError) -> Self { fn from(e: ReqError) -> Self {
UError::NetError(e.to_string(), String::new()) UError::NetError(e.to_string(), String::new())

@ -1,17 +1,32 @@
#![allow(non_upper_case_globals)] #![allow(non_upper_case_globals)]
pub mod api; #[cfg(not(target_arch = "wasm32"))]
pub mod builder; #[path = "."]
pub mod cache; pub mod exports {
pub mod config; pub mod api;
pub mod datatypes; pub mod builder;
pub mod errors; pub mod cache;
pub mod executor; pub mod config;
pub mod messaging; pub mod datatypes;
pub mod models; pub mod errors;
pub mod utils; pub mod executor;
pub mod messaging;
pub mod models;
pub mod utils;
}
#[cfg(target_arch = "wasm32")]
#[path = "."]
pub mod exports {
pub mod config;
pub mod errors;
pub mod messaging;
pub mod models;
pub mod utils;
}
pub use config::UID; pub use config::UID;
pub use errors::{UError, UErrorBt, ULocalError, ULocalResult, UResult}; pub use errors::{UError, UErrorBt, ULocalError, ULocalResult, UResult};
pub use exports::*;
pub mod schema_exports { pub mod schema_exports {
pub use crate::models::{Agentstate, Jobstate, Jobtype}; pub use crate::models::{Agentstate, Jobstate, Jobtype};

@ -4,12 +4,10 @@ use serde::{Deserialize, Serialize};
use std::{fmt, time::SystemTime}; use std::{fmt, time::SystemTime};
use strum::Display; use strum::Display;
use crate::{ #[cfg(not(target_arch = "wasm32"))]
builder::NamedJobBuilder, messaging::Reportable, models::schema::*, unwrap_enum, use crate::builder::NamedJobBuilder;
utils::systime_to_string, UID, use crate::{messaging::Reportable, models::schema::*, unwrap_enum, utils::systime_to_string, UID};
};
use guess_host_triple::guess_host_triple;
use uuid::Uuid; use uuid::Uuid;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, DbEnum, Display)] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, DbEnum, Display)]
@ -65,6 +63,7 @@ impl fmt::Display for Agent {
} }
} }
#[cfg(not(target_arch = "wasm32"))]
impl Agent { impl Agent {
pub fn with_id(uid: Uuid) -> Self { pub fn with_id(uid: Uuid) -> Self {
Self { Self {
@ -73,6 +72,7 @@ impl Agent {
} }
} }
#[cfg(unix)]
pub async fn gather() -> Self { pub async fn gather() -> Self {
let mut builder = NamedJobBuilder::from_shell(vec![ let mut builder = NamedJobBuilder::from_shell(vec![
("hostname", "hostname"), ("hostname", "hostname"),
@ -91,13 +91,14 @@ impl Agent {
hostname: decoder(builder.pop("hostname")), hostname: decoder(builder.pop("hostname")),
is_root: &decoder(builder.pop("is_root")) == "0", is_root: &decoder(builder.pop("is_root")) == "0",
username: decoder(builder.pop("username")), username: decoder(builder.pop("username")),
platform: guess_host_triple().unwrap_or("unknown").to_string(), platform: guess_host_triple::guess_host_triple()
.unwrap_or("unknown")
.to_string(),
..Default::default() ..Default::default()
} }
} }
pub async fn run() -> Reportable { pub async fn run() -> Reportable {
#[cfg(unix)]
Reportable::Agent(Agent::gather().await) Reportable::Agent(Agent::gather().await)
} }
} }

@ -1,16 +1,16 @@
use super::JobState; use super::JobState;
#[cfg(not(target_arch = "wasm32"))]
use crate::{cache::JobCache, utils::TempFile};
use crate::{ use crate::{
cache::JobCache,
errors::UError, errors::UError,
messaging::Reportable, messaging::Reportable,
models::schema::*, models::schema::*,
utils::{systime_to_string, ProcOutput, TempFile}, utils::{systime_to_string, ProcOutput},
UID, UID,
}; };
use diesel::{Identifiable, Insertable, Queryable}; use diesel::{Identifiable, Insertable, Queryable};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{fmt, time::SystemTime}; use std::{fmt, time::SystemTime};
use tokio::process::Command;
use uuid::Uuid; use uuid::Uuid;
#[derive( #[derive(
@ -75,8 +75,10 @@ impl Default for AssignedJob {
} }
} }
#[cfg(not(target_arch = "wasm32"))]
impl AssignedJob { impl AssignedJob {
pub async fn run(mut self) -> Reportable { pub async fn run(mut self) -> Reportable {
use tokio::process::Command;
let (argv, _payload) = { let (argv, _payload) = {
let meta = JobCache::get(&self.job_id).unwrap(); let meta = JobCache::get(&self.job_id).unwrap();
if let Some(ref payload) = meta.payload { if let Some(ref payload) = meta.payload {

@ -1,7 +1,6 @@
use super::JobType; use super::JobType;
use crate::{models::schema::*, utils::Stripped, UError, UResult}; use crate::{models::schema::*, utils::Stripped, UError, UResult};
use diesel::{Identifiable, Insertable, Queryable}; use diesel::{Identifiable, Insertable, Queryable};
use guess_host_triple::guess_host_triple;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
use std::str::from_utf8; use std::str::from_utf8;
@ -61,7 +60,12 @@ impl Default for JobMeta {
alias: None, alias: None,
argv: String::new(), argv: String::new(),
exec_type: JobType::Shell, exec_type: JobType::Shell,
platform: guess_host_triple().unwrap_or("unknown").to_string(), #[cfg(not(target_arch = "wasm32"))]
platform: guess_host_triple::guess_host_triple()
.unwrap_or("unknown")
.to_string(),
#[cfg(target_arch = "wasm32")]
platform: "unknown".to_string(),
payload: None, payload: None,
} }
} }

@ -1,9 +1,3 @@
use nix::{
sys::signal::{signal, SigHandler, Signal},
unistd::{chdir, close as fdclose, fork, getppid, setsid, ForkResult},
};
use std::process::exit;
pub trait OneOrVec<T> { pub trait OneOrVec<T> {
fn into_vec(self) -> Vec<T>; fn into_vec(self) -> Vec<T>;
} }
@ -31,38 +25,6 @@ macro_rules! unwrap_enum {
}; };
} }
pub fn daemonize() {
if getppid().as_raw() != 1 {
setsig(Signal::SIGTTOU, SigHandler::SigIgn);
setsig(Signal::SIGTTIN, SigHandler::SigIgn);
setsig(Signal::SIGTSTP, SigHandler::SigIgn);
}
for fd in 0..=2 {
match fdclose(fd) {
_ => (),
}
}
match chdir("/") {
_ => (),
};
match fork() {
Ok(ForkResult::Parent { .. }) => {
exit(0);
}
Ok(ForkResult::Child) => match setsid() {
_ => (),
},
Err(_) => exit(255),
}
}
pub fn setsig(sig: Signal, hnd: SigHandler) {
unsafe {
signal(sig, hnd).unwrap();
}
}
pub fn init_env() { pub fn init_env() {
let envs = [".env", ".env.private"]; let envs = [".env", ".env.private"];
for envfile in &envs { for envfile in &envs {

@ -4,7 +4,10 @@ mod fmt;
mod misc; mod misc;
mod proc_output; mod proc_output;
mod storage; mod storage;
#[cfg(not(target_arch = "wasm32"))]
mod tempfile; mod tempfile;
#[cfg(unix)]
mod unix;
mod vec_display; mod vec_display;
pub use combined_result::*; pub use combined_result::*;
@ -13,5 +16,9 @@ pub use fmt::*;
pub use misc::*; pub use misc::*;
pub use proc_output::*; pub use proc_output::*;
pub use storage::*; pub use storage::*;
#[cfg(not(target_arch = "wasm32"))]
pub use tempfile::*; pub use tempfile::*;
pub use vec_display::*; pub use vec_display::*;
#[cfg(unix)]
pub use unix::*;

@ -0,0 +1,37 @@
use nix::{
sys::signal::{signal, SigHandler, Signal},
unistd::{chdir, close as fdclose, fork, getppid, setsid, ForkResult},
};
use std::process::exit;
pub fn daemonize() {
if getppid().as_raw() != 1 {
setsig(Signal::SIGTTOU, SigHandler::SigIgn);
setsig(Signal::SIGTTIN, SigHandler::SigIgn);
setsig(Signal::SIGTSTP, SigHandler::SigIgn);
}
for fd in 0..=2 {
match fdclose(fd) {
_ => (),
}
}
match chdir("/") {
_ => (),
};
match fork() {
Ok(ForkResult::Parent { .. }) => {
exit(0);
}
Ok(ForkResult::Child) => match setsid() {
_ => (),
},
Err(_) => exit(255),
}
}
pub fn setsig(sig: Signal, hnd: SigHandler) {
unsafe {
signal(sig, hnd).unwrap();
}
}

@ -9,7 +9,8 @@ RSYNC="rsync -arzh --progress"
ssh $SERVER mkdir -p $REMOTE_DIR/{release,deploy} ssh $SERVER mkdir -p $REMOTE_DIR/{release,deploy}
$RSYNC $ROOTDIR/release/u_server $REMOTE_PATH/release/u_server $RSYNC $ROOTDIR/release/u_server $REMOTE_PATH/release/u_server
$RSYNC --exclude="*.sh" $ROOTDIR/certs/ $REMOTE_PATH/certs $RSYNC $ROOTDIR/certs/server.{crt,key} $REMOTE_PATH/certs
$RSYNC $ROOTDIR/certs/ca.crt $REMOTE_PATH/certs
$RSYNC $ROOTDIR/migrations/ $REMOTE_PATH/migrations $RSYNC $ROOTDIR/migrations/ $REMOTE_PATH/migrations
$RSYNC $ROOTDIR/.env* $REMOTE_PATH/ $RSYNC $ROOTDIR/.env* $REMOTE_PATH/
$RSYNC $ROOTDIR/integration/docker-compose.yml $REMOTE_PATH/deploy/ $RSYNC $ROOTDIR/integration/docker-compose.yml $REMOTE_PATH/deploy/

Loading…
Cancel
Save