VirtWeb/virtweb_backend/src/libvirt_client.rs

189 lines
6.6 KiB
Rust
Raw Normal View History

2023-09-06 16:54:38 +00:00
use crate::actors::libvirt_actor;
2023-10-04 09:18:50 +00:00
use crate::actors::libvirt_actor::LibVirtActor;
2023-12-28 18:29:26 +00:00
use crate::libvirt_lib_structures::domain::{DomainState, DomainXML};
use crate::libvirt_lib_structures::network::NetworkXML;
use crate::libvirt_lib_structures::nwfilter::NetworkFilterXML;
use crate::libvirt_lib_structures::XMLUuid;
use crate::libvirt_rest_structures::hypervisor::HypervisorInfo;
use crate::libvirt_rest_structures::net::NetworkInfo;
use crate::libvirt_rest_structures::vm::VMInfo;
2023-09-06 16:54:38 +00:00
use actix::Addr;
#[derive(Clone)]
pub struct LibVirtClient(pub Addr<LibVirtActor>);
impl LibVirtClient {
/// Get hypervisor info
pub async fn get_info(&self) -> anyhow::Result<HypervisorInfo> {
self.0.send(libvirt_actor::GetHypervisorInfo).await?
}
2023-10-04 09:18:50 +00:00
2023-10-09 17:16:33 +00:00
/// Get the full list of domain
2023-10-31 09:51:13 +00:00
pub async fn get_full_domains_list(&self) -> anyhow::Result<Vec<DomainXML>> {
2023-10-09 17:16:33 +00:00
let ids = self.0.send(libvirt_actor::GetDomainsListReq).await??;
let mut info = Vec::with_capacity(ids.len());
for id in ids {
info.push(self.get_single_domain(id).await?)
}
Ok(info)
}
2023-10-04 17:03:20 +00:00
/// Get the information about a single domain
2023-10-31 08:26:42 +00:00
pub async fn get_single_domain(&self, id: XMLUuid) -> anyhow::Result<DomainXML> {
2023-10-04 17:03:20 +00:00
self.0.send(libvirt_actor::GetDomainXMLReq(id)).await?
}
/// Get the source XML configuration of a single domain
pub async fn get_single_domain_xml(&self, id: XMLUuid) -> anyhow::Result<String> {
self.0
.send(libvirt_actor::GetSourceDomainXMLReq(id))
.await?
}
2023-10-04 09:18:50 +00:00
/// Update a domain
pub async fn update_domain(&self, vm_def: VMInfo, xml: DomainXML) -> anyhow::Result<XMLUuid> {
self.0
.send(libvirt_actor::DefineDomainReq(vm_def, xml))
.await?
2023-10-04 09:18:50 +00:00
}
2023-10-09 16:45:41 +00:00
2023-10-13 14:44:56 +00:00
/// Delete a domain
2023-10-31 08:26:42 +00:00
pub async fn delete_domain(&self, id: XMLUuid, keep_files: bool) -> anyhow::Result<()> {
2023-10-13 14:44:56 +00:00
self.0
.send(libvirt_actor::DeleteDomainReq { id, keep_files })
.await?
}
2023-10-09 16:45:41 +00:00
/// Get the state of a domain
2023-10-31 08:26:42 +00:00
pub async fn get_domain_state(&self, id: XMLUuid) -> anyhow::Result<DomainState> {
2023-10-09 16:45:41 +00:00
self.0.send(libvirt_actor::GetDomainStateReq(id)).await?
}
2023-10-10 10:35:43 +00:00
/// Start a domain
2023-10-31 08:26:42 +00:00
pub async fn start_domain(&self, id: XMLUuid) -> anyhow::Result<()> {
2023-10-10 10:35:43 +00:00
self.0.send(libvirt_actor::StartDomainReq(id)).await?
}
/// Shutdown a domain
2023-10-31 08:26:42 +00:00
pub async fn shutdown_domain(&self, id: XMLUuid) -> anyhow::Result<()> {
self.0.send(libvirt_actor::ShutdownDomainReq(id)).await?
}
/// Kill a domain
2023-10-31 08:26:42 +00:00
pub async fn kill_domain(&self, id: XMLUuid) -> anyhow::Result<()> {
self.0.send(libvirt_actor::KillDomainReq(id)).await?
}
/// Reset a domain
2023-10-31 08:26:42 +00:00
pub async fn reset_domain(&self, id: XMLUuid) -> anyhow::Result<()> {
self.0.send(libvirt_actor::ResetDomainReq(id)).await?
}
/// Suspend a domain
2023-10-31 08:26:42 +00:00
pub async fn suspend_domain(&self, id: XMLUuid) -> anyhow::Result<()> {
self.0.send(libvirt_actor::SuspendDomainReq(id)).await?
}
/// Resume a domain
2023-10-31 08:26:42 +00:00
pub async fn resume_domain(&self, id: XMLUuid) -> anyhow::Result<()> {
self.0.send(libvirt_actor::ResumeDomainReq(id)).await?
}
2023-10-13 13:27:01 +00:00
/// Take a screenshot of the domain
2023-10-31 08:26:42 +00:00
pub async fn screenshot_domain(&self, id: XMLUuid) -> anyhow::Result<Vec<u8>> {
2023-10-13 13:27:01 +00:00
self.0.send(libvirt_actor::ScreenshotDomainReq(id)).await?
}
2023-10-28 15:30:27 +00:00
/// Get auto-start status of a domain
2023-10-31 08:26:42 +00:00
pub async fn is_domain_autostart(&self, id: XMLUuid) -> anyhow::Result<bool> {
2023-10-28 15:30:27 +00:00
self.0.send(libvirt_actor::IsDomainAutostart(id)).await?
}
2023-10-31 08:26:42 +00:00
/// Update autostart value of a domain
pub async fn set_domain_autostart(&self, id: XMLUuid, autostart: bool) -> anyhow::Result<()> {
2023-10-28 15:30:27 +00:00
self.0
.send(libvirt_actor::SetDomainAutostart(id, autostart))
.await?
}
2023-10-31 08:26:42 +00:00
/// Update a network configuration
pub async fn update_network(
&self,
net_def: NetworkInfo,
network: NetworkXML,
) -> anyhow::Result<XMLUuid> {
self.0
.send(libvirt_actor::DefineNetwork(net_def, network))
.await?
2023-10-31 08:26:42 +00:00
}
2023-10-31 09:51:13 +00:00
/// Get the full list of networks
pub async fn get_full_networks_list(&self) -> anyhow::Result<Vec<NetworkXML>> {
let ids = self.0.send(libvirt_actor::GetNetworksListReq).await??;
let mut info = Vec::with_capacity(ids.len());
for id in ids {
info.push(self.get_single_network(id).await?)
}
Ok(info)
}
/// Get the information about a single network
pub async fn get_single_network(&self, id: XMLUuid) -> anyhow::Result<NetworkXML> {
self.0.send(libvirt_actor::GetNetworkXMLReq(id)).await?
}
2023-10-31 11:03:37 +00:00
2023-12-11 17:41:59 +00:00
/// Get the source XML configuration of a single network
pub async fn get_single_network_xml(&self, id: XMLUuid) -> anyhow::Result<String> {
self.0
.send(libvirt_actor::GetSourceNetworkXMLReq(id))
.await?
}
2023-10-31 11:03:37 +00:00
/// Delete a network
pub async fn delete_network(&self, id: XMLUuid) -> anyhow::Result<()> {
self.0.send(libvirt_actor::DeleteNetwork(id)).await?
}
/// Get auto-start status of a network
pub async fn is_network_autostart(&self, id: XMLUuid) -> anyhow::Result<bool> {
self.0.send(libvirt_actor::IsNetworkAutostart(id)).await?
}
/// Update autostart value of a network
pub async fn set_network_autostart(&self, id: XMLUuid, autostart: bool) -> anyhow::Result<()> {
self.0
.send(libvirt_actor::SetNetworkAutostart(id, autostart))
.await?
}
/// Check out whether a network is started or not
pub async fn is_network_started(&self, id: XMLUuid) -> anyhow::Result<bool> {
self.0.send(libvirt_actor::IsNetworkStarted(id)).await?
}
/// Start a network
pub async fn start_network(&self, id: XMLUuid) -> anyhow::Result<()> {
self.0.send(libvirt_actor::StartNetwork(id)).await?
}
/// Stop a network
pub async fn stop_network(&self, id: XMLUuid) -> anyhow::Result<()> {
self.0.send(libvirt_actor::StopNetwork(id)).await?
}
2023-12-28 14:12:38 +00:00
/// Get the full list of network filters
pub async fn get_full_network_filters_list(&self) -> anyhow::Result<Vec<NetworkFilterXML>> {
let ids = self.0.send(libvirt_actor::GetNWFiltersListReq).await??;
let mut info = Vec::with_capacity(ids.len());
for id in ids {
info.push(self.get_single_network_filter(id).await?)
}
Ok(info)
}
/// Get the information about a single domain
pub async fn get_single_network_filter(&self, id: XMLUuid) -> anyhow::Result<NetworkFilterXML> {
self.0.send(libvirt_actor::GetNWFilterXMLReq(id)).await?
}
2023-09-06 16:54:38 +00:00
}