Skip to content

Commit

Permalink
chore: use the Engine API from containerd-shim-wasm crate
Browse files Browse the repository at this point in the history
This commits updates to use the latest API from the `containerd-shim-wasm` crate.
It follows the PR containerd/runwasi#293

Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
  • Loading branch information
Mossaka committed Sep 19, 2023
1 parent 7e5e6e5 commit 0bc6a8e
Show file tree
Hide file tree
Showing 16 changed files with 325 additions and 495 deletions.
51 changes: 47 additions & 4 deletions containerd-shim-lunatic-v1/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion containerd-shim-lunatic-v1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
containerd-shim = "0.5.0"
containerd-shim-wasm = { git = "https://github.com/containerd/runwasi", rev = "7e978edeaf34b54efb895738357c805cf888b76d", features = ["cgroupsv2"] }
containerd-shim-wasm = { git = "https://github.com/containerd/runwasi", rev = "4d212b968d24d42a27952e8b04979382b543a613", features = ["cgroupsv2"] }
libcontainer = { git = "https://github.com/containers/youki", rev = "09e67372a892f22a89eeef62ff429c3cbcac6d41", features = ["v1","v2"], default-features = false }
nix = "0.26.2"
serde = "1.0.183"
Expand Down
56 changes: 18 additions & 38 deletions containerd-shim-lunatic-v1/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,38 @@ use std::path::PathBuf;
use std::sync::Arc;
use tokio::runtime::Runtime;

use containerd_shim_wasm::libcontainer_instance::LinuxContainerExecutor;
use containerd_shim_wasm::sandbox::Stdio;
use libcontainer::workload::{Executor, ExecutorError, ExecutorValidationError};
use containerd_shim_wasm::{
container::{Engine, RuntimeContext},
sandbox::Stdio,
};
use lunatic_process::{
env::{Environments, LunaticEnvironments},
runtimes,
};
use oci_spec::runtime::Spec;
use utils::{get_args, is_linux_executable};

use crate::common::{run_wasm, RunWasm};

#[derive(Clone)]
pub struct LunaticExecutor {
stdio: Stdio,
}
#[derive(Clone, Default)]
pub struct LunaticEngine;

impl LunaticExecutor {
pub fn new(stdio: Stdio) -> Self {
Self { stdio }
impl Engine for LunaticEngine {
fn name() -> &'static str {
"lunatic"
}

fn wasm_exec(&self, spec: &Spec) -> anyhow::Result<()> {
self.stdio
.take()
.redirect()
.context("failed to redirect stdio")?;
let cmd = get_args(spec).first().context("no cmd provided")?.clone();
fn run_wasi(&self, ctx: &impl RuntimeContext, stdio: Stdio) -> Result<i32> {
log::info!("setting up wasi");
stdio.redirect()?;
let cmd = ctx.args().first().context("no cmd provided")?.clone();
let rt = Runtime::new().context("failed to create runtime")?;
rt.block_on(async {
if let Err(e) = rt.block_on(async {
log::info!(" >>> building lunatic application");
crate::executor::exec(cmd).await
})
}
}

impl Executor for LunaticExecutor {
fn exec(&self, spec: &Spec) -> Result<(), ExecutorError> {
if is_linux_executable(spec).is_ok() {
log::info!("executing linux container");
LinuxContainerExecutor::new(self.stdio.clone()).exec(spec)
} else {
if let Err(e) = self.wasm_exec(spec) {
log::error!(" >>> error: {:?}", e);
std::process::exit(137);
}
std::process::exit(0);
}) {
log::error!(" >>> error: {:?}", e);
return Ok(137);
}
}

fn validate(&self, _spec: &Spec) -> Result<(), ExecutorValidationError> {
Ok(())
Ok(0)
}
}

Expand Down
82 changes: 6 additions & 76 deletions containerd-shim-lunatic-v1/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,86 +1,16 @@
use std::{
env,
path::PathBuf,
sync::{Arc, Condvar, Mutex},
};
use std::env;

use containerd_shim::{parse, run};
use containerd_shim_wasm::sandbox::instance_utils::determine_rootdir;
use containerd_shim_wasm::sandbox::stdio::Stdio;
use containerd_shim_wasm::{
libcontainer_instance::LibcontainerInstance,
sandbox::{instance::ExitCode, Error, InstanceConfig, ShimCli},
};
use libcontainer::container::{builder::ContainerBuilder, Container};
use libcontainer::syscall::syscall::SyscallType;
use containerd_shim_wasm::container::Instance;
use containerd_shim_wasm::sandbox::ShimCli;

use anyhow::Result;
use executor::LunaticEngine;

use crate::executor::LunaticExecutor;
pub type LunaticInstance = Instance<LunaticEngine>;

mod common;
mod executor;

static DEFAULT_CONTAINER_ROOT_DIR: &str = "/run/containerd/lunatic";

pub struct Wasi {
id: String,
exit_code: ExitCode,
bundle: String,
rootdir: PathBuf,
stdio: Stdio,
}

impl LibcontainerInstance for Wasi {
type Engine = ();

fn new_libcontainer(id: String, cfg: Option<&InstanceConfig<Self::Engine>>) -> Self {
let cfg = cfg.unwrap();
let bundle = cfg.get_bundle().unwrap_or_default();

Wasi {
id,
exit_code: Arc::new((Mutex::new(None), Condvar::new())),
rootdir: determine_rootdir(
bundle.as_str(),
cfg.get_namespace().as_str(),
DEFAULT_CONTAINER_ROOT_DIR,
)
.unwrap(),
bundle,
stdio: Stdio::init_from_cfg(cfg).expect("failed to open stdio"),
}
}

fn get_exit_code(&self) -> ExitCode {
self.exit_code.clone()
}

fn get_id(&self) -> String {
self.id.clone()
}

fn get_root_dir(&self) -> std::result::Result<PathBuf, Error> {
Ok(self.rootdir.clone())
}

fn build_container(&self) -> Result<Container, Error> {
log::info!("Building container");

let err_msg = |err| format!("failed to create container: {}", err);
let container = ContainerBuilder::new(self.id.clone(), SyscallType::Linux)
.with_executor(LunaticExecutor::new(self.stdio.take()))
.with_root_path(self.rootdir.clone())
.map_err(|err| Error::Others(err_msg(err)))?
.as_init(&self.bundle)
.with_systemd(false)
.build()
.map_err(|err| Error::Others(err_msg(err)))?;
log::info!(">>> Container built.");
Ok(container)
}
}

fn parse_version() {
let os_args: Vec<_> = env::args_os().collect();
let flags = parse(&os_args[1..]).unwrap();
Expand All @@ -96,5 +26,5 @@ fn parse_version() {

fn main() {
parse_version();
run::<ShimCli<Wasi>>("io.containerd.lunatic.v1", None);
run::<ShimCli<LunaticInstance>>("io.containerd.lunatic.v1", None);
}
51 changes: 47 additions & 4 deletions containerd-shim-slight-v1/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion containerd-shim-slight-v1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Containerd shim for running Slight workloads.
chrono = "0.4"
clap = { version = "4.1", features = ["derive", "env"] }
containerd-shim = "0.5.0"
containerd-shim-wasm = { git = "https://github.com/containerd/runwasi", rev = "7e978edeaf34b54efb895738357c805cf888b76d", features = ["cgroupsv2"] }
containerd-shim-wasm = { git = "https://github.com/containerd/runwasi", rev = "4d212b968d24d42a27952e8b04979382b543a613", features = ["cgroupsv2"] }
libcontainer = { git = "https://github.com/containers/youki", rev = "09e67372a892f22a89eeef62ff429c3cbcac6d41", features = ["v1","v2"], default-features = false }
log = "0.4"
tokio = { version = "1", features = [ "full" ] }
Expand Down
Loading

0 comments on commit 0bc6a8e

Please sign in to comment.