Skip to content

Commit

Permalink
Merge pull request #49 from tinythings/isbm-machine-id-relocated
Browse files Browse the repository at this point in the history
machine id relocated
  • Loading branch information
isbm authored Nov 29, 2024
2 parents ace7bcf + 181be43 commit 7579f5e
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

20 changes: 20 additions & 0 deletions docs/global_config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,26 @@ and contains the following directives:
and writable directories are limited to only a few. In this case *root* must be
set according to the system setup.

``id.path``

By default, the minion Id is the ``/etc/machine-id``. However, this file is usually
present on a regular Linux server and desktop distributions, but practically never
on the embedded systems. For this reason, the alternative location of the ``machine-id``
needs to be specified. On many embedded Linux systems and Android, usually ``/etc`` is
read-only, and very few places are allowed to be written.

This option takes one of the following:

- An absolute path to an existing ``machine-id`` file
- ``relative`` keyword, so it is ``$MINION_ROOT/machine-id``, which is ``/etc/sysinspect/machine-id``
by default.

.. code-block:: yaml
id.path: </absolute/path>|relative
``master.ip``

Corresponds to ``bind.ip`` of Master node and should be identical.
Expand Down
1 change: 1 addition & 0 deletions libsysinspect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ tera = { version = "1.20.0", features = ["preserve_order", "date-locale"] }
textwrap = { version = "0.16.1", features = ["hyphenation", "terminal_size"] }
tokio = { version = "1.41.1", features = ["full"] }
unicode-segmentation = "1.12.0"
uuid = { version = "1.11.0", features = ["v4"] }
walkdir = "2.5.0"
16 changes: 16 additions & 0 deletions libsysinspect/src/cfg/mmconf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub struct MinionConfig {
/// Port of Master's fileserver. Default: 4201
#[serde(rename = "master.fileserver.port")]
master_fileserver_port: Option<u32>,

#[serde(rename = "id.path")]
machine_id: Option<String>,
}

impl MinionConfig {
Expand Down Expand Up @@ -89,6 +92,19 @@ impl MinionConfig {
pub fn traits_dir(&self) -> PathBuf {
self.root_dir().join(CFG_TRAITS_ROOT)
}

/// Return machine Id path
pub fn machine_id_path(&self) -> PathBuf {
if let Some(mid) = self.machine_id.clone() {
if mid.eq("relative") {
return self.root_dir().join("machine-id");
} else {
return PathBuf::from(mid);
}
}

PathBuf::from("/etc/machine-id")
}
}

#[derive(Debug, Serialize, Deserialize, Default, Clone)]
Expand Down
6 changes: 2 additions & 4 deletions libsysinspect/src/traits/systraits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use std::{
collections::HashMap,
fs::{self},
os::unix::fs::PermissionsExt,
path::PathBuf,
process::Command,
};

Expand Down Expand Up @@ -125,10 +124,9 @@ impl SystemTraits {
self.put(SYS_OS_DISTRO.to_string(), json!(sysinfo::System::distribution_id()));

// Machine Id (not always there)
let mip = PathBuf::from("/etc/machine-id");
let mut mid = String::default();
if mip.exists() {
if let Ok(id) = fs::read_to_string(mip) {
if self.cfg.machine_id_path().exists() {
if let Ok(id) = fs::read_to_string(self.cfg.machine_id_path()) {
mid = id.trim().to_string();
}
}
Expand Down
25 changes: 25 additions & 0 deletions libsysinspect/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
pub mod dataconv;
pub mod iofs;

use crate::SysinspectError;
use std::{fs, io, path::PathBuf};
use uuid::Uuid;

/// The `/etc/machine-id` is not always present, especially
/// on the custom embedded systems. However, this file is used
/// to identify a minion.
///
/// Write the `/etc/machine-id` (or other location), if not any yet.
pub fn write_machine_id(p: Option<PathBuf>) -> Result<(), SysinspectError> {
let p = p.unwrap_or(PathBuf::from("/etc/machine-id"));
if !p.exists() {
if let Err(err) = fs::write(p, Uuid::new_v4().to_string().replace("-", "")) {
return Err(SysinspectError::IoErr(err));
}
} else {
return Err(SysinspectError::IoErr(io::Error::new(
io::ErrorKind::AlreadyExists,
format!("File \"{}\" already exists", p.to_str().unwrap_or_default()),
)));
}

Ok(())
}
1 change: 1 addition & 0 deletions sysinspect.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ config:
# Root directory where minion keeps all data.
# Default: /etc/sysinspect — same as for master
root: /etc/sysinspect
id.path: relative
master.ip: 192.168.2.31
master.port: 4200
11 changes: 7 additions & 4 deletions sysminion/src/minion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use libsysinspect::{
},
rsa,
traits::{self},
util::dataconv,
util::{self, dataconv},
SysinspectError,
};
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -63,8 +63,6 @@ impl SysMinion {
}

let cfg = MinionConfig::new(cfp)?;
traits::get_minion_traits(Some(&cfg));

let (rstm, wstm) = TcpStream::connect(cfg.master()).await.unwrap().into_split();
let instance = SysMinion {
cfg: cfg.clone(),
Expand All @@ -83,6 +81,11 @@ impl SysMinion {
/// This creates all directory structures if none etc.
fn init(&self) -> Result<(), SysinspectError> {
log::info!("Initialising minion");
// Machine id?
if !self.cfg.machine_id_path().exists() {
util::write_machine_id(Some(self.cfg.machine_id_path()))?;
}

// Place for models
if !self.cfg.models_dir().exists() {
log::debug!(
Expand Down Expand Up @@ -111,7 +114,7 @@ impl SysMinion {
}

let mut out: Vec<String> = vec![];
for t in traits::get_minion_traits(None).items() {
for t in traits::get_minion_traits(Some(&self.cfg)).items() {
out.push(format!(
"{}: {}",
t.to_owned(),
Expand Down

0 comments on commit 7579f5e

Please sign in to comment.