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
world
.env
modules/sqlite/sqlite_worker/.cargo/config.toml
src/bootstrapped_processes.rs

View File

@ -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();
}

View File

@ -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<std::io::Cursor<Vec<u8>>>)> =
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<std::io::Cursor<Vec<u8>>>)> {
/// read in `include!()`ed .zip package files
async fn get_zipped_packages() -> Vec<(String, zip::ZipArchive<std::io::Cursor<&'static [u8]>>)> {
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));
}
}