refactor: move spawn_vm out of worker into provisioner

feat: spawn vm params to settings
This commit is contained in:
2024-11-27 14:38:23 +00:00
parent 088f22cea4
commit d28ca286fb
15 changed files with 491 additions and 202 deletions

1
lnvps_db/Cargo.lock generated
View File

@ -843,6 +843,7 @@ dependencies = [
"serde",
"serde_with",
"sqlx",
"url",
]
[[package]]

View File

@ -13,4 +13,5 @@ sqlx = { version = "0.8.2", features = ["chrono", "migrate", "runtime-tokio"] }
serde = { version = "1.0.213", features = ["derive"] }
serde_with = { version = "3.11.0", features = ["macros", "hex"] }
chrono = { version = "0.4.38", features = ["serde"] }
async-trait = "0.1.83"
async-trait = "0.1.83"
url = "2.5.4"

View File

@ -1,19 +1,20 @@
use crate::{LNVpsDb, Vm, VmTemplate};
use anyhow::Result;
use async_trait::async_trait;
use std::ops::Deref;
#[async_trait]
pub trait Hydrate {
pub trait Hydrate<D> {
/// Load parent resources
async fn hydrate_up(&mut self, db: &Box<dyn LNVpsDb>) -> Result<()>;
async fn hydrate_up(&mut self, db: &D) -> Result<()>;
/// Load child resources
async fn hydrate_down(&mut self, db: &Box<dyn LNVpsDb>) -> Result<()>;
async fn hydrate_down(&mut self, db: &D) -> Result<()>;
}
#[async_trait]
impl Hydrate for Vm {
async fn hydrate_up(&mut self, db: &Box<dyn LNVpsDb>) -> Result<()> {
impl<D: Deref<Target = dyn LNVpsDb> + Sync> Hydrate<D> for Vm {
async fn hydrate_up(&mut self, db: &D) -> Result<()> {
let image = db.get_os_image(self.image_id).await?;
let template = db.get_vm_template(self.template_id).await?;
let ssh_key = db.get_user_ssh_key(self.ssh_key_id).await?;
@ -24,7 +25,7 @@ impl Hydrate for Vm {
Ok(())
}
async fn hydrate_down(&mut self, db: &Box<dyn LNVpsDb>) -> Result<()> {
async fn hydrate_down(&mut self, db: &D) -> Result<()> {
//let payments = db.list_vm_payment(self.id).await?;
let ips = db.list_vm_ip_assignments(self.id).await?;
@ -35,8 +36,8 @@ impl Hydrate for Vm {
}
#[async_trait]
impl Hydrate for VmTemplate {
async fn hydrate_up(&mut self, db: &Box<dyn LNVpsDb>) -> Result<()> {
impl<D: Deref<Target = dyn LNVpsDb> + Sync> Hydrate<D> for VmTemplate {
async fn hydrate_up(&mut self, db: &D) -> Result<()> {
let cost_plan = db.get_cost_plan(self.cost_plan_id).await?;
let region = db.get_host_region(self.region_id).await?;
self.cost_plan = Some(cost_plan);
@ -44,7 +45,7 @@ impl Hydrate for VmTemplate {
Ok(())
}
async fn hydrate_down(&mut self, db: &Box<dyn LNVpsDb>) -> Result<()> {
async fn hydrate_down(&mut self, db: &D) -> Result<()> {
todo!()
}
}

View File

@ -1,7 +1,10 @@
use anyhow::{anyhow, Result};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use sqlx::FromRow;
use std::path::PathBuf;
use url::Url;
#[serde_as]
#[derive(Serialize, Deserialize, FromRow, Clone, Debug)]
@ -122,11 +125,26 @@ pub struct VmOsImage {
pub version: String,
pub enabled: bool,
pub release_date: DateTime<Utc>,
#[serde(skip_serializing)]
/// URL location of cloud image
pub url: String,
}
impl VmOsImage {
pub fn filename(&self) -> Result<String> {
let u: Url = self.url.parse()?;
let mut name: PathBuf = u
.path_segments()
.ok_or(anyhow!("Invalid URL"))?
.last()
.ok_or(anyhow!("Invalid URL"))?
.parse()?;
name.set_extension("img");
Ok(name.to_string_lossy().to_string())
}
}
#[derive(Serialize, Deserialize, FromRow, Clone, Debug)]
pub struct IpRange {
pub id: u64,