Enhance configuration management by implementing loading and saving of HostConfig and HostResources from disk, and refactor related structures for improved clarity and functionality.
This commit is contained in:
parent
414eacad2b
commit
a1e5ad641b
@ -1,4 +1,7 @@
|
||||
#[derive(Debug)]
|
||||
use anyhow::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct HostConfig {
|
||||
pub brain_url: String,
|
||||
pub host_ip_address: String,
|
||||
@ -13,6 +16,7 @@ pub struct HostConfig {
|
||||
pub delete_archive: bool,
|
||||
}
|
||||
|
||||
/*
|
||||
impl Default for HostConfig {
|
||||
fn default() -> Self {
|
||||
// TODO: load from config file
|
||||
@ -41,3 +45,13 @@ impl Default for HostConfig {
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
impl HostConfig {
|
||||
pub fn load_from_disk(path: &str) -> Result<Self> {
|
||||
let content = std::fs::read_to_string(path)?;
|
||||
let config: Self = serde_yml::from_str(&content)?;
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
}
|
||||
|
53
src/data.rs
53
src/data.rs
@ -9,25 +9,57 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::container::delete_enclave;
|
||||
use crate::container::deploy_enclave;
|
||||
use crate::global::app_config_dir;
|
||||
use crate::global::APP_NAME_PREFIX;
|
||||
use crate::utils::handle_package;
|
||||
use crate::utils::prepare_port_map;
|
||||
use crate::HostConfig;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
use crate::global::APP_CONFIG_DIR;
|
||||
use crate::global::APP_NAME_PREFIX;
|
||||
use crate::global::USED_RESOURCES;
|
||||
|
||||
#[derive(Debug, Default, Deserialize, Serialize)]
|
||||
pub struct HostResources {
|
||||
pub existing_apps: HashSet<String>,
|
||||
pub reserved_vcpus: u32,
|
||||
pub reserved_memory: u32,
|
||||
pub reserved_memory_mb: u32,
|
||||
pub reserved_disk_mb: u32,
|
||||
pub reserved_host_ports: HashSet<u16>,
|
||||
}
|
||||
|
||||
impl HostResources {
|
||||
// TODO: implement load and save
|
||||
pub fn new() -> Self {
|
||||
// TODO: compute host resources
|
||||
Self::default()
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Serialize, Deserialize)]
|
||||
// TODO: implement load and save
|
||||
fn save_to_disk(&self) -> Result<()> {
|
||||
let mut file = File::create(&*USED_RESOURCES)?;
|
||||
file.write_all(serde_yml::to_string(self)?.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load_from_disk() -> Result<Self> {
|
||||
let content = std::fs::read_to_string(&*USED_RESOURCES)?;
|
||||
let res: Self = serde_yml::from_str(&content)?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub fn reserve_resources(&mut self, app: &App) -> Result<()> {
|
||||
self.reserved_memory_mb += app.app_resource.memory_mb;
|
||||
self.reserved_vcpus += app.app_resource.vcpu;
|
||||
self.reserved_disk_mb += app.app_resource.disk_mb;
|
||||
|
||||
for (_, port) in app.mapped_ports.iter() {
|
||||
self.reserved_host_ports.insert(*port);
|
||||
}
|
||||
self.existing_apps.insert(app.uuid.clone());
|
||||
|
||||
self.save_to_disk()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
|
||||
pub struct App {
|
||||
pub uuid: String,
|
||||
pub name: String,
|
||||
@ -58,7 +90,7 @@ impl App {
|
||||
}
|
||||
if host_config.max_mem_reservation_mb
|
||||
< host_resource
|
||||
.reserved_memory
|
||||
.reserved_memory_mb
|
||||
.saturating_add(new_app_req.resource.memory_mb)
|
||||
{
|
||||
return Err(anyhow!("not enough memory available"));
|
||||
@ -96,14 +128,15 @@ impl App {
|
||||
.existing_apps
|
||||
.insert(app_instance.uuid.clone());
|
||||
|
||||
host_resource.reserve_resources(&app_instance)?;
|
||||
|
||||
Ok(app_instance)
|
||||
}
|
||||
|
||||
fn write_config(&self) -> Result<()> {
|
||||
let app_config_dir = app_config_dir();
|
||||
std::fs::create_dir_all(&app_config_dir)?;
|
||||
std::fs::create_dir_all(&*APP_CONFIG_DIR)?;
|
||||
|
||||
let mut file = File::create(app_config_dir + &self.uuid + ".yaml")?;
|
||||
let mut file = File::create(format!("{}{}.yaml", *APP_CONFIG_DIR, &self.uuid))?;
|
||||
file.write_all(serde_yml::to_string(self)?.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
use std::sync::LazyLock;
|
||||
|
||||
pub const NODE_PUBKEY: &str = "0xd0837609aedd53854651210327db90f5c2626188a00e940bbc9eea2c7e6838b7";
|
||||
pub const ADMIN_PUBKEY: &str = "0x28a3a71197250b0fa4dd0f86288e07ec9cc78ce3338e21e2ebef84dd7780e3eb";
|
||||
|
||||
@ -10,7 +12,14 @@ pub const APP_NAME_PREFIX: &str = "dtpm";
|
||||
const DETEE_DIR_ENV_NAME: &str = "DETEE_DIR";
|
||||
|
||||
// TODO: try lazy static later
|
||||
pub fn app_config_dir() -> String {
|
||||
|
||||
pub static USED_RESOURCES: LazyLock<String> = LazyLock::new(|| {
|
||||
let home = home::home_dir().unwrap().to_string_lossy().into_owned();
|
||||
std::env::var(DETEE_DIR_ENV_NAME).unwrap_or(format!("{home}/.detee/app_daemon/"))
|
||||
}
|
||||
std::env::var(DETEE_DIR_ENV_NAME)
|
||||
.unwrap_or(format!("{home}/.detee/app_daemon/used_resources.yaml"))
|
||||
});
|
||||
|
||||
pub static APP_CONFIG_DIR: LazyLock<String> = LazyLock::new(|| {
|
||||
let home = home::home_dir().unwrap().to_string_lossy().into_owned();
|
||||
std::env::var(DETEE_DIR_ENV_NAME).unwrap_or(format!("{home}/.detee/app_daemon/deployed_apps/"))
|
||||
});
|
||||
|
24
src/main.rs
24
src/main.rs
@ -14,8 +14,8 @@ use detee_shared::pb::brain::DaemonMessageApp;
|
||||
use detee_shared::pb::brain::MappedPort;
|
||||
use detee_shared::pb::brain::NewAppRes;
|
||||
use detee_shared::types::brain::AppDeployConfig;
|
||||
use global::app_config_dir;
|
||||
use log::info;
|
||||
use log::warn;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::mpsc::Receiver;
|
||||
use tokio::sync::mpsc::Sender;
|
||||
@ -26,6 +26,8 @@ use utils::cleanup_enclave_disk_and_package;
|
||||
pub use crate::config::HostConfig;
|
||||
pub use crate::data::HostResources;
|
||||
|
||||
use global::APP_CONFIG_DIR;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct AppHandler {
|
||||
pub receiver: Receiver<BrainMessageApp>,
|
||||
@ -39,11 +41,24 @@ impl AppHandler {
|
||||
// TODO: load from config and resources from file,
|
||||
// if not found use default and save host resources to file
|
||||
|
||||
let host_config = match HostConfig::load_from_disk(&APP_CONFIG_DIR) {
|
||||
Ok(config) => config,
|
||||
Err(e) => panic!("Could not load config: {e:?}"),
|
||||
};
|
||||
let host_resource = match HostResources::load_from_disk() {
|
||||
Ok(res) => res,
|
||||
Err(e) => {
|
||||
warn!("Could not load resources from disk: {e:?}");
|
||||
info!("Creating new resource calculator.");
|
||||
HostResources::new()
|
||||
}
|
||||
};
|
||||
|
||||
Self {
|
||||
receiver,
|
||||
sender,
|
||||
host_config: HostConfig::default(),
|
||||
host_resource: HostResources::default(),
|
||||
host_config,
|
||||
host_resource,
|
||||
}
|
||||
}
|
||||
|
||||
@ -106,8 +121,7 @@ impl AppHandler {
|
||||
}
|
||||
|
||||
async fn handle_del_app_req(&mut self, uuid: String) -> Result<()> {
|
||||
let app_config_dir = app_config_dir();
|
||||
let app_handle_file_name = format!("{app_config_dir}{}.yaml", &uuid);
|
||||
let app_handle_file_name = format!("{}{}.yaml", *APP_CONFIG_DIR, &uuid);
|
||||
let content = std::fs::read_to_string(&app_handle_file_name)?;
|
||||
let app_instance: App = serde_yml::from_str(&content)?;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user