From 84ab1655ae49c1bc09ccd1c266cd811f58ca9998 Mon Sep 17 00:00:00 2001 From: Thomas Schubart <24721048+Furisto@users.noreply.github.com> Date: Sun, 11 Dec 2022 16:12:35 +0100 Subject: [PATCH] Lint & format Signed-off-by: Thomas Schubart <24721048+Furisto@users.noreply.github.com> --- crates/libcontainer/src/workload/mod.rs | 2 +- crates/libcontainer/src/workload/wasmer.rs | 3 ++- crates/libcontainer/src/workload/wasmtime.rs | 23 ++++++++++++------ tools/wasm-sample/README.md | 25 ++++++++++++++++++++ 4 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 tools/wasm-sample/README.md diff --git a/crates/libcontainer/src/workload/mod.rs b/crates/libcontainer/src/workload/mod.rs index 548d0ac9..358bc93a 100644 --- a/crates/libcontainer/src/workload/mod.rs +++ b/crates/libcontainer/src/workload/mod.rs @@ -1,7 +1,7 @@ use anyhow::{Context, Result}; use oci_spec::runtime::Spec; -use self::{default::DefaultExecutor}; +use self::default::DefaultExecutor; #[cfg(feature = "wasm-wasmedge")] use self::wasmedge::WasmEdgeExecutor; #[cfg(feature = "wasm-wasmer")] diff --git a/crates/libcontainer/src/workload/wasmer.rs b/crates/libcontainer/src/workload/wasmer.rs index 54dd2809..5901f45f 100644 --- a/crates/libcontainer/src/workload/wasmer.rs +++ b/crates/libcontainer/src/workload/wasmer.rs @@ -42,7 +42,8 @@ impl Executor for WasmerExecutor { .finalize()?; let store = Store::default(); - let module = Module::from_file(&store, &args[0]).with_context(|| format!("could not load wasm module from {}", &args[0]))?; + let module = Module::from_file(&store, &args[0]) + .with_context(|| format!("could not load wasm module from {}", &args[0]))?; let imports = wasm_env .import_object(&module) diff --git a/crates/libcontainer/src/workload/wasmtime.rs b/crates/libcontainer/src/workload/wasmtime.rs index 5093ca8a..50d73d82 100644 --- a/crates/libcontainer/src/workload/wasmtime.rs +++ b/crates/libcontainer/src/workload/wasmtime.rs @@ -51,20 +51,29 @@ impl Executor for WasmtimeExecutor { .with_context(|| format!("could not load wasm module from {}", &cmd))?; let mut linker = Linker::new(&engine); - wasmtime_wasi::add_to_linker(&mut linker, |s| s).context("cannot add wasi context to linker")?; + wasmtime_wasi::add_to_linker(&mut linker, |s| s) + .context("cannot add wasi context to linker")?; let wasi = WasiCtxBuilder::new() .inherit_stdio() - .args(&args).context("cannot add args to wasi context")? - .envs(&envs).context("cannot add environment variables to wasi context")? + .args(args) + .context("cannot add args to wasi context")? + .envs(&envs) + .context("cannot add environment variables to wasi context")? .build(); let mut store = Store::new(&engine, wasi); - - let instance = linker.instantiate(&mut store, &module).context("wasm module could not be instantiated")?; - let start = instance.get_func(&mut store, "_start").ok_or(anyhow!("could not retrieve wasm module main function"))?; - start.call(&mut store, &mut[], &mut[]).context("wasm module was not executed successfully") + let instance = linker + .instantiate(&mut store, &module) + .context("wasm module could not be instantiated")?; + let start = instance + .get_func(&mut store, "_start") + .ok_or_else(|| anyhow!("could not retrieve wasm module main function"))?; + + start + .call(&mut store, &[], &mut []) + .context("wasm module was not executed successfully") } fn can_handle(spec: &Spec) -> Result { diff --git a/tools/wasm-sample/README.md b/tools/wasm-sample/README.md new file mode 100644 index 00000000..3485ef03 --- /dev/null +++ b/tools/wasm-sample/README.md @@ -0,0 +1,25 @@ +This is a simple wasm module for testing purposes. It prints out the arguments given to the program and all environment variables. You can compile the module with + +``` +cargo build --target wasm32-wasi +``` + +If you want youki to execute the module you must copy it to the root file system of the container and reference it in the args of the config.json. You must also ensure that the annotations contain `"run.oci.handler": "wasm"` and that youki has been compiled with one of the supported wasm runtimes. For further information please check the [documentation](https://containers.github.io/youki/user/webassembly.html). + +``` +"ociVersion": "1.0.2-dev", + "annotations": { + "run.oci.handler": "wasm" + }, + "process": { + "terminal": true, + "user": { + "uid": 0, + "gid": 0 + }, + "args": [ + "/wasm-sample.wasm", + "hello", + "wasm" + ], +```