From acb9baee23d470ebfc6d803a3f9abecca36ee8b0 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 3 Dec 2024 21:26:19 +0100 Subject: [PATCH] Implement routes to shutdown, kill, reset, suspend, resume and take screenshot of VMs of a group --- .../src/controllers/group_controller.rs | 49 +++++++++++++++++++ remote_backend/src/main.rs | 19 +++++-- remote_backend/src/virtweb_client.rs | 46 +++++++++++++++++ 3 files changed, 109 insertions(+), 5 deletions(-) diff --git a/remote_backend/src/controllers/group_controller.rs b/remote_backend/src/controllers/group_controller.rs index 7142c05..ad5deb7 100644 --- a/remote_backend/src/controllers/group_controller.rs +++ b/remote_backend/src/controllers/group_controller.rs @@ -28,3 +28,52 @@ pub async fn vm_start( ) -> HttpResult { Ok(HttpResponse::Ok().json(virtweb_client::group_vm_start(&path.gid, query.vm_id).await?)) } + +/// Shutdown one or all VM +pub async fn vm_shutdown( + path: web::Path, + query: web::Query, +) -> HttpResult { + Ok(HttpResponse::Ok().json(virtweb_client::group_vm_shutdown(&path.gid, query.vm_id).await?)) +} + +/// Kill one or all VM +pub async fn vm_kill(path: web::Path, query: web::Query) -> HttpResult { + Ok(HttpResponse::Ok().json(virtweb_client::group_vm_kill(&path.gid, query.vm_id).await?)) +} + +/// Reset one or all VM +pub async fn vm_reset( + path: web::Path, + query: web::Query, +) -> HttpResult { + Ok(HttpResponse::Ok().json(virtweb_client::group_vm_reset(&path.gid, query.vm_id).await?)) +} + +/// Suspend one or all VM +pub async fn vm_suspend( + path: web::Path, + query: web::Query, +) -> HttpResult { + Ok(HttpResponse::Ok().json(virtweb_client::group_vm_suspend(&path.gid, query.vm_id).await?)) +} + +/// Resume one or all VM +pub async fn vm_resume( + path: web::Path, + query: web::Query, +) -> HttpResult { + Ok(HttpResponse::Ok().json(virtweb_client::group_vm_resume(&path.gid, query.vm_id).await?)) +} + +/// Screenshot one or all VM +pub async fn vm_screenshot( + path: web::Path, + query: web::Query, +) -> HttpResult { + let screenshot = virtweb_client::group_vm_screenshot(&path.gid, query.vm_id).await?; + + Ok(HttpResponse::Ok() + .insert_header(("content-type", "image/png")) + .body(screenshot)) +} diff --git a/remote_backend/src/main.rs b/remote_backend/src/main.rs index 533c9c3..4febb40 100644 --- a/remote_backend/src/main.rs +++ b/remote_backend/src/main.rs @@ -96,21 +96,30 @@ async fn main() -> std::io::Result<()> { "/api/group/{gid}/vm/start", web::get().to(group_controller::vm_start), ) - /*.route( + .route( "/api/group/{gid}/vm/shutdown", web::get().to(group_controller::vm_shutdown), ) - .route("/api/group/{gid}/vm/kill", web::get().to(group_controller::vm_kill)) - .route("/api/group/{gid}/vm/reset", web::get().to(group_controller::vm_reset)) + .route( + "/api/group/{gid}/vm/kill", + web::get().to(group_controller::vm_kill), + ) + .route( + "/api/group/{gid}/vm/reset", + web::get().to(group_controller::vm_reset), + ) .route( "/api/group/{gid}/vm/suspend", web::get().to(group_controller::vm_suspend), ) - .route("/api/group/{gid}/vm/resume", web::get().to(group_controller::vm_resume)) + .route( + "/api/group/{gid}/vm/resume", + web::get().to(group_controller::vm_resume), + ) .route( "/api/group/{gid}/vm/screenshot", web::get().to(group_controller::vm_screenshot), - )*/ + ) // VM routes .route("/api/vm/{uid}/state", web::get().to(vm_controller::state)) .route("/api/vm/{uid}/start", web::get().to(vm_controller::start)) diff --git a/remote_backend/src/virtweb_client.rs b/remote_backend/src/virtweb_client.rs index 7e75cad..9821ad4 100644 --- a/remote_backend/src/virtweb_client.rs +++ b/remote_backend/src/virtweb_client.rs @@ -389,6 +389,52 @@ pub async fn group_vm_start( json_request(id.route_vm_start(vm_id)).await } +/// Shutdown one or all VMs of a group +pub async fn group_vm_shutdown( + id: &GroupID, + vm_id: Option, +) -> anyhow::Result { + json_request(id.route_vm_shutdown(vm_id)).await +} + +/// Kill one or all VMs of a group +pub async fn group_vm_kill(id: &GroupID, vm_id: Option) -> anyhow::Result { + json_request(id.route_vm_kill(vm_id)).await +} + +/// Reset one or all VMs of a group +pub async fn group_vm_reset( + id: &GroupID, + vm_id: Option, +) -> anyhow::Result { + json_request(id.route_vm_reset(vm_id)).await +} + +/// Suspend one or all VMs of a group +pub async fn group_vm_suspend( + id: &GroupID, + vm_id: Option, +) -> anyhow::Result { + json_request(id.route_vm_suspend(vm_id)).await +} + +/// Resume one or all VMs of a group +pub async fn group_vm_resume( + id: &GroupID, + vm_id: Option, +) -> anyhow::Result { + json_request(id.route_vm_resume(vm_id)).await +} + +/// Get the screenshot of one or all VMs of a group +pub async fn group_vm_screenshot(id: &GroupID, vm_id: Option) -> anyhow::Result> { + Ok(request(id.route_vm_screenshot(vm_id)) + .await? + .bytes() + .await? + .to_vec()) +} + /// Get current server information pub async fn get_server_info() -> anyhow::Result { json_request("/api/server/info").await