diff --git a/Cargo.toml b/Cargo.toml index c47ddc7..9841179 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ driftwood = "0" handlebars = { version = "~3.4.0", features = ["dir_source"] } html-escape = "~0.2.6" log = "~0.4.8" +os_pipe = "1" pretty_env_logger = "~0.3.1" serde_json = "~1.0.0" sqlx = { version = "~0.5.1", features = ["chrono", "json", "migrate", "offline", "sqlite", "uuid", "runtime-async-std-rustls"] } diff --git a/src/agent/main.rs b/src/agent/main.rs index 989273b..68a4260 100644 --- a/src/agent/main.rs +++ b/src/agent/main.rs @@ -33,8 +33,34 @@ mod routes { * This will take in the commands to actually execute */ pub async fn execute(mut req: Request<()>) -> Result { - let commands: CommandRequest = req.body_json().await?; - debug!("Commands to exec: {:?}", commands); + let c: CommandRequest = req.body_json().await?; + debug!("Commands to exec: {:?}", c); + + for command in c.commands.iter() { + use os_pipe::pipe; + use std::io::{BufRead, BufReader}; + use std::process::Command; + let mut cmd = Command::new("sh"); + cmd.args(["-c", &command.script]); + let (reader, writer) = pipe().unwrap(); + let writer_clone = writer.try_clone().unwrap(); + cmd.stdout(writer); + cmd.stderr(writer_clone); + let mut handle = cmd.spawn()?; + drop(cmd); + + debug!("executing: {}", &command.script); + let bufr = BufReader::new(reader); + for line in bufr.lines() { + if let Ok(buffer) = line { + debug!("output: {}", buffer); + } + } + + let status = handle.wait()?; + debug!("status of {}: {:?}", &command.script, status); + } + Ok("{}".into()) } diff --git a/src/lib.rs b/src/lib.rs index 6e4c2fd..e8d233a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,12 +21,12 @@ struct CapsResponse { #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] pub struct Command { - script: String, + pub script: String, } #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] pub struct CommandRequest { - commands: Vec, + pub commands: Vec, } #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]