Skip to content

Commit f8370e4

Browse files
committed
PR Review
Signed-off-by: Lukas Frank <[email protected]>
1 parent 9b91b28 commit f8370e4

File tree

12 files changed

+303
-195
lines changed

12 files changed

+303
-195
lines changed

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;

src/daemon.rs

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -58,35 +58,36 @@ impl FeOSAPI {
5858
log_receiver,
5959
}
6060
}
61-
}
6261

63-
fn handle_error(e: vm::Error) -> tonic::Status {
64-
match e {
65-
vm::Error::AlreadyExists => Status::new(tonic::Code::AlreadyExists, "vm already exists"),
66-
vm::Error::NotFound => Status::new(tonic::Code::NotFound, "vm not found"),
67-
vm::Error::SocketFailure(e) => {
68-
info!("socket error: {:?}", e);
69-
Status::new(tonic::Code::Internal, "failed to ")
70-
}
71-
vm::Error::InvalidInput(e) => {
72-
info!("invalid input error: {:?}", e);
73-
Status::new(tonic::Code::Internal, "invalid input")
74-
}
75-
vm::Error::CHCommandFailure(e) => {
76-
info!("failed to connect to cloud hypervisor: {:?}", e);
77-
Status::new(
78-
tonic::Code::Internal,
79-
"failed to connect to cloud hypervisor",
80-
)
81-
}
82-
vm::Error::CHApiFailure(e) => {
83-
info!("failed to connect to cloud hypervisor api: {:?}", e);
84-
Status::new(
85-
tonic::Code::Internal,
86-
"failed to connect to cloud hypervisor api",
87-
)
62+
fn handle_error(&self, e: vm::Error) -> Status {
63+
match e {
64+
vm::Error::AlreadyExists => {
65+
Status::new(tonic::Code::AlreadyExists, "vm already exists")
66+
}
67+
vm::Error::NotFound => Status::new(tonic::Code::NotFound, "vm not found"),
68+
vm::Error::SocketFailure(e) => {
69+
info!("socket error: {:?}", e);
70+
Status::new(tonic::Code::Internal, "failed to ")
71+
}
72+
vm::Error::InvalidInput(e) => {
73+
info!("invalid input error: {:?}", e);
74+
Status::new(tonic::Code::Internal, "invalid input")
75+
}
76+
vm::Error::CHCommandFailure(e) => {
77+
info!("failed to connect to cloud hypervisor: {:?}", e);
78+
Status::new(
79+
tonic::Code::Internal,
80+
"failed to connect to cloud hypervisor",
81+
)
82+
}
83+
vm::Error::CHApiFailure(e) => {
84+
info!("failed to connect to cloud hypervisor api: {:?}", e);
85+
Status::new(
86+
tonic::Code::Internal,
87+
"failed to connect to cloud hypervisor api",
88+
)
89+
}
8890
}
89-
vm::Error::Failed => Status::new(tonic::Code::AlreadyExists, "vm already exists"),
9091
}
9192
}
9293

@@ -191,7 +192,9 @@ impl FeosGrpc for FeOSAPI {
191192
info!("Got create_vm request");
192193

193194
let id = Uuid::new_v4();
194-
self.vmm.init_vmm(id, true).map_err(handle_error)?;
195+
self.vmm
196+
.init_vmm(id, true)
197+
.map_err(|e| self.handle_error(e))?;
195198

196199
let root_fs = PathBuf::from(format!(
197200
"./images/{}/application.vnd.ironcore.image.rootfs.v1alpha1.rootfs",
@@ -205,7 +208,7 @@ impl FeosGrpc for FeOSAPI {
205208
vm::BootMode::FirmwareBoot(vm::FirmwareBootMode { root_fs }),
206209
request.get_ref().ignition.clone(),
207210
)
208-
.map_err(handle_error)?;
211+
.map_err(|e| self.handle_error(e))?;
209212

210213
Ok(Response::new(feos_grpc::CreateVmResponse {
211214
uuid: id.to_string(),
@@ -221,8 +224,8 @@ impl FeosGrpc for FeOSAPI {
221224
let id = request.get_ref().uuid.to_owned();
222225
let id =
223226
Uuid::parse_str(&id).map_err(|_| Status::invalid_argument("failed to parse uuid"))?;
224-
self.vmm.ping_vmm(id).map_err(handle_error)?;
225-
let vm_status = self.vmm.get_vm(id).map_err(handle_error)?;
227+
self.vmm.ping_vmm(id).map_err(|e| self.handle_error(e))?;
228+
let vm_status = self.vmm.get_vm(id).map_err(|e| self.handle_error(e))?;
226229

227230
Ok(Response::new(feos_grpc::GetVmResponse { info: vm_status }))
228231
}
@@ -236,7 +239,7 @@ impl FeosGrpc for FeOSAPI {
236239
let id = Uuid::parse_str(&request.get_ref().uuid)
237240
.map_err(|_| Status::invalid_argument("Failed to parse UUID"))?;
238241

239-
self.vmm.boot_vm(id).map_err(handle_error)?;
242+
self.vmm.boot_vm(id).map_err(|e| self.handle_error(e))?;
240243

241244
Ok(Response::new(feos_grpc::BootVmResponse {}))
242245
}
@@ -263,7 +266,10 @@ impl FeosGrpc for FeOSAPI {
263266
};
264267
let id = Uuid::parse_str(&initial_request.uuid)
265268
.map_err(|_| Status::invalid_argument("failed to parse uuid"))?;
266-
let socket_path = self.vmm.get_vm_console_path(id).map_err(handle_error)?;
269+
let socket_path = self
270+
.vmm
271+
.get_vm_console_path(id)
272+
.map_err(|e| self.handle_error(e))?;
267273

268274
tokio::spawn(async move {
269275
let stream = match UnixStream::connect(&socket_path).await {
@@ -331,7 +337,7 @@ impl FeosGrpc for FeOSAPI {
331337

332338
self.vmm
333339
.add_net_device(id, net_config)
334-
.map_err(handle_error)?;
340+
.map_err(|e| self.handle_error(e))?;
335341

336342
Ok(Response::new(feos_grpc::AttachNicVmResponse {}))
337343
}
@@ -346,7 +352,7 @@ impl FeosGrpc for FeOSAPI {
346352
.map_err(|_| Status::invalid_argument("Failed to parse UUID"))?;
347353

348354
// TODO differentiate between kill and shutdown
349-
self.vmm.kill_vm(id).map_err(handle_error)?;
355+
self.vmm.kill_vm(id).map_err(|e| self.handle_error(e))?;
350356

351357
Ok(Response::new(feos_grpc::ShutdownVmResponse {}))
352358
}
@@ -507,11 +513,7 @@ pub async fn daemon_start(
507513
Ok(())
508514
}
509515

510-
pub async fn start_feos(
511-
ipv6_address: Ipv6Addr,
512-
prefix_length: u8,
513-
test_mode: bool,
514-
) -> Result<(), String> {
516+
pub async fn start_feos(ipv6_address: Ipv6Addr, prefix_length: u8) -> Result<(), String> {
515517
println!(
516518
"
517519

0 commit comments

Comments
 (0)