aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2022-05-01 17:18:30 -0500
committerToby Vincent <tobyv13@gmail.com>2022-05-01 17:18:30 -0500
commit730305eb1a6f9615dccb9bd94b1c371bba003615 (patch)
tree2e37fd7d64a55381e44a4b03ce51f2a580ff1f88
parentc83a62dbf5bb9afc55dbef85d6a1d9932c99a1e8 (diff)
fix: properly deserialized machinectl list output
-rw-r--r--zone_nspawn/src/lib.rs1
-rw-r--r--zone_nspawn/src/machine.rs41
-rw-r--r--zone_nspawn/src/nspawn.rs8
3 files changed, 35 insertions, 15 deletions
diff --git a/zone_nspawn/src/lib.rs b/zone_nspawn/src/lib.rs
index 32b8a32..5eb3dcf 100644
--- a/zone_nspawn/src/lib.rs
+++ b/zone_nspawn/src/lib.rs
@@ -1,5 +1,4 @@
pub use crate::error::{Error, Result};
-pub use crate::machine::Machine;
pub use crate::nspawn::NSpawn;
mod error;
diff --git a/zone_nspawn/src/machine.rs b/zone_nspawn/src/machine.rs
index 9d77242..03ad3a8 100644
--- a/zone_nspawn/src/machine.rs
+++ b/zone_nspawn/src/machine.rs
@@ -1,19 +1,17 @@
-use serde::{Deserialize, Serialize};
+use serde::Deserialize;
+use std::process::Output;
pub(crate) use crate::Error;
-#[derive(Debug, Serialize, Deserialize, Clone)]
+// TODO: test deserialization before allowing this
+// #[allow(dead_code)]
+#[derive(Debug, Deserialize)]
pub struct Machine {
pub machine: String,
-
pub class: String,
-
pub service: String,
-
pub os: String,
-
pub version: String,
-
pub addresses: String,
}
@@ -21,9 +19,6 @@ impl TryFrom<Machine> for zone_core::Container {
type Error = Error;
fn try_from(machine: Machine) -> Result<Self, Error> {
- // let name = machine.machine.to_str().ok_or(Error::Parsing(machine))?;
- // let name = machine.machine;
-
let mut iter = machine.machine.split('-');
let (id, parent, user) = (
@@ -42,3 +37,29 @@ impl TryFrom<Machine> for zone_core::Container {
Ok(zone_core::Container { id, parent, user })
}
}
+
+#[derive(Debug, Deserialize)]
+pub(crate) struct MachineList(Vec<Machine>);
+
+impl IntoIterator for MachineList {
+ type Item = Machine;
+ type IntoIter = std::vec::IntoIter<Self::Item>;
+
+ fn into_iter(self) -> Self::IntoIter {
+ self.0.into_iter()
+ }
+}
+
+impl From<MachineList> for Vec<zone_core::Container> {
+ fn from(m: MachineList) -> Self {
+ m.into_iter().filter_map(|m| m.try_into().ok()).collect()
+ }
+}
+
+impl TryFrom<Output> for MachineList {
+ type Error = Error;
+
+ fn try_from(o: Output) -> Result<Self, Self::Error> {
+ serde_json::from_slice::<MachineList>(o.stdout.as_slice()).map_err(Error::from)
+ }
+}
diff --git a/zone_nspawn/src/nspawn.rs b/zone_nspawn/src/nspawn.rs
index 89f51ef..ac9599c 100644
--- a/zone_nspawn/src/nspawn.rs
+++ b/zone_nspawn/src/nspawn.rs
@@ -5,7 +5,7 @@ use tokio::sync::mpsc::UnboundedReceiver;
use wspty::{PtyCommand, PtyMaster};
use zone_core::{Container, Runtime};
-use crate::{Error, Result};
+use crate::{machine::MachineList, Error, Result};
#[derive(Default, Debug)]
pub struct NSpawn;
@@ -42,9 +42,9 @@ impl Runtime for NSpawn {
Command::new("machinectl")
.arg("list")
.args(["-o", "json"])
- .output()
- .map_err(Error::from)
- .and_then(|o| serde_json::from_slice(o.stdout.as_slice()).map_err(Error::from))
+ .output()?
+ .try_into()
+ .map(MachineList::into)
}
fn create(&self, root: PathBuf, container: &Container) -> Result<()> {