fix: improve patch host code
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-03-28 13:38:49 +00:00
parent ea6499558d
commit e2d6d84439
2 changed files with 57 additions and 42 deletions

View File

@ -6,7 +6,7 @@ use crate::json_api::JsonApi;
use crate::settings::{QemuConfig, SshConfig}; use crate::settings::{QemuConfig, SshConfig};
use crate::ssh_client::SshClient; use crate::ssh_client::SshClient;
use crate::status::{VmRunningState, VmState}; use crate::status::{VmRunningState, VmState};
use anyhow::{anyhow, bail, ensure, Result}; use anyhow::{anyhow, bail, ensure, Context, Result};
use chrono::Utc; use chrono::Utc;
use futures::StreamExt; use futures::StreamExt;
use ipnetwork::IpNetwork; use ipnetwork::IpNetwork;
@ -500,14 +500,25 @@ impl VmHostClient for ProxmoxClient {
if let Some(n) = nodes.iter().find(|n| n.name == self.node) { if let Some(n) = nodes.iter().find(|n| n.name == self.node) {
let storages = self.list_storage(&n.name).await?; let storages = self.list_storage(&n.name).await?;
let info = VmHostInfo { let info = VmHostInfo {
cpu: n.max_cpu.unwrap_or(0), cpu: n.max_cpu
memory: n.max_mem.unwrap_or(0), .context("Missing cpu count, please make sure you have Sys.Audit permission")?,
memory: n.max_mem
.context("Missing memory size, please make sure you have Sys.Audit permission")?,
disks: storages disks: storages
.into_iter() .into_iter()
.map(|s| VmHostDiskInfo { .filter_map(|s| {
name: s.storage, let size = s.total
size: s.total.unwrap_or(0), .context("Missing disk size, please make sure you have Datastore.Audit permission")
used: s.used.unwrap_or(0), .ok()?;
let used = s.used
.context("Missing used disk, please make sure you have Datastore.Audit permission")
.ok()?;
Some(VmHostDiskInfo {
name: s.storage,
size,
used,
})
}) })
.collect(), .collect(),
}; };

View File

@ -2,13 +2,13 @@ use crate::host::get_host_client;
use crate::provisioner::LNVpsProvisioner; use crate::provisioner::LNVpsProvisioner;
use crate::settings::{ProvisionerConfig, Settings, SmtpConfig}; use crate::settings::{ProvisionerConfig, Settings, SmtpConfig};
use crate::status::{VmRunningState, VmState, VmStateCache}; use crate::status::{VmRunningState, VmState, VmStateCache};
use anyhow::Result; use anyhow::{bail, Result};
use chrono::{DateTime, Datelike, Days, Utc}; use chrono::{DateTime, Datelike, Days, Utc};
use lettre::message::{MessageBuilder, MultiPart}; use lettre::message::{MessageBuilder, MultiPart};
use lettre::transport::smtp::authentication::Credentials; use lettre::transport::smtp::authentication::Credentials;
use lettre::AsyncTransport; use lettre::AsyncTransport;
use lettre::{AsyncSmtpTransport, Tokio1Executor}; use lettre::{AsyncSmtpTransport, Tokio1Executor};
use lnvps_db::{LNVpsDb, Vm}; use lnvps_db::{LNVpsDb, Vm, VmHost};
use log::{debug, error, info, warn}; use log::{debug, error, info, warn};
use nostr::{EventBuilder, PublicKey, ToBech32}; use nostr::{EventBuilder, PublicKey, ToBech32};
use nostr_sdk::Client; use nostr_sdk::Client;
@ -280,45 +280,49 @@ impl Worker {
Ok(()) Ok(())
} }
async fn patch_host(&self, host: &mut VmHost) -> Result<()> {
let client = match get_host_client(host, &self.settings.provisioner_config) {
Ok(h) => h,
Err(e) => bail!("Failed to get host client: {} {}", host.name, e),
};
let info = client.get_info().await?;
let needs_update = info.cpu != host.cpu || info.memory != host.memory;
if needs_update {
host.cpu = info.cpu;
host.memory = info.memory;
self.db.update_host(host).await?;
info!(
"Updated host {}: cpu={}, memory={}",
host.name, host.cpu, host.memory
);
}
let mut host_disks = self.db.list_host_disks(host.id).await?;
for disk in &info.disks {
if let Some(mut hd) = host_disks.iter_mut().find(|d| d.name == disk.name) {
if hd.size != disk.size {
hd.size = disk.size;
self.db.update_host_disk(hd).await?;
info!(
"Updated host disk {}: size={},type={},interface={}",
hd.name, hd.size, hd.kind, hd.interface
);
}
} else {
warn!("Un-mapped host disk {}", disk.name);
}
}
Ok(())
}
async fn try_job(&mut self, job: &WorkJob) -> Result<()> { async fn try_job(&mut self, job: &WorkJob) -> Result<()> {
match job { match job {
WorkJob::PatchHosts => { WorkJob::PatchHosts => {
let mut hosts = self.db.list_hosts().await?; let mut hosts = self.db.list_hosts().await?;
for mut host in &mut hosts { for mut host in &mut hosts {
info!("Patching host {}", host.name); info!("Patching host {}", host.name);
let client = match get_host_client(host, &self.settings.provisioner_config) { if let Err(e) = self.patch_host(&mut host).await {
Ok(h) => h, error!("Failed to patch host {}: {}", host.name, e);
Err(e) => {
warn!("Failed to get host client: {} {}", host.name, e);
continue;
}
};
let info = client.get_info().await?;
let needs_update = info.cpu != host.cpu || info.memory != host.memory;
if needs_update {
host.cpu = info.cpu;
host.memory = info.memory;
self.db.update_host(host).await?;
info!(
"Updated host {}: cpu={}, memory={}",
host.name, host.cpu, host.memory
);
}
let mut host_disks = self.db.list_host_disks(host.id).await?;
for disk in &info.disks {
if let Some(mut hd) = host_disks.iter_mut().find(|d| d.name == disk.name) {
if hd.size != disk.size {
hd.size = disk.size;
self.db.update_host_disk(hd).await?;
info!(
"Updated host disk {}: size={},type={},interface={}",
hd.name, hd.size, hd.kind, hd.interface
);
}
} else {
warn!("Un-mapped host disk {}", disk.name);
}
} }
} }
} }