use std::{
ffi::OsString,
fs,
path::{Path, PathBuf},
};
use indexmap::IndexMap;
use serde::Deserialize;
use thiserror::Error;
use crate::error::PathedError;
#[derive(Debug, Error)]
pub(crate) enum InventoryError {
#[error(transparent)]
Load(LoadError),
#[error("ID `{0}` is not in the inventory")]
UnknownId(
String,
),
#[error("ID `{0}` was selected more than once")]
DuplicateSelectedId(
String,
),
}
#[derive(Debug, Error)]
pub(crate) enum LoadError {
#[error("failed to read inventory")]
Read(#[source] PathedError),
#[error("failed to parse inventory")]
Parse(#[source] PathedError<toml::de::Error>),
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct Inventory {
hosts: IndexMap<String, Host>,
}
impl IntoIterator for Inventory {
type IntoIter = indexmap::map::IntoValues<String, Host>;
type Item = Host;
fn into_iter(self) -> Self::IntoIter {
self.hosts.into_values()
}
}
impl Inventory {
pub(crate) fn load(path: &Path) -> Result<Self, InventoryError> {
let contents = fs::read_to_string(path).map_err(|source| {
InventoryError::Load(LoadError::Read(PathedError::new(
path.to_path_buf(),
source,
)))
})?;
let mut inventory: Self = toml::from_str(&contents).map_err(|source| {
InventoryError::Load(LoadError::Parse(PathedError::new(
path.to_path_buf(),
source,
)))
})?;
let directory = path.parent().unwrap_or_else(|| Path::new("."));
for (id, host) in &mut inventory.hosts {
host.id.clone_from(id);
if host.flake_path.is_relative() {
host.flake_path = directory.join(&host.flake_path);
}
}
Ok(inventory)
}
pub(crate) fn flake_groups(&self) -> Vec<(PathBuf, usize)> {
let mut flakes = IndexMap::new();
for host in self.hosts.values() {
let count = flakes.entry(host.flake_path.clone()).or_insert(0);
*count += 1;
}
flakes.into_iter().collect()
}
pub(crate) fn select<S: AsRef<str>>(mut self, requested: &[S]) -> Result<Self, InventoryError> {
if requested.is_empty() {
return Ok(self);
}
let mut hosts = IndexMap::new();
for id in requested {
let id = id.as_ref();
if hosts.contains_key(id) {
return Err(InventoryError::DuplicateSelectedId(id.to_owned()));
}
let Some((id, host)) = self.hosts.shift_remove_entry(id) else {
return Err(InventoryError::UnknownId(id.to_owned()));
};
hosts.insert(id, host);
}
Ok(Self { hosts })
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct Host {
#[serde(skip)]
id: String,
ssh_host: String,
flake_path: PathBuf,
flake_name: String,
}
impl Host {
pub(crate) fn id(&self) -> &str {
&self.id
}
pub(crate) fn flake_path(&self) -> &Path {
&self.flake_path
}
pub(crate) fn flake_name(&self) -> &str {
&self.flake_name
}
pub(crate) fn display_flake(&self) -> String {
let name = self
.flake_path
.file_name()
.map_or_else(|| self.flake_path.as_os_str(), |name| name)
.to_string_lossy();
format!("{name}#{}", self.flake_name)
}
pub(crate) fn flake(&self) -> OsString {
self.flake_output(&self.flake_name)
}
pub(crate) fn nixos_output(&self) -> OsString {
self.flake_output(&format!(
"nixosConfigurations.{}.config.system.build.toplevel.outPath",
self.flake_name
))
}
pub(crate) fn verifier_output(&self, system: &str) -> OsString {
self.flake_output(&format!("danceVerifiers.{system}.{}", self.flake_name))
}
fn flake_output(&self, output: &str) -> OsString {
let mut selector = self.flake_path.as_os_str().to_owned();
selector.push("#");
selector.push(output);
selector
}
pub(crate) fn ssh_url(&self) -> String {
format!("root@{}", self.ssh_host)
}
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use indexmap::IndexMap;
use super::{Host, Inventory, InventoryError, LoadError};
fn inventory(ids: &[&str]) -> Inventory {
Inventory {
hosts: ids
.iter()
.map(|id| {
(
(*id).to_owned(),
Host {
id: (*id).to_owned(),
ssh_host: (*id).to_owned(),
flake_path: PathBuf::from(id),
flake_name: (*id).to_owned(),
},
)
})
.collect::<IndexMap<_, _>>(),
}
}
#[test]
fn reports_inventory_load_errors() {
let directory = tempfile::tempdir().expect("temporary directory should be created");
let missing = directory.path().join("missing.toml");
assert!(matches!(
Inventory::load(&missing),
Err(InventoryError::Load(LoadError::Read(error))) if error.path == missing
));
let malformed = directory.path().join("malformed.toml");
std::fs::write(&malformed, "not TOML =").expect("malformed inventory should be written");
assert!(matches!(
Inventory::load(&malformed),
Err(InventoryError::Load(LoadError::Parse(error))) if error.path == malformed
));
}
#[test]
fn parses_and_selects_hosts_by_unique_inventory_id() {
let parsed: Inventory = toml::from_str(
r#"
[hosts.party]
ssh_host = "party"
flake_path = "sgflt.net"
flake_name = "party"
[hosts.www]
ssh_host = "www"
flake_path = "www"
flake_name = "www"
"#,
)
.expect("keyed hosts should parse");
assert_eq!(
parsed.hosts.keys().map(String::as_str).collect::<Vec<_>>(),
["party", "www"]
);
let requested = ["party".to_owned(), "www".to_owned()];
let selected = inventory(&["www", "other", "party"])
.select(&requested)
.expect("known hosts should be selected");
let ids = selected.into_iter().map(|host| host.id).collect::<Vec<_>>();
assert_eq!(ids, ["party", "www"]);
let mut shared = inventory(&["www", "party", "other"]);
shared
.hosts
.get_index_mut(1)
.expect("second host should exist")
.1
.flake_path = PathBuf::from("www");
let flakes = shared.flake_groups();
assert_eq!(
flakes,
[(PathBuf::from("www"), 2), (PathBuf::from("other"), 1)]
);
let unknown = inventory(&["www"]).select(&["party"]);
assert!(matches!(
unknown,
Err(InventoryError::UnknownId(id)) if id == "party"
));
let duplicate = inventory(&["www"]).select(&["www", "www"]);
assert!(matches!(
duplicate,
Err(InventoryError::DuplicateSelectedId(id)) if id == "www"
));
}
}