Skip to content

Commit

Permalink
Init minion's config globally for access from different spots
Browse files Browse the repository at this point in the history
  • Loading branch information
isbm committed Dec 5, 2024
1 parent 39a5ed1 commit 8585d58
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
12 changes: 11 additions & 1 deletion libsysinspect/src/cfg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ Config reader
pub mod mmconf;

use crate::SysinspectError;
use mmconf::MinionConfig;
use nix::unistd::Uid;
use once_cell::sync::OnceCell;
use std::{env, path::PathBuf};

pub const APP_CONF: &str = "sysinspect.conf";
pub const APP_DOTCONF: &str = ".sysinspect";
pub const APP_HOME: &str = "/etc/sysinspect";

/// Select app conf
pub fn select_config(p: Option<&str>) -> Result<PathBuf, SysinspectError> {
pub fn select_config_path(p: Option<&str>) -> Result<PathBuf, SysinspectError> {
// Override path from options
if let Some(ovrp) = p {
let ovrp = PathBuf::from(ovrp);
Expand Down Expand Up @@ -52,3 +54,11 @@ pub fn select_config(p: Option<&str>) -> Result<PathBuf, SysinspectError> {

Err(SysinspectError::ConfigError("No config has been found".to_string()))
}

/// Minion Confinguration
static _MINION_CFG: OnceCell<MinionConfig> = OnceCell::new();

/// Returns a copy of initialised traits.
pub fn get_minion_config(p: Option<&str>) -> Result<MinionConfig, SysinspectError> {
Ok(_MINION_CFG.get_or_try_init(|| MinionConfig::new(select_config_path(p)?))?.to_owned())
}
4 changes: 2 additions & 2 deletions libsysinspect/src/mdescr/mspec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{datapatch, mspecdef::ModelSpec};
use crate::{cfg::select_config, tmpl::render::ModelTplRender, traits::systraits::SystemTraits, SysinspectError};
use crate::{cfg::select_config_path, tmpl::render::ModelTplRender, traits::systraits::SystemTraits, SysinspectError};
use serde_yaml::Value;
use std::{
collections::HashMap,
Expand Down Expand Up @@ -141,7 +141,7 @@ impl SpecLoader {
}

// Load app config and merge to the main model
base.push(serde_yaml::from_str::<Value>(&fs::read_to_string(select_config(None)?)?)?);
base.push(serde_yaml::from_str::<Value>(&fs::read_to_string(select_config_path(None)?)?)?);

let mut base = self.merge_parts(&mut base)?;
if !iht.is_empty() {
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::ArgMatches;
use colored::Colorize;
use libsysinspect::{
cfg::{mmconf::MasterConfig, select_config},
cfg::{mmconf::MasterConfig, select_config_path},
inspector::SysInspectRunner,
logger,
reactor::handlers,
Expand Down Expand Up @@ -50,7 +50,7 @@ fn set_logger(p: &ArgMatches) {

/// Get configuration of the master
fn get_cfg(p: &ArgMatches) -> Result<MasterConfig, SysinspectError> {
MasterConfig::new(select_config(p.get_one::<&str>("config").cloned())?)
MasterConfig::new(select_config_path(p.get_one::<&str>("config").cloned())?)
}

fn main() {
Expand Down
4 changes: 2 additions & 2 deletions sysmaster/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod rmt;

use clidef::cli;
use libsysinspect::{
cfg::{mmconf::MasterConfig, select_config},
cfg::{mmconf::MasterConfig, select_config_path},
logger, SysinspectError,
};
use log::LevelFilter;
Expand Down Expand Up @@ -49,7 +49,7 @@ async fn main() -> Result<(), SysinspectError> {
// Get config
let mut cfp = PathBuf::from(params.get_one::<String>("config").unwrap_or(&"".to_string()).to_owned());
if !cfp.exists() {
cfp = match select_config(None) {
cfp = match select_config_path(None) {
Ok(cfp) => {
log::debug!("Reading config at {}", cfp.to_str().unwrap_or_default());
cfp
Expand Down
6 changes: 2 additions & 4 deletions sysminion/src/minion.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{filedata::MinionFiledata, proto, rsa::MinionRSAKeyManager};
use libsysinspect::{
cfg::{self, mmconf::MinionConfig},
cfg::{get_minion_config, mmconf::MinionConfig},
inspector::SysInspectRunner,
proto::{
errcodes::ProtoErrorCode,
Expand Down Expand Up @@ -37,9 +37,7 @@ pub struct SysMinion {

impl SysMinion {
pub async fn new(cfp: &str, fingerprint: Option<String>) -> Result<Arc<SysMinion>, SysinspectError> {
let cfp = cfg::select_config(Some(cfp))?;

let cfg = MinionConfig::new(cfp)?;
let cfg = get_minion_config(Some(cfp))?;
let (rstm, wstm) = TcpStream::connect(cfg.master()).await.unwrap().into_split();
let instance = SysMinion {
cfg: cfg.clone(),
Expand Down

0 comments on commit 8585d58

Please sign in to comment.