added fetch_module
This commit is contained in:
@@ -39,6 +39,7 @@ serde_json = { version = "1.0.117", optional = false }
|
||||
crossbeam = "0.8.4"
|
||||
toml = { version = "0.8.13", features = ["parse"], optional = false }
|
||||
dyn-clone = "1.0.17"
|
||||
sha2 = "0.10.8"
|
||||
|
||||
# buildin modules
|
||||
#mize_module_blob = { path = "./modules/modules/Blob" }
|
||||
@@ -72,7 +73,6 @@ http_req = { git = "https://github.com/c2vi/http_req", optional = true, default-
|
||||
axum = { version = "0.6.1", features = ["ws", "headers"], optional = true }
|
||||
axum-extra = { version = "0.4.2", features = ["spa"], optional = true }
|
||||
axum-macros = { version = "0.3.0", optional = true }
|
||||
sha2 = "0.10.8"
|
||||
#tokio-core = "0.1.18"
|
||||
#tokio-io = "0.1.13"
|
||||
|
||||
|
||||
@@ -298,10 +298,14 @@ impl Instance {
|
||||
}
|
||||
|
||||
pub fn load_module(&mut self, name: &str) -> MizeResult<()> {
|
||||
// platform specific init code
|
||||
crate::platform::any::load_module(self, name, None)
|
||||
}
|
||||
|
||||
pub fn fetch_module(&mut self, name: &str) -> MizeResult<String> {
|
||||
// platform specific way to fetch a module
|
||||
crate::platform::any::fetch_module(self, name)
|
||||
}
|
||||
|
||||
pub fn load_module_at(&mut self, name: &str, path: PathBuf) -> MizeResult<()> {
|
||||
// platform specific init code
|
||||
crate::platform::any::load_module(self, name, Some(path))
|
||||
|
||||
15
src/lib.rs
15
src/lib.rs
@@ -57,6 +57,7 @@ pub mod platform {
|
||||
pub mod os;
|
||||
|
||||
pub mod any {
|
||||
//////////// instance_init
|
||||
#[cfg(feature = "wasm-target")]
|
||||
pub use super::wasm::wasm_instance_init as instance_init;
|
||||
|
||||
@@ -67,6 +68,7 @@ pub mod platform {
|
||||
pub use super::super::instance_init;
|
||||
|
||||
|
||||
//////////// load_module
|
||||
#[cfg(feature = "os-target")]
|
||||
pub use super::os::load_module;
|
||||
|
||||
@@ -76,6 +78,17 @@ pub mod platform {
|
||||
#[cfg(not(any(feature = "os-target", feature = "wasm-target")))]
|
||||
pub use super::super::load_module;
|
||||
|
||||
|
||||
//////////// fetch_module
|
||||
#[cfg(feature = "os-target")]
|
||||
pub use super::os::fetch_module;
|
||||
|
||||
#[cfg(feature = "wasm-target")]
|
||||
pub use super::super::fetch_module;
|
||||
|
||||
#[cfg(not(any(feature = "os-target", feature = "wasm-target")))]
|
||||
pub use super::super::fetch_module;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,4 +98,6 @@ pub fn instance_init(instance: &mut core::instance::Instance) {}
|
||||
|
||||
pub fn load_module(instance: &mut core::instance::Instance, name: &str, path: Option<PathBuf>) -> MizeResult<()> { Ok(()) }
|
||||
|
||||
pub fn fetch_module(instance: &mut core::instance::Instance, name: &str, path: Option<PathBuf>) -> MizeResult<String> { Ok("oh noooooooooooooo, something went really really wrong, if this ends up in the executable....".to_owned()) }
|
||||
|
||||
|
||||
|
||||
@@ -175,7 +175,7 @@ pub fn get_module_hash(instance: &mut Instance, name: &str, mut options: ItemDat
|
||||
|
||||
}
|
||||
|
||||
fn fetch_module(instance: &mut Instance, store_path: &str, module_url: &str, module_hash: &str, module_name: &str, module_selector: &str) -> MizeResult<()> {
|
||||
fn download_module(instance: &mut Instance, store_path: &str, module_url: &str, module_hash: &str, module_name: &str, module_selector: &str) -> MizeResult<()> {
|
||||
|
||||
|
||||
let tmp_file_path_gz = format!("{}/{}.tar.gz", store_path, module_hash);
|
||||
@@ -184,7 +184,7 @@ fn fetch_module(instance: &mut Instance, store_path: &str, module_url: &str, mod
|
||||
|
||||
let mut tmp_gz_file = File::create(&tmp_file_path_gz)?;
|
||||
|
||||
debug!("fetching module '{}' with hash: {}", module_name, module_hash);
|
||||
debug!("downloading module '{}' with hash: {}", module_name, module_hash);
|
||||
|
||||
let response = http_req::request::get(format!("http://{}/mize/dist/{}-{}.tar.gz", module_url, module_hash, module_name), &mut tmp_gz_file)?;
|
||||
|
||||
@@ -214,12 +214,11 @@ fn fetch_module(instance: &mut Instance, store_path: &str, module_url: &str, mod
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load_module(instance: &mut Instance, module_name: &str, path: Option<PathBuf>) -> MizeResult<()> {
|
||||
pub fn fetch_module(instance: &mut Instance, module_name: &str) -> MizeResult<String> {
|
||||
|
||||
let module_dir_str = instance.get(format!("0/config/module_dir/{}", module_name))?.value_string();
|
||||
|
||||
let module_path = match module_dir_str {
|
||||
Err(_) => {
|
||||
if let Ok(module_path) = instance.get(format!("0/config/module_dir/{}", module_name))?.value_string() {
|
||||
return Ok(module_path);
|
||||
} else {
|
||||
// if null, load it from the url
|
||||
let module_url = instance.get("0/config/module_url")?.value_string()?;
|
||||
|
||||
@@ -227,21 +226,18 @@ pub fn load_module(instance: &mut Instance, module_name: &str, path: Option<Path
|
||||
|
||||
let (module_hash, module_selector) = get_module_hash(instance, module_name, ItemData::new())?;
|
||||
|
||||
if !PathBuf::from(format!("{}/modules/{}", store_path, module_hash.as_str())).exists()
|
||||
|| !PathBuf::from(format!("{}/modules/{}/lib/libmize_module_{}.so", store_path.as_str(), module_hash.as_str(), module_name)).exists()
|
||||
{
|
||||
// fetch module if it does not exist
|
||||
fetch_module(instance, store_path.as_str(), module_url.as_str(), module_hash.as_str(), module_name, module_selector.as_str())?;
|
||||
if !PathBuf::from(format!("{}/modules/{}", store_path, module_hash.as_str())).exists() {
|
||||
// download module if it does not exist
|
||||
download_module(instance, store_path.as_str(), module_url.as_str(), module_hash.as_str(), module_name, module_selector.as_str())?;
|
||||
}
|
||||
|
||||
// the module_path
|
||||
format!("{}/modules/{}/lib/libmize_module_{}.so", store_path, module_hash, module_name)
|
||||
},
|
||||
Ok(module_dir_str) => {
|
||||
// the module_path with the module_dir from 0/config
|
||||
format!("{}/lib/libmize_module_{}.so", module_dir_str.as_str(), module_name)
|
||||
return Ok(format!("{}/modules/{}", store_path, module_hash.as_str()));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub fn load_module(instance: &mut Instance, module_name: &str, path: Option<PathBuf>) -> MizeResult<()> {
|
||||
|
||||
let module_path = format!("{}/lib/libmmize_module_{}.so", fetch_module(instance, module_name)?, module_name);
|
||||
|
||||
let lib = unsafe { libloading::Library::new(module_path)? };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user