Refactor to run as root

fix HostResources in new installation
app configuration paths to use DEPLOYED_APPS_CONFIG_DIR and update related file handling
This commit is contained in:
Noor 2025-03-14 16:31:58 +00:00
parent d7ae481085
commit 704d49b54a
Signed by: noormohammedb
GPG Key ID: E424C39E19EFD7DF
4 changed files with 40 additions and 22 deletions

1
Cargo.lock generated

@ -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",

@ -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<Self> {
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<Self> {
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(())
}

@ -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<IPInfo> =
LazyLock::new(|| tokio::task::block_in_place(|| get_ip_info().unwrap()));
pub static DAEMON_CONFIG_BASE_DIR: LazyLock<String> =
LazyLock::new(|| "/etc/detee/app_daemon".to_string());
pub static USED_RESOURCES_PATH: 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/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<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/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<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/"))
pub static DEPLOYED_APPS_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/"))
let base_dir = DAEMON_CONFIG_BASE_DIR.to_string();
format!("{base_dir}/deployed_apps")
});
pub static SECRET_KEY_PATH: 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/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<String> = LazyLock::new(get_public_key);

@ -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)?;