feat: get host for template
feat: apply load factor
This commit is contained in:
3
lnvps_db/migrations/20250305132119_load_factor.sql
Normal file
3
lnvps_db/migrations/20250305132119_load_factor.sql
Normal file
@ -0,0 +1,3 @@
|
||||
-- Add migration script here
|
||||
alter table vm_host
|
||||
add column load_factor float not null default 1.0;
|
@ -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>>;
|
||||
|
||||
|
@ -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]
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user