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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use marine::HostImportDescriptor;
use std::collections::HashMap;
use std::collections::HashSet;
use std::path::{PathBuf};
#[derive(Default)]
pub struct ModuleDescriptor {
pub file_name: String,
pub import_name: String,
pub config: FaaSModuleConfig,
}
#[derive(Default)]
pub struct FaaSConfig {
pub modules_dir: Option<PathBuf>,
pub modules_config: Vec<ModuleDescriptor>,
pub default_modules_config: Option<FaaSModuleConfig>,
}
#[derive(Default)]
pub struct FaaSModuleConfig {
pub mem_pages_count: Option<u32>,
pub logger_enabled: bool,
pub host_imports: HashMap<String, HostImportDescriptor>,
pub wasi: Option<FaaSWASIConfig>,
pub logging_mask: i32,
}
impl FaaSModuleConfig {
pub fn extend_wasi_envs(&mut self, new_envs: HashMap<Vec<u8>, Vec<u8>>) {
match &mut self.wasi {
Some(FaaSWASIConfig { envs, .. }) => envs.extend(new_envs),
w @ None => {
*w = Some(FaaSWASIConfig {
envs: new_envs,
preopened_files: HashSet::new(),
mapped_dirs: HashMap::new(),
})
}
};
}
pub fn extend_wasi_files(
&mut self,
new_preopened_files: HashSet<PathBuf>,
new_mapped_dirs: HashMap<String, PathBuf>,
) {
match &mut self.wasi {
Some(FaaSWASIConfig {
preopened_files,
mapped_dirs,
..
}) => {
preopened_files.extend(new_preopened_files);
mapped_dirs.extend(new_mapped_dirs);
}
w @ None => {
*w = Some(FaaSWASIConfig {
envs: HashMap::new(),
preopened_files: new_preopened_files,
mapped_dirs: new_mapped_dirs,
})
}
};
}
}
#[derive(Debug, Clone, Default)]
pub struct FaaSWASIConfig {
pub envs: HashMap<Vec<u8>, Vec<u8>>,
pub preopened_files: HashSet<PathBuf>,
pub mapped_dirs: HashMap<String, PathBuf>,
}
use super::TomlFaaSConfig;
use super::TomlFaaSModuleConfig;
use super::TomlWASIConfig;
use super::TomlFaaSNamedModuleConfig;
use crate::FaaSError;
use std::convert::{TryFrom, TryInto};
impl TryFrom<TomlFaaSConfig> for FaaSConfig {
type Error = FaaSError;
fn try_from(toml_config: TomlFaaSConfig) -> Result<Self, Self::Error> {
let modules_dir = toml_config.modules_dir.map(PathBuf::from);
let default_modules_config = toml_config.default.map(|m| m.try_into()).transpose()?;
let modules_config = toml_config
.module
.into_iter()
.map(ModuleDescriptor::try_from)
.collect::<Result<Vec<_>, _>>()?;
Ok(FaaSConfig {
modules_dir,
modules_config,
default_modules_config,
})
}
}
impl TryFrom<TomlFaaSNamedModuleConfig> for ModuleDescriptor {
type Error = FaaSError;
fn try_from(config: TomlFaaSNamedModuleConfig) -> Result<Self, Self::Error> {
Ok(ModuleDescriptor {
file_name: config.file_name.unwrap_or(format!("{}.wasm", config.name)),
import_name: config.name,
config: config.config.try_into()?,
})
}
}
impl TryFrom<TomlFaaSModuleConfig> for FaaSModuleConfig {
type Error = FaaSError;
fn try_from(toml_config: TomlFaaSModuleConfig) -> Result<Self, Self::Error> {
let mounted_binaries = toml_config.mounted_binaries.unwrap_or_default();
let mounted_binaries = mounted_binaries
.into_iter()
.map(|(import_func_name, host_cmd)| {
let host_cmd = host_cmd.try_into::<String>()?;
Ok((import_func_name, host_cmd))
})
.collect::<Result<Vec<_>, Self::Error>>()?;
let mut host_cli_imports = HashMap::new();
for (import_name, host_cmd) in mounted_binaries {
host_cli_imports.insert(
import_name,
crate::host_imports::create_mounted_binary_import(host_cmd),
);
}
let wasi = toml_config.wasi.map(|w| w.try_into()).transpose()?;
Ok(FaaSModuleConfig {
mem_pages_count: toml_config.mem_pages_count,
logger_enabled: toml_config.logger_enabled.unwrap_or(true),
host_imports: host_cli_imports,
wasi,
logging_mask: toml_config.logging_mask.unwrap_or(i32::max_value()),
})
}
}
impl TryFrom<TomlWASIConfig> for FaaSWASIConfig {
type Error = FaaSError;
fn try_from(toml_config: TomlWASIConfig) -> Result<Self, Self::Error> {
let to_vec = |elem: (String, toml::Value)| -> Result<(Vec<u8>, Vec<u8>), Self::Error> {
let to = elem
.1
.try_into::<String>()
.map_err(FaaSError::ParseConfigError)?;
Ok((elem.0.into_bytes(), to.into_bytes()))
};
let to_path = |elem: (String, toml::Value)| -> Result<(String, PathBuf), Self::Error> {
let to = elem
.1
.try_into::<String>()
.map_err(FaaSError::ParseConfigError)?;
Ok((elem.0, PathBuf::from(to)))
};
let envs = toml_config.envs.unwrap_or_default();
let envs = envs
.into_iter()
.map(to_vec)
.collect::<Result<HashMap<_, _>, _>>()?;
let preopened_files = toml_config.preopened_files.unwrap_or_default();
let preopened_files = preopened_files
.into_iter()
.map(PathBuf::from)
.collect::<HashSet<_>>();
let mapped_dirs = toml_config.mapped_dirs.unwrap_or_default();
let mapped_dirs = mapped_dirs
.into_iter()
.map(to_path)
.collect::<Result<HashMap<_, _>, _>>()?;
Ok(FaaSWASIConfig {
envs,
preopened_files,
mapped_dirs,
})
}
}