Implement routes to shutdown, kill, reset, suspend, resume and take screenshot of VMs of a group
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
Pierre HUBERT 2024-12-03 21:26:19 +01:00
parent 269f6027b7
commit acb9baee23
3 changed files with 109 additions and 5 deletions

View File

@ -28,3 +28,52 @@ pub async fn vm_start(
) -> HttpResult { ) -> HttpResult {
Ok(HttpResponse::Ok().json(virtweb_client::group_vm_start(&path.gid, query.vm_id).await?)) 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<GroupIDInPath>,
query: web::Query<VMIDInQuery>,
) -> 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<GroupIDInPath>, query: web::Query<VMIDInQuery>) -> 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<GroupIDInPath>,
query: web::Query<VMIDInQuery>,
) -> 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<GroupIDInPath>,
query: web::Query<VMIDInQuery>,
) -> 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<GroupIDInPath>,
query: web::Query<VMIDInQuery>,
) -> 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<GroupIDInPath>,
query: web::Query<VMIDInQuery>,
) -> 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))
}

View File

@ -96,21 +96,30 @@ async fn main() -> std::io::Result<()> {
"/api/group/{gid}/vm/start", "/api/group/{gid}/vm/start",
web::get().to(group_controller::vm_start), web::get().to(group_controller::vm_start),
) )
/*.route( .route(
"/api/group/{gid}/vm/shutdown", "/api/group/{gid}/vm/shutdown",
web::get().to(group_controller::vm_shutdown), web::get().to(group_controller::vm_shutdown),
) )
.route("/api/group/{gid}/vm/kill", web::get().to(group_controller::vm_kill)) .route(
.route("/api/group/{gid}/vm/reset", web::get().to(group_controller::vm_reset)) "/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( .route(
"/api/group/{gid}/vm/suspend", "/api/group/{gid}/vm/suspend",
web::get().to(group_controller::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( .route(
"/api/group/{gid}/vm/screenshot", "/api/group/{gid}/vm/screenshot",
web::get().to(group_controller::vm_screenshot), web::get().to(group_controller::vm_screenshot),
)*/ )
// VM routes // VM routes
.route("/api/vm/{uid}/state", web::get().to(vm_controller::state)) .route("/api/vm/{uid}/state", web::get().to(vm_controller::state))
.route("/api/vm/{uid}/start", web::get().to(vm_controller::start)) .route("/api/vm/{uid}/start", web::get().to(vm_controller::start))

View File

@ -389,6 +389,52 @@ pub async fn group_vm_start(
json_request(id.route_vm_start(vm_id)).await 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<VMUuid>,
) -> anyhow::Result<TreatmentResult> {
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<VMUuid>) -> anyhow::Result<TreatmentResult> {
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<VMUuid>,
) -> anyhow::Result<TreatmentResult> {
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<VMUuid>,
) -> anyhow::Result<TreatmentResult> {
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<VMUuid>,
) -> anyhow::Result<TreatmentResult> {
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<VMUuid>) -> anyhow::Result<Vec<u8>> {
Ok(request(id.route_vm_screenshot(vm_id))
.await?
.bytes()
.await?
.to_vec())
}
/// Get current server information /// Get current server information
pub async fn get_server_info() -> anyhow::Result<SystemInfo> { pub async fn get_server_info() -> anyhow::Result<SystemInfo> {
json_request("/api/server/info").await json_request("/api/server/info").await