From 0344777ac683c325e33a8f244bd69f4f245ec2dd Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Thu, 24 Aug 2023 02:20:24 +0000 Subject: [PATCH] feat: add version flag to all the shims Signed-off-by: jiaxiao zhou --- crates/containerd-shim-wasmedge/build.rs | 24 +++++++++++++++++++ .../bin/containerd-shim-wasmedge-v1/main.rs | 2 ++ .../bin/containerd-shim-wasmedged-v1/main.rs | 2 ++ crates/containerd-shim-wasmedge/src/lib.rs | 17 +++++++++++++ crates/containerd-shim-wasmtime/build.rs | 24 +++++++++++++++++++ .../bin/containerd-shim-wasmtime-v1/main.rs | 2 ++ .../bin/containerd-shim-wasmtimed-v1/main.rs | 2 ++ crates/containerd-shim-wasmtime/src/lib.rs | 17 +++++++++++++ 8 files changed, 90 insertions(+) create mode 100644 crates/containerd-shim-wasmedge/build.rs create mode 100644 crates/containerd-shim-wasmtime/build.rs diff --git a/crates/containerd-shim-wasmedge/build.rs b/crates/containerd-shim-wasmedge/build.rs new file mode 100644 index 000000000..e5e6ae0b0 --- /dev/null +++ b/crates/containerd-shim-wasmedge/build.rs @@ -0,0 +1,24 @@ +use std::process::Command; +use std::str::from_utf8; + +fn main() { + let output = match Command::new("git").arg("rev-parse").arg("HEAD").output() { + Ok(output) => output, + Err(_) => { + return; + } + }; + let mut hash = from_utf8(&output.stdout).unwrap().trim().to_string(); + + let output_dirty = match Command::new("git").arg("diff").arg("--exit-code").output() { + Ok(output) => output, + Err(_) => { + return; + } + }; + + if !output_dirty.status.success() { + hash.push_str(".m"); + } + println!("cargo:rustc-env=CARGO_GIT_HASH={}", hash); +} diff --git a/crates/containerd-shim-wasmedge/src/bin/containerd-shim-wasmedge-v1/main.rs b/crates/containerd-shim-wasmedge/src/bin/containerd-shim-wasmedge-v1/main.rs index 5783e1f67..b86bf9c6c 100644 --- a/crates/containerd-shim-wasmedge/src/bin/containerd-shim-wasmedge-v1/main.rs +++ b/crates/containerd-shim-wasmedge/src/bin/containerd-shim-wasmedge-v1/main.rs @@ -1,7 +1,9 @@ use containerd_shim as shim; use containerd_shim_wasm::sandbox::ShimCli; use containerd_shim_wasmedge::instance::Wasi as WasiInstance; +use containerd_shim_wasmedge::parse_version; fn main() { + parse_version(); shim::run::>("io.containerd.wasmedge.v1", None); } diff --git a/crates/containerd-shim-wasmedge/src/bin/containerd-shim-wasmedged-v1/main.rs b/crates/containerd-shim-wasmedge/src/bin/containerd-shim-wasmedged-v1/main.rs index 4d1e77db5..696480dfe 100644 --- a/crates/containerd-shim-wasmedge/src/bin/containerd-shim-wasmedged-v1/main.rs +++ b/crates/containerd-shim-wasmedge/src/bin/containerd-shim-wasmedged-v1/main.rs @@ -1,6 +1,8 @@ use containerd_shim as shim; use containerd_shim_wasm::sandbox::manager::Shim; +use containerd_shim_wasmedge::parse_version; fn main() { + parse_version(); shim::run::("containerd-shim-wasmedged-v1", None) } diff --git a/crates/containerd-shim-wasmedge/src/lib.rs b/crates/containerd-shim-wasmedge/src/lib.rs index 5e92dcfb8..a6aa4913d 100644 --- a/crates/containerd-shim-wasmedge/src/lib.rs +++ b/crates/containerd-shim-wasmedge/src/lib.rs @@ -1,3 +1,7 @@ +use std::env; + +use containerd_shim::parse; + pub mod error; #[cfg_attr(unix, path = "instance/instance_linux.rs")] @@ -8,6 +12,19 @@ pub mod oci_utils; #[cfg(unix)] pub mod executor; +pub fn parse_version() { + let os_args: Vec<_> = env::args_os().collect(); + let flags = parse(&os_args[1..]).unwrap(); + if flags.version { + println!("{}:", os_args[0].to_string_lossy()); + println!(" Version: {}", env!("CARGO_PKG_VERSION")); + println!(" Revision: {}", env!("CARGO_GIT_HASH")); + println!(); + + std::process::exit(0); + } +} + #[cfg(unix)] #[cfg(test)] mod test { diff --git a/crates/containerd-shim-wasmtime/build.rs b/crates/containerd-shim-wasmtime/build.rs new file mode 100644 index 000000000..e5e6ae0b0 --- /dev/null +++ b/crates/containerd-shim-wasmtime/build.rs @@ -0,0 +1,24 @@ +use std::process::Command; +use std::str::from_utf8; + +fn main() { + let output = match Command::new("git").arg("rev-parse").arg("HEAD").output() { + Ok(output) => output, + Err(_) => { + return; + } + }; + let mut hash = from_utf8(&output.stdout).unwrap().trim().to_string(); + + let output_dirty = match Command::new("git").arg("diff").arg("--exit-code").output() { + Ok(output) => output, + Err(_) => { + return; + } + }; + + if !output_dirty.status.success() { + hash.push_str(".m"); + } + println!("cargo:rustc-env=CARGO_GIT_HASH={}", hash); +} diff --git a/crates/containerd-shim-wasmtime/src/bin/containerd-shim-wasmtime-v1/main.rs b/crates/containerd-shim-wasmtime/src/bin/containerd-shim-wasmtime-v1/main.rs index 97472616d..cfbb8015d 100644 --- a/crates/containerd-shim-wasmtime/src/bin/containerd-shim-wasmtime-v1/main.rs +++ b/crates/containerd-shim-wasmtime/src/bin/containerd-shim-wasmtime-v1/main.rs @@ -1,7 +1,9 @@ use containerd_shim as shim; use containerd_shim_wasm::sandbox::ShimCli; use containerd_shim_wasmtime::instance::Wasi as WasiInstance; +use containerd_shim_wasmtime::parse_version; fn main() { + parse_version(); shim::run::>("io.containerd.wasmtime.v1", None); } diff --git a/crates/containerd-shim-wasmtime/src/bin/containerd-shim-wasmtimed-v1/main.rs b/crates/containerd-shim-wasmtime/src/bin/containerd-shim-wasmtimed-v1/main.rs index b3d4e3045..c1de68b14 100644 --- a/crates/containerd-shim-wasmtime/src/bin/containerd-shim-wasmtimed-v1/main.rs +++ b/crates/containerd-shim-wasmtime/src/bin/containerd-shim-wasmtimed-v1/main.rs @@ -1,6 +1,8 @@ use containerd_shim as shim; use containerd_shim_wasm::sandbox::manager::Shim; +use containerd_shim_wasmtime::parse_version; fn main() { + parse_version(); shim::run::("containerd-shim-wasmtimed-v1", None) } diff --git a/crates/containerd-shim-wasmtime/src/lib.rs b/crates/containerd-shim-wasmtime/src/lib.rs index 6e4ac2761..1f0a707ce 100644 --- a/crates/containerd-shim-wasmtime/src/lib.rs +++ b/crates/containerd-shim-wasmtime/src/lib.rs @@ -1,3 +1,7 @@ +use std::env; + +use containerd_shim::parse; + pub mod error; #[cfg_attr(unix, path = "instance/instance_linux.rs")] #[cfg_attr(windows, path = "instance/instance_windows.rs")] @@ -6,3 +10,16 @@ pub mod oci_wasmtime; #[cfg(unix)] pub mod executor; + +pub fn parse_version() { + let os_args: Vec<_> = env::args_os().collect(); + let flags = parse(&os_args[1..]).unwrap(); + if flags.version { + println!("{}:", os_args[0].to_string_lossy()); + println!(" Version: {}", env!("CARGO_PKG_VERSION")); + println!(" Revision: {}", env!("CARGO_GIT_HASH")); + println!(); + + std::process::exit(0); + } +}