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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * Copyright 2020 Fluence Labs Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use crate::FaaSError;
use crate::Result;

use serde_derive::Serialize;
use serde_derive::Deserialize;

use std::convert::TryInto;
use std::path::PathBuf;

/*
An example of the config:

modules_dir = "wasm/artifacts/wasm_modules"
service_base_dir = "/Users/user/tmp"

[[module]]
    name = "ipfs_node.wasm"
    mem_pages_count = 100
    logger_enabled = true

    [module.imports]
    mysql = "/usr/bin/mysql"
    ipfs = "/usr/local/bin/ipfs"

    [module.wasi]
    envs = []
    preopened_files = ["/Users/user/tmp"]
    mapped_dirs = ["tmp" = "/Users/user/tmp"]

[default]
    mem_pages_count = 100
    logger_enabled = true

    [default.imports]
    mysql = "/usr/bin/mysql"
    ipfs = "/usr/local/bin/ipfs"

    [default.wasi]
    envs = []
    preopened_files = ["/Users/user/tmp"]
    mapped_dirs = ["tmp" = "/Users/user/tmp"]
 */

#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct RawModulesConfig {
    pub modules_dir: Option<String>,
    pub service_base_dir: Option<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub module: Vec<RawModuleConfig>,
    pub default: Option<RawDefaultModuleConfig>,
}

impl RawModulesConfig {
    /// Load config from filesystem
    pub fn load<P: Into<PathBuf>>(path: P) -> Result<Self> {
        let path = path.into();
        let bytes = std::fs::read(&path)?;
        toml::from_slice(bytes.as_slice()).map_err(|e| {
            FaaSError::ConfigParseError(format!("Error parsing config {:?}: {:?}", path, e))
        })
    }
}

impl TryInto<ModulesConfig> for RawModulesConfig {
    type Error = FaaSError;

    fn try_into(self) -> Result<ModulesConfig> {
        from_raw_modules_config(self)
    }
}

#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct RawModuleConfig {
    pub name: String,
    pub mem_pages_count: Option<u32>,
    pub logger_enabled: Option<bool>,
    pub imports: Option<toml::value::Table>,
    pub wasi: Option<RawWASIConfig>,
}

impl TryInto<ModuleConfig> for RawModuleConfig {
    type Error = FaaSError;

    fn try_into(self) -> Result<ModuleConfig> {
        from_raw_module_config(self).map(|(_, module_config)| module_config)
    }
}

#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct RawDefaultModuleConfig {
    pub mem_pages_count: Option<u32>,
    pub logger_enabled: Option<bool>,
    pub imports: Option<toml::value::Table>,
    pub wasi: Option<RawWASIConfig>,
}

impl RawModuleConfig {
    pub fn new(name: String) -> Self {
        Self {
            name,
            mem_pages_count: None,
            logger_enabled: None,
            imports: None,
            wasi: None,
        }
    }
}

#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct RawWASIConfig {
    pub envs: Option<Vec<String>>,
    pub preopened_files: Option<Vec<String>>,
    pub mapped_dirs: Option<toml::value::Table>,
}

/// Describes behaviour of all modules from a node.
#[derive(Debug, Clone, Default)]
pub struct ModulesConfig {
    /// Used for preparing filesystem on the service initialization stage.
    /// TODO: do we need that dir to be optional? We require it on creation anyways
    pub service_base_dir: Option<String>,

    /// Path to a dir where compiled Wasm modules are located.
    pub modules_dir: Option<String>,

    /// Settings for a module with particular name.
    pub modules_config: Vec<(String, ModuleConfig)>,

    /// Settings for a module that name's not been found in modules_config.
    pub default_modules_config: Option<ModuleConfig>,
}

/// Various settings that could be used to guide FCE how to load a module in a proper way.
#[derive(Debug, Clone, Default)]
pub struct ModuleConfig {
    /// Maximum memory size accessible by a module in Wasm pages (64 Kb).
    pub mem_pages_count: Option<u32>,

    /// Defines whether FaaS should provide a special host log_utf8_string function for this module.
    pub logger_enabled: bool,

    /// A list of CLI host imports that should be provided for this module.
    pub imports: Option<Vec<(String, String)>>,

    /// A WASI config.
    pub wasi: Option<WASIConfig>,
}

impl ModuleConfig {
    pub fn extend_wasi_envs(mut self, new_envs: Vec<Vec<u8>>) -> Self {
        match &mut self.wasi {
            Some(WASIConfig {
                envs: Some(envs), ..
            }) => envs.extend(new_envs),
            Some(w @ WASIConfig { envs: None, .. }) => w.envs = Some(new_envs),
            w @ None => {
                *w = Some(WASIConfig {
                    envs: Some(new_envs),
                    preopened_files: None,
                    mapped_dirs: None,
                })
            }
        }

        self
    }

    #[rustfmt::skip]
    pub fn extend_wasi_files(
        mut self,
        new_preopened_files: Vec<String>,
        new_mapped_dirs: Vec<(String, String)>,
    ) -> Self {
        match &mut self.wasi {
            Some(WASIConfig {
                preopened_files,
                mapped_dirs,
                ..
            }) => {
                match preopened_files {
                    Some(files) => files.extend(new_preopened_files),
                    f @ None => *f = Some(new_preopened_files),
                };
                match mapped_dirs {
                    Some(dirs) => dirs.extend(new_mapped_dirs),
                    d @ None => *d = Some(new_mapped_dirs),
                };
            },
            w @ None => {
                *w = Some(WASIConfig {
                    envs: None,
                    preopened_files: Some(new_preopened_files),
                    mapped_dirs: Some(new_mapped_dirs),
                })
            }
        }

        self
    }
}

#[derive(Debug, Clone, Default)]
pub struct WASIConfig {
    /// A list of environment variables available for this module.
    pub envs: Option<Vec<Vec<u8>>>,

    /// A list of files available for this module.
    /// A loaded module could have access only to files from this list.
    pub preopened_files: Option<Vec<String>>,

    /// Mapping from a usually short to full file name.
    pub mapped_dirs: Option<Vec<(String, String)>>,
}

/// Prepare config after parsing it from TOML.
fn from_raw_modules_config(config: RawModulesConfig) -> Result<ModulesConfig> {
    let service_base_dir = config.service_base_dir;
    let modules_config = config
        .module
        .into_iter()
        .map(from_raw_module_config)
        .collect::<Result<Vec<_>>>()?;

    let default_modules_config = config
        .default
        .map(from_raw_default_module_config)
        .transpose()?;

    Ok(ModulesConfig {
        service_base_dir,
        modules_dir: config.modules_dir,
        modules_config,
        default_modules_config,
    })
}

/// Parse config from TOML.
pub(crate) fn load_config(config_file_path: std::path::PathBuf) -> Result<RawModulesConfig> {
    let file_content = std::fs::read(config_file_path)?;
    Ok(toml::from_slice(&file_content)?)
}

fn from_raw_module_config(config: RawModuleConfig) -> Result<(String, ModuleConfig)> {
    let imports = config.imports.map(parse_imports).transpose()?;
    let wasi = config.wasi.map(from_raw_wasi_config);
    Ok((
        config.name,
        ModuleConfig {
            mem_pages_count: config.mem_pages_count,
            logger_enabled: config.logger_enabled.unwrap_or_default(),
            imports,
            wasi,
        },
    ))
}

fn from_raw_default_module_config(config: RawDefaultModuleConfig) -> Result<ModuleConfig> {
    let imports = config.imports.map(parse_imports).transpose()?;
    let wasi = config.wasi.map(from_raw_wasi_config);
    Ok(ModuleConfig {
        mem_pages_count: config.mem_pages_count,
        logger_enabled: config.logger_enabled.unwrap_or_default(),
        imports,
        wasi,
    })
}

fn from_raw_wasi_config(wasi: RawWASIConfig) -> WASIConfig {
    let envs = wasi
        .envs
        .map(|env| env.into_iter().map(|e| e.into_bytes()).collect::<Vec<_>>());

    let mapped_dirs = wasi.mapped_dirs.map(|mapped_dir| {
        mapped_dir
            .into_iter()
            .map(|(from, to)| (from, to.try_into::<String>().unwrap()))
            .collect::<Vec<_>>()
    });

    WASIConfig {
        envs,
        preopened_files: wasi.preopened_files,
        mapped_dirs,
    }
}

fn parse_imports(imports: toml::value::Table) -> Result<Vec<(String, String)>> {
    imports
        .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<_>>>()
}