mirror of
https://github.com/fluencelabs/marine.git
synced 2024-12-12 14:55:32 +00:00
Get file name of the interpreter from a config (#48)
This commit is contained in:
parent
d12a6c152a
commit
2dc09858a8
@ -28,8 +28,8 @@ use fluence_faas::IValue;
|
|||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use crate::errors::AquamarineVMError::InvalidAquamarinePath;
|
||||||
|
|
||||||
const AQUAMARINE_WASM_FILE_NAME: &str = "aquamarine";
|
|
||||||
const CALL_SERVICE_NAME: &str = "call_service";
|
const CALL_SERVICE_NAME: &str = "call_service";
|
||||||
const CURRENT_PEER_ID_ENV_NAME: &str = "CURRENT_PEER_ID";
|
const CURRENT_PEER_ID_ENV_NAME: &str = "CURRENT_PEER_ID";
|
||||||
|
|
||||||
@ -38,6 +38,8 @@ unsafe impl Send for AquamarineVM {}
|
|||||||
pub struct AquamarineVM {
|
pub struct AquamarineVM {
|
||||||
faas: FluenceFaaS,
|
faas: FluenceFaaS,
|
||||||
particle_data_store: PathBuf,
|
particle_data_store: PathBuf,
|
||||||
|
/// file name of the AIR interpreter .wasm
|
||||||
|
wasm_filename: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AquamarineVM {
|
impl AquamarineVM {
|
||||||
@ -45,8 +47,11 @@ impl AquamarineVM {
|
|||||||
pub fn new(config: AquamarineVMConfig) -> Result<Self> {
|
pub fn new(config: AquamarineVMConfig) -> Result<Self> {
|
||||||
use AquamarineVMError::InvalidDataStorePath;
|
use AquamarineVMError::InvalidDataStorePath;
|
||||||
|
|
||||||
let faas_config = Self::make_faas_config(
|
let (wasm_dir, wasm_filename) = split_dirname(config.aquamarine_wasm_path)?;
|
||||||
config.aquamarine_wasm_path,
|
|
||||||
|
let faas_config = make_faas_config(
|
||||||
|
wasm_dir,
|
||||||
|
&wasm_filename,
|
||||||
config.call_service,
|
config.call_service,
|
||||||
config.current_peer_id,
|
config.current_peer_id,
|
||||||
config.logging_mask,
|
config.logging_mask,
|
||||||
@ -60,6 +65,7 @@ impl AquamarineVM {
|
|||||||
Ok(Self {
|
Ok(Self {
|
||||||
faas,
|
faas,
|
||||||
particle_data_store,
|
particle_data_store,
|
||||||
|
wasm_filename,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,22 +88,52 @@ impl AquamarineVM {
|
|||||||
IValue::String(data.into()),
|
IValue::String(data.into()),
|
||||||
];
|
];
|
||||||
|
|
||||||
let result = self.faas.call_with_ivalues(
|
let result =
|
||||||
AQUAMARINE_WASM_FILE_NAME,
|
self.faas
|
||||||
"invoke",
|
.call_with_ivalues(&self.wasm_filename, "invoke", &args, <_>::default())?;
|
||||||
&args,
|
|
||||||
<_>::default(),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let raw_outcome = Self::make_raw_outcome(result)?;
|
let raw_outcome = make_raw_outcome(result)?;
|
||||||
std::fs::write(&prev_data_path, &raw_outcome.data)
|
std::fs::write(&prev_data_path, &raw_outcome.data)
|
||||||
.map_err(|e| PersistDataError(e, prev_data_path))?;
|
.map_err(|e| PersistDataError(e, prev_data_path))?;
|
||||||
|
|
||||||
raw_outcome.try_into()
|
raw_outcome.try_into()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Splits given path into its directory and file stem
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// For path `/path/to/aquamarine.wasm` result will be `Ok(PathBuf(/path/to), "aquamarine")`
|
||||||
|
fn split_dirname(path: PathBuf) -> Result<(PathBuf, String)> {
|
||||||
|
let metadata = path.metadata().map_err(|err| InvalidAquamarinePath {
|
||||||
|
invalid_path: path.clone(),
|
||||||
|
reason: "failed to get file's metadata (doesn't exist or invalid permissions)",
|
||||||
|
io_error: Some(err),
|
||||||
|
})?;
|
||||||
|
|
||||||
|
if !metadata.is_file() {
|
||||||
|
return Err(InvalidAquamarinePath {
|
||||||
|
invalid_path: path,
|
||||||
|
reason: "is not a file",
|
||||||
|
io_error: None,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let file_stem = path
|
||||||
|
.file_stem()
|
||||||
|
.expect("checked to be a file, file name must be defined");
|
||||||
|
let file_stem = file_stem.to_string_lossy().into_owned();
|
||||||
|
|
||||||
|
let mut path = path;
|
||||||
|
// drop file name from path
|
||||||
|
path.pop();
|
||||||
|
|
||||||
|
Ok((path, file_stem))
|
||||||
|
}
|
||||||
|
|
||||||
fn make_faas_config(
|
fn make_faas_config(
|
||||||
aquamarine_wasm_path: PathBuf,
|
aquamarine_wasm_dir: PathBuf,
|
||||||
|
aquamarine_wasm_file: &str,
|
||||||
call_service: HostImportDescriptor,
|
call_service: HostImportDescriptor,
|
||||||
current_peer_id: String,
|
current_peer_id: String,
|
||||||
logging_mask: i64,
|
logging_mask: i64,
|
||||||
@ -127,16 +163,9 @@ impl AquamarineVM {
|
|||||||
};
|
};
|
||||||
aquamarine_module_config.extend_wasi_envs(envs);
|
aquamarine_module_config.extend_wasi_envs(envs);
|
||||||
|
|
||||||
let mut aquamarine_wasm_dir = aquamarine_wasm_path;
|
|
||||||
// faas config requires a path to the directory with Wasm modules
|
|
||||||
aquamarine_wasm_dir.pop();
|
|
||||||
|
|
||||||
FaaSConfig {
|
FaaSConfig {
|
||||||
modules_dir: Some(aquamarine_wasm_dir),
|
modules_dir: Some(aquamarine_wasm_dir),
|
||||||
modules_config: vec![(
|
modules_config: vec![(String::from(aquamarine_wasm_file), aquamarine_module_config)],
|
||||||
String::from(AQUAMARINE_WASM_FILE_NAME),
|
|
||||||
aquamarine_module_config,
|
|
||||||
)],
|
|
||||||
default_modules_config: None,
|
default_modules_config: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -209,7 +238,6 @@ impl AquamarineVM {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// This API is intended for testing purposes
|
// This API is intended for testing purposes
|
||||||
#[cfg(feature = "raw-aquamarine-vm-api")]
|
#[cfg(feature = "raw-aquamarine-vm-api")]
|
||||||
@ -228,14 +256,11 @@ impl AquamarineVM {
|
|||||||
IValue::String(data.into()),
|
IValue::String(data.into()),
|
||||||
];
|
];
|
||||||
|
|
||||||
let result = self.faas.call_with_ivalues(
|
let result =
|
||||||
AQUAMARINE_WASM_FILE_NAME,
|
self.faas
|
||||||
"invoke",
|
.call_with_ivalues(&self.wasm_filename, "invoke", &args, <_>::default())?;
|
||||||
&args,
|
|
||||||
<_>::default(),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let raw_outcome = Self::make_raw_outcome(result)?;
|
let raw_outcome = make_raw_outcome(result)?;
|
||||||
raw_outcome.try_into()
|
raw_outcome.try_into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,13 @@ pub enum AquamarineVMError {
|
|||||||
|
|
||||||
/// Errors related to particle_data_store path from supplied config.
|
/// Errors related to particle_data_store path from supplied config.
|
||||||
InvalidDataStorePath(IOError, PathBuf),
|
InvalidDataStorePath(IOError, PathBuf),
|
||||||
|
|
||||||
|
/// Specified path to AIR interpreter .wasm file was invalid
|
||||||
|
InvalidAquamarinePath {
|
||||||
|
invalid_path: PathBuf,
|
||||||
|
io_error: Option<IOError>,
|
||||||
|
reason: &'static str,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error for AquamarineVMError {}
|
impl Error for AquamarineVMError {}
|
||||||
@ -57,6 +64,18 @@ impl std::fmt::Display for AquamarineVMError {
|
|||||||
"an error occurred while creating data storage {:?} by {:?} path",
|
"an error occurred while creating data storage {:?} by {:?} path",
|
||||||
err, path
|
err, path
|
||||||
),
|
),
|
||||||
|
|
||||||
|
AquamarineVMError::InvalidAquamarinePath {
|
||||||
|
invalid_path,
|
||||||
|
io_error,
|
||||||
|
reason,
|
||||||
|
} => {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"path to AIR interpreter .wasm ({:?}) is invalid: {}; IO Error: {:?}",
|
||||||
|
invalid_path, reason, io_error
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user