From 704d49b54afc8e1ac453cf9e3d3aed6b8c35856d Mon Sep 17 00:00:00 2001 From: Noor Date: Fri, 14 Mar 2025 16:31:58 +0000 Subject: [PATCH] Refactor to run as root fix HostResources in new installation app configuration paths to use DEPLOYED_APPS_CONFIG_DIR and update related file handling --- Cargo.lock | 1 + src/data.rs | 20 +++++++++++++------- src/global.rs | 37 ++++++++++++++++++++++++------------- src/main.rs | 4 ++-- 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7a2a1a6..ebe28b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -410,6 +410,7 @@ dependencies = [ [[package]] name = "detee-shared" version = "0.1.0" +source = "git+ssh://git@gitea.detee.cloud/noormohammedb/detee-shared?branch=stable_01#099f0a0488bce8e59c9c9e9a5e9b1f24998f1633" dependencies = [ "base64", "prost", diff --git a/src/data.rs b/src/data.rs index 51d9863..773e9ff 100644 --- a/src/data.rs +++ b/src/data.rs @@ -13,8 +13,8 @@ use crate::utils::handle_package; use crate::utils::prepare_port_map; use crate::HostConfig; -use crate::global::APP_CONFIG_DIR; use crate::global::APP_NAME_PREFIX; +use crate::global::DEPLOYED_APPS_CONFIG_DIR; use crate::global::USED_RESOURCES_PATH; #[derive(Debug, Clone, Default, Deserialize, Serialize)] @@ -40,7 +40,11 @@ impl HostResources { } pub fn load_from_disk() -> Result { - let content = std::fs::read_to_string(&*USED_RESOURCES_PATH)?; + let content = std::fs::read_to_string(&*USED_RESOURCES_PATH).unwrap_or_else(|_| { + let host_resource = Self::new(); + host_resource.save_to_disk().unwrap(); + serde_yml::to_string(&host_resource).unwrap() + }); let res: Self = serde_yml::from_str(&content)?; Ok(res) } @@ -92,13 +96,15 @@ impl App { host_config: &HostConfig, host_resource: &mut HostResources, ) -> Result { - if new_app_req.price_per_unit < host_config.price { + if new_app_req.node_unit_price < 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 content = std::fs::read_to_string(format!( + "{}/{}.yaml", + *DEPLOYED_APPS_CONFIG_DIR, &new_app_req.uuid + ))?; let app: App = serde_yml::from_str(&content)?; return Err(anyhow!("app already exists\n{:?}", app)); } @@ -172,9 +178,9 @@ impl App { } fn write_config(&self) -> Result<()> { - std::fs::create_dir_all(&*APP_CONFIG_DIR)?; + std::fs::create_dir_all(&*DEPLOYED_APPS_CONFIG_DIR)?; - let mut file = File::create(format!("{}{}.yaml", *APP_CONFIG_DIR, &self.uuid))?; + let mut file = File::create(format!("{}/{}.yaml", *DEPLOYED_APPS_CONFIG_DIR, &self.uuid))?; file.write_all(serde_yml::to_string(self)?.as_bytes())?; Ok(()) } diff --git a/src/global.rs b/src/global.rs index 4c48363..1ff12e5 100644 --- a/src/global.rs +++ b/src/global.rs @@ -5,37 +5,48 @@ use std::fs::File; use std::io::Write; use std::sync::LazyLock; -pub const PACKAGE_ARCHIVE_POSTFIX: &str = "-enclave_packager.tar.gz"; +pub const PACKAGE_ARCHIVE_POSTFIX: &str = "-enclave_package.tar.gz"; pub const PACKAGE_ARCHIVE_DIR_PATH: &str = "./enclave_archives"; pub const PACKAGE_DIR_PATH: &str = "./enclaves"; pub const APP_NAME_PREFIX: &str = "dtpm"; -const DETEE_DIR_ENV_NAME: &str = "DETEE_DIR"; +// const DETEE_DIR_ENV_NAME: &str = "DETEE_DIR"; pub static IP_INFO: LazyLock = LazyLock::new(|| tokio::task::block_in_place(|| get_ip_info().unwrap())); +pub static DAEMON_CONFIG_BASE_DIR: LazyLock = + LazyLock::new(|| "/etc/detee/app_daemon".to_string()); + pub static USED_RESOURCES_PATH: LazyLock = 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/used_resources.yaml")) + // 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/used_resources.yaml")) + let base_dir = DAEMON_CONFIG_BASE_DIR.to_string(); + format!("{base_dir}/used_resources.yaml") }); pub static DAEMON_CONFIG_PATH: LazyLock = 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/config.yaml")) + // 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/config.yaml")) + let base_dir = DAEMON_CONFIG_BASE_DIR.to_string(); + format!("{base_dir}/config.yaml") }); -pub static APP_CONFIG_DIR: LazyLock = 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/")) +pub static DEPLOYED_APPS_CONFIG_DIR: LazyLock = 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/")) + let base_dir = DAEMON_CONFIG_BASE_DIR.to_string(); + format!("{base_dir}/deployed_apps") }); pub static SECRET_KEY_PATH: LazyLock = 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/node_secret_key.pem")) + // 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/node_secret_key.pem")) + let base_dir = DAEMON_CONFIG_BASE_DIR.to_string(); + format!("{base_dir}/node_secret_key.pem") }); pub static PUBLIC_KEY: LazyLock = LazyLock::new(get_public_key); diff --git a/src/main.rs b/src/main.rs index 6fe18b0..f0377cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,8 +29,8 @@ use utils::cleanup_enclave_disk_and_package; pub use crate::config::HostConfig; pub use crate::data::HostResources; -use global::APP_CONFIG_DIR; use global::DAEMON_CONFIG_PATH; +use global::DEPLOYED_APPS_CONFIG_DIR; #[derive(Debug)] pub struct AppHandler { @@ -140,7 +140,7 @@ impl AppHandler { } async fn handle_del_app_req(&mut self, uuid: String) -> Result<()> { - let app_handle_file_name = format!("{}{}.yaml", *APP_CONFIG_DIR, &uuid); + let app_handle_file_name = format!("{}/{}.yaml", *DEPLOYED_APPS_CONFIG_DIR, &uuid); let content = std::fs::read_to_string(&app_handle_file_name)?; let app_instance: App = serde_yml::from_str(&content)?;