|
| 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 | +} |
0 commit comments