diff --git a/lnvps_db/src/model.rs b/lnvps_db/src/model.rs index 959799f..5f5d4ae 100644 --- a/lnvps_db/src/model.rs +++ b/lnvps_db/src/model.rs @@ -151,6 +151,24 @@ pub enum OsDistribution { RedHatEnterprise = 7, } +impl FromStr for OsDistribution { + type Err = anyhow::Error; + + fn from_str(s: &str) -> std::result::Result { + match s.to_lowercase().as_str() { + "ubuntu" => Ok(OsDistribution::Ubuntu), + "debian" => Ok(OsDistribution::Debian), + "centos" => Ok(OsDistribution::CentOS), + "fedora" => Ok(OsDistribution::Fedora), + "freebsd" => Ok(OsDistribution::FreeBSD), + "opensuse" => Ok(OsDistribution::OpenSUSE), + "archlinux" => Ok(OsDistribution::ArchLinux), + "redhatenterprise" => Ok(OsDistribution::RedHatEnterprise), + _ => Err(anyhow!("unknown distribution {}", s)), + } + } +} + /// OS Images are templates which are used as a basis for /// provisioning new vms #[derive(FromRow, Clone, Debug)] diff --git a/src/dvm/lnvps.rs b/src/dvm/lnvps.rs index ed80796..46d6f71 100644 --- a/src/dvm/lnvps.rs +++ b/src/dvm/lnvps.rs @@ -1,7 +1,9 @@ use crate::dvm::{build_status_for_job, DVMHandler, DVMJobRequest}; use crate::provisioner::LNVpsProvisioner; use anyhow::Context; -use lnvps_db::{DiskInterface, DiskType, LNVpsDb, PaymentMethod, UserSshKey, VmCustomTemplate}; +use lnvps_db::{ + DiskInterface, DiskType, LNVpsDb, OsDistribution, PaymentMethod, UserSshKey, VmCustomTemplate, +}; use nostr::prelude::DataVendingMachineStatus; use nostr::Tag; use nostr_sdk::Client; @@ -54,6 +56,14 @@ impl DVMHandler for LnvpsDvm { .get("ssh_key") .context("missing ssh_key parameter")?; let ssh_key_name = request.params.get("ssh_key_name"); + let os_image = request + .params + .get("os_image") + .context("missing os_image parameter")?; + let os_version = request + .params + .get("os_version") + .context("missing os_version parameter")?; let region = request.params.get("region"); let db = provisioner.get_db(); @@ -112,8 +122,16 @@ impl DVMHandler for LnvpsDvm { db.insert_user_ssh_key(&new_key).await? }; + let image = OsDistribution::from_str(os_image)?; + let image = db + .list_os_image() + .await? + .into_iter() + .find(|i| i.distribution == image && i.version == *os_version) + .context("no os image found")?; + let vm = provisioner - .provision_custom(uid, template, 0, ssh_key_id, None) + .provision_custom(uid, template, image.id, ssh_key_id, None) .await?; let invoice = provisioner.renew(vm.id, PaymentMethod::Lightning).await?;