Can enable autostart of VMs

This commit is contained in:
2023-10-28 17:30:27 +02:00
parent 9a15fb4f60
commit 335aec788e
10 changed files with 304 additions and 56 deletions

View File

@ -308,3 +308,39 @@ impl Handler<ScreenshotDomainReq> for LibVirtActor {
Ok(png_out.into_inner())
}
}
#[derive(Message)]
#[rtype(result = "anyhow::Result<bool>")]
pub struct IsDomainAutostart(pub DomainXMLUuid);
impl Handler<IsDomainAutostart> for LibVirtActor {
type Result = anyhow::Result<bool>;
fn handle(&mut self, msg: IsDomainAutostart, _ctx: &mut Self::Context) -> Self::Result {
log::debug!(
"Check if autostart is enabled for a domain: {}",
msg.0.as_string()
);
let domain = Domain::lookup_by_uuid_string(&self.m, &msg.0.as_string())?;
Ok(domain.get_autostart()?)
}
}
#[derive(Message)]
#[rtype(result = "anyhow::Result<()>")]
pub struct SetDomainAutostart(pub DomainXMLUuid, pub bool);
impl Handler<SetDomainAutostart> for LibVirtActor {
type Result = anyhow::Result<()>;
fn handle(&mut self, msg: SetDomainAutostart, _ctx: &mut Self::Context) -> Self::Result {
log::debug!(
"Set autostart enabled={} for a domain: {}",
msg.1,
msg.0.as_string()
);
let domain = Domain::lookup_by_uuid_string(&self.m, &msg.0.as_string())?;
domain.set_autostart(msg.1)?;
Ok(())
}
}

View File

@ -90,6 +90,28 @@ pub async fn update(
Ok(HttpResponse::Ok().finish())
}
#[derive(serde::Serialize, serde::Deserialize)]
pub struct VMAutostart {
autostart: bool,
}
/// Get autostart value of a vm
pub async fn get_autostart(client: LibVirtReq, id: web::Path<SingleVMUUidReq>) -> HttpResult {
Ok(HttpResponse::Ok().json(VMAutostart {
autostart: client.is_domain_autostart(id.uid).await?,
}))
}
/// Configure autostart value for a vm
pub async fn set_autostart(
client: LibVirtReq,
id: web::Path<SingleVMUUidReq>,
body: web::Json<VMAutostart>,
) -> HttpResult {
client.set_domain_autostart(id.uid, body.autostart).await?;
Ok(HttpResponse::Accepted().json("OK"))
}
#[derive(serde::Deserialize)]
pub struct DeleteVMQuery {
keep_files: bool,

View File

@ -79,4 +79,19 @@ impl LibVirtClient {
pub async fn screenshot_domain(&self, id: DomainXMLUuid) -> anyhow::Result<Vec<u8>> {
self.0.send(libvirt_actor::ScreenshotDomainReq(id)).await?
}
/// Get auto-start status of a domain
pub async fn is_domain_autostart(&self, id: DomainXMLUuid) -> anyhow::Result<bool> {
self.0.send(libvirt_actor::IsDomainAutostart(id)).await?
}
pub async fn set_domain_autostart(
&self,
id: DomainXMLUuid,
autostart: bool,
) -> anyhow::Result<()> {
self.0
.send(libvirt_actor::SetDomainAutostart(id, autostart))
.await?
}
}

View File

@ -77,7 +77,6 @@ pub struct VMInfo {
pub iso_file: Option<String>,
/// Storage - https://access.redhat.com/documentation/fr-fr/red_hat_enterprise_linux/6/html/virtualization_administration_guide/sect-virtualization-virtualized_block_devices-adding_storage_devices_to_guests#sect-Virtualization-Adding_storage_devices_to_guests-Adding_file_based_storage_to_a_guest
pub disks: Vec<Disk>,
// TODO : autostart
// TODO : network interface
}

View File

@ -144,6 +144,14 @@ 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}/autostart",
web::get().to(vm_controller::get_autostart),
)
.route(
"/api/vm/{uid}/autostart",
web::put().to(vm_controller::set_autostart),
)
.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))