1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
use clap::Args;
use rocket::{
response::{self, Responder},
serde::json::Json,
FromForm, FromFormField, Request,
};
use rocket_okapi::okapi::schemars::{self, JsonSchema};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use strum_macros::{Display, EnumString};
use tabled::Tabled;
use zone_zfs::file_system::FileSystem;
pub static DEFAULT_ENDPOINT: &str = "127.0.0.1:8000";
pub trait PartialEqOrDefault {
fn eq_or_default(&self, other: &Self) -> bool;
}
#[derive(
Debug,
Serialize,
Deserialize,
JsonSchema,
Tabled,
FromFormField,
PartialEq,
Clone,
EnumString,
Display,
)]
#[serde(rename_all = "camelCase")]
#[strum(ascii_case_insensitive)]
pub enum ContainerStatus {
Running,
Stopped,
Unknown,
}
impl Default for ContainerStatus {
fn default() -> Self {
ContainerStatus::Unknown
}
}
#[derive(Debug, Default, Serialize, Deserialize, JsonSchema, Tabled, FromForm, Clone, Args)]
#[serde(rename_all = "camelCase")]
pub struct Container {
#[header("ID")]
pub id: u64,
#[header("Template")]
pub template: String,
#[header("User")]
pub user: String,
#[header("Status")]
pub status: ContainerStatus,
}
impl PartialEqOrDefault for Container {
fn eq_or_default(&self, other: &Self) -> bool {
(self.id == other.id || self.id == Self::default().id)
&& (self.template == other.template || self.template == Self::default().template)
&& (self.user == other.user || self.user == Self::default().user)
&& (self.status == other.status || self.status == Self::default().status)
}
}
#[rocket::async_trait]
impl<'r> Responder<'r, 'static> for Container {
fn respond_to(self, request: &'r Request<'_>) -> response::Result<'static> {
Json(self).respond_to(request)
}
}
impl TryFrom<FileSystem> for Container {
type Error = zone_zfs::Error;
fn try_from(file_system: FileSystem) -> Result<Self, Self::Error> {
let path_buf = PathBuf::from(&file_system)
.file_name()
.ok_or_else(|| {
Self::Error::FileSystem(format!("Invalid FileSystem path: {:?}", file_system))
})?
.to_string_lossy()
.into_owned();
let (user, id) = path_buf.rsplit_once("-").ok_or_else(|| {
Self::Error::FileSystem(format!("Invalid FileSystem name: {:?}", file_system))
})?;
let id = id.parse::<u64>().map_err(|err| {
Self::Error::FileSystem(format!("Failed to parse container ID: {:?}", err))
})?;
let template = PathBuf::from(&file_system)
.parent()
.ok_or_else(|| {
Self::Error::FileSystem(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 = zone_nspawn::Error;
fn try_from(value: zone_nspawn::Container) -> Result<Self, Self::Error> {
// id, template, user
let v: Vec<&str> = value.machine.split("-").collect();
Ok(Container {
id: v[0].parse().map_err(|err| Self::Error::Parsing(format!("Failed to parse container id: {:?}", err)))?,
template: v[1].to_owned(),
user: v[2].to_owned(),
status: ContainerStatus::Running,
})
}
}
|