App daemon message

activate new app in db
update node resource in db
This commit is contained in:
Noor 2025-05-12 18:09:30 +05:30
parent f831e31d8f
commit 6f40a9c770
Signed by: noormohammedb
GPG Key ID: D83EFB8B3B967146
3 changed files with 116 additions and 2 deletions

@ -1,4 +1,4 @@
use crate::constants::{ACCOUNT, ACTIVE_APP, APP_NODE}; use crate::constants::{ACCOUNT, ACTIVE_APP, APP_NODE, NEW_APP_REQ};
use crate::db::general::Report; use crate::db::general::Report;
use super::Error; use super::Error;
@ -72,6 +72,28 @@ pub struct NewAppReq {
pub created_at: Datetime, pub created_at: Datetime,
} }
impl NewAppReq {
pub async fn get(db: &Surreal<Client>, id: &str) -> Result<Option<Self>, Error> {
let new_app_req: Option<Self> = db.select((NEW_APP_REQ, id)).await?;
Ok(new_app_req)
}
pub async fn submit_error(
db: &Surreal<Client>,
id: &str,
error: String,
) -> Result<Option<Self>, Error> {
#[derive(Serialize)]
struct NewAppError {
error: String,
}
let record: Option<Self> =
db.update((NEW_APP_REQ, id)).merge(NewAppError { error }).await?;
Ok(record)
}
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct AppNodeWithReports { pub struct AppNodeWithReports {
pub id: RecordId, pub id: RecordId,
@ -112,6 +134,38 @@ pub struct ActiveApp {
hratls_pubkey: String, hratls_pubkey: String,
} }
impl ActiveApp {
pub async fn activate(db: &Surreal<Client>, id: &str) -> Result<(), Error> {
let new_app_req = match NewAppReq::get(db, id).await? {
Some(r) => r,
None => return Ok(()),
};
let active_app = Self {
id: RecordId::from((ACTIVE_APP, id)),
admin: new_app_req.admin,
app_node: new_app_req.app_node,
app_name: new_app_req.app_name,
mapped_ports: vec![],
host_ipv4: String::new(),
vcpus: new_app_req.vcpu as u64,
memory_mb: new_app_req.memory_mb as u64,
disk_size_gb: new_app_req.disk_mb as u64,
created_at: new_app_req.created_at.clone(),
price_per_unit: new_app_req.price_per_unit,
locked_nano: new_app_req.locked_nano,
collected_at: new_app_req.created_at,
mr_enclave: new_app_req.mr_enclave.clone(),
package_url: new_app_req.package_url.clone(),
hratls_pubkey: new_app_req.hratls_pubkey.clone(),
};
let _: Vec<ActiveApp> = db.insert(()).relation(active_app).await?;
Ok(())
}
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct ActiveAppWithNode { pub struct ActiveAppWithNode {
pub id: RecordId, pub id: RecordId,
@ -153,6 +207,26 @@ impl ActiveAppWithNode {
} }
} }
#[derive(Debug, Serialize)]
pub struct AppNodeResources {
pub avail_no_of_port: u32,
pub avail_vcpus: u32,
pub avail_memory_mb: u32,
pub avail_storage_mb: u32,
pub max_ports_per_app: u32,
}
impl AppNodeResources {
pub async fn merge(
self,
db: &Surreal<Client>,
node_pubkey: &str,
) -> Result<Option<AppNode>, Error> {
let app_node: Option<AppNode> = db.update((APP_NODE, node_pubkey)).merge(self).await?;
Ok(app_node)
}
}
impl From<&old_brain::BrainData> for Vec<AppNode> { impl From<&old_brain::BrainData> for Vec<AppNode> {
fn from(old_data: &old_brain::BrainData) -> Self { fn from(old_data: &old_brain::BrainData) -> Self {
let mut nodes = Vec::new(); let mut nodes = Vec::new();

@ -136,7 +136,35 @@ impl BrainAppDaemon for AppDaemonServer {
return Err(Status::unauthenticated("Could not authenticate the app daemon")); return Err(Status::unauthenticated("Could not authenticate the app daemon"));
} }
todo!() while let Some(daemon_message) = req_stream.next().await {
match daemon_message {
Ok(msg) => match msg.msg {
Some(daemon_message_app::Msg::NewAppRes(new_app_resp)) => {
if !new_app_resp.error.is_empty() {
db::NewAppReq::submit_error(
&self.db,
&new_app_resp.uuid,
new_app_resp.error,
)
.await?;
} else {
db::ActiveApp::activate(&self.db, &new_app_resp.uuid).await?;
}
}
Some(daemon_message_app::Msg::AppNodeResources(app_node_resources)) => {
let node_resource: db::AppNodeResources = app_node_resources.into();
node_resource.merge(&self.db, &pubkey).await?;
}
_ => {}
},
Err(e) => {
log::warn!("App Daemon Disconnected: {e:?}")
}
}
}
Ok(Response::new(Empty {}))
} }
} }

@ -329,3 +329,15 @@ impl From<db::AppDaemonMsg> for BrainMessageApp {
} }
} }
} }
impl From<AppNodeResources> for db::AppNodeResources {
fn from(value: AppNodeResources) -> Self {
Self {
avail_no_of_port: value.avail_no_of_port,
avail_vcpus: value.avail_vcpus,
avail_memory_mb: value.avail_memory_mb,
avail_storage_mb: value.avail_storage_mb,
max_ports_per_app: value.max_ports_per_app,
}
}
}