From 6c9825c8cc2439a20eacb55f511ef3b7898b57a3 Mon Sep 17 00:00:00 2001 From: Aisha M Date: Fri, 20 Dec 2024 13:39:24 -0500 Subject: [PATCH] wip --- hipcheck/src/cli.rs | 1 - hipcheck/src/exec.rs | 37 +++++++++++++++++++++++++++++++- hipcheck/src/main.rs | 35 ++++--------------------------- hipcheck/src/session/mod.rs | 42 ++++++++++++------------------------- 4 files changed, 53 insertions(+), 62 deletions(-) diff --git a/hipcheck/src/cli.rs b/hipcheck/src/cli.rs index 8b2c758f..a0df02a5 100644 --- a/hipcheck/src/cli.rs +++ b/hipcheck/src/cli.rs @@ -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() } diff --git a/hipcheck/src/exec.rs b/hipcheck/src/exec.rs index ef4bd6d1..9275ad36 100644 --- a/hipcheck/src/exec.rs +++ b/hipcheck/src/exec.rs @@ -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 { @@ -278,6 +279,27 @@ impl ExecConfig { Self::from_str(&read_string(path)?) } + pub fn find_file() -> Result { + 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 { // These are the default values let data = r#"plugin { @@ -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 { diff --git a/hipcheck/src/main.rs b/hipcheck/src/main.rs index 72985232..5d8ef30b 100644 --- a/hipcheck/src/main.rs +++ b/hipcheck/src/main.rs @@ -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, @@ -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; - 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![ diff --git a/hipcheck/src/session/mod.rs b/hipcheck/src/session/mod.rs index 0bf21871..cec5e639 100644 --- a/hipcheck/src/session/mod.rs +++ b/hipcheck/src/session/mod.rs @@ -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::{ @@ -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); @@ -320,26 +321,9 @@ fn load_exec_config(exec_path: Option<&Path>) -> Result { }, 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(); } };