Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
aamohd committed Dec 20, 2024
1 parent 5ffc34c commit 6c9825c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 62 deletions.
1 change: 0 additions & 1 deletion hipcheck/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ 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
37 changes: 36 additions & 1 deletion hipcheck/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
use crate::{
error::Error,
hc_error,
plugin::PluginExecutor,
util::{
fs::read_string,
kdl::{extract_data, ParseKdlNode},
},
};
use kdl::{KdlDocument, KdlNode, KdlValue};
use std::{path::Path, str::FromStr};
use std::{path::Path, str::FromStr, env};

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PluginBackoffInterval {
Expand Down Expand Up @@ -278,6 +279,27 @@ impl ExecConfig {
Self::from_str(&read_string(path)?)
}

pub fn find_file() -> Result<Self, Error> {
let exec_file = "Exec.kdl";
let mut curr_dir= env::current_dir().unwrap();
loop {
let target_path = curr_dir.join(exec_file);
let target_ref = target_path.as_path();
if target_ref.exists() {
// Parse found file
log::info!("Using Exec Config at {:?}", target_ref);
return Self::from_file(target_ref)
}
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");
return Self::default();
}
}
}

pub fn default() -> Result<Self, Error> {
// These are the default values
let data = r#"plugin {
Expand All @@ -289,6 +311,19 @@ impl ExecConfig {
}"#;
Self::from_str(data)
}

pub fn get_plugin_executor(plugin_data: PluginConfig) -> PluginExecutor {
// let plugin_data = exec_config.plugin_data;
PluginExecutor::new(
/* max_spawn_attempts */ plugin_data.max_spawn.attempts,
/* max_conn_attempts */ plugin_data.max_conn.attempts,
/* port_range */ 40000..u16::MAX,
/* backoff_interval_micros */ plugin_data.backoff.micros,
/* jitter_percent */ plugin_data.jitter.percent,
/*grpc_buffer*/ plugin_data.grpc_buffer.size,
)
.unwrap()
}
}

impl FromStr for ExecConfig {
Expand Down
35 changes: 4 additions & 31 deletions hipcheck/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
config::{normalized_unresolved_analysis_tree_from_policy, Config},
error::{Context as _, Error, Result},
exec::ExecConfig,
plugin::{try_set_arch, Plugin, PluginExecutor, PluginWithConfig},
plugin::{try_set_arch, Plugin, PluginWithConfig},
policy::{config_to_policy, PolicyFile},
report::report_builder::{build_report, Report},
score::score_results,
Expand Down Expand Up @@ -510,40 +510,13 @@ fn cmd_plugin(args: PluginArgs, config: &CliConfig) -> ExitCode {
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.")
} else {
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
ExecConfig::find_file()
.context("Failed to load the exec config. Please make sure the exec config file is in the provided location and is formatted correctly.")
}.unwrap();

let plugin_data = exec_config.plugin_data;
let plugin_executor = ExecConfig::get_plugin_executor(plugin_data);

let plugin_executor = PluginExecutor::new(
/* max_spawn_attempts */ plugin_data.max_spawn.attempts,
/* max_conn_attempts */ plugin_data.max_conn.attempts,
/* port_range */ 40000..u16::MAX,
/* backoff_interval_micros */ plugin_data.backoff.micros,
/* jitter_percent */ plugin_data.jitter.percent,
/*grpc_buffer*/ plugin_data.grpc_buffer.size,
)
.unwrap();
let engine = match HcEngineImpl::new(
plugin_executor,
vec![
Expand Down
42 changes: 13 additions & 29 deletions hipcheck/src/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use crate::{
util::command::DependentProgram,
util::{git::get_git_version, npm::get_npm_version},
version::{VersionQuery, VersionQueryStorage},
PluginExecutor,
};
use chrono::prelude::*;
use std::{
Expand Down Expand Up @@ -224,14 +223,16 @@ impl Session {
let exec_config = session.exec_config();
let plugin_data = &exec_config.plugin_data;

let executor = PluginExecutor::new(
/* max_spawn_attempts */ plugin_data.max_spawn.attempts,
/* max_conn_attempts */ plugin_data.max_conn.attempts,
/* port_range */ 40000..u16::MAX,
/* backoff_interval_micros */ plugin_data.backoff.micros,
/* jitter_percent */ plugin_data.jitter.percent,
/* grpc_buffer */ plugin_data.grpc_buffer.size,
)?;
let executor = ExecConfig::get_plugin_executor(plugin_data.clone());

// let executor = PluginExecutor::new(
// /* max_spawn_attempts */ plugin_data.max_spawn.attempts,
// /* max_conn_attempts */ plugin_data.max_conn.attempts,
// /* port_range */ 40000..u16::MAX,
// /* backoff_interval_micros */ plugin_data.backoff.micros,
// /* jitter_percent */ plugin_data.jitter.percent,
// /* grpc_buffer */ plugin_data.grpc_buffer.size,
// )?;
let core = start_plugins(policy.as_ref(), &plugin_cache, executor)?;
session.set_core(core);

Expand Down Expand Up @@ -320,26 +321,9 @@ fn load_exec_config(exec_path: Option<&Path>) -> Result<ExecConfig> {
},
None => {
// Search for file if not provided
let exec_file = "Exec.kdl";
let mut curr_dir= env::current_dir().unwrap();
loop {
let target_path = curr_dir.join(exec_file);
let target_ref = target_path.as_path();
if target_ref.exists() {
exec_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.")
.unwrap();
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");
exec_config = ExecConfig::default().unwrap();
break;
}
}
exec_config = ExecConfig::find_file()
.context("Failed to load the exec config. Please make sure the exec config file is in the provided location and is formatted correctly.")
.unwrap();
}
};

Expand Down

0 comments on commit 6c9825c

Please sign in to comment.