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| {
let size = s.total
.context("Missing disk size, please make sure you have Datastore.Audit permission")
.ok()?;
let used = s.used
.context("Missing used disk, please make sure you have Datastore.Audit permission")
.ok()?;
Some(VmHostDiskInfo {
name: s.storage, name: s.storage,
size: s.total.unwrap_or(0), size,
used: s.used.unwrap_or(0), 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,18 +280,10 @@ impl Worker {
Ok(()) Ok(())
} }
async fn try_job(&mut self, job: &WorkJob) -> Result<()> { async fn patch_host(&self, host: &mut VmHost) -> Result<()> {
match job {
WorkJob::PatchHosts => {
let mut hosts = self.db.list_hosts().await?;
for mut host in &mut hosts {
info!("Patching host {}", host.name);
let client = match get_host_client(host, &self.settings.provisioner_config) { let client = match get_host_client(host, &self.settings.provisioner_config) {
Ok(h) => h, Ok(h) => h,
Err(e) => { Err(e) => bail!("Failed to get host client: {} {}", host.name, e),
warn!("Failed to get host client: {} {}", host.name, e);
continue;
}
}; };
let info = client.get_info().await?; let info = client.get_info().await?;
let needs_update = info.cpu != host.cpu || info.memory != host.memory; let needs_update = info.cpu != host.cpu || info.memory != host.memory;
@ -320,6 +312,18 @@ impl Worker {
warn!("Un-mapped host disk {}", disk.name); warn!("Un-mapped host disk {}", disk.name);
} }
} }
Ok(())
}
async fn try_job(&mut self, job: &WorkJob) -> Result<()> {
match job {
WorkJob::PatchHosts => {
let mut hosts = self.db.list_hosts().await?;
for mut host in &mut hosts {
info!("Patching host {}", host.name);
if let Err(e) = self.patch_host(&mut host).await {
error!("Failed to patch host {}: {}", host.name, e);
}
} }
} }
WorkJob::CheckVm { vm_id } => { WorkJob::CheckVm { vm_id } => {