diff options
Diffstat (limited to 'zone_core')
-rw-r--r-- | zone_core/Cargo.toml | 3 | ||||
-rw-r--r-- | zone_core/src/lib.rs | 43 |
2 files changed, 46 insertions, 0 deletions
diff --git a/zone_core/Cargo.toml b/zone_core/Cargo.toml index 8ac3fa3..6051564 100644 --- a/zone_core/Cargo.toml +++ b/zone_core/Cargo.toml @@ -19,3 +19,6 @@ serde = "1.0.136" strum = "0.23.0" strum_macros = "0.23.1" tabled = "0.4.2" +zone_zfs = { version = "0.1.0", path = "../zone_zfs" } +zone_nspawn = { version = "0.1.0", path = "../zone_nspawn" } +anyhow = "1.0.53" diff --git a/zone_core/src/lib.rs b/zone_core/src/lib.rs index ca204fd..1c7fab2 100644 --- a/zone_core/src/lib.rs +++ b/zone_core/src/lib.rs @@ -1,9 +1,13 @@ +use std::path::PathBuf; + +use anyhow::{Context}; use clap::Args; use rocket::{FromForm, FromFormField}; use rocket_okapi::okapi::schemars::{self, JsonSchema}; use serde::{Deserialize, Serialize}; use strum_macros::{Display, EnumString}; use tabled::Tabled; +use zone_zfs::file_system::FileSystem; pub static DEFAULT_ENDPOINT: &str = "127.0.0.1:8000"; @@ -61,3 +65,42 @@ impl PartialEqOrDefault for Container { && (self.status == other.status || self.status == Self::default().status) } } + +impl TryFrom<FileSystem> for Container { + type Error = anyhow::Error; + + fn try_from(file_system: FileSystem) -> Result<Self, Self::Error> { + let path_buf = PathBuf::from(&file_system) + .file_name() + .context(format!("Invalid FileSystem path: {:?}", file_system))? + .to_string_lossy() + .into_owned(); + + let (user, id) = path_buf + .rsplit_once("-") + .context(format!("Invalid FileSystem name: {:?}", file_system))?; + + let id = id.parse::<u64>().context("Failed to parse container ID")?; + + let template = PathBuf::from(&file_system) + .parent() + .context(format!("Invalid path for filesystem: {:?}", &file_system))? + .to_string_lossy() + .into_owned(); + + Ok(Container { + id, + template, + user: user.to_string(), + status: ContainerStatus::default(), + }) + } +} + +impl TryFrom<zone_nspawn::Container> for Container { + type Error = anyhow::Error; + + fn try_from(_value: zone_nspawn::Container) -> Result<Self, Self::Error> { + todo!() + } +} |