container creation from brain and send response

This commit is contained in:
Noor 2025-01-30 20:25:46 +05:30
parent 7f5fb2efad
commit 17400339fc
Signed by: noormohammedb
GPG Key ID: D83EFB8B3B967146
3 changed files with 62 additions and 14 deletions

2
Cargo.lock generated

@ -318,7 +318,7 @@ dependencies = [
[[package]] [[package]]
name = "detee-shared" name = "detee-shared"
version = "0.1.0" version = "0.1.0"
source = "git+ssh://git@gitea.detee.cloud/noormohammedb/detee-shared#a2899ba5a25794aec60a093695675ff24f967484" source = "git+ssh://git@gitea.detee.cloud/noormohammedb/detee-shared#6e1b1853838905c44d535d984d1221dd5d0dc2bc"
dependencies = [ dependencies = [
"base64", "base64",
"prost", "prost",

@ -22,15 +22,11 @@ pub struct Container {
} }
impl DaemonState { impl DaemonState {
pub fn new() -> Self {
Self::default()
}
pub async fn create_new_container( pub async fn create_new_container(
&mut self, &mut self,
req_data: ContainerConfig, req_data: ContainerConfig,
unarchive_dir: String, unarchive_dir: String,
) -> Result<Vec<(u16, u16)>, Box<dyn std::error::Error>> { ) -> Result<Vec<(u16, u16)>> {
let publishing_ports = req_data.resource.clone().unwrap().port; let publishing_ports = req_data.resource.clone().unwrap().port;
let uuid = req_data.uuid; let uuid = req_data.uuid;
let container_name = format!("dtpm-{uuid}"); let container_name = format!("dtpm-{uuid}");

@ -12,12 +12,16 @@ use detee_shared::pb::daemon::BrainMessage;
use detee_shared::pb::daemon::DaemonMessage; use detee_shared::pb::daemon::DaemonMessage;
use detee_shared::pb::daemon::NewContainerRes; use detee_shared::pb::daemon::NewContainerRes;
use detee_shared::pb::shared::ContainerContracts; use detee_shared::pb::shared::ContainerContracts;
use detee_shared::types::shared::Container;
use detee_shared::pb::shared::MappedPort;
use detee_shared::types::shared::Container as ContainerConfig;
use tokio::sync::mpsc::Receiver; use tokio::sync::mpsc::Receiver;
use tokio::sync::mpsc::Sender; use tokio::sync::mpsc::Sender;
use tokio::time::sleep; use tokio::time::sleep;
use utils::handle_package;
#[derive(Debug)]
pub struct Config { pub struct Config {
pub brain_url: String, pub brain_url: String,
} }
@ -30,11 +34,12 @@ impl Default for Config {
} }
} }
#[derive(Debug)]
pub struct ContainerHandler { pub struct ContainerHandler {
receiver: Receiver<BrainMessage>, pub receiver: Receiver<BrainMessage>,
sender: Sender<DaemonMessage>, pub sender: Sender<DaemonMessage>,
config: Config, pub config: Config,
// res: state::Resources, pub data: DaemonState,
} }
impl ContainerHandler { impl ContainerHandler {
@ -43,6 +48,7 @@ impl ContainerHandler {
receiver, receiver,
sender, sender,
config: Config::default(), config: Config::default(),
data: DaemonState::default(),
} }
} }
@ -72,12 +78,58 @@ impl ContainerHandler {
} }
} }
async fn handle_new_container_req(&mut self, new_container_req: Container) { async fn handle_new_container_req(&mut self, new_container_req: ContainerConfig) {
dbg!(&new_container_req); dbg!(&new_container_req);
let container_uuid = new_container_req.uuid.clone();
let unarchive_dir = match handle_package(new_container_req.package_url.clone()).await {
Ok(unarchive_dir) => unarchive_dir,
Err(e) => {
let res = DaemonMessage {
msg: Some(daemon_message::Msg::NewContainerResp(NewContainerRes {
uuid: new_container_req.uuid,
status: "failed".to_string(),
error: e.to_string(),
..Default::default()
})),
};
println!("sending response {:?}", res);
let _ = self.sender.send(res).await;
return;
}
};
let mapped_ports = match self
.data
.create_new_container(new_container_req, unarchive_dir)
.await
{
Ok(mapped_ports) => mapped_ports.into_iter().map(MappedPort::from).collect(),
Err(e) => {
let res = DaemonMessage {
msg: Some(daemon_message::Msg::NewContainerResp(NewContainerRes {
uuid: container_uuid,
status: "failed".to_string(),
error: e.to_string(),
..Default::default()
})),
};
println!("sending response {:?}", res);
let _ = self.sender.send(res).await;
return;
}
};
let res = DaemonMessage { let res = DaemonMessage {
msg: Some(daemon_message::Msg::NewContainerResp(NewContainerRes { msg: Some(daemon_message::Msg::NewContainerResp(NewContainerRes {
uuid: new_container_req.uuid, uuid: container_uuid,
..Default::default() status: "Success".to_string(),
error: "".to_string(),
ip_address: "".to_string(),
mapped_ports,
})), })),
}; };