separate load_fs and bootstrap_fs

This commit is contained in:
bitful-pannul 2023-10-05 20:42:05 +03:00
parent 3ad2a200ca
commit 31aad8d5e3
2 changed files with 28 additions and 30 deletions

View File

@ -10,21 +10,17 @@ use tokio::sync::Mutex;
use tokio::time::{interval, Duration}; use tokio::time::{interval, Duration};
mod manifest; mod manifest;
pub async fn bootstrap( pub async fn load_fs(
our_name: String, our_name: String,
home_directory_path: String, home_directory_path: String,
file_key: Vec<u8>, file_key: Vec<u8>,
fs_config: FsConfig, fs_config: FsConfig,
) -> Result<(ProcessMap, Manifest), FsError> { ) -> Result<(ProcessMap, Manifest), FsError> {
// fs bootstrapping, create home_directory, fs directory inside it, manifest + log if none. // load/create fs directory, manifest + log if none.
if let Err(e) = create_dir_if_dne(&home_directory_path).await {
panic!("{}", e);
}
let fs_directory_path_str = format!("{}/fs", &home_directory_path); let fs_directory_path_str = format!("{}/fs", &home_directory_path);
if let Err(e) = create_dir_if_dne(&fs_directory_path_str).await { let new_boot = create_dir_if_dne(&fs_directory_path_str).await.expect("failed creating fs dir!");
panic!("{}", e);
}
let fs_directory_path: std::path::PathBuf = let fs_directory_path: std::path::PathBuf =
fs::canonicalize(fs_directory_path_str).await.unwrap(); fs::canonicalize(fs_directory_path_str).await.unwrap();
@ -51,7 +47,7 @@ pub async fn bootstrap(
.expect("fs: failed to open WAL file"); .expect("fs: failed to open WAL file");
// in memory details about files. // in memory details about files.
let manifest = Manifest::load( let mut manifest = Manifest::load(
manifest_file, manifest_file,
wal_file, wal_file,
&fs_directory_path, &fs_directory_path,
@ -60,9 +56,8 @@ pub async fn bootstrap(
) )
.await .await
.expect("manifest load failed!"); .expect("manifest load failed!");
// mimic the FsAction::GetState case and get current state of ProcessId::name("kernel")
// serialize it to a ProcessHandles from process id to JoinHandle
// get kernel state for booting up
let kernel_process_id = FileIdentifier::Process(ProcessId::Name("kernel".into())); let kernel_process_id = FileIdentifier::Process(ProcessId::Name("kernel".into()));
let mut process_map: ProcessMap = HashMap::new(); let mut process_map: ProcessMap = HashMap::new();
@ -76,14 +71,17 @@ pub async fn bootstrap(
} }
} }
// NOTE OnPanic if new_boot {
// wasm bytes initial source of truth is the compiled .wasm file on-disk, but onPanic needs to come from somewhere to. // bootstrap filesystem
// for apps in /modules, special cases can be defined here. let _ = bootstrap(&our_name, &kernel_process_id, &mut process_map, &mut manifest).await.expect("fresh bootstrap failed!");
}
// we also add special-case capabilities spawned "out of thin air" here, for distro processes. Ok((process_map, manifest))
// at the moment, all bootstrapped processes are given the capability to message all others. }
// this can be easily changed in the future.
// they are also given access to all runtime modules by name // function run only upon fresh boot.
// goes through /modules, gets their .wasm bytes, injects into fs and kernel state.
async fn bootstrap(our_name: &str, kernel_process_id: &FileIdentifier, process_map: &mut ProcessMap, manifest: &mut Manifest) -> Result<()> {
let names_and_bytes = get_processes_from_directories().await; let names_and_bytes = get_processes_from_directories().await;
const RUNTIME_MODULES: [&str; 8] = [ const RUNTIME_MODULES: [&str; 8] = [
"filesystem", "filesystem",
@ -100,7 +98,7 @@ pub async fn bootstrap(
for (process_name, _) in &names_and_bytes { for (process_name, _) in &names_and_bytes {
special_capabilities.insert(Capability { special_capabilities.insert(Capability {
issuer: Address { issuer: Address {
node: our_name.clone(), node: our_name.to_string(),
process: ProcessId::Name(process_name.into()), process: ProcessId::Name(process_name.into()),
}, },
params: "\"messaging\"".into(), params: "\"messaging\"".into(),
@ -109,7 +107,7 @@ pub async fn bootstrap(
for runtime_module in RUNTIME_MODULES { for runtime_module in RUNTIME_MODULES {
special_capabilities.insert(Capability { special_capabilities.insert(Capability {
issuer: Address { issuer: Address {
node: our_name.clone(), node: our_name.to_string(),
process: ProcessId::Name(runtime_module.into()), process: ProcessId::Name(runtime_module.into()),
}, },
params: "\"messaging\"".into(), params: "\"messaging\"".into(),
@ -118,7 +116,7 @@ pub async fn bootstrap(
// give all distro processes the ability to send messages across the network // give all distro processes the ability to send messages across the network
special_capabilities.insert(Capability { special_capabilities.insert(Capability {
issuer: Address { issuer: Address {
node: our_name.clone(), node: our_name.to_string(),
process: ProcessId::Name("kernel".into()), process: ProcessId::Name("kernel".into()),
}, },
params: "\"network\"".into(), params: "\"network\"".into(),
@ -181,8 +179,7 @@ pub async fn bootstrap(
.write(&kernel_process_id, &serialized_process_map) .write(&kernel_process_id, &serialized_process_map)
.await; .await;
} }
Ok(())
Ok((process_map, manifest))
} }
async fn get_processes_from_directories() -> Vec<(String, Vec<u8>)> { async fn get_processes_from_directories() -> Vec<(String, Vec<u8>)> {
@ -578,17 +575,18 @@ pub fn hash_bytes(bytes: &[u8]) -> [u8; 32] {
hasher.finalize().into() hasher.finalize().into()
} }
async fn create_dir_if_dne(path: &str) -> Result<(), FsError> { // returns bool: if dir is new
async fn create_dir_if_dne(path: &str) -> Result<bool, FsError> {
if let Err(_) = fs::read_dir(&path).await { if let Err(_) = fs::read_dir(&path).await {
match fs::create_dir_all(&path).await { match fs::create_dir_all(&path).await {
Ok(_) => Ok(()), Ok(_) => Ok(true),
Err(e) => Err(FsError::CreateInitialDirError { Err(e) => Err(FsError::CreateInitialDirError {
path: path.into(), path: path.into(),
error: format!("{}", e), error: format!("{}", e),
}), }),
} }
} else { } else {
Ok(()) Ok(false)
} }
} }

View File

@ -188,7 +188,7 @@ async fn main() {
let http_server_port = http_server::find_open_port(8080).await.unwrap(); let http_server_port = http_server::find_open_port(8080).await.unwrap();
let (kill_tx, kill_rx) = oneshot::channel::<bool>(); let (kill_tx, kill_rx) = oneshot::channel::<bool>();
let keyfile = fs::read(format!("{}/.keys", home_directory_path)).await; let keyfile = fs::read(format!("{}/.keys", home_directory_path)).await;
let (our, networking_keypair, jwt_secret_bytes, file_key): ( let (our, networking_keypair, jwt_secret_bytes, file_key): (
Identity, Identity,
signature::Ed25519KeyPair, signature::Ed25519KeyPair,
@ -365,7 +365,7 @@ async fn main() {
file_key.to_vec(), file_key.to_vec(),
) )
}; };
// bootstrap FS. // load in fs.
let _ = print_sender let _ = print_sender
.send(Printout { .send(Printout {
verbosity: 0, verbosity: 0,
@ -373,14 +373,14 @@ async fn main() {
}) })
.await; .await;
let (kernel_process_map, manifest) = filesystem::bootstrap( let (kernel_process_map, manifest) = filesystem::load_fs(
our.name.clone(), our.name.clone(),
home_directory_path.clone(), home_directory_path.clone(),
file_key, file_key,
fs_config, fs_config,
) )
.await .await
.expect("fs bootstrap failed!"); .expect("fs load failed!");
let _ = kill_tx.send(true); let _ = kill_tx.send(true);
let _ = print_sender let _ = print_sender