-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.rs
135 lines (118 loc) · 3.13 KB
/
config.rs
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
use std::path::PathBuf;
fn serde_true() -> bool {
true
}
fn serde_false() -> bool {
false
}
#[derive(serde::Deserialize, serde::Serialize, Debug)]
pub enum DiskType {
#[serde(rename = "9p")]
Virtio9p,
#[serde(rename = "block")]
VirtioBlock,
}
impl DiskType {
pub fn is_9p(&self) -> bool {
match self {
Self::Virtio9p => true,
Self::VirtioBlock => false,
}
}
}
#[derive(serde::Deserialize, serde::Serialize, Debug)]
#[allow(dead_code)]
pub struct MicrovmConfig {
#[serde(rename = "disk-type")]
pub disk_type: DiskType,
#[serde(default = "serde_false")]
pub pic: bool,
#[serde(default = "serde_false")]
pub pit: bool,
#[serde(default = "serde_false")]
pub rtc: bool,
#[serde(rename = "auto-kernel-cmdline")]
#[serde(default = "serde_true")]
pub auto_kernel_cmdline: bool,
#[serde(default = "serde_false")]
pub acpi: bool,
}
#[derive(serde::Deserialize, serde::Serialize, Debug)]
#[allow(dead_code)]
pub struct QemuConfig {
#[serde(rename = "disk-type")]
pub disk_type: DiskType,
}
#[derive(serde::Deserialize, serde::Serialize, Debug)]
pub struct FirecrackerConfig {
#[serde(rename = "copy-rootfs")]
#[serde(default = "serde_true")]
pub copy_rootfs: bool,
}
#[derive(serde::Deserialize, serde::Serialize, Debug)]
#[serde(tag = "type")]
#[serde(rename_all = "lowercase")]
pub enum VmmConfig {
Microvm(MicrovmConfig),
Qemu(QemuConfig),
Firecracker(FirecrackerConfig),
}
fn default_memory_size() -> u32 {
128
}
fn default_num_cpus() -> u32 {
1
}
#[derive(serde::Deserialize, serde::Serialize, Debug)]
pub struct VmConfig {
pub cmdline: String,
pub kernel: PathBuf,
pub image: PathBuf,
#[serde(default = "default_memory_size")]
pub memory: u32,
#[serde(default = "default_num_cpus")]
pub cpus: u32,
pub hypervisor: VmmConfig,
}
#[derive(serde::Deserialize, serde::Serialize, Debug)]
pub struct ContainerConfig {
pub image: String,
#[serde(default = "default_memory_size")]
pub memory: u32,
#[serde(default)]
pub runtime: Option<String>,
}
#[derive(serde::Deserialize, serde::Serialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum RuntimeConfig {
Vm(VmConfig),
Container(ContainerConfig),
}
#[derive(serde::Deserialize, serde::Serialize, Debug)]
#[allow(dead_code)]
pub struct FunctionConfig {
pub keepalive: u64,
#[serde(rename = "concurrent-requests")]
pub concurrent_requests: usize,
#[serde(rename = "single-use")]
#[serde(default = "serde_false")]
pub single_use: bool,
#[serde(flatten)]
pub runtime: RuntimeConfig,
}
impl FunctionConfig {
pub fn from_string(string: &str) -> Result<FunctionConfig, String> {
let conf = toml::from_str::<FunctionConfig>(string).map_err(|e| e.message().to_owned())?;
if conf.concurrent_requests < 1 {
return Err("concurrent-requests < 1".to_owned());
}
Ok(conf)
}
pub fn needs_netif(&self) -> bool {
if let RuntimeConfig::Vm(_) = self.runtime {
return true;
} else {
return false;
}
}
}