marine/config/
marine_config.rs

1/*
2 * Copyright 2020 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use marine_wasm_backend_traits::WasmBackend;
18use marine_core::generic::HostImportDescriptor;
19use marine_core::HostAPIVersion;
20
21use std::collections::HashMap;
22use std::path::Path;
23use std::path::PathBuf;
24
25#[derive(Clone, Default, Debug)]
26pub struct ConfigContext {
27    pub base_path: Option<PathBuf>,
28}
29
30pub struct WithContext<'c, T> {
31    pub context: &'c ConfigContext,
32    pub data: T,
33}
34
35impl ConfigContext {
36    pub fn wrapped<T>(&self, data: T) -> WithContext<'_, T> {
37        WithContext {
38            context: self,
39            data,
40        }
41    }
42}
43
44/// Info to load a module from filesystem into runtime.
45#[derive(Default)]
46pub struct ModuleDescriptor<WB: WasmBackend> {
47    pub load_from: Option<PathBuf>,
48    pub file_name: String,
49    pub import_name: String,
50    pub config: MarineModuleConfig<WB>,
51}
52
53impl<WB: WasmBackend> ModuleDescriptor<WB> {
54    pub fn get_path(&self, modules_dir: &Option<PathBuf>) -> Result<PathBuf, MarineError> {
55        match &self.load_from {
56            None => match modules_dir {
57                Some(dir) => Ok(dir.join(Path::new(&self.file_name))),
58                None => Err(MarineError::InvalidConfig(format!(
59                    r#""modules_dir" field is not defined, but it is required to load module "{}""#,
60                    self.import_name
61                ))),
62            },
63            Some(path) => {
64                if path.is_file() {
65                    Ok(path.clone())
66                } else {
67                    Ok(path.join(Path::new(&self.file_name)))
68                }
69            }
70        }
71    }
72}
73
74/// Describes the behaviour of the Marine component.
75pub struct MarineConfig<WB: WasmBackend> {
76    /// Path to a dir where compiled Wasm modules are located.
77    pub modules_dir: Option<PathBuf>,
78
79    /// Total memory available for the service (in bytes)
80    pub total_memory_limit: Option<u64>,
81
82    /// Settings for a module with particular name (not HashMap because the order is matter).
83    pub modules_config: Vec<ModuleDescriptor<WB>>,
84
85    /// Settings for a module that name's not been found in modules_config.
86    pub default_modules_config: Option<MarineModuleConfig<WB>>,
87}
88
89// Manual implementation because #[derive(Default)] does not allow direct usage of non-Default wasm backend.
90impl<WB: WasmBackend> Default for MarineConfig<WB> {
91    fn default() -> Self {
92        Self {
93            modules_dir: <_>::default(),
94            total_memory_limit: <_>::default(),
95            modules_config: <_>::default(),
96            default_modules_config: <_>::default(),
97        }
98    }
99}
100
101/// Various settings that could be used to guide Marine how to load a module in a proper way.
102#[derive(Default)]
103pub struct MarineModuleConfig<WB: WasmBackend> {
104    /// Defines whether Marine should provide a special host log_utf8_string function for this module.
105    pub logger_enabled: bool,
106
107    /// Export from host functions that will be accessible on the Wasm side by provided name.
108    /// The imports are provided separately for each marine host api version
109    pub host_imports: HashMap<HostAPIVersion, HashMap<String, HostImportDescriptor<WB>>>,
110
111    /// A WASI config.
112    pub wasi: Option<MarineWASIConfig>,
113
114    /// Mask used to filter logs, for details see `log_utf8_string`
115    pub logging_mask: i32,
116}
117
118impl<WB: WasmBackend> MarineModuleConfig<WB> {
119    pub fn extend_wasi_envs(&mut self, new_envs: HashMap<String, String>) {
120        match &mut self.wasi {
121            Some(MarineWASIConfig { envs, .. }) => envs.extend(new_envs),
122            w @ None => {
123                *w = Some(MarineWASIConfig {
124                    envs: new_envs,
125                    mapped_dirs: HashMap::new(),
126                })
127            }
128        };
129    }
130
131    pub fn root_wasi_files_at(&mut self, root: &Path) {
132        match &mut self.wasi {
133            Some(MarineWASIConfig { mapped_dirs, .. }) => {
134                mapped_dirs.values_mut().for_each(|path| {
135                    *path = root.join(&path);
136                });
137            }
138            None => {}
139        }
140    }
141}
142
143#[derive(Debug, Clone, Default)]
144pub struct MarineWASIConfig {
145    /// A list of environment variables available for this module.
146    pub envs: HashMap<String, String>,
147
148    /// Mapping from a usually short to full file name.
149    pub mapped_dirs: HashMap<String, PathBuf>,
150}
151
152use super::TomlMarineConfig;
153use super::TomlMarineModuleConfig;
154use super::TomlWASIConfig;
155use super::TomlMarineNamedModuleConfig;
156use crate::MarineError;
157use crate::MarineResult;
158use crate::config::as_relative_to_base;
159use crate::config::raw_marine_config::MemoryLimit;
160
161use std::convert::TryFrom;
162use std::convert::TryInto;
163
164impl<WB: WasmBackend> TryFrom<TomlMarineConfig> for MarineConfig<WB> {
165    type Error = MarineError;
166
167    fn try_from(toml_config: TomlMarineConfig) -> Result<Self, Self::Error> {
168        let base_path = toml_config.base_path;
169        let context = ConfigContext {
170            base_path: Some(base_path),
171        };
172
173        let modules_dir = toml_config
174            .modules_dir
175            .map(|dir| as_relative_to_base(context.base_path.as_deref(), &dir))
176            .transpose()?;
177
178        let default_modules_config = toml_config
179            .default
180            .map(|m| context.wrapped(m).try_into())
181            .transpose()?;
182
183        let modules_config = toml_config
184            .module
185            .into_iter()
186            .map(|toml_module| ModuleDescriptor::try_from(context.wrapped(toml_module)))
187            .collect::<MarineResult<Vec<_>>>()?;
188
189        let total_memory_limit = match toml_config.total_memory_limit {
190            MemoryLimit::Infinity => None,
191            MemoryLimit::Value(bytesize) => Some(bytesize.as_u64()),
192        };
193
194        Ok(MarineConfig {
195            modules_dir,
196            total_memory_limit,
197            modules_config,
198            default_modules_config,
199        })
200    }
201}
202
203impl<'c, WB: WasmBackend> TryFrom<WithContext<'c, TomlMarineNamedModuleConfig>>
204    for ModuleDescriptor<WB>
205{
206    type Error = MarineError;
207
208    fn try_from(config: WithContext<'c, TomlMarineNamedModuleConfig>) -> Result<Self, Self::Error> {
209        let WithContext {
210            context,
211            data: config,
212        } = config;
213
214        let file_name = config.file_name.unwrap_or(format!("{}.wasm", config.name));
215        let load_from = config
216            .load_from
217            .map(|path| as_relative_to_base(context.base_path.as_deref(), &path))
218            .transpose()?;
219
220        Ok(ModuleDescriptor {
221            load_from,
222            file_name,
223            import_name: config.name,
224            config: context.wrapped(config.config).try_into()?,
225        })
226    }
227}
228
229impl<'c, WB: WasmBackend> TryFrom<WithContext<'c, TomlMarineModuleConfig>>
230    for MarineModuleConfig<WB>
231{
232    type Error = MarineError;
233
234    fn try_from(toml_config: WithContext<'c, TomlMarineModuleConfig>) -> Result<Self, Self::Error> {
235        let WithContext {
236            context,
237            data: toml_config,
238        } = toml_config;
239
240        let mounted_binaries = toml_config.mounted_binaries.unwrap_or_default();
241        let mounted_binaries = mounted_binaries
242            .into_iter()
243            .map(|(import_func_name, host_cmd)| {
244                let host_cmd = host_cmd.try_into::<PathBuf>()?;
245                Ok((import_func_name, host_cmd))
246            })
247            .collect::<Result<Vec<_>, Self::Error>>()?;
248
249        let mut host_imports = HashMap::from([
250            (HostAPIVersion::V0, HashMap::new()),
251            (HostAPIVersion::V1, HashMap::new()),
252            (HostAPIVersion::V2, HashMap::new()),
253            (HostAPIVersion::V3, HashMap::new()),
254        ]);
255        for (import_name, host_cmd) in mounted_binaries {
256            let host_cmd = as_relative_to_base(context.base_path.as_deref(), &host_cmd)?;
257            for (_, host_cli_imports) in &mut host_imports {
258                host_cli_imports.insert(
259                    import_name.clone(),
260                    crate::host_imports::create_mounted_binary_import(host_cmd.clone()),
261                );
262            }
263        }
264
265        let wasi = toml_config.wasi.map(|w| w.try_into()).transpose()?;
266
267        Ok(MarineModuleConfig {
268            logger_enabled: toml_config.logger_enabled.unwrap_or(true),
269            host_imports,
270            wasi,
271            logging_mask: toml_config.logging_mask.unwrap_or(i32::max_value()),
272        })
273    }
274}
275
276impl TryFrom<TomlWASIConfig> for MarineWASIConfig {
277    type Error = MarineError;
278
279    fn try_from(toml_config: TomlWASIConfig) -> Result<Self, Self::Error> {
280        let to_string = |elem: (String, toml::Value)| -> Result<(String, String), Self::Error> {
281            let to = elem
282                .1
283                .try_into::<String>()
284                .map_err(MarineError::ParseConfigError)?;
285            Ok((elem.0, to))
286        };
287
288        let to_path = |elem: (String, toml::Value)| -> Result<(String, PathBuf), Self::Error> {
289            let to = elem
290                .1
291                .try_into::<PathBuf>()
292                .map_err(MarineError::ParseConfigError)?;
293
294            Ok((elem.0, to))
295        };
296
297        let envs = toml_config.envs.unwrap_or_default();
298        let envs = envs
299            .into_iter()
300            .map(to_string)
301            .collect::<Result<HashMap<_, _>, _>>()?;
302
303        let mapped_dirs = toml_config.mapped_dirs.unwrap_or_default();
304        let mapped_dirs = mapped_dirs
305            .into_iter()
306            .map(to_path)
307            .collect::<Result<HashMap<_, _>, _>>()?;
308
309        Ok(MarineWASIConfig { envs, mapped_dirs })
310    }
311}