Skip to content

Commit

Permalink
Merge pull request #167 from Mossaka/version
Browse files Browse the repository at this point in the history
feat(runc-shim): add a version flag
  • Loading branch information
mxpv authored Aug 24, 2023
2 parents a5a29fc + deea90f commit 621ebbb
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 16 deletions.
39 changes: 39 additions & 0 deletions crates/runc-shim/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

use std::{process::Command, 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);
}
18 changes: 17 additions & 1 deletion crates/runc-shim/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
limitations under the License.
*/

use containerd_shim::asynchronous::run;
use std::env;

use containerd_shim::{asynchronous::run, parse};

mod common;
mod console;
Expand All @@ -27,7 +29,21 @@ mod task;

use service::Service;

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);
}
}

#[tokio::main]
async fn main() {
parse_version();
run::<Service>("io.containerd.runc.v2-rs", None).await;
}
17 changes: 3 additions & 14 deletions crates/shim/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,17 @@ pub struct Flags {
/// Shim action (start / delete).
/// See <https:/containerd/containerd/blob/master/runtime/v2/shim/shim.go#L191>
pub action: String,
/// Version of the shim.
pub version: bool,
}

/// Parses command line arguments passed to the shim.
/// This func replicates https:/containerd/containerd/blob/master/runtime/v2/shim/shim.go#L110
pub fn parse<S: AsRef<OsStr>>(args: &[S]) -> Result<Flags> {
let mut flags = Flags::default();

let args: Vec<String> = go_flag::parse_args(args, |f| {
f.add_flag("debug", &mut flags.debug);
f.add_flag("v", &mut flags.version);
f.add_flag("namespace", &mut flags.namespace);
f.add_flag("id", &mut flags.id);
f.add_flag("socket", &mut flags.socket);
Expand All @@ -61,12 +63,6 @@ pub fn parse<S: AsRef<OsStr>>(args: &[S]) -> Result<Flags> {
flags.action = action.into();
}

if flags.namespace.is_empty() {
return Err(Error::InvalidArgument(String::from(
"Shim namespace cannot be empty",
)));
}

Ok(flags)
}

Expand Down Expand Up @@ -125,11 +121,4 @@ mod tests {
assert_eq!(flags.action, "start");
assert_eq!(flags.id, "");
}

#[test]
fn no_namespace() {
let empty: [String; 0] = [];
let result = parse(&empty).err();
assert!(result.is_some())
}
}
2 changes: 1 addition & 1 deletion crates/shim/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub use crate::synchronous::*;
pub mod error;

mod args;
pub use args::Flags;
pub use args::{parse, Flags};
#[cfg(feature = "async")]
pub mod asynchronous;
pub mod cgroup;
Expand Down
44 changes: 44 additions & 0 deletions crates/shim/src/synchronous/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ where
let os_args: Vec<_> = env::args_os().collect();
let flags = args::parse(&os_args[1..])?;

if flags.namespace.is_empty() {
return Err(Error::InvalidArgument(String::from(
"Shim namespace cannot be empty",
)));
}

let ttrpc_address = env::var(TTRPC_ADDRESS)?;

// Create shim instance
Expand Down Expand Up @@ -593,4 +599,42 @@ mod tests {
panic!("{:?}", err);
}
}

struct Nop {}

struct NopTask {}
impl Task for NopTask {}

impl Shim for Nop {
type T = NopTask;

fn new(_runtime_id: &str, _args: &Flags, _config: &mut Config) -> Self {
Nop {}
}

fn start_shim(&mut self, _opts: StartOpts) -> Result<String> {
Ok("".to_string())
}

fn delete_shim(&mut self) -> Result<DeleteResponse> {
Ok(DeleteResponse::default())
}

fn wait(&mut self) {}

fn create_task_service(&self, _publisher: RemotePublisher) -> Self::T {
NopTask {}
}
}

#[test]
fn no_namespace() {
let runtime_id = "test";
let res = bootstrap::<Nop>(runtime_id, None);
assert!(res.is_err());
assert!(res
.unwrap_err()
.to_string()
.contains("Shim namespace cannot be empty"));
}
}

0 comments on commit 621ebbb

Please sign in to comment.