detee-cli/src/sgx/utils.rs
Noor eb8cac48f2
Wip on app resource ratio and sloat system
complete refactor of app deployment
disabled app deployment from yaml
2025-06-30 16:51:57 +05:30

95 lines
3.1 KiB
Rust

// SPDX-License-Identifier: Apache-2.0
use crate::constants::HRATLS_APP_PORT;
use crate::sgx::get_one_contract;
use crate::sgx::Error;
use detee_shared::sgx::types::dtpm::{DtpmConfig, EnvironmentEntry};
pub async fn hratls_url_and_mr_enclave_from_app_id(
app_id: &str,
) -> Result<(String, Option<[u8; 32]>), crate::sgx::grpc_dtpm::Error> {
let app_contract = get_one_contract(app_id).await;
if app_contract.is_err() {
eprintln!("Could not find App contract with ID: {}", app_id);
std::process::exit(1);
}
let app_contract = app_contract.unwrap();
let mr_enclave = app_contract
.public_package_mr_enclave
.clone()
.filter(|vec| vec.len() == 32)
.and_then(|vec| vec.try_into().ok());
let public_ip = app_contract.public_ipv4.clone();
let dtpm_port = app_contract
.mapped_ports
.iter()
.find(|port| port.guest_port == HRATLS_APP_PORT)
.ok_or(crate::sgx::grpc_dtpm::Error::Dtpm("Could not find DTMP port".to_string()))?
.host_port;
Ok((format!("https://{public_ip}:{dtpm_port}"), mr_enclave))
}
pub async fn fetch_config(url: &str) -> Result<DtpmConfig, Error> {
let launch_config_str = reqwest::get(url).await?.text().await?;
let launch_config = serde_yaml::from_str::<DtpmConfig>(&launch_config_str)?;
Ok(launch_config)
}
pub fn calculate_nanolp_for_app(
vcpus: u32,
memory_mib: u32,
disk_size_mib: u32,
hours: u64,
node_price: u64,
) -> u64 {
// this calculation needs to match the calculation of the network
let total_units = (vcpus as f64 * 5f64)
+ (memory_mib as f64 / 200f64)
+ (disk_size_mib as f64 / 1024f64 / 10f64);
let locked_nano = (hours as f64 * 60f64 * total_units * node_price as f64) as u64;
eprintln!(
"Node price: {}/unit/minute. Total Units for hardware requested: {:.4}. Locking {} LP (offering the App for {} hours).",
node_price as f64 / 1_000_000_000.0,
total_units,
locked_nano as f64 / 1_000_000_000.0,
hours
);
locked_nano
}
pub fn override_envs_and_args_launch_config(
mut launch_config: DtpmConfig,
envs: Vec<String>,
args: Vec<String>,
) -> DtpmConfig {
if envs.is_empty() && args.is_empty() {
return launch_config;
}
for env in envs {
let mut env = env.split("=");
let key = env.next().expect("environment variable must be in the format 'key=value'");
let value =
env.next().expect("environment variable pair must be in the format 'key=value'");
if launch_config.environments.iter().any(|env| env.name == key) {
let existing_env =
launch_config.environments.iter_mut().find(|env| env.name == key).unwrap();
existing_env.name = key.to_string();
existing_env.value = value.to_string();
} else {
launch_config
.environments
.push(EnvironmentEntry { name: key.to_string(), value: value.to_string() });
}
}
for arg in args {
launch_config.child_processes.first_mut().unwrap().arguments.push(arg);
}
launch_config
}