Skip to content

Commit 656be0f

Browse files
authored
Isolated container (#51)
1 parent a4ebd26 commit 656be0f

File tree

15 files changed

+1011
-587
lines changed

15 files changed

+1011
-587
lines changed

build.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
fn main() -> Result<(), Box<dyn std::error::Error>> {
22
tonic_build::configure()
33
.protoc_arg("--experimental_allow_proto3_optional")
4-
.compile_protos(&["proto/feos.proto", "proto/container.proto"], &["proto"])?;
4+
.compile_protos(
5+
&[
6+
"proto/feos.proto",
7+
"proto/container.proto",
8+
"proto/isolated_container.proto",
9+
],
10+
&["proto"],
11+
)?;
512
Ok(())
613
}

client/build.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ fn main() {
22
tonic_build::configure()
33
.build_server(false)
44
.compile(
5-
&["../proto/feos.proto", "../proto/container.proto"],
5+
&[
6+
"../proto/feos.proto",
7+
"../proto/container.proto",
8+
"../proto/isolated_container.proto",
9+
],
610
&["../proto"],
711
)
812
.unwrap();

client/src/client.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use tonic::transport::Endpoint;
77
use tonic::Request;
88

99
use crate::client_container::ContainerCommand;
10+
use crate::client_isolated_container::IsolatedContainerCommand;
1011
use feos_grpc::feos_grpc_client::FeosGrpcClient;
1112
use feos_grpc::*;
1213

@@ -63,6 +64,7 @@ pub enum Command {
6364
uuid: String,
6465
},
6566
Container(ContainerCommand),
67+
IsolatedContainer(IsolatedContainerCommand),
6668
}
6769

6870
fn format_address(ip: &str, port: u16) -> String {
@@ -89,6 +91,14 @@ pub async fn run_client(opt: Opt) -> Result<(), Box<dyn std::error::Error>> {
8991
crate::client_container::run_container_client(opt.server_ip, opt.port, container_cmd)
9092
.await?;
9193
}
94+
Command::IsolatedContainer(container_cmd) => {
95+
crate::client_isolated_container::run_isolated_container_client(
96+
opt.server_ip,
97+
opt.port,
98+
container_cmd,
99+
)
100+
.await?;
101+
}
92102
Command::Reboot => {
93103
let request = Request::new(RebootRequest {});
94104
let response = client.reboot(request).await?;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use std::time::Duration;
2+
use structopt::StructOpt;
3+
use tokio::time::timeout;
4+
use tonic::transport::Endpoint;
5+
use tonic::Request;
6+
7+
use isolated_container_grpc::isolated_container_service_client::IsolatedContainerServiceClient;
8+
use isolated_container_grpc::*;
9+
10+
pub mod isolated_container_grpc {
11+
tonic::include_proto!("isolated_container");
12+
}
13+
14+
#[derive(StructOpt, Debug)]
15+
pub enum IsolatedContainerCommand {
16+
Create {
17+
image: String,
18+
#[structopt(name = "COMMAND", required = true, min_values = 1)]
19+
command: Vec<String>,
20+
},
21+
Run {
22+
uuid: String,
23+
},
24+
Stop {
25+
uuid: String,
26+
},
27+
State {
28+
uuid: String,
29+
},
30+
}
31+
32+
pub async fn run_isolated_container_client(
33+
server_ip: String,
34+
port: u16,
35+
cmd: IsolatedContainerCommand,
36+
) -> Result<(), Box<dyn std::error::Error>> {
37+
let address = format!("http://{}:{}", server_ip, port);
38+
let channel = Endpoint::from_shared(address)?.connect().await?;
39+
let mut client = IsolatedContainerServiceClient::new(channel);
40+
41+
match cmd {
42+
IsolatedContainerCommand::Create { image, command } => {
43+
let request = Request::new(CreateContainerRequest { image, command });
44+
match timeout(Duration::from_secs(30), client.create_container(request)).await {
45+
Ok(response) => match response {
46+
Ok(response) => println!("CREATE ISOLATED CONTAINER RESPONSE={:?}", response),
47+
Err(e) => eprintln!("Error creating isolated container: {:?}", e),
48+
},
49+
Err(_) => eprintln!("Request timed out after 30 seconds"),
50+
}
51+
}
52+
IsolatedContainerCommand::Run { uuid } => {
53+
let request = Request::new(RunContainerRequest { uuid });
54+
match client.run_container(request).await {
55+
Ok(response) => println!("RUN ISOLATED CONTAINER RESPONSE={:?}", response),
56+
Err(e) => eprintln!("Error running isolated container: {:?}", e),
57+
}
58+
}
59+
IsolatedContainerCommand::Stop { uuid } => {
60+
let request = Request::new(StopContainerRequest { uuid });
61+
match client.stop_container(request).await {
62+
Ok(response) => println!("STOP ISOLATED CONTAINER RESPONSE={:?}", response),
63+
Err(e) => eprintln!("Error stopping isolated container: {:?}", e),
64+
}
65+
}
66+
IsolatedContainerCommand::State { uuid } => {
67+
let request = Request::new(StateContainerRequest { uuid });
68+
match client.state_container(request).await {
69+
Ok(response) => println!("STATE ISOLATED CONTAINER RESPONSE={:?}", response),
70+
Err(e) => eprintln!("Error getting isolated container state: {:?}", e),
71+
}
72+
}
73+
}
74+
75+
Ok(())
76+
}

client/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod client;
22
mod client_container;
3+
mod client_isolated_container;
34

45
use client::Opt;
56
use structopt::StructOpt;

proto/isolated_container.proto

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
syntax = "proto3";
2+
package isolated_container;
3+
4+
service IsolatedContainerService {
5+
rpc CreateContainer (CreateContainerRequest) returns (CreateContainerResponse);
6+
rpc RunContainer (RunContainerRequest) returns (RunContainerResponse);
7+
rpc StopContainer (StopContainerRequest) returns (StopContainerResponse);
8+
rpc StateContainer (StateContainerRequest) returns (StateContainerResponse);
9+
}
10+
11+
message CreateContainerRequest {
12+
string image = 1;
13+
repeated string command = 2;
14+
}
15+
message CreateContainerResponse {
16+
string uuid = 1;
17+
}
18+
19+
message RunContainerRequest {
20+
string uuid = 1;
21+
}
22+
message RunContainerResponse {}
23+
24+
message StopContainerRequest {
25+
string uuid = 1;
26+
}
27+
message StopContainerResponse {}
28+
29+
message StateContainerRequest {
30+
string uuid = 1;
31+
}
32+
message StateContainerResponse {
33+
string state = 1;
34+
optional int32 pid = 2;
35+
}

0 commit comments

Comments
 (0)