diff --git a/virtweb_backend/src/actors/libvirt_actor.rs b/virtweb_backend/src/actors/libvirt_actor.rs index 309e79f..75d5c5b 100644 --- a/virtweb_backend/src/actors/libvirt_actor.rs +++ b/virtweb_backend/src/actors/libvirt_actor.rs @@ -113,6 +113,34 @@ impl Handler for LibVirtActor { } } +#[derive(Message)] +#[rtype(result = "anyhow::Result<()>")] +pub struct DeleteDomainReq { + pub id: DomainXMLUuid, + pub keep_files: bool, +} + +impl Handler for LibVirtActor { + type Result = anyhow::Result<()>; + + fn handle(&mut self, msg: DeleteDomainReq, _ctx: &mut Self::Context) -> Self::Result { + log::debug!( + "Delete domain: {:?} (keep files: {})", + msg.id, + msg.keep_files + ); + + let domain = Domain::lookup_by_uuid_string(&self.m, &msg.id.as_string())?; + domain.undefine_flags(match msg.keep_files { + true => sys::VIR_DOMAIN_UNDEFINE_KEEP_NVRAM, + false => sys::VIR_DOMAIN_UNDEFINE_NVRAM, + })?; + + // TODO : delete files, if requested + Ok(()) + } +} + #[derive(Message)] #[rtype(result = "anyhow::Result")] pub struct GetDomainStateReq(pub DomainXMLUuid); @@ -121,7 +149,7 @@ impl Handler for LibVirtActor { type Result = anyhow::Result; fn handle(&mut self, msg: GetDomainStateReq, _ctx: &mut Self::Context) -> Self::Result { - log::debug!("Get domain state:\n{}", msg.0.as_string()); + log::debug!("Get domain state: {}", msg.0.as_string()); let domain = Domain::lookup_by_uuid_string(&self.m, &msg.0.as_string())?; let (state, _) = domain.get_state()?; Ok(match state { @@ -147,7 +175,7 @@ impl Handler for LibVirtActor { type Result = anyhow::Result<()>; fn handle(&mut self, msg: StartDomainReq, _ctx: &mut Self::Context) -> Self::Result { - log::debug!("Start domain:\n{}", msg.0.as_string()); + log::debug!("Start domain: {}", msg.0.as_string()); let domain = Domain::lookup_by_uuid_string(&self.m, &msg.0.as_string())?; domain.create()?; Ok(()) @@ -162,7 +190,7 @@ impl Handler for LibVirtActor { type Result = anyhow::Result<()>; fn handle(&mut self, msg: ShutdownDomainReq, _ctx: &mut Self::Context) -> Self::Result { - log::debug!("Shutdown domain:\n{}", msg.0.as_string()); + log::debug!("Shutdown domain: {}", msg.0.as_string()); let domain = Domain::lookup_by_uuid_string(&self.m, &msg.0.as_string())?; domain.shutdown()?; Ok(()) @@ -177,7 +205,7 @@ impl Handler for LibVirtActor { type Result = anyhow::Result<()>; fn handle(&mut self, msg: KillDomainReq, _ctx: &mut Self::Context) -> Self::Result { - log::debug!("Kill domain:\n{}", msg.0.as_string()); + log::debug!("Kill domain: {}", msg.0.as_string()); let domain = Domain::lookup_by_uuid_string(&self.m, &msg.0.as_string())?; domain.destroy()?; Ok(()) @@ -192,7 +220,7 @@ impl Handler for LibVirtActor { type Result = anyhow::Result<()>; fn handle(&mut self, msg: ResetDomainReq, _ctx: &mut Self::Context) -> Self::Result { - log::debug!("Reset domain:\n{}", msg.0.as_string()); + log::debug!("Reset domain: {}", msg.0.as_string()); let domain = Domain::lookup_by_uuid_string(&self.m, &msg.0.as_string())?; domain.reset()?; Ok(()) @@ -207,7 +235,7 @@ impl Handler for LibVirtActor { type Result = anyhow::Result<()>; fn handle(&mut self, msg: SuspendDomainReq, _ctx: &mut Self::Context) -> Self::Result { - log::debug!("Suspend domain:\n{}", msg.0.as_string()); + log::debug!("Suspend domain: {}", msg.0.as_string()); let domain = Domain::lookup_by_uuid_string(&self.m, &msg.0.as_string())?; domain.suspend()?; Ok(()) @@ -222,7 +250,7 @@ impl Handler for LibVirtActor { type Result = anyhow::Result<()>; fn handle(&mut self, msg: ResumeDomainReq, _ctx: &mut Self::Context) -> Self::Result { - log::debug!("Resume domain:\n{}", msg.0.as_string()); + log::debug!("Resume domain: {}", msg.0.as_string()); let domain = Domain::lookup_by_uuid_string(&self.m, &msg.0.as_string())?; domain.resume()?; Ok(()) @@ -237,7 +265,7 @@ impl Handler for LibVirtActor { type Result = anyhow::Result>; fn handle(&mut self, msg: ScreenshotDomainReq, _ctx: &mut Self::Context) -> Self::Result { - log::debug!("Take screenshot of domain:\n{}", msg.0.as_string()); + log::debug!("Take screenshot of domain: {}", msg.0.as_string()); let domain = Domain::lookup_by_uuid_string(&self.m, &msg.0.as_string())?; let stream = Stream::new(&self.m, 0)?; diff --git a/virtweb_backend/src/controllers/mod.rs b/virtweb_backend/src/controllers/mod.rs index a3c4e25..842ae5d 100644 --- a/virtweb_backend/src/controllers/mod.rs +++ b/virtweb_backend/src/controllers/mod.rs @@ -12,13 +12,19 @@ pub mod vm_controller; /// Custom error to ease controller writing #[derive(Debug)] -pub struct HttpErr { - err: anyhow::Error, +pub enum HttpErr { + Err(anyhow::Error), + HTTPResponse(HttpResponse), } impl Display for HttpErr { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - Display::fmt(&self.err, f) + match self { + HttpErr::Err(err) => Display::fmt(err, f), + HttpErr::HTTPResponse(res) => { + Display::fmt(&format!("HTTP RESPONSE {}", res.status().as_str()), f) + } + } } } @@ -31,54 +37,57 @@ impl actix_web::error::ResponseError for HttpErr { impl From for HttpErr { fn from(err: anyhow::Error) -> HttpErr { - HttpErr { err } + HttpErr::Err(err) } } impl From for HttpErr { fn from(value: serde_json::Error) -> Self { - HttpErr { err: value.into() } + HttpErr::Err(value.into()) } } impl From> for HttpErr { fn from(value: Box) -> Self { - HttpErr { - err: std::io::Error::new(ErrorKind::Other, value.to_string()).into(), - } + HttpErr::Err(std::io::Error::new(ErrorKind::Other, value.to_string()).into()) } } impl From for HttpErr { fn from(value: std::io::Error) -> Self { - HttpErr { err: value.into() } + HttpErr::Err(value.into()) } } impl From for HttpErr { fn from(value: std::num::ParseIntError) -> Self { - HttpErr { err: value.into() } + HttpErr::Err(value.into()) } } impl From for HttpErr { fn from(value: tempfile::PersistError) -> Self { - HttpErr { err: value.into() } + HttpErr::Err(value.into()) } } impl From for HttpErr { fn from(value: reqwest::Error) -> Self { - HttpErr { err: value.into() } + HttpErr::Err(value.into()) } } impl From for HttpErr { fn from(value: reqwest::header::ToStrError) -> Self { - HttpErr { err: value.into() } + HttpErr::Err(value.into()) } } +impl From for HttpErr { + fn from(value: HttpResponse) -> Self { + HttpErr::HTTPResponse(value) + } +} pub type HttpResult = Result; pub type LibVirtReq = web::Data; diff --git a/virtweb_backend/src/controllers/vm_controller.rs b/virtweb_backend/src/controllers/vm_controller.rs index 8b72424..3875cd5 100644 --- a/virtweb_backend/src/controllers/vm_controller.rs +++ b/virtweb_backend/src/controllers/vm_controller.rs @@ -65,6 +65,49 @@ pub async fn get_single(client: LibVirtReq, id: web::Path) -> H })) } +/// Update a VM information +pub async fn update( + client: LibVirtReq, + id: web::Path, + req: web::Json, +) -> HttpResult { + let mut domain = req.0.to_domain().map_err(|e| { + log::error!("Failed to extract domain info! {e}"); + HttpResponse::BadRequest().body(e.to_string()) + })?; + + domain.uuid = Some(id.uid); + client.update_domain(domain).await?; + + Ok(HttpResponse::Ok().finish()) +} + +#[derive(serde::Deserialize)] +pub struct DeleteVMQuery { + keep_files: bool, +} + +/// Delete a VM +pub async fn delete( + client: LibVirtReq, + id: web::Path, + req: web::Json, +) -> HttpResult { + if let Err(e) = client.kill_domain(id.uid).await { + log::info!("Failed to kill domain before deleting it: {e}"); + } + + client + .delete_domain(id.uid, req.keep_files) + .await + .map_err(|e| { + log::error!("Failed to delete domain! {e}"); + HttpResponse::InternalServerError().body(e.to_string()) + })?; + + Ok(HttpResponse::Ok().finish()) +} + /// Start a VM pub async fn start(client: LibVirtReq, id: web::Path) -> HttpResult { Ok(match client.start_domain(id.uid).await { diff --git a/virtweb_backend/src/libvirt_client.rs b/virtweb_backend/src/libvirt_client.rs index d7c53d0..2319b17 100644 --- a/virtweb_backend/src/libvirt_client.rs +++ b/virtweb_backend/src/libvirt_client.rs @@ -33,6 +33,13 @@ impl LibVirtClient { self.0.send(libvirt_actor::DefineDomainReq(xml)).await? } + /// Delete a domain + pub async fn delete_domain(&self, id: DomainXMLUuid, keep_files: bool) -> anyhow::Result<()> { + self.0 + .send(libvirt_actor::DeleteDomainReq { id, keep_files }) + .await? + } + /// Get the state of a domain pub async fn get_domain_state(&self, id: DomainXMLUuid) -> anyhow::Result { self.0.send(libvirt_actor::GetDomainStateReq(id)).await? diff --git a/virtweb_backend/src/libvirt_lib_structures.rs b/virtweb_backend/src/libvirt_lib_structures.rs index e46088a..a4ba609 100644 --- a/virtweb_backend/src/libvirt_lib_structures.rs +++ b/virtweb_backend/src/libvirt_lib_structures.rs @@ -19,7 +19,7 @@ impl DomainXMLUuid { #[derive(serde::Serialize, serde::Deserialize)] #[serde(rename = "os")] pub struct OSXML { - #[serde(rename(serialize = "@firmware"))] + #[serde(rename(serialize = "@firmware"), default)] pub firmware: String, pub r#type: OSTypeXML, pub loader: Option, diff --git a/virtweb_backend/src/main.rs b/virtweb_backend/src/main.rs index ee8698b..4f740ca 100644 --- a/virtweb_backend/src/main.rs +++ b/virtweb_backend/src/main.rs @@ -138,6 +138,8 @@ async fn main() -> std::io::Result<()> { .route("/api/vm/create", web::post().to(vm_controller::create)) .route("/api/vm/list", web::get().to(vm_controller::list_all)) .route("/api/vm/{uid}", web::get().to(vm_controller::get_single)) + .route("/api/vm/{uid}", web::put().to(vm_controller::update)) + .route("/api/vm/{uid}", web::delete().to(vm_controller::delete)) .route("/api/vm/{uid}/start", web::get().to(vm_controller::start)) .route( "/api/vm/{uid}/shutdown",