Skip to content

Commit

Permalink
Update the runner trait to accept a webc::Container
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Mar 27, 2023
1 parent 869cc91 commit a75522e
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 250 deletions.
202 changes: 0 additions & 202 deletions lib/wasi/src/runners/container.rs

This file was deleted.

13 changes: 8 additions & 5 deletions lib/wasi/src/runners/emscripten.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
//! WebC container support for running Emscripten modules
use crate::runners::WapmContainer;
use anyhow::{anyhow, Context, Error};
use serde::{Deserialize, Serialize};
use wasmer::{FunctionEnv, Instance, Module, Store};
use wasmer_emscripten::{
generate_emscripten_env, is_emscripten_module, run_emscripten_instance, EmEnv,
EmscriptenGlobals,
};
use webc::metadata::{annotations::Emscripten, Command};
use webc::{
metadata::{annotations::Emscripten, Command},
Container,
};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct EmscriptenRunner {
Expand Down Expand Up @@ -57,16 +59,17 @@ impl crate::runners::Runner for EmscriptenRunner {
&mut self,
command_name: &str,
command: &Command,
container: &WapmContainer,
container: &Container,
) -> Result<Self::Output, Error> {
let Emscripten {
atom: atom_name,
main_args,
..
} = command.annotation("emscripten")?.unwrap_or_default();
let atom_name = atom_name.context("The atom name is required")?;
let atom_bytes = container
.get_atom(&atom_name)
let atoms = container.atoms();
let atom_bytes = atoms
.get(&atom_name)
.with_context(|| format!("Unable to read the \"{atom_name}\" atom"))?;

let mut module = Module::new(&self.store, atom_bytes)?;
Expand Down
6 changes: 1 addition & 5 deletions lib/wasi/src/runners/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mod container;
mod runner;

#[cfg(feature = "webc_runner_rt_emscripten")]
Expand All @@ -10,10 +9,7 @@ mod wasi_common;
#[cfg(feature = "webc_runner_rt_wcgi")]
pub mod wcgi;

pub use self::{
container::{Bindings, WapmContainer, WebcParseError, WitBindings},
runner::Runner,
};
pub use self::runner::Runner;

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct MappedDirectory {
Expand Down
10 changes: 4 additions & 6 deletions lib/wasi/src/runners/runner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use anyhow::Error;
use webc::metadata::Command;

use crate::runners::WapmContainer;
use webc::{metadata::Command, Container};

/// Trait that all runners have to implement
pub trait Runner {
Expand All @@ -19,11 +17,11 @@ pub trait Runner {
&mut self,
command_name: &str,
cmd: &Command,
container: &WapmContainer,
container: &Container,
) -> Result<Self::Output, Error>;

/// Runs the container if the container has an `entrypoint` in the manifest
fn run(&mut self, container: &WapmContainer) -> Result<Self::Output, Error> {
fn run(&mut self, container: &Container) -> Result<Self::Output, Error> {
let cmd = match container.manifest().entrypoint.as_ref() {
Some(s) => s,
None => {
Expand All @@ -35,7 +33,7 @@ pub trait Runner {
}

/// Runs the given `cmd` on the container
fn run_cmd(&mut self, container: &WapmContainer, cmd: &str) -> Result<Self::Output, Error> {
fn run_cmd(&mut self, container: &Container, cmd: &str) -> Result<Self::Output, Error> {
let command_to_exec = container
.manifest()
.commands
Expand Down
24 changes: 15 additions & 9 deletions lib/wasi/src/runners/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ use std::sync::Arc;

use anyhow::{Context, Error};
use serde::{Deserialize, Serialize};
use virtual_fs::WebcVolumeFileSystem;
use wasmer::{Module, Store};
use webc::metadata::{annotations::Wasi, Command};
use webc::{
metadata::{annotations::Wasi, Command},
Container,
};

use crate::{
runners::{wasi_common::CommonWasiOptions, MappedDirectory, WapmContainer},
runners::{wasi_common::CommonWasiOptions, MappedDirectory},
PluggableRuntime, VirtualTaskManager, WasiEnvBuilder,
};

Expand Down Expand Up @@ -118,13 +122,14 @@ impl WasiRunner {

fn prepare_webc_env(
&self,
container: &WapmContainer,
container: &Container,
program_name: &str,
wasi: &Wasi,
) -> Result<WasiEnvBuilder, anyhow::Error> {
let mut builder =
self.wasi
.prepare_webc_env(container.container_fs(), program_name, wasi)?;
let fs = WebcVolumeFileSystem::mount_all(container);
let mut builder = self
.wasi
.prepare_webc_env(Arc::new(fs), program_name, wasi)?;

if let Some(tasks) = &self.tasks {
let rt = PluggableRuntime::new(Arc::clone(tasks));
Expand All @@ -148,14 +153,15 @@ impl crate::runners::Runner for WasiRunner {
&mut self,
command_name: &str,
command: &Command,
container: &WapmContainer,
container: &Container,
) -> Result<Self::Output, Error> {
let wasi = command
.annotation("wasi")?
.unwrap_or_else(|| Wasi::new(command_name));
let atom_name = &wasi.atom;
let atom = container
.get_atom(atom_name)
let atoms = container.atoms();
let atom = atoms
.get(atom_name)
.with_context(|| format!("Unable to get the \"{atom_name}\" atom"))?;

let mut module = Module::new(&self.store, atom)?;
Expand Down
18 changes: 12 additions & 6 deletions lib/wasi/src/runners/wasi_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ impl CommonWasiOptions {
let fs = prepare_filesystem(&self.mapped_dirs, container_fs, |path| {
builder.add_preopen_dir(path).map_err(Error::from)
})?;
builder.set_fs(fs);

builder.add_preopen_dir("/")?;
builder.add_preopen_dir(".")?;
if fs.read_dir(".".as_ref()).is_ok() {
// Sometimes "." won't be mounted so preopening will fail.
builder.add_preopen_dir(".")?;
}

builder.set_fs(fs);

self.populate_env(wasi, &mut builder);
self.populate_args(wasi, &mut builder);
Expand Down Expand Up @@ -80,7 +85,6 @@ fn prepare_filesystem(

if !mapped_dirs.is_empty() {
let host_fs: Arc<dyn FileSystem + Send + Sync> = Arc::new(crate::default_fs_backing());
dbg!(mapped_dirs);

for dir in mapped_dirs {
let MappedDirectory { host, guest } = dir;
Expand Down Expand Up @@ -133,7 +137,8 @@ fn create_dir_all(fs: &dyn FileSystem, path: &Path) -> Result<(), Error> {
mod tests {
use tempfile::TempDir;

use crate::runners::WapmContainer;
use virtual_fs::WebcVolumeFileSystem;
use webc::Container;

use super::*;

Expand All @@ -149,9 +154,10 @@ mod tests {
guest: "/home".to_string(),
host: sub_dir,
}];
let container = WapmContainer::from_bytes(PYTHON.into()).unwrap();
let container = Container::from_bytes(PYTHON).unwrap();
let webc_fs = WebcVolumeFileSystem::mount_all(&container);

let fs = prepare_filesystem(&mapping, container.container_fs(), |_| Ok(())).unwrap();
let fs = prepare_filesystem(&mapping, Arc::new(webc_fs), |_| Ok(())).unwrap();

assert!(fs.metadata("/home/file.txt".as_ref()).unwrap().is_file());
assert!(fs.metadata("lib".as_ref()).unwrap().is_dir());
Expand Down
Loading

0 comments on commit a75522e

Please sign in to comment.