From e3bd31db98a99944a936bf975318c8792eed5381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Espi=C3=A9=2ER?= <60436978+remi-espie@users.noreply.github.com> Date: Wed, 24 Apr 2024 19:44:46 +0200 Subject: [PATCH] feat(agent): add a debug agent (#28) * feat: rework branch with every changes Signed-off-by: ESPIE * chore: fmt Signed-off-by: ESPIE --------- Signed-off-by: ESPIE --- src/agent/Cargo.toml | 3 ++ src/agent/src/agents/debug.rs | 56 ++++++++++++++++++++++++++++++++ src/agent/src/agents/mod.rs | 6 ++++ src/agent/src/workload/runner.rs | 5 +++ 4 files changed, 70 insertions(+) create mode 100644 src/agent/src/agents/debug.rs diff --git a/src/agent/Cargo.toml b/src/agent/Cargo.toml index 2499f26..9e11866 100644 --- a/src/agent/Cargo.toml +++ b/src/agent/Cargo.toml @@ -12,3 +12,6 @@ clap = { version = "4.5.4", features = ["derive"] } rand = "0.8.5" serde = { version = "1.0.197", features = ["derive"] } toml = "0.8.12" + +[features] +debug-agent = [] \ No newline at end of file diff --git a/src/agent/src/agents/debug.rs b/src/agent/src/agents/debug.rs new file mode 100644 index 0000000..ceae270 --- /dev/null +++ b/src/agent/src/agents/debug.rs @@ -0,0 +1,56 @@ +use super::AgentOutput; +use crate::agents::Agent; +use crate::{workload, AgentResult}; +use std::fs::create_dir_all; +use std::time::SystemTime; + +pub struct DebugAgent { + workload_config: workload::config::Config, +} + +impl From for DebugAgent { + fn from(workload_config: workload::config::Config) -> Self { + Self { workload_config } + } +} + +impl Agent for DebugAgent { + fn prepare(&self) -> AgentResult { + let dir = format!("/tmp/{}", self.workload_config.workload_name); + + println!("Function directory: {}", dir); + + create_dir_all(&dir).expect("Unable to create directory"); + + std::fs::write( + format!("{}/debug.txt", &dir), + format!( + "Debug agent for {} - written at {:?}", + self.workload_config.workload_name, + SystemTime::now(), + ), + ) + .expect("Unable to write debug.txt file"); + + Ok(AgentOutput { + exit_code: 0, + stdout: "Build successfully!".into(), + stderr: String::default(), + }) + } + + fn run(&self) -> AgentResult { + let dir = format!("/tmp/{}", self.workload_config.workload_name); + + let content = std::fs::read_to_string(format!("{}/debug.txt", &dir)) + .expect("Unable to read debug.txt file"); + + std::fs::remove_dir_all(dir).expect("Unable to remove directory"); + + Ok(AgentOutput { + exit_code: 0, + stdout: content, + stderr: String::default(), + }) + } +} diff --git a/src/agent/src/agents/mod.rs b/src/agent/src/agents/mod.rs index 2aeef1e..5d3062b 100644 --- a/src/agent/src/agents/mod.rs +++ b/src/agent/src/agents/mod.rs @@ -1,6 +1,8 @@ use crate::AgentResult; use serde::Deserialize; +#[cfg(feature = "debug-agent")] +pub mod debug; pub mod rust; #[derive(Debug, Clone)] @@ -19,12 +21,16 @@ pub trait Agent { #[serde(rename_all = "kebab-case")] pub enum Language { Rust, + #[cfg(feature = "debug-agent")] + Debug, } impl std::fmt::Display for Language { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Language::Rust => write!(f, "rust"), + #[cfg(feature = "debug-agent")] + Language::Debug => write!(f, "debug"), } } } diff --git a/src/agent/src/workload/runner.rs b/src/agent/src/workload/runner.rs index 494fa03..cb07d80 100644 --- a/src/agent/src/workload/runner.rs +++ b/src/agent/src/workload/runner.rs @@ -4,6 +4,9 @@ use crate::{ AgentResult, }; +#[cfg(feature = "debug-agent")] +use crate::agents::debug; + /// Runner for a workload. /// Will execute the workload based on the inner agent (language). pub struct Runner { @@ -15,6 +18,8 @@ impl Runner { pub fn new(config: Config) -> Self { let agent: Box = match config.language { Language::Rust => Box::new(rust::RustAgent::from(config.clone())), + #[cfg(feature = "debug-agent")] + Language::Debug => Box::new(debug::DebugAgent::from(config.clone())), }; Runner { config, agent }