11use crate :: container:: container_service:: container_service_client:: ContainerServiceClient ;
2- use crate :: container:: container_service:: { CreateContainerRequest , RunContainerRequest } ;
2+ use crate :: container:: container_service:: { CreateContainerRequest , RunContainerRequest , StateContainerRequest } ;
33use crate :: daemon:: FeOSAPI ;
44use crate :: feos_grpc:: Empty ;
55use crate :: ringbuffer:: RingBuffer ;
@@ -88,7 +88,7 @@ fn handle_error(e: vm::Error) -> tonic::Status {
8888 }
8989}
9090
91- async fn retry_get_channel ( path : String ) -> Result < Channel , Error > {
91+ async fn get_channel ( path : String ) -> Result < Channel , Error > {
9292 async fn get_channel ( path : String ) -> Result < Channel , Error > {
9393 let channel = Endpoint :: try_from ( "http://[::]:50051" )
9494 . map_err ( |e| Error :: Failed ) ?
@@ -192,7 +192,7 @@ impl IsolatedContainerService for IsolatedContainerAPI {
192192 . expect ( "failed to start network" ) ;
193193
194194 let path = format ! ( "vsock{}.sock" , network:: Manager :: device_name( & id) ) ;
195- let channel = retry_get_channel ( path) . await . expect ( "abc" ) ;
195+ let channel = get_channel ( path) . await . expect ( "abc" ) ;
196196
197197 let mut client = ContainerServiceClient :: new ( channel) ;
198198 let request = tonic:: Request :: new ( CreateContainerRequest {
@@ -236,7 +236,7 @@ impl IsolatedContainerService for IsolatedContainerAPI {
236236 } ;
237237
238238 let path = format ! ( "vsock{}.sock" , network:: Manager :: device_name( & vm_id) ) ;
239- let channel = retry_get_channel ( path) . await . expect ( "abc" ) ;
239+ let channel = get_channel ( path) . await . expect ( "abc" ) ;
240240
241241 let mut client = ContainerServiceClient :: new ( channel) ;
242242 let request = tonic:: Request :: new ( RunContainerRequest {
@@ -255,7 +255,20 @@ impl IsolatedContainerService for IsolatedContainerAPI {
255255 ) -> Result < Response < isolated_container_service:: StopContainerResponse > , Status > {
256256 info ! ( "Got stop_container request" ) ;
257257
258+
258259 let container_id: String = request. get_ref ( ) . uuid . clone ( ) ;
260+ let container_id = Uuid :: parse_str ( & container_id)
261+ . map_err ( |_| Status :: invalid_argument ( "failed to parse uuid" ) ) ?;
262+
263+ self . network
264+ . stop_dhcp ( container_id)
265+ . await
266+ . expect ( "failed to start network" ) ;
267+
268+ self . vmm . kill_vm ( container_id) . map_err ( handle_error) ?;
269+
270+ let mut vm_to_container = self . vm_to_container . lock ( ) . unwrap ( ) ;
271+ vm_to_container. remove ( & container_id) ;
259272
260273 Ok ( Response :: new (
261274 isolated_container_service:: StopContainerResponse { } ,
@@ -270,10 +283,34 @@ impl IsolatedContainerService for IsolatedContainerAPI {
270283
271284 let container_id: String = request. get_ref ( ) . uuid . clone ( ) ;
272285
286+ let vm_id: String = request. get_ref ( ) . uuid . clone ( ) ;
287+ let vm_id = Uuid :: parse_str ( & vm_id)
288+ . map_err ( |_| Status :: invalid_argument ( "failed to parse uuid" ) ) ?;
289+
290+ let container_id = {
291+ let vm_to_container = self
292+ . vm_to_container
293+ . lock ( )
294+ . map_err ( |_| Status :: internal ( "Failed to lock mutex" ) ) ?;
295+ * vm_to_container
296+ . get ( & vm_id)
297+ . ok_or_else ( || Status :: not_found ( format ! ( "VM with ID '{}' not found" , vm_id) ) ) ?
298+ } ;
299+
300+ let path = format ! ( "vsock{}.sock" , network:: Manager :: device_name( & vm_id) ) ;
301+ let channel = get_channel ( path) . await . expect ( "abc" ) ;
302+
303+ let mut client = ContainerServiceClient :: new ( channel) ;
304+ let request = tonic:: Request :: new ( StateContainerRequest {
305+ uuid : container_id. to_string ( ) ,
306+ } ) ;
307+ let response = client. state_container ( request) . await ?;
308+
309+
273310 Ok ( Response :: new (
274311 isolated_container_service:: StateContainerResponse {
275- state : "" . to_string ( ) ,
276- pid : None ,
312+ state : response . get_ref ( ) . state . to_string ( ) ,
313+ pid : response . get_ref ( ) . pid ,
277314 } ,
278315 ) )
279316 }
0 commit comments