Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add version flag to all the shims #268

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions crates/containerd-shim-wasmedge/build.rs
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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::<ShimCli<WasiInstance>>("io.containerd.wasmedge.v1", None);
}
Original file line number Diff line number Diff line change
@@ -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::<Shim>("containerd-shim-wasmedged-v1", None)
}
17 changes: 17 additions & 0 deletions crates/containerd-shim-wasmedge/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::env;

use containerd_shim::parse;

pub mod error;

#[cfg_attr(unix, path = "instance/instance_linux.rs")]
Expand All @@ -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);
}
}
Comment on lines +15 to +26
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: create a containerd_shim_wasm::run function that takes the version / revision, and handles this logic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

Copy link
Member Author

@Mossaka Mossaka Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is a good way to handle this in containerd-shim-wasm crate, as each shim implementation still needs to read os args, parse flags and get the version / revision... Could not save much logic here. With that said, I will leave it as is.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant something like:

// somewhere in containerd-shim-wasm
pub fn run<Shim: ...>(runtime_id: &str, opts: Option<Config>, version: &str, revision: &str) {
    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);
    }
    shim::run::<Shim>(runtime_id, opts)
}

and

// in main.rs
use containerd_shim_wasm::run;
fn main() {
    run::<ShimCli<WasiInstance>>(
        "io.containerd.wasmtime.v1",
        None,
        env!("CARGO_PKG_VERSION"),
        env!("CARGO_GIT_HASH"),
    );
}

then you can remove parse_version from all the lib.rs


#[cfg(unix)]
#[cfg(test)]
mod test {
Expand Down
24 changes: 24 additions & 0 deletions crates/containerd-shim-wasmtime/build.rs
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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::<ShimCli<WasiInstance>>("io.containerd.wasmtime.v1", None);
}
Original file line number Diff line number Diff line change
@@ -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::<Shim>("containerd-shim-wasmtimed-v1", None)
}
17 changes: 17 additions & 0 deletions crates/containerd-shim-wasmtime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand All @@ -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);
}
}