Skip to content

Commit 621ebbb

Browse files
authored
Merge pull request #167 from Mossaka/version
feat(runc-shim): add a version flag
2 parents a5a29fc + deea90f commit 621ebbb

File tree

5 files changed

+104
-16
lines changed

5 files changed

+104
-16
lines changed

crates/runc-shim/build.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
use std::{process::Command, str::from_utf8};
18+
19+
fn main() {
20+
let output = match Command::new("git").arg("rev-parse").arg("HEAD").output() {
21+
Ok(output) => output,
22+
Err(_) => {
23+
return;
24+
}
25+
};
26+
let mut hash = from_utf8(&output.stdout).unwrap().trim().to_string();
27+
28+
let output_dirty = match Command::new("git").arg("diff").arg("--exit-code").output() {
29+
Ok(output) => output,
30+
Err(_) => {
31+
return;
32+
}
33+
};
34+
35+
if !output_dirty.status.success() {
36+
hash.push_str(".m");
37+
}
38+
println!("cargo:rustc-env=CARGO_GIT_HASH={}", hash);
39+
}

crates/runc-shim/src/main.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
limitations under the License.
1515
*/
1616

17-
use containerd_shim::asynchronous::run;
17+
use std::env;
18+
19+
use containerd_shim::{asynchronous::run, parse};
1820

1921
mod common;
2022
mod console;
@@ -27,7 +29,21 @@ mod task;
2729

2830
use service::Service;
2931

32+
fn parse_version() {
33+
let os_args: Vec<_> = env::args_os().collect();
34+
let flags = parse(&os_args[1..]).unwrap();
35+
if flags.version {
36+
println!("{}:", os_args[0].to_string_lossy());
37+
println!(" Version: {}", env!("CARGO_PKG_VERSION"));
38+
println!(" Revision: {}", env!("CARGO_GIT_HASH"));
39+
println!();
40+
41+
std::process::exit(0);
42+
}
43+
}
44+
3045
#[tokio::main]
3146
async fn main() {
47+
parse_version();
3248
run::<Service>("io.containerd.runc.v2-rs", None).await;
3349
}

crates/shim/src/args.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@ pub struct Flags {
3939
/// Shim action (start / delete).
4040
/// See <https://github.com/containerd/containerd/blob/master/runtime/v2/shim/shim.go#L191>
4141
pub action: String,
42+
/// Version of the shim.
43+
pub version: bool,
4244
}
4345

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

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

64-
if flags.namespace.is_empty() {
65-
return Err(Error::InvalidArgument(String::from(
66-
"Shim namespace cannot be empty",
67-
)));
68-
}
69-
7066
Ok(flags)
7167
}
7268

@@ -125,11 +121,4 @@ mod tests {
125121
assert_eq!(flags.action, "start");
126122
assert_eq!(flags.id, "");
127123
}
128-
129-
#[test]
130-
fn no_namespace() {
131-
let empty: [String; 0] = [];
132-
let result = parse(&empty).err();
133-
assert!(result.is_some())
134-
}
135124
}

crates/shim/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub use crate::synchronous::*;
6565
pub mod error;
6666

6767
mod args;
68-
pub use args::Flags;
68+
pub use args::{parse, Flags};
6969
#[cfg(feature = "async")]
7070
pub mod asynchronous;
7171
pub mod cgroup;

crates/shim/src/synchronous/mod.rs

+44
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ where
201201
let os_args: Vec<_> = env::args_os().collect();
202202
let flags = args::parse(&os_args[1..])?;
203203

204+
if flags.namespace.is_empty() {
205+
return Err(Error::InvalidArgument(String::from(
206+
"Shim namespace cannot be empty",
207+
)));
208+
}
209+
204210
let ttrpc_address = env::var(TTRPC_ADDRESS)?;
205211

206212
// Create shim instance
@@ -593,4 +599,42 @@ mod tests {
593599
panic!("{:?}", err);
594600
}
595601
}
602+
603+
struct Nop {}
604+
605+
struct NopTask {}
606+
impl Task for NopTask {}
607+
608+
impl Shim for Nop {
609+
type T = NopTask;
610+
611+
fn new(_runtime_id: &str, _args: &Flags, _config: &mut Config) -> Self {
612+
Nop {}
613+
}
614+
615+
fn start_shim(&mut self, _opts: StartOpts) -> Result<String> {
616+
Ok("".to_string())
617+
}
618+
619+
fn delete_shim(&mut self) -> Result<DeleteResponse> {
620+
Ok(DeleteResponse::default())
621+
}
622+
623+
fn wait(&mut self) {}
624+
625+
fn create_task_service(&self, _publisher: RemotePublisher) -> Self::T {
626+
NopTask {}
627+
}
628+
}
629+
630+
#[test]
631+
fn no_namespace() {
632+
let runtime_id = "test";
633+
let res = bootstrap::<Nop>(runtime_id, None);
634+
assert!(res.is_err());
635+
assert!(res
636+
.unwrap_err()
637+
.to_string()
638+
.contains("Shim namespace cannot be empty"));
639+
}
596640
}

0 commit comments

Comments
 (0)