Skip to content

Commit

Permalink
feat: allow file-based executor
Browse files Browse the repository at this point in the history
Signed-off-by: Aisha M <[email protected]>
  • Loading branch information
aamohd committed Dec 17, 2024
1 parent 752bc2c commit 41690cf
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 32 deletions.
8 changes: 3 additions & 5 deletions hipcheck/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use crate::{
cache::repo::{RepoCacheDeleteScope, RepoCacheListScope, RepoCacheSort},
env,
error::Context,
error::Result,
hc_error,
Expand Down Expand Up @@ -112,7 +111,7 @@ struct PathArgs {
long = "exec",
global = true,
help_heading = "Path Flags",
long_help = "Path to the exec file."
long_help = "Path to the execution config file."
)]
exec: Option<PathBuf>,
}
Expand Down Expand Up @@ -234,6 +233,7 @@ impl CliConfig {

/// Get the path to the exec config file
pub fn exec(&self) -> Option<&Path> {
// Question - should I remove the path from backups
self.path_args.exec.as_deref()
}

Expand Down Expand Up @@ -315,9 +315,7 @@ impl CliConfig {
policy: std::env::current_dir()
.ok()
.map(|dir| pathbuf![&dir, "Hipcheck.kdl"]),
exec: env::current_dir()
.ok()
.map(|dir| pathbuf![&dir, "config/Config.kdl"]),
exec: None
},
deprecated_args: DeprecatedArgs {
config: dirs::home_dir().map(|dir| pathbuf![&dir, "hipcheck", "config"]),
Expand Down
2 changes: 1 addition & 1 deletion hipcheck/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::{
engine::HcEngine,
error::{Context, Result},
executor::ExecConfig,
exec::ExecConfig,
hc_error,
policy::{
policy_file::{PolicyAnalysis, PolicyCategory, PolicyCategoryChild},
Expand Down
29 changes: 29 additions & 0 deletions hipcheck/src/executor.rs → hipcheck/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,24 @@ impl ExecConfig {
{
Self::from_str(&read_string(path)?)
}

pub fn default() -> Result<Self, Error> {

// let path = env::current_dir()
// .ok()
// .map(|dir| pathbuf![&dir, "Config.kdl"])
// .as_ref();

// These are the default values
let data = r#"plugin {
backoff-interval 100000
max-spawn-attempts 3
max-conn-attempts 5
jitter-percent 10
grpc-msg-buffer-size 10
}"#;
Self::from_str(data)
}
}

impl FromStr for ExecConfig {
Expand Down Expand Up @@ -496,4 +514,15 @@ mod test {
assert_eq!(config.plugin_data.jitter.percent, 10);
assert_eq!(config.plugin_data.grpc_buffer.size, 10);
}

#[test]
fn test_default_exec_config() {
let config = ExecConfig::default().unwrap();

assert_eq!(config.plugin_data.backoff.micros, 100000);
assert_eq!(config.plugin_data.max_spawn.attempts, 3);
assert_eq!(config.plugin_data.max_conn.attempts, 5);
assert_eq!(config.plugin_data.jitter.percent, 10);
assert_eq!(config.plugin_data.grpc_buffer.size, 10);
}
}
43 changes: 31 additions & 12 deletions hipcheck/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod cli;
mod config;
mod engine;
mod error;
mod executor;
mod exec;
mod init;
mod plugin;
mod policy;
Expand All @@ -27,7 +27,7 @@ use crate::{
cli::Format,
config::{normalized_unresolved_analysis_tree_from_policy, Config},
error::{Context as _, Error, Result},
executor::ExecConfig,
exec::ExecConfig,
plugin::{try_set_arch, Plugin, PluginExecutor, PluginWithConfig},
policy::{config_to_policy, PolicyFile},
report::report_builder::{build_report, Report},
Expand Down Expand Up @@ -93,7 +93,7 @@ fn main() -> ExitCode {
Some(FullCommands::Ready) => cmd_ready(&config),
Some(FullCommands::Update(args)) => cmd_update(&args),
Some(FullCommands::Cache(args)) => return cmd_cache(args, &config),
Some(FullCommands::Plugin(args)) => cmd_plugin(args, &config),
Some(FullCommands::Plugin(args)) => return cmd_plugin(args, &config),
Some(FullCommands::PrintConfig) => cmd_print_config(config.config()),
Some(FullCommands::PrintCache) => cmd_print_home(config.cache()),
Some(FullCommands::Scoring) => {
Expand Down Expand Up @@ -487,7 +487,7 @@ fn check_policy_path(config: &CliConfig) -> StdResult<PathBuf, PathCheckError> {
Ok(path.to_owned())
}

fn cmd_plugin(args: PluginArgs, config: &CliConfig) {
fn cmd_plugin(args: PluginArgs, config: &CliConfig) -> ExitCode {
use crate::engine::{async_query, HcEngine, HcEngineImpl};
use std::sync::Arc;
use tokio::task::JoinSet;
Expand All @@ -508,14 +508,32 @@ fn cmd_plugin(args: PluginArgs, config: &CliConfig) {
};
let exec_config = if let Some(p) = config.exec() {
ExecConfig::from_file(p)
.context("Failed to load the exec config. Please make sure the exec config file is in the provided location and is formatted correctly.")
.context("Failed to load the exec config. Please make sure the exec config file is in the provided location and is formatted correctly.")
} else {
Err(hc_error!(
"No exec file found. Please provide an exec config file before running Hipcheck."
))
};
let exec_file = "Exec.kdl";
let mut curr_dir= env::current_dir().unwrap();
let config: Result<ExecConfig>;
loop {
let target_path = curr_dir.join(exec_file);
let target_ref = target_path.as_path();
if target_ref.exists() {
config = ExecConfig::from_file(target_ref)
.context("Failed to load the exec config. Please make sure the exec config file is in the provided location and is formatted correctly.");
break;
}
if let Some(parent) = curr_dir.parent() {
curr_dir = parent.to_path_buf();
} else {
log::info!("Using a default Exec Config");
config = ExecConfig::default();
break;
}
}

config
}.unwrap();

let plugin_data = exec_config.unwrap().plugin_data;
let plugin_data = exec_config.plugin_data;

let plugin_executor = PluginExecutor::new(
/* max_spawn_attempts */ plugin_data.max_spawn.attempts,
Expand All @@ -535,8 +553,8 @@ fn cmd_plugin(args: PluginArgs, config: &CliConfig) {
) {
Ok(e) => e,
Err(e) => {
println!("Failed to create engine: {e}");
return;
Shell::print_error(&hc_error!("Failed to create engine {}", e), Format::Human);
return ExitCode::FAILURE;
}
};
if args.asynch {
Expand Down Expand Up @@ -595,6 +613,7 @@ fn cmd_plugin(args: PluginArgs, config: &CliConfig) {
// x.join().unwrap();
// }
}
return ExitCode::SUCCESS
}

fn cmd_ready(config: &CliConfig) {
Expand Down
2 changes: 1 addition & 1 deletion hipcheck/src/plugin/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl PluginExecutor {
port,
grpc,
proc,
grpc_buffer: self.grpc_buffer,
grpc_query_buffer_size: self.grpc_buffer,
});
}
Err(hc_error!(
Expand Down
4 changes: 2 additions & 2 deletions hipcheck/src/plugin/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub struct PluginContext {
pub proc: Child,

/// The size of the gRPC buffer
pub grpc_buffer: usize,
pub grpc_query_buffer_size: usize,
}

// Redefinition of `grpc` field's functions with more useful types, additional
Expand Down Expand Up @@ -310,7 +310,7 @@ impl PluginContext {

let opt_explain_default_query = self.explain_default_query().await?;

let (tx, out_rx) = mpsc::channel::<PluginQuery>(self.grpc_buffer);
let (tx, out_rx) = mpsc::channel::<PluginQuery>(self.grpc_query_buffer_size);
let rx = self.initiate_query_protocol(out_rx).await?;

Ok(PluginTransport {
Expand Down
50 changes: 39 additions & 11 deletions hipcheck/src/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
},
engine::{start_plugins, HcEngine, HcEngineStorage},
error::{Context as _, Error, Result},
executor::ExecConfig,
exec::ExecConfig,
hc_error,
policy::{config_to_policy, PolicyFile},
report::{ReportParams, ReportParamsStorage},
Expand All @@ -34,6 +34,7 @@ use crate::{
};
use chrono::prelude::*;
use std::{
env,
fmt,
path::{Path, PathBuf},
rc::Rc,
Expand Down Expand Up @@ -298,23 +299,50 @@ pub fn load_policy_and_data(policy_path: Option<&Path>) -> Result<(PolicyFile, P
}

fn load_exec_config(exec_path: Option<&Path>) -> Result<ExecConfig> {
// Start the phase QUESTION
// Start the phase
let phase = SpinnerPhase::start("loading exec config");
// Increment the phase into the "running" stage.
phase.inc();
// Set the spinner phase to tick constantly, 10 times a second.
phase.enable_steady_tick(Duration::from_millis(100));

// Resolve the path to the exec config file.
let valid_path = exec_path.ok_or_else(|| {
hc_error!(
"Failed to load exec config. Please make sure the path set by the --exec flag exists."
)
})?;

// Load the exec config file
let exec_config = ExecConfig::from_file(valid_path)
.context("Failed to load the exec config. Please make sure the exec config file is in the provided location and is formatted correctly.")?;
let exec_config = match exec_path {
Some(p) => {
// Use the path provided
let config: Result<ExecConfig>;
if p.exists() == false {
hc_error!("Failed to load exec config. Please make sure the path set by the --exec flag exists.");
}
config = ExecConfig::from_file(p)
.context("Failed to load the exec config. Please make sure the exec config file is in the provided location and is formatted correctly.");
config
},
None => {
// Search for file if not provided
let exec_file = "Exec.kdl";
let mut curr_dir= env::current_dir().unwrap();
let config: Result<ExecConfig>;
loop {
let target_path = curr_dir.join(exec_file);
let target_ref = target_path.as_path();
if target_ref.exists() {
config = ExecConfig::from_file(target_ref)
.context("Failed to load the exec config. Please make sure the exec config file is in the provided location and is formatted correctly.");
break;
}
if let Some(parent) = curr_dir.parent() {
curr_dir = parent.to_path_buf();
} else {
// If file not found, use default values
log::info!("Using a default Exec Config");
config = ExecConfig::default();
break;
}
}
config
}
}.unwrap();

phase.finish_successful();

Expand Down

0 comments on commit 41690cf

Please sign in to comment.