Skip to content

Commit

Permalink
Merge pull request #51 from tinythings/isbm-ansible-integration
Browse files Browse the repository at this point in the history
Add bridge module for the integration with Ansible
  • Loading branch information
isbm authored Dec 6, 2024
2 parents 93978d8 + a9a1896 commit 9a4b95d
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 65 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
docs/_build
man/*8
package/
.tmp/
.tmp/
/vendor
56 changes: 28 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion libsysinspect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ prettytable-rs = "0.10.0"
rand = "0.8.5"
regex = "1.10.6"
rsa = { version = "0.9.6", features = ["pkcs5", "sha1", "sha2"] }
rustpython = { version = "0.4.0", features = ["freeze-stdlib"] }
#rustpython = { path = "../../RustPython", features = ["freeze-stdlib"] }
rustpython-pylib = { version = "0.4.0", features = ["freeze-stdlib"] }
rustpython = { version = "0.4.0", features = ["freeze-stdlib"] }
rustpython-vm = { version = "0.4.0", features = ["freeze-stdlib"] }
serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
Expand Down
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
37 changes: 37 additions & 0 deletions libsysinspect/src/pylang/pylib/pysystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rustpython_vm::pymodule;
#[pymodule]
pub mod syscore {
use crate::{
cfg::{get_minion_config, mmconf::MinionConfig},
traits::{self, systraits::SystemTraits},
util::dataconv,
};
Expand Down Expand Up @@ -69,6 +70,42 @@ pub mod syscore {
Ok(MinionTraits::new())
}

#[pyattr]
#[pyclass(module = "syscore", name = "__AnsibleBridge")]
#[derive(Debug, PyPayload)]
pub struct AnsibleBridge {
cfg: MinionConfig,
}

#[pyclass]
impl AnsibleBridge {
#[pymethod]
pub fn sharelib(&self, _vm: &VirtualMachine) -> PyObjectRef {
_vm.ctx.new_str(self.cfg.sharelib_dir().to_string_lossy().to_string()).into()
}

#[pymethod]
pub fn root_dir(&self, _vm: &VirtualMachine) -> PyObjectRef {
_vm.ctx.new_str(self.cfg.root_dir().to_string_lossy().to_string()).into()
}

#[pymethod]
pub fn builtin_path(&self, name: String, _vm: &VirtualMachine) -> PyObjectRef {
let p = self.cfg.sharelib_dir().join(format!("lib/ansible/modules/{name}.py"));
if p.exists() {
return _vm.ctx.new_str(p.to_string_lossy().to_string()).into();
}

_vm.ctx.none()
}
}

#[pyfunction]
#[allow(non_snake_case)]
fn AnsibleBridge() -> PyResult<AnsibleBridge> {
Ok(AnsibleBridge { cfg: get_minion_config(None).unwrap() })
}

#[derive(Debug)]
struct Inner {
retcode: usize,
Expand Down
2 changes: 1 addition & 1 deletion libsysinspect/src/reactor/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl Receiver {
/// `eid` - Entity Id
/// `response` - ActionResponse object
pub fn register(&mut self, eid: String, response: ActionResponse) {
log::trace!("Registered action response: {:#?}", response);
log::debug!("Registered action response: {:#?}", response);
self.actions.insert(eid, response);

// XXX: And process here as well!
Expand Down
5 changes: 4 additions & 1 deletion libsysinspect/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ pub fn matches_traits(qt: Vec<Vec<HashMap<String, Value>>>, traits: SystemTraits
or_op_c.contains(&true)
}

/// System traits instance
/// System traits instance. Traits are system properties and attributes
/// on which a minion is running.
///
/// P.S. These are not Rust traits. :-)
static _TRAITS: OnceCell<SystemTraits> = OnceCell::new();

/// Returns a copy of initialised traits.
Expand Down
3 changes: 3 additions & 0 deletions modules/cfg/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ansible Integration

The `cfg.mgmt` module implements Ansible integration.
Loading

0 comments on commit 9a4b95d

Please sign in to comment.