Add system information

This commit is contained in:
Pierre HUBERT 2024-05-03 21:24:43 +02:00
parent 2fefdb97be
commit 9407d8f0a8
4 changed files with 58 additions and 2 deletions

View File

@ -7,6 +7,7 @@ use std::io::ErrorKind;
pub mod auth_controller;
pub mod server_controller;
pub mod sys_info_controller;
pub mod vm_controller;
/// Custom error to ease controller writing

View File

@ -0,0 +1,22 @@
use crate::controllers::HttpResult;
use crate::virtweb_client;
use actix_web::HttpResponse;
#[derive(serde::Serialize)]
struct SysInfoStatus {
allowed: bool,
}
/// Check if system info can be retrieved
pub async fn config() -> HttpResult {
let info = virtweb_client::get_token_info().await?;
Ok(HttpResponse::Ok().json(SysInfoStatus {
allowed: info.can_retrieve_system_info(),
}))
}
/// Get current system status
pub async fn status() -> HttpResult {
Ok(HttpResponse::Ok().json(virtweb_client::get_server_info().await?))
}

View File

@ -11,7 +11,9 @@ use actix_web::{web, App, HttpServer};
use light_openid::basic_state_manager::BasicStateManager;
use remote_backend::app_config::AppConfig;
use remote_backend::constants;
use remote_backend::controllers::{auth_controller, server_controller, vm_controller};
use remote_backend::controllers::{
auth_controller, server_controller, sys_info_controller, vm_controller,
};
use remote_backend::middlewares::auth_middleware::AuthChecker;
use std::time::Duration;
@ -97,6 +99,14 @@ async fn main() -> std::io::Result<()> {
"/api/vm/{uid}/screenshot",
web::get().to(vm_controller::screenshot),
)
.route(
"/api/sysinfo/config",
web::get().to(sys_info_controller::config),
)
.route(
"/api/sysinfo/status",
web::get().to(sys_info_controller::status),
)
})
.bind(&AppConfig::get().listen_address)?
.run()

View File

@ -84,6 +84,19 @@ pub struct VMState {
pub state: String,
}
#[derive(serde::Deserialize, serde::Serialize, Debug)]
pub struct SystemSystemInfo {
physical_core_count: usize,
uptime: usize,
used_memory: usize,
available_memory: usize,
}
#[derive(serde::Deserialize, serde::Serialize, Debug)]
pub struct SystemInfo {
system: SystemSystemInfo,
}
#[derive(serde::Deserialize, Debug)]
pub struct TokenRight {
verb: String,
@ -134,6 +147,11 @@ impl TokenInfo {
.map(|r| VMUuid::from_str(r.path.rsplit_once('/').unwrap().1).unwrap())
.collect::<Vec<_>>()
}
/// Check if system info can be retrived
pub fn can_retrieve_system_info(&self) -> bool {
self.is_route_allowed("GET", "/api/server/info")
}
}
/// Perform a request on the API
@ -224,7 +242,7 @@ pub async fn vm_resume(id: VMUuid) -> anyhow::Result<()> {
Ok(())
}
/// Resume a vm
/// Grab a screenshot of the VM
pub async fn vm_screenshot(id: VMUuid) -> anyhow::Result<Vec<u8>> {
Ok(request(id.route_screenshot())
.await?
@ -232,3 +250,8 @@ pub async fn vm_screenshot(id: VMUuid) -> anyhow::Result<Vec<u8>> {
.await?
.to_vec())
}
/// Get current server information
pub async fn get_server_info() -> anyhow::Result<SystemInfo> {
json_request("/api/server/info").await
}