diff --git a/src/config.rs b/src/config.rs index 14d6d74..4999fc2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -5,8 +5,9 @@ use serde::{Deserialize, Serialize}; pub struct HostConfig { pub brain_url: String, pub host_ip_address: String, - pub operator_pubkey: String, + pub operator_wallet: String, pub max_cores_per_app: u32, + pub max_memory_mb_per_app: u32, pub max_vcpu_reservation: u32, pub max_mem_reservation_mb: u32, pub max_disk_reservation_mb: u32, @@ -24,37 +25,6 @@ fn default_reserved_no_of_port() -> u32 { 16 } -/* -impl Default for HostConfig { - fn default() -> Self { - // TODO: load from config file - let brain_url = - std::env::var("BRAIN_URL").unwrap_or_else(|_| "http://127.0.0.1:31337".to_string()); - let owner_wallet = "0x".to_string(); - let host_ip_address = "127.0.0.1".to_string(); - - let max_cores_per_app = 4; - let max_vcpu_reservation = 8; - let max_mem_reservation_mb = 8192; - let max_ports_per_app = 9; - let price = 0; - - Self { - brain_url, - host_ip_address, - owner_wallet, - max_cores_per_app, - max_ports_per_app, - max_vcpu_reservation, - max_mem_reservation_mb, - price, - - delete_archive: true, - } - } -} -*/ - impl HostConfig { pub fn load_from_disk(path: &str) -> Result { let content = std::fs::read_to_string(path)?; diff --git a/src/data.rs b/src/data.rs index 4d6a68a..51d9863 100644 --- a/src/data.rs +++ b/src/data.rs @@ -92,11 +92,28 @@ impl App { host_config: &HostConfig, host_resource: &mut HostResources, ) -> Result { + if new_app_req.price_per_unit < host_config.price { + return Err(anyhow!("price is too low")); + } + + if host_resource.existing_apps.contains(&new_app_req.uuid) { + let content = + std::fs::read_to_string(format!("{}{}.yaml", *APP_CONFIG_DIR, &new_app_req.uuid))?; + let app: App = serde_yml::from_str(&content)?; + return Err(anyhow!("app already exists\n{:?}", app)); + } + let app_uuid = new_app_req.uuid.clone(); if host_config.max_cores_per_app < new_app_req.resource.vcpu { return Err(anyhow!("too many vcpus for app")); } + + if host_config.max_memory_mb_per_app < new_app_req.resource.memory_mb { + return Err(anyhow!("too much memory for app")); + } + + // TODO: revert state if any error if host_config.max_vcpu_reservation < host_resource .reserved_vcpus diff --git a/src/grpc.rs b/src/grpc.rs index 8740be0..49a60bf 100644 --- a/src/grpc.rs +++ b/src/grpc.rs @@ -30,11 +30,12 @@ pub async fn register_node(config: &crate::HostConfig) -> Result