From 6e7a337709c93e623e6fc08830cdd3ba8b4126cf Mon Sep 17 00:00:00 2001 From: Noor Date: Tue, 4 Feb 2025 16:50:52 +0530 Subject: [PATCH] complete refactor of brain proto format --- build.rs | 9 +++- proto/brain.proto | 110 +++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 4 ++ src/types.rs | 1 + src/types/brain.rs | 66 +++++++++++++++++++++++++++ 5 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 proto/brain.proto create mode 100644 src/types/brain.rs diff --git a/build.rs b/build.rs index 173750b..badef58 100644 --- a/build.rs +++ b/build.rs @@ -2,7 +2,14 @@ fn main() -> Result<(), Box> { tonic_build::configure() .build_server(true) .protoc_arg("--experimental_allow_proto3_optional") - .compile_protos(&["proto/daemon.proto", "proto/shared.proto"], &["proto"])?; + .compile_protos( + &[ + "proto/daemon.proto", + "proto/shared.proto", + "proto/brain.proto", + ], + &["proto"], + )?; Ok(()) } diff --git a/proto/brain.proto b/proto/brain.proto new file mode 100644 index 0000000..a2f8da9 --- /dev/null +++ b/proto/brain.proto @@ -0,0 +1,110 @@ +syntax = "proto3"; + +package brain; + +message Empty { +} + +message AppContract { + string uuid = 1; + string package_url = 2; + string admin_pubkey = 3; + string node_pubkey = 4; + string public_ipv4 = 5; + AppResource resource = 6; + repeated MappedPort exposed_ports = 7; + string created_at = 8; + string updated_at = 9; + uint64 nano_per_minute = 10; + uint64 locked_nano = 11; + string collected_at = 12; +} + +message NewAppReq { + string package_url = 1; + string node_pubkey = 2; + AppResource resource = 3; + string uuid = 4; + string admin_pubkey = 5; +} + +message AppResource { + uint32 memory_mb = 1; + uint32 disk_mb = 2; + uint32 vcpu = 3; + repeated uint32 ports = 4; +} + +message NewAppRes { + string uuid = 1; + string status = 2; + string ip_address = 3; + repeated MappedPort mapped_ports = 4; + string error = 5; +} + +message MappedPort { + uint32 host_port = 1; + uint32 app_port = 2; +} + +message DelAppReq { + string uuid= 1; +} + +message ListAppContractsReq { + string uuid = 1; + string admin_pubkey = 2; + string node_pubkey = 3; +} + +service BrainAppCli { + rpc CreateApp (NewAppReq) returns (NewAppRes); + rpc DeleteApp (DelAppReq) returns (Empty); + rpc ListAppContracts (ListAppContractsReq) returns (stream AppContract); +} + +message RegisterAppNodeReq { + string node_pubkey = 1; + string owner_pubkey = 2; + string main_ip = 3; + string country = 4; + string region = 5; + string city = 6; +} + +message AppNodeResources { + string node_pubkey = 1; + uint32 avail_ports = 2; + uint32 avail_ipv4 = 3; + uint32 avail_ipv6 = 4; + uint32 avail_vcpus = 5; + uint32 avail_memory_mb = 6; + uint32 avail_storage_gb = 7; + uint32 max_ports_per_vm = 8; +} + +message BrainMessageApp { + oneof Msg { + NewAppReq new_app_req = 1; + DelAppReq delete_app_req = 2; + // resource usage + } +} + +message DaemonMessageApp { + oneof Msg { + string pubkey = 1; + NewAppRes new_app_resp = 2; + AppNodeResources app_node_resources = 3; + } +} +message Pubkey { + string pubkey = 1; +} + +service BrainAppDaemon { + rpc RegisterNode (RegisterAppNodeReq) returns (stream AppContract); + rpc BrainMessages (Pubkey) returns (stream BrainMessageApp); + rpc DaemonMessages (stream DaemonMessageApp) returns (Empty); +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 0b77d13..270b3bb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,10 @@ pub mod pb { pub mod daemon { tonic::include_proto!("deamon"); } + + pub mod brain { + tonic::include_proto!("brain"); + } } pub mod types; diff --git a/src/types.rs b/src/types.rs index eec3c89..ef95ce9 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1 +1,2 @@ +pub mod brain; pub mod shared; diff --git a/src/types/brain.rs b/src/types/brain.rs new file mode 100644 index 0000000..42fd199 --- /dev/null +++ b/src/types/brain.rs @@ -0,0 +1,66 @@ +use crate::pb::brain::{AppResource, NewAppReq}; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)] +pub struct Resource { + pub memory_mb: u32, + pub disk_mb: u32, + pub vcpu: u32, + pub port: Vec, +} + +impl From for Resource { + fn from(pb_val: AppResource) -> Self { + Self { + memory_mb: pb_val.memory_mb, + disk_mb: pb_val.disk_mb, + vcpu: pb_val.vcpu, + port: pb_val.ports, + } + } +} +impl From for AppResource { + fn from(val: Resource) -> AppResource { + AppResource { + memory_mb: val.memory_mb, + disk_mb: val.disk_mb, + vcpu: val.vcpu, + ports: val.port, + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize, Default)] +pub struct AppDeployConfig { + pub package_url: String, + pub resource: Resource, + #[serde(default)] + pub uuid: String, + // #[serde(default)] + pub admin_pubkey: String, + pub node_pubkey: String, +} + +impl From for AppDeployConfig { + fn from(pb_val: NewAppReq) -> Self { + Self { + package_url: pb_val.package_url, + resource: pb_val.resource.map(Resource::from).unwrap_or_default(), + uuid: pb_val.uuid, + admin_pubkey: pb_val.admin_pubkey, + node_pubkey: pb_val.node_pubkey, + } + } +} + +impl From for NewAppReq { + fn from(val: AppDeployConfig) -> NewAppReq { + NewAppReq { + package_url: val.package_url, + resource: Some(val.resource.into()), + uuid: val.uuid, + admin_pubkey: val.admin_pubkey, + node_pubkey: val.node_pubkey, + } + } +}