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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use std::str::FromStr;
use std::{collections::HashMap, fmt::Display};
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use super::util::parse_key_val;
use crate::commands::containers::types::ContainerType;
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
pub struct Vgpu {
#[serde(rename = "type")]
pub type_: String,
pub count: u32,
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, Default)]
#[repr(u32)]
pub enum RamSizes {
#[default]
#[serde(rename = "128M")]
M128,
#[serde(rename = "256M")]
M256,
#[serde(rename = "512M")]
M512,
#[serde(rename = "1G")]
G1,
#[serde(rename = "2G")]
G2,
#[serde(rename = "4G")]
G4,
#[serde(rename = "8G")]
G8,
#[serde(rename = "16G")]
G16,
#[serde(rename = "32G")]
G32,
#[serde(rename = "64G")]
G64,
}
impl FromStr for RamSizes {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> {
serde_json::from_str(&format!("\"{}\"", s.to_uppercase())).map_err(|e| anyhow!(e))
}
}
impl ToString for RamSizes {
fn to_string(&self) -> String {
serde_json::to_string(self).unwrap().replace('"', "")
}
}
impl RamSizes {
pub fn values() -> Vec<RamSizes> {
vec![
RamSizes::M128,
RamSizes::M256,
RamSizes::M512,
RamSizes::G1,
RamSizes::G2,
RamSizes::G4,
RamSizes::G8,
RamSizes::G16,
RamSizes::G32,
RamSizes::G64,
]
}
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
pub struct Resources {
pub vcpu: f64,
pub ram: String,
#[serde(skip)]
pub vgpu: Vec<Vgpu>,
}
impl Default for Resources {
fn default() -> Self {
Resources {
vcpu: 0.5,
ram: RamSizes::default().to_string(),
vgpu: vec![],
}
}
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, Default)]
pub enum ScalingStrategy {
#[default]
#[serde(rename = "manual")]
Manual,
#[serde(rename = "autoscale")]
Autoscaled,
}
impl FromStr for ScalingStrategy {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> {
serde_json::from_str(&format!("\"{}\"", s.to_lowercase())).map_err(|e| anyhow!(e))
}
}
impl Display for ScalingStrategy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
serde_json::to_string(self).unwrap().replace('"', "")
)
}
}
impl ScalingStrategy {
pub fn values() -> Vec<ScalingStrategy> {
vec![ScalingStrategy::Manual, ScalingStrategy::Autoscaled]
}
}
#[derive(Debug, Deserialize, Serialize, Clone, Default, PartialEq, Eq)]
pub struct Image {
pub name: String,
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Default)]
pub struct Config {
pub version: String,
#[serde(rename = "type")]
pub type_: ContainerType,
pub image: Image,
pub env: HashMap<String, String>,
pub container_strategy: ScalingStrategy,
pub resources: Resources,
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Default)]
pub struct Deployment {
pub id: String,
pub name: String,
pub created_at: String,
pub container_count: u64,
pub config: Config,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct SingleDeployment {
pub deployment: Deployment,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct MultipleDeployments {
pub deployments: Vec<Deployment>,
}
#[derive(Debug, Serialize, Clone, Default, PartialEq)]
pub struct CreateDeployment {
pub container_strategy: ScalingStrategy,
pub env: HashMap<String, String>,
pub image: Image,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
pub resources: Resources,
#[serde(rename = "type")]
pub type_: ContainerType,
}
impl CreateDeployment {
pub fn from_deployment(deployment: &Deployment) -> Self {
Self {
container_strategy: deployment.config.container_strategy.clone(),
env: deployment.config.env.clone(),
image: deployment.config.image.clone(),
name: Some(deployment.name.clone()),
resources: deployment.config.resources.clone(),
type_: deployment.config.type_.clone(),
}
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Env(pub String, pub String);
impl FromStr for Env {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> {
match parse_key_val(s) {
Ok((key, val)) => Ok(Env(key, val)),
Err(e) => Err(anyhow!("Could not pase env value: {}", e)),
}
}
}
#[derive(Debug, Serialize)]
pub struct ScaleRequest {
pub scale: u64,
}