feat: load resources only from template

This commit is contained in:
2025-02-17 15:55:12 +00:00
parent 94ca78940f
commit 0b84a3e6ab
4 changed files with 17 additions and 20 deletions

View File

@ -0,0 +1,5 @@
-- Add migration script here
alter table vm
drop column cpu,
drop column memory,
drop column disk_size;

View File

@ -231,12 +231,6 @@ pub struct Vm {
pub created: DateTime<Utc>,
/// When the VM expires
pub expires: DateTime<Utc>,
/// How many vCPU's this VM has
pub cpu: u16,
/// How much RAM this VM has in bytes
pub memory: u64,
/// How big the disk is on this VM in bytes
pub disk_size: u64,
/// The [VmHostDisk] this VM is on
pub disk_id: u64,
/// Network MAC address

View File

@ -222,7 +222,7 @@ impl LNVpsDb for LNVpsDbMysql {
}
async fn insert_vm(&self, vm: &Vm) -> Result<u64> {
Ok(sqlx::query("insert into vm(host_id,user_id,image_id,template_id,ssh_key_id,created,expires,cpu,memory,disk_size,disk_id,mac_address) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) returning id")
Ok(sqlx::query("insert into vm(host_id,user_id,image_id,template_id,ssh_key_id,created,expires,disk_id,mac_address) values(?, ?, ?, ?, ?, ?, ?, ?, ?) returning id")
.bind(vm.host_id)
.bind(vm.user_id)
.bind(vm.image_id)
@ -230,9 +230,6 @@ impl LNVpsDb for LNVpsDbMysql {
.bind(vm.ssh_key_id)
.bind(vm.created)
.bind(vm.expires)
.bind(vm.cpu)
.bind(vm.memory)
.bind(vm.disk_size)
.bind(vm.disk_id)
.bind(&vm.mac_address)
.fetch_one(&self.db)
@ -251,14 +248,11 @@ impl LNVpsDb for LNVpsDbMysql {
}
async fn update_vm(&self, vm: &Vm) -> Result<()> {
sqlx::query("update vm set image_id=?,template_id=?,ssh_key_id=?,expires=?,cpu=?,memory=?,disk_size=?,disk_id=? where id=?")
sqlx::query("update vm set image_id=?,template_id=?,ssh_key_id=?,expires=?,disk_id=? where id=?")
.bind(vm.image_id)
.bind(vm.template_id)
.bind(vm.ssh_key_id)
.bind(vm.expires)
.bind(vm.cpu)
.bind(vm.memory)
.bind(vm.disk_size)
.bind(vm.disk_id)
.bind(vm.id)
.execute(&self.db)

View File

@ -122,6 +122,12 @@ impl LNVpsProvisioner {
bail!("No host drive found!")
};
let template = if let Some(t) = &vm.template {
t
} else {
&self.db.get_vm_template(vm.template_id).await?
};
Ok(VmConfig {
cpu: Some(self.config.cpu.clone()),
kvm: Some(self.config.kvm),
@ -132,8 +138,8 @@ impl LNVpsProvisioner {
on_boot: Some(true),
bios: Some(VmBios::OVMF),
boot: Some("order=scsi0".to_string()),
cores: Some(vm.cpu as i32),
memory: Some((vm.memory / 1024 / 1024).to_string()),
cores: Some(template.cpu as i32),
memory: Some((template.memory / 1024 / 1024).to_string()),
scsi_hw: Some("virtio-scsi-pci".to_string()),
serial_0: Some("socket".to_string()),
scsi_1: Some(format!("{}:cloudinit", &drive.name)),
@ -213,9 +219,6 @@ impl Provisioner for LNVpsProvisioner {
ssh_key_id: ssh_key.id,
created: Utc::now(),
expires: Utc::now(),
cpu: template.cpu,
memory: template.memory,
disk_size: template.disk_size,
disk_id: pick_disk.id,
mac_address: format!(
"bc:24:11:{}:{}:{}",
@ -370,6 +373,7 @@ impl Provisioner for LNVpsProvisioner {
bail!("Cant spawn VM's in read-only mode");
}
let vm = self.db.get_vm(vm_id).await?;
let template = self.db.get_vm_template(vm.template_id).await?;
let host = self.db.get_host(vm.host_id).await?;
let client = get_host_client(&host)?;
let vm_id = 100 + vm.id as i32;
@ -425,7 +429,7 @@ impl Provisioner for LNVpsProvisioner {
node: host.name.clone(),
vm_id,
disk: "scsi0".to_string(),
size: vm.disk_size.to_string(),
size: template.disk_size.to_string(),
})
.await?;
client.wait_for_task(&j_resize).await?;