Can start a domain

This commit is contained in:
Pierre HUBERT 2023-10-10 12:35:43 +02:00
parent 908b0f4c56
commit 0c7128e6eb
6 changed files with 38 additions and 1 deletions

View File

@ -135,3 +135,18 @@ impl Handler<GetDomainStateReq> for LibVirtActor {
})
}
}
#[derive(Message)]
#[rtype(result = "anyhow::Result<()>")]
pub struct StartDomainReq(pub DomainXMLUuid);
impl Handler<StartDomainReq> 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());
let domain = Domain::lookup_by_uuid_string(&self.m, &msg.0.as_string())?;
domain.create()?;
Ok(())
}
}

View File

@ -64,3 +64,14 @@ pub async fn get_single(client: LibVirtReq, id: web::Path<SingleVMUUidReq>) -> H
state,
}))
}
/// Start a VM
pub async fn start(client: LibVirtReq, id: web::Path<SingleVMUUidReq>) -> HttpResult {
Ok(match client.start_domain(id.uid).await {
Ok(_) => HttpResponse::Ok().json("Domain started"),
Err(e) => {
log::error!("Failed to start domain {:?} ! {e}", id.uid);
HttpResponse::InternalServerError().json("Failed to start domain!")
}
})
}

View File

@ -37,4 +37,9 @@ impl LibVirtClient {
pub async fn get_domain_state(&self, id: DomainXMLUuid) -> anyhow::Result<DomainState> {
self.0.send(libvirt_actor::GetDomainStateReq(id)).await?
}
/// Start a domain
pub async fn start_domain(&self, id: DomainXMLUuid) -> anyhow::Result<()> {
self.0.send(libvirt_actor::StartDomainReq(id)).await?
}
}

View File

@ -1,4 +1,4 @@
#[derive(serde::Serialize, serde::Deserialize, Clone, Copy)]
#[derive(serde::Serialize, serde::Deserialize, Clone, Copy, Debug)]
pub struct DomainXMLUuid(pub uuid::Uuid);
impl DomainXMLUuid {

View File

@ -66,6 +66,11 @@ pub struct VMInfo {
pub architecture: VMArchitecture,
/// VM allocated memory, in megabytes
pub memory: usize,
// TODO : storage
// TODO : iso
// TODO : autostart
// TODO : vnc
// TODO : interface
}
impl VMInfo {

View File

@ -138,6 +138,7 @@ 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}/start", web::get().to(vm_controller::start))
})
.bind(&AppConfig::get().listen_address)?
.run()