Implement routes to start VMs of a group

This commit is contained in:
Pierre HUBERT 2024-12-03 21:18:05 +01:00
parent 26fee59c5d
commit 269f6027b7
4 changed files with 83 additions and 3 deletions

View File

@ -0,0 +1,30 @@
use crate::controllers::HttpResult;
use crate::virtweb_client;
use crate::virtweb_client::{GroupID, VMUuid};
use actix_web::{web, HttpResponse};
#[derive(serde::Deserialize)]
pub struct GroupIDInPath {
gid: GroupID,
}
#[derive(serde::Deserialize)]
pub struct VMIDInQuery {
vm_id: Option<VMUuid>,
}
/// Get the state of one or all VM
pub async fn vm_state(
path: web::Path<GroupIDInPath>,
query: web::Query<VMIDInQuery>,
) -> HttpResult {
Ok(HttpResponse::Ok().json(virtweb_client::group_vm_state(&path.gid, query.vm_id).await?))
}
/// Start one or all VM
pub async fn vm_start(
path: web::Path<GroupIDInPath>,
query: web::Query<VMIDInQuery>,
) -> HttpResult {
Ok(HttpResponse::Ok().json(virtweb_client::group_vm_start(&path.gid, query.vm_id).await?))
}

View File

@ -6,6 +6,7 @@ use std::fmt::{Display, Formatter};
use std::io::ErrorKind; use std::io::ErrorKind;
pub mod auth_controller; pub mod auth_controller;
pub mod group_controller;
pub mod server_controller; pub mod server_controller;
pub mod static_controller; pub mod static_controller;
pub mod sys_info_controller; pub mod sys_info_controller;

View File

@ -12,7 +12,8 @@ use light_openid::basic_state_manager::BasicStateManager;
use remote_backend::app_config::AppConfig; use remote_backend::app_config::AppConfig;
use remote_backend::constants; use remote_backend::constants;
use remote_backend::controllers::{ use remote_backend::controllers::{
auth_controller, server_controller, static_controller, sys_info_controller, vm_controller, auth_controller, group_controller, server_controller, static_controller, sys_info_controller,
vm_controller,
}; };
use remote_backend::middlewares::auth_middleware::AuthChecker; use remote_backend::middlewares::auth_middleware::AuthChecker;
use std::time::Duration; use std::time::Duration;
@ -86,6 +87,30 @@ async fn main() -> std::io::Result<()> {
"/api/server/rights", "/api/server/rights",
web::get().to(server_controller::rights), web::get().to(server_controller::rights),
) )
// Groups routes
.route(
"/api/group/{gid}/vm/state",
web::get().to(group_controller::vm_state),
)
.route(
"/api/group/{gid}/vm/start",
web::get().to(group_controller::vm_start),
)
/*.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/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/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

@ -1,6 +1,7 @@
use crate::app_config::AppConfig; use crate::app_config::AppConfig;
use crate::utils::time; use crate::utils::time;
use lazy_regex::regex; use lazy_regex::regex;
use std::collections::HashMap;
use std::fmt::Display; use std::fmt::Display;
use std::str::FromStr; use std::str::FromStr;
use thiserror::Error; use thiserror::Error;
@ -102,9 +103,15 @@ impl GroupID {
} }
} }
#[derive(Eq, PartialEq, Debug, Copy, Clone, serde::Serialize, serde::Deserialize)] #[derive(Eq, PartialEq, Debug, Copy, Clone, serde::Serialize, serde::Deserialize, Hash)]
pub struct VMUuid(Uuid); pub struct VMUuid(Uuid);
#[derive(Default, serde::Deserialize, serde::Serialize)]
pub struct TreatmentResult {
ok: usize,
failed: usize,
}
impl VMUuid { impl VMUuid {
pub fn route_info(&self) -> String { pub fn route_info(&self) -> String {
format!("/api/vm/{}", self.0) format!("/api/vm/{}", self.0)
@ -268,12 +275,13 @@ async fn request<D: Display>(uri: D) -> anyhow::Result<reqwest::Response> {
let url = format!("{}{}", AppConfig::get().virtweb_base_url, uri); let url = format!("{}{}", AppConfig::get().virtweb_base_url, uri);
log::debug!("Will query {uri}..."); log::debug!("Will query {uri}...");
let uri = uri.to_string();
let jwt = TokenClaims { let jwt = TokenClaims {
sub: AppConfig::get().virtweb_token_id.to_string(), sub: AppConfig::get().virtweb_token_id.to_string(),
iat: time() - 60 * 2, iat: time() - 60 * 2,
exp: time() + 60 * 3, exp: time() + 60 * 3,
verb: "GET".to_string(), verb: "GET".to_string(),
path: uri.to_string(), path: uri.split_once('?').map(|s| s.0).unwrap_or(&uri).to_string(),
nonce: Uuid::new_v4().to_string(), nonce: Uuid::new_v4().to_string(),
}; };
let jwt = AppConfig::get().token_private_key().sign_jwt(&jwt)?; let jwt = AppConfig::get().token_private_key().sign_jwt(&jwt)?;
@ -365,6 +373,22 @@ pub async fn group_vm_info(id: &GroupID) -> anyhow::Result<Vec<VMInfo>> {
json_request(id.route_vm_info()).await json_request(id.route_vm_info()).await
} }
/// Get the state of one or all VMs of a group
pub async fn group_vm_state(
id: &GroupID,
vm_id: Option<VMUuid>,
) -> anyhow::Result<HashMap<VMUuid, String>> {
json_request(id.route_vm_state(vm_id)).await
}
/// Start one or all VMs of a group
pub async fn group_vm_start(
id: &GroupID,
vm_id: Option<VMUuid>,
) -> anyhow::Result<TreatmentResult> {
json_request(id.route_vm_start(vm_id)).await
}
/// 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