added fetch_module

This commit is contained in:
Sebastian Moser
2024-10-20 12:36:14 +02:00
parent 03312ee42a
commit 601362a109
4 changed files with 42 additions and 27 deletions

View File

@@ -39,6 +39,7 @@ serde_json = { version = "1.0.117", optional = false }
crossbeam = "0.8.4" crossbeam = "0.8.4"
toml = { version = "0.8.13", features = ["parse"], optional = false } toml = { version = "0.8.13", features = ["parse"], optional = false }
dyn-clone = "1.0.17" dyn-clone = "1.0.17"
sha2 = "0.10.8"
# buildin modules # buildin modules
#mize_module_blob = { path = "./modules/modules/Blob" } #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 = { version = "0.6.1", features = ["ws", "headers"], optional = true }
axum-extra = { version = "0.4.2", features = ["spa"], optional = true } axum-extra = { version = "0.4.2", features = ["spa"], optional = true }
axum-macros = { version = "0.3.0", optional = true } axum-macros = { version = "0.3.0", optional = true }
sha2 = "0.10.8"
#tokio-core = "0.1.18" #tokio-core = "0.1.18"
#tokio-io = "0.1.13" #tokio-io = "0.1.13"

View File

@@ -298,10 +298,14 @@ impl Instance {
} }
pub fn load_module(&mut self, name: &str) -> MizeResult<()> { pub fn load_module(&mut self, name: &str) -> MizeResult<()> {
// platform specific init code
crate::platform::any::load_module(self, name, None) 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<()> { pub fn load_module_at(&mut self, name: &str, path: PathBuf) -> MizeResult<()> {
// platform specific init code // platform specific init code
crate::platform::any::load_module(self, name, Some(path)) crate::platform::any::load_module(self, name, Some(path))

View File

@@ -57,6 +57,7 @@ pub mod platform {
pub mod os; pub mod os;
pub mod any { pub mod any {
//////////// instance_init
#[cfg(feature = "wasm-target")] #[cfg(feature = "wasm-target")]
pub use super::wasm::wasm_instance_init as instance_init; pub use super::wasm::wasm_instance_init as instance_init;
@@ -67,6 +68,7 @@ pub mod platform {
pub use super::super::instance_init; pub use super::super::instance_init;
//////////// load_module
#[cfg(feature = "os-target")] #[cfg(feature = "os-target")]
pub use super::os::load_module; pub use super::os::load_module;
@@ -76,6 +78,17 @@ pub mod platform {
#[cfg(not(any(feature = "os-target", feature = "wasm-target")))] #[cfg(not(any(feature = "os-target", feature = "wasm-target")))]
pub use super::super::load_module; 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 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()) }

View File

@@ -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); 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)?; 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)?; 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(()) 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(); if let Ok(module_path) = instance.get(format!("0/config/module_dir/{}", module_name))?.value_string() {
return Ok(module_path);
let module_path = match module_dir_str { } else {
Err(_) => {
// if null, load it from the url // if null, load it from the url
let module_url = instance.get("0/config/module_url")?.value_string()?; 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())?; 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() 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() // 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())?;
// 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())?;
} }
// the module_path return Ok(format!("{}/modules/{}", store_path, module_hash.as_str()));
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)
} }
}; }
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)? }; let lib = unsafe { libloading::Library::new(module_path)? };