bundle bootstrap processes into binary

This commit is contained in:
hosted-fornet 2023-12-19 14:04:41 -08:00
parent 4baf515d5f
commit 859d7db4fa
3 changed files with 38 additions and 28 deletions

2
.gitignore vendored
View File

@ -13,4 +13,4 @@ modules/**/wit
target.wasm target.wasm
world world
.env .env
modules/sqlite/sqlite_worker/.cargo/config.toml src/bootstrapped_processes.rs

View File

@ -158,7 +158,15 @@ fn main() {
.unwrap(); .unwrap();
run_command(Command::new("touch").args([&format!("{}/world", pwd.display())])).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()); let modules_dir = format!("{}/modules", pwd.display());
for entry in std::fs::read_dir(modules_dir).unwrap() { for entry in std::fs::read_dir(modules_dir).unwrap() {
let entry_path = entry.unwrap().path(); let entry_path = entry.unwrap().path();
@ -185,12 +193,16 @@ fn main() {
} }
// After processing all sub-apps, zip the parent's pkg/ directory // After processing all sub-apps, zip the parent's pkg/ directory
let writer = std::fs::File::create(format!( let zip_filename = format!(
"{}/target/{}.zip", "{}.zip",
entry_path.file_name().unwrap().to_str().unwrap(),
);
let zip_path = format!(
"{}/target/{}",
pwd.display(), pwd.display(),
entry_path.file_name().unwrap().to_str().unwrap() zip_filename,
)) );
.unwrap(); let writer = std::fs::File::create(&zip_path).unwrap();
let options = zip::write::FileOptions::default() let options = zip::write::FileOptions::default()
.compression_method(zip::CompressionMethod::Stored) .compression_method(zip::CompressionMethod::Stored)
.unix_permissions(0o755); .unix_permissions(0o755);
@ -216,5 +228,14 @@ fn main() {
} }
} }
zip.finish().unwrap(); zip.finish().unwrap();
// Add zip bytes to bootstrapped_processes.rs
writeln!(
bootstrapped_processes,
" (\"{}\", include_bytes!(\"{}\")),",
zip_filename,
zip_path,
).unwrap();
} }
writeln!(bootstrapped_processes, "];").unwrap();
} }

View File

@ -10,6 +10,8 @@ use tokio::sync::Mutex;
use crate::types::*; use crate::types::*;
include!("bootstrapped_processes.rs");
pub async fn load_state( pub async fn load_state(
our_name: String, our_name: String,
home_directory_path: String, home_directory_path: String,
@ -344,8 +346,7 @@ async fn bootstrap(
}); });
} }
let packages: Vec<(String, zip::ZipArchive<std::io::Cursor<Vec<u8>>>)> = let packages = get_zipped_packages().await;
get_zipped_packages().await;
for (package_name, mut package) in packages { for (package_name, mut package) in packages {
// special case tester: only load it in if in simulation mode // special case tester: only load it in if in simulation mode
@ -591,29 +592,17 @@ async fn bootstrap(
Ok(()) Ok(())
} }
/// go into /target folder and get all .zip package files /// read in `include!()`ed .zip package files
async fn get_zipped_packages() -> Vec<(String, zip::ZipArchive<std::io::Cursor<Vec<u8>>>)> { async fn get_zipped_packages() -> Vec<(String, zip::ZipArchive<std::io::Cursor<&'static [u8]>>)> {
println!("fs: reading distro packages...\r"); println!("fs: reading distro packages...\r");
let target_path = std::path::Path::new("target");
let mut packages = Vec::new(); let mut packages = Vec::new();
if let Ok(mut entries) = fs::read_dir(target_path).await { for (package_name, bytes) in BOOTSTRAPPED_PROCESSES.iter() {
while let Ok(Some(entry)) = entries.next_entry().await { if let Ok(zip) = zip::ZipArchive::new(std::io::Cursor::new(*bytes)) {
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 // add to list of packages
println!("fs: found package: {}\r", package_name); println!("fs: found package: {}\r", package_name);
packages.push((package_name, zip)); packages.push((package_name.to_string(), zip));
}
}
}
} }
} }