Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

63: Added some lightweight Nix state #81

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion rust/hpos-hal/src/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use procfs::{CpuInfo, FromBufRead};
use serde_derive::{Deserialize, Serialize};
use std::fmt::{self, Display};
use std::io;
use std::{fs, fs::File};
use std::{fs, fs::read_to_string, fs::File};
use thiserror::Error;
use thiserror_context::{impl_context, Context};

Expand Down Expand Up @@ -88,6 +88,8 @@ pub struct HoloSystemInventory {
pub kernel_version: String,
/// OpenSSH Host public keys.
pub ssh_host_keys: Vec<SSHPubKey>,
/// Small amounts of static state gathered from Nix
pub nix_state: Option<HoloNixState>,
}

/// A data structure representing an OpenSSH public key. When stored, each key is a single line of
Expand All @@ -105,6 +107,60 @@ pub struct SSHPubKey {
pub label: String,
}

/// A collection of infrequently-changing Nix state.
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct HoloNixState {
/// Nix channels configurated
pub channels: Vec<(String, String)>,
/// Canonicalisation of `/run/current-system` symlink
pub current_system: String,
}

impl HoloNixState {
const NIX_CURRENT_SYSTEM_FILE: &str = "/run/current-system";
// If we ever decide to enable `use-xdg-base-directories`, we'll need this to point to
// `$XDG_STATE_HOME/nix/channels` instead.
const NIX_CHANNELS_FILE: &str = "/root/.nix-channels";
fn from_host() -> Self {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some tests for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Did you have anything specific in mind? I can put some wrapper functions around the constant path name, have the test(s) create a dummy channel file and use this code to parse it if we think there's value there.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some unit tests would be fine by me. Just anything to run through a happy path and validate it works as intended

let path = match fs::canonicalize(Self::NIX_CURRENT_SYSTEM_FILE) {
Ok(s) => s.to_string_lossy().to_string(),
Err(e) => {
// This only ought to happen if we're not running under Nix. Regardless, we can't
// do much more here and can't panic either.
info!(
"Unable to canonicalise '{}': {}",
Self::NIX_CURRENT_SYSTEM_FILE,
e
);
return Self {
channels: vec![],
current_system: "".to_string(),
};
}
};

let content: Vec<String> = read_to_string(Self::NIX_CHANNELS_FILE)
.unwrap_or_default()
.lines()
.map(|line| String::from(line.trim()))
.collect();

let mut channels: Vec<(String, String)> = vec![];
for line in content {
let mut cols = line.split(" ");
channels.push((
cols.next().unwrap_or_default().to_string(),
cols.next().unwrap_or_default().to_string(),
))
}

Self {
channels: vec![],
current_system: path.to_string(),
}
}
}

/// Data structure containing any SMBIOS/DMI attributes and identifiers that might be present.
/// These are generally useful for identifying components within a node when provided by the
/// vendor. Most vendors provide this info, holoport hardware currently doesn't provide anything
Expand Down Expand Up @@ -408,6 +464,7 @@ impl HoloInventory {
machine_id: systemd_machine_id(),
kernel_version: linux_kernel_build(),
ssh_host_keys: ssh_host_keys(),
nix_state: Some(HoloNixState::from_host()),
},
drives: HoloDriveInventory::from_host(),
cpus: HoloProcessorInventory::from_host(),
Expand Down