feat: get host for template

feat: apply load factor
This commit is contained in:
2025-03-05 13:25:27 +00:00
parent 2bd6b5f09f
commit a212ed661a
9 changed files with 273 additions and 84 deletions

View File

@ -0,0 +1,3 @@
-- Add migration script here
alter table vm_host
add column load_factor float not null default 1.0;

View File

@ -80,6 +80,9 @@ pub trait LNVpsDb: Sync + Send {
/// List VM templates
async fn list_vm_templates(&self) -> Result<Vec<VmTemplate>>;
/// Insert a new VM template
async fn insert_vm_template(&self, template: &VmTemplate) -> Result<u64>;
/// List all VM's
async fn list_vms(&self) -> Result<Vec<Vm>>;

View File

@ -31,10 +31,11 @@ pub struct UserSshKey {
pub key_data: String,
}
#[derive(Clone, Debug, sqlx::Type)]
#[derive(Clone, Debug, sqlx::Type, Default, PartialEq, Eq)]
#[repr(u16)]
/// The type of VM host
pub enum VmHostKind {
#[default]
Proxmox = 0,
LibVirt = 1,
}
@ -55,7 +56,7 @@ pub struct VmHostRegion {
pub enabled: bool,
}
#[derive(FromRow, Clone, Debug)]
#[derive(FromRow, Clone, Debug, Default)]
/// A VM host
pub struct VmHost {
/// Unique id of this host
@ -76,9 +77,11 @@ pub struct VmHost {
pub enabled: bool,
/// API token used to control this host via [ip]
pub api_token: String,
/// Load factor for provisioning
pub load_factor: f32,
}
#[derive(FromRow, Clone, Debug)]
#[derive(FromRow, Clone, Debug, Default)]
pub struct VmHostDisk {
pub id: u64,
pub host_id: u64,
@ -89,7 +92,7 @@ pub struct VmHostDisk {
pub enabled: bool,
}
#[derive(Clone, Debug, sqlx::Type, Default)]
#[derive(Clone, Debug, sqlx::Type, Default, PartialEq, Eq)]
#[repr(u16)]
pub enum DiskType {
#[default]
@ -97,7 +100,7 @@ pub enum DiskType {
SSD = 1,
}
#[derive(Clone, Debug, sqlx::Type, Default)]
#[derive(Clone, Debug, sqlx::Type, Default, PartialEq, Eq)]
#[repr(u16)]
pub enum DiskInterface {
#[default]
@ -106,7 +109,7 @@ pub enum DiskInterface {
PCIe = 2,
}
#[derive(Clone, Debug, sqlx::Type, Default)]
#[derive(Clone, Debug, sqlx::Type, Default, PartialEq, Eq)]
#[repr(u16)]
pub enum OsDistribution {
#[default]

View File

@ -114,7 +114,7 @@ impl LNVpsDb for LNVpsDbMysql {
}
async fn list_hosts(&self) -> Result<Vec<VmHost>> {
sqlx::query_as("select * from vm_host")
sqlx::query_as("select * from vm_host where enabled = 1")
.fetch_all(&self.db)
.await
.map_err(Error::new)
@ -216,6 +216,25 @@ impl LNVpsDb for LNVpsDbMysql {
.map_err(Error::new)
}
async fn insert_vm_template(&self, template: &VmTemplate) -> Result<u64> {
Ok(sqlx::query("insert into vm_template(name,enabled,created,expires,cpu,memory,disk_size,disk_type,disk_interface,cost_plan_id,region_id) values(?,?,?,?,?,?,?,?,?,?,?) returning id")
.bind(&template.name)
.bind(&template.enabled)
.bind(&template.created)
.bind(&template.expires)
.bind(template.cpu)
.bind(template.memory)
.bind(template.disk_size)
.bind(&template.disk_type)
.bind(&template.disk_interface)
.bind(template.cost_plan_id)
.bind(template.region_id)
.fetch_one(&self.db)
.await
.map_err(Error::new)?
.try_get(0)?)
}
async fn list_vms(&self) -> Result<Vec<Vm>> {
sqlx::query_as("select * from vm where deleted = 0")
.fetch_all(&self.db)