Skip to content

Commit

Permalink
feat: update the wws shim to use the latest main from runwasi
Browse files Browse the repository at this point in the history
Signed-off-by: jiaxiao zhou <[email protected]>
  • Loading branch information
Mossaka committed Aug 20, 2023
1 parent df29600 commit 6c2d5a3
Show file tree
Hide file tree
Showing 9 changed files with 713 additions and 309 deletions.
647 changes: 528 additions & 119 deletions containerd-shim-wws-v1/Cargo.lock

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions containerd-shim-wws-v1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@ Containerd shim for running Wasm Workers Server workloads.
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
containerd-shim = "0.3"
containerd-shim-wasm = "0.1"
containerd-shim = "0.4.0"
containerd-shim-wasm = { git = "https://github.com/containerd/runwasi", rev = "6287dff637b0ac96a43a6bbe2c7919f8b2f2cf27", features = ["cgroupsv2"]}
wws-config = { git = "https://github.com/vmware-labs/wasm-workers-server", tag = "v1.4.0" }
wws-server = { git = "https://github.com/vmware-labs/wasm-workers-server", tag = "v1.4.0" }
wws-router = { git = "https://github.com/vmware-labs/wasm-workers-server", tag = "v1.4.0" }
log = "0.4"
tokio = { version = "1", features = [ "full" ] }
tokio-util = { version = "0.7", features = [ "codec" ]}
chrono = "0.4"
libcontainer = { version = "0.1", features = ["v2"], default-features = false }
oci-spec = "0.6.2"
libc = "0.2.147"
nix = "0.26.2"
anyhow = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

[target.x86_64-unknown-linux-musl.dependencies]
openssl = { version = "=0.10.48", features = ["vendored"] }
Expand Down
88 changes: 88 additions & 0 deletions containerd-shim-wws-v1/src/executor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use anyhow::Result;
use log::{error, info};
use nix::unistd::{dup, dup2};
use std::{os::fd::RawFd, path::PathBuf};
use tokio::runtime::Runtime;

use containerd_shim_wasm::sandbox::oci;
use libc::STDERR_FILENO;
use libcontainer::workload::{Executor, ExecutorError};
use oci_spec::runtime::Spec;
use wws_config::Config;
use wws_router::Routes;
use wws_server::serve;

/// URL to listen to in wws
const WWS_ADDR: &str = "0.0.0.0";
const WWS_PORT: u16 = 3000;
const EXECUTOR_NAME: &str = "wws";

pub struct WwsExecutor {
pub stderr: Option<RawFd>,
}

impl WwsExecutor {}

impl Executor for WwsExecutor {
fn exec(&self, spec: &Spec) -> Result<(), ExecutorError> {
let args = oci::get_args(spec);
if args.is_empty() {
return Err(ExecutorError::InvalidArg);
}

prepare_stdio(self.stderr).map_err(|err| {
ExecutorError::Other(format!("failed to prepare stdio for container: {}", err))
})?;

let path = PathBuf::from("/");

let config = match Config::load(&path) {
Ok(c) => c,
Err(err) => {
error!("[wws] There was an error reading the .wws.toml file. It will be ignored");
error!("[wws] Error: {err}");

Config::default()
}
};

// Check if there're missing runtimes
if config.is_missing_any_runtime(&path) {
error!("[wws] Required language runtimes are not installed. Some files may not be considered workers");
error!("[wws] You can install the missing runtimes with: wws runtimes install");
}

let routes = Routes::new(&path, "", Vec::new(), &config);

let rt = Runtime::new().unwrap();
rt.block_on(async {
let f = serve(&path, routes, WWS_ADDR, WWS_PORT, false, None)
.await
.unwrap();
info!(" >>> notifying main thread we are about to start");
tokio::select! {
_ = f => {
log::info!(" >>> server shut down: exiting");
std::process::exit(0);
},
};
});
std::process::exit(137);
}

fn can_handle(&self, _spec: &Spec) -> bool {
true
}

fn name(&self) -> &'static str {
EXECUTOR_NAME
}
}

fn prepare_stdio(stderr: Option<RawFd>) -> Result<()> {
if let Some(stderr) = stderr {
dup(STDERR_FILENO)?;
dup2(stderr, STDERR_FILENO)?;
}
Ok(())
}
Loading

0 comments on commit 6c2d5a3

Please sign in to comment.