From b3a1ab720de31a23a45edefaf770a1941a7b1b18 Mon Sep 17 00:00:00 2001 From: ghe0 Date: Sun, 27 Apr 2025 01:51:10 +0300 Subject: [PATCH] new brain proto and new staging address --- Cargo.lock | 2 +- Cargo.toml | 3 ++- src/global.rs | 2 +- src/grpc.rs | 15 ++++++-------- src/main.rs | 56 +++++++++++++++++++++++---------------------------- 5 files changed, 35 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b96bce5..1cbc30a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -395,7 +395,7 @@ dependencies = [ [[package]] name = "detee-shared" version = "0.1.0" -source = "git+ssh://git@gitea.detee.cloud/testnet/proto?branch=main#b5289f1f5ba3ddae2ee066d6deb073ce92436b71" +source = "git+ssh://git@gitea.detee.cloud/testnet/proto?branch=surreal_brain#d6ca058d2de78b5257517034bca2b2c7d5929db8" dependencies = [ "bincode", "prost", diff --git a/Cargo.toml b/Cargo.toml index 0fcab8c..70376ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,8 @@ serde_json = "1.0.135" bs58 = "0.5.1" chrono = "0.4.39" -detee-shared = { git = "ssh://git@gitea.detee.cloud/testnet/proto", branch = "main" } +# TODO: switch this back to main after the upgrade +detee-shared = { git = "ssh://git@gitea.detee.cloud/testnet/proto", branch = "surreal_brain" } # detee-shared = { path = "../detee-shared" } [build-dependencies] diff --git a/src/global.rs b/src/global.rs index e436073..e84b7f1 100644 --- a/src/global.rs +++ b/src/global.rs @@ -6,7 +6,7 @@ use sha2::{Digest, Sha256}; use std::{fs::File, io::Read, io::Write}; pub(crate) const DETEE_ROOT_CA: &str = "/etc/detee/root_ca.pem"; -pub(crate) const BRAIN_STAGING: (&str, &str) = ("https://159.65.58.38:31337", "staging-brain"); +pub(crate) const BRAIN_STAGING: (&str, &str) = ("https://173.234.17.2:48741", "staging-brain"); pub(crate) const BRAIN_TESTING: (&str, &str) = ("https://164.92.249.180:31337", "testnet-brain"); pub(crate) const VM_BOOT_DIR: &str = "/var/lib/detee/boot/"; pub(crate) const USED_RESOURCES: &str = "/etc/detee/daemon/used_resources.yaml"; diff --git a/src/grpc.rs b/src/grpc.rs index 1d00ee5..8e53c32 100644 --- a/src/grpc.rs +++ b/src/grpc.rs @@ -1,10 +1,8 @@ -use crate::global::*; -use crate::snp_proto::VmDaemonMessage; +use crate::{global::*, snp_proto::VmDaemonMessage}; use anyhow::Result; +use detee_shared::vm_proto::DeleteVmReq; use log::{debug, info, warn}; -use snp_proto::{ - brain_vm_daemon_client::BrainVmDaemonClient, BrainVmMessage, RegisterVmNodeReq, VmContract, -}; +use snp_proto::{brain_vm_daemon_client::BrainVmDaemonClient, BrainVmMessage, RegisterVmNodeReq}; use tokio::{ sync::mpsc::{Receiver, Sender}, task::JoinSet, @@ -36,9 +34,8 @@ async fn client(network: &str) -> Result> { Ok(BrainVmDaemonClient::new(channel)) } -pub async fn register_node(config: &crate::config::Config) -> Result> { - use tonic::metadata::AsciiMetadataValue; - use tonic::Request; +pub async fn register_node(config: &crate::config::Config) -> Result> { + use tonic::{metadata::AsciiMetadataValue, Request}; let mut client = client(&config.network).await?; debug!("Starting node registration..."); let ip_info = IP_INFO.clone(); @@ -68,7 +65,7 @@ pub async fn register_node(config: &crate::config::Config) -> Result { - debug!("Received contract from brain: {node:?}"); + debug!("Received deleted VM from brain: {node:?}"); contracts.push(node); } Err(e) => { diff --git a/src/main.rs b/src/main.rs index 1019b13..5b257aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,7 @@ mod global; mod grpc; mod state; -use crate::global::*; -use crate::{config::Config, grpc::snp_proto}; +use crate::{config::Config, global::*, grpc::snp_proto}; use anyhow::{anyhow, Result}; use log::{debug, info, warn}; use std::{fs::File, path::Path}; @@ -205,31 +204,27 @@ impl VMHandler { } } - fn clear_deleted_contracts(&mut self, contracts: Vec) { - for uuid in self.res.existing_vms.clone() { - if contracts.iter().find(|c| c.uuid == uuid).is_none() { - info!("VM {uuid} exists locally but not found in brain. Deleting..."); - let content = - match std::fs::read_to_string(VM_CONFIG_DIR.to_string() + &uuid + ".yaml") { - Ok(content) => content, - Err(e) => { - log::error!( - "Could not find VM config for {uuid}. Cannot delete VM: {e:?}" - ); - continue; - } - }; - let vm: crate::state::VM = match serde_yaml::from_str(&content) { - Ok(vm) => vm, - Err(e) => { - log::error!("VM config corrupted for {uuid}. Cannot delete VM: {e:?}"); - continue; - } - }; - match vm.delete(&mut self.res) { - Ok(()) => info!("Successfully deleted VM {uuid}"), - Err(e) => log::error!("Deletion failed for VM {uuid}: {e:?}"), + fn clear_deleted_contracts(&mut self, deleted_vms: Vec) { + for deleted_vm in deleted_vms { + let uuid = deleted_vm.uuid; + let content = match std::fs::read_to_string(VM_CONFIG_DIR.to_string() + &uuid + ".yaml") + { + Ok(content) => content, + Err(e) => { + log::debug!("Could not find VM config for {uuid}. Maybe it already got deleted? Error: {e:?}"); + continue; } + }; + let vm: crate::state::VM = match serde_yaml::from_str(&content) { + Ok(vm) => vm, + Err(e) => { + log::error!("VM config corrupted for {uuid}. Cannot delete VM: {e:?}"); + continue; + } + }; + match vm.delete(&mut self.res) { + Ok(()) => info!("Successfully deleted VM {uuid}"), + Err(e) => log::error!("Deletion failed for VM {uuid}: {e:?}"), } } } @@ -252,13 +247,12 @@ async fn main() { let mut vm_handler = VMHandler::new(brain_msg_rx, daemon_msg_tx.clone()); let network = vm_handler.config.network.clone(); + let contracts: Vec = vm_handler.res.existing_vms.clone().into_iter().collect(); - info!("Registering with the brain and getting back VM Contracts (if they exist)."); - let mut contracts: Vec = Vec::new(); + info!("Registering with the brain and getting back deleted VMs."); match grpc::register_node(&vm_handler.config).await { - Ok(c) => { - contracts.append(&mut c.iter().map(|c| c.uuid.clone()).collect()); - vm_handler.clear_deleted_contracts(c) + Ok(deleted_vms) => { + vm_handler.clear_deleted_contracts(deleted_vms) } Err(e) => log::error!("Could not get contracts from brain: {e:?}"), };