Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add support for virtual machine information #1989

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix cspell typos
leongross committed Jan 28, 2024
commit 010c044b1241bc684630f911d53ce0f272e6f7cc
1 change: 1 addition & 0 deletions cspell.yaml
Original file line number Diff line number Diff line change
@@ -178,3 +178,4 @@ words:
- zbus
- zvariant
- zwlr
- virt
26 changes: 13 additions & 13 deletions src/blocks/virt.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
//! uri | URI of the hypervisor | qemu:///system
//! `interval` | Update interval, in seconds. | `5`
//! `format` | A string to customise the output of this block. See below for available placeholders. | `" $icon $running.eng(w:1) "`
//! `uru` | The path to the virtualization domain.
//! `uri` | The path to the virtualization domain.
//!
//! Key | Value | Type | Unit
//! ----------|----------------------------------------|--------|-----
@@ -26,7 +26,7 @@
//! block = "virt"
//! uri = "qemu:///system"
//! interval = 2
//! format = " $icon $active/$total ($memory_activey@$cpu_active) "
//! format = " $icon $active/$total ($memory $active@$cpu_active) "
//! ```
//!

@@ -36,7 +36,7 @@ use virt::error::Error;
use virt::sys;

#[derive(Deserialize, Debug, SmartDefault)]
#[serde(default)]
#[serde(deny_unknown_fields, default)]
pub struct Config {
#[default("qemu:///system".into())]
pub uri: ShellString,
@@ -47,45 +47,45 @@ pub struct Config {

pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
let format = config.format.with_default("$icon $active/$total")?;
let mut widget = Widget::new().with_format(format.clone());

let flags: sys::virConnectListAllDomainsFlags = sys::VIR_CONNECT_LIST_DOMAINS_ACTIVE | sys::VIR_CONNECT_LIST_DOMAINS_INACTIVE;
let uri: &str = "qemu:///system";

dbg!(&widget);

loop {
println!("Connecting to hypervisor '{}'", &uri);
let mut con = match Connect::open(uri) {
Ok(c) => c,
Err(e) => panic!("No connection to hypervisor: {}", e),
};

let info: LibvirtInfo = LibvirtInfo::new(&mut con, flags).unwrap();
let info: LibvirtInfo = LibvirtInfo::new(&mut con, flags)
.await
.unwrap();
let mut widget = Widget::new().with_format(format.clone());

widget.set_values(map!(
"icon" => Value::icon("".to_string()),
// "icon" => Value::icon("".to_string()),
// "total" => Value::number(virt_active_doms + virt_inactive_doms),
"running" => Value::number(info.active),
"stopped" => Value::number(info.inactive),
// "paused" => Value::number(virt_inactive_domains),
"total" => Value::number(info.total),
));

api.set_widget(widget.clone())?;

println!("Disconnecting from hypervisor");
api.set_widget(widget)?;
disconnect(&mut con);

select! {
_ = sleep(config.interval.0) => (),
_ = api.wait_for_update_request() => (),
}

}
}


#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, SmartDefault)]
#[serde(deny_unknown_fields, default)]
struct LibvirtInfo {
#[serde(rename = "VMActive")]
active: u32,
@@ -111,7 +111,7 @@ fn disconnect(con: &mut Connect) {
}

impl LibvirtInfo {
pub fn new(con: &mut Connect, flags: sys::virConnectListAllDomainsFlags) -> Result<Self, Error> {
async fn new(con: &mut Connect, flags: sys::virConnectListAllDomainsFlags) -> Result<Self, Error> {
println!("Connected to hypervisor");
match con.get_uri() {
Ok(u) => println!("Connected to hypervisor at '{}'", u),