From 859d7db4fa936b003644a58d7a1645605f730a8b Mon Sep 17 00:00:00 2001 From: hosted-fornet Date: Tue, 19 Dec 2023 14:04:41 -0800 Subject: [PATCH] bundle bootstrap processes into binary --- .gitignore | 2 +- build.rs | 33 +++++++++++++++++++++++++++------ src/state.rs | 31 ++++++++++--------------------- 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index c1d2267b..d013db92 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,4 @@ modules/**/wit target.wasm world .env -modules/sqlite/sqlite_worker/.cargo/config.toml +src/bootstrapped_processes.rs diff --git a/build.rs b/build.rs index 1a5b7c48..fa9dffd9 100644 --- a/build.rs +++ b/build.rs @@ -158,7 +158,15 @@ fn main() { .unwrap(); run_command(Command::new("touch").args([&format!("{}/world", pwd.display())])).unwrap(); - // Build wasm32-wasi apps. + // Build wasm32-wasi apps, zip, and add to bootstrapped_processes.rs + let mut bootstrapped_processes = fs::File::create(format!( + "{}/src/bootstrapped_processes.rs", + pwd.display(), + )).unwrap(); + writeln!( + bootstrapped_processes, + "pub static BOOTSTRAPPED_PROCESSES: &[(&str, &'static [u8])] = &[", + ).unwrap(); let modules_dir = format!("{}/modules", pwd.display()); for entry in std::fs::read_dir(modules_dir).unwrap() { let entry_path = entry.unwrap().path(); @@ -185,12 +193,16 @@ fn main() { } // After processing all sub-apps, zip the parent's pkg/ directory - let writer = std::fs::File::create(format!( - "{}/target/{}.zip", + let zip_filename = format!( + "{}.zip", + entry_path.file_name().unwrap().to_str().unwrap(), + ); + let zip_path = format!( + "{}/target/{}", pwd.display(), - entry_path.file_name().unwrap().to_str().unwrap() - )) - .unwrap(); + zip_filename, + ); + let writer = std::fs::File::create(&zip_path).unwrap(); let options = zip::write::FileOptions::default() .compression_method(zip::CompressionMethod::Stored) .unix_permissions(0o755); @@ -216,5 +228,14 @@ fn main() { } } zip.finish().unwrap(); + + // Add zip bytes to bootstrapped_processes.rs + writeln!( + bootstrapped_processes, + " (\"{}\", include_bytes!(\"{}\")),", + zip_filename, + zip_path, + ).unwrap(); } + writeln!(bootstrapped_processes, "];").unwrap(); } diff --git a/src/state.rs b/src/state.rs index 01c5e5de..c8c9955d 100644 --- a/src/state.rs +++ b/src/state.rs @@ -10,6 +10,8 @@ use tokio::sync::Mutex; use crate::types::*; +include!("bootstrapped_processes.rs"); + pub async fn load_state( our_name: String, home_directory_path: String, @@ -344,8 +346,7 @@ async fn bootstrap( }); } - let packages: Vec<(String, zip::ZipArchive>>)> = - get_zipped_packages().await; + let packages = get_zipped_packages().await; for (package_name, mut package) in packages { // special case tester: only load it in if in simulation mode @@ -591,29 +592,17 @@ async fn bootstrap( Ok(()) } -/// go into /target folder and get all .zip package files -async fn get_zipped_packages() -> Vec<(String, zip::ZipArchive>>)> { +/// read in `include!()`ed .zip package files +async fn get_zipped_packages() -> Vec<(String, zip::ZipArchive>)> { println!("fs: reading distro packages...\r"); - let target_path = std::path::Path::new("target"); let mut packages = Vec::new(); - if let Ok(mut entries) = fs::read_dir(target_path).await { - while let Ok(Some(entry)) = entries.next_entry().await { - if entry.file_name().to_string_lossy().ends_with(".zip") { - let package_name = entry - .file_name() - .to_string_lossy() - .trim_end_matches(".zip") - .to_string(); - if let Ok(bytes) = fs::read(entry.path()).await { - if let Ok(zip) = zip::ZipArchive::new(std::io::Cursor::new(bytes)) { - // add to list of packages - println!("fs: found package: {}\r", package_name); - packages.push((package_name, zip)); - } - } - } + for (package_name, bytes) in BOOTSTRAPPED_PROCESSES.iter() { + if let Ok(zip) = zip::ZipArchive::new(std::io::Cursor::new(*bytes)) { + // add to list of packages + println!("fs: found package: {}\r", package_name); + packages.push((package_name.to_string(), zip)); } }