Constant
centralize configuration constants update database initialization for test env
This commit is contained in:
		
							parent
							
								
									784414815e
								
							
						
					
					
						commit
						2d108a53b5
					
				| @ -1,24 +1,26 @@ | ||||
| use detee_shared::general_proto::brain_general_cli_server::BrainGeneralCliServer; | ||||
| use detee_shared::vm_proto::brain_vm_cli_server::BrainVmCliServer; | ||||
| use detee_shared::vm_proto::brain_vm_daemon_server::BrainVmDaemonServer; | ||||
| use surreal_brain::constants::{ | ||||
|     BRAIN_GRPC_ADDR, CERT_KEY_PATH, CERT_PATH, DB_ADDRESS, DB_NAME, DB_NS, | ||||
| }; | ||||
| use surreal_brain::db; | ||||
| use surreal_brain::grpc::BrainGeneralCliForReal; | ||||
| use surreal_brain::grpc::BrainVmCliForReal; | ||||
| use surreal_brain::grpc::BrainVmDaemonForReal; | ||||
| use surreal_brain::grpc::{BrainGeneralCliForReal, BrainVmDaemonForReal}; | ||||
| use tonic::transport::{Identity, Server, ServerTlsConfig}; | ||||
| 
 | ||||
| #[tokio::main] | ||||
| async fn main() { | ||||
|     env_logger::builder().filter_level(log::LevelFilter::Debug).init(); | ||||
|     db::init().await.unwrap(); | ||||
|     let addr = "0.0.0.0:31337".parse().unwrap(); | ||||
|     db::init(DB_ADDRESS, DB_NS, DB_NAME).await.unwrap(); | ||||
|     let addr = BRAIN_GRPC_ADDR.parse().unwrap(); | ||||
| 
 | ||||
|     let snp_daemon_server = BrainVmDaemonServer::new(BrainVmDaemonForReal {}); | ||||
|     let snp_cli_server = BrainVmCliServer::new(BrainVmCliForReal {}); | ||||
|     let general_service_server = BrainGeneralCliServer::new(BrainGeneralCliForReal {}); | ||||
| 
 | ||||
|     let cert = std::fs::read_to_string("/etc/detee/brain/brain-crt.pem").unwrap(); | ||||
|     let key = std::fs::read_to_string("/etc/detee/brain/brain-key.pem").unwrap(); | ||||
|     let cert = std::fs::read_to_string(CERT_PATH).unwrap(); | ||||
|     let key = std::fs::read_to_string(CERT_KEY_PATH).unwrap(); | ||||
| 
 | ||||
|     let identity = Identity::from_pem(cert, key); | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										34
									
								
								src/constants.rs
									
									
									
									
									
										Normal file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										34
									
								
								src/constants.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,34 @@ | ||||
| pub const BRAIN_GRPC_ADDR: &str = "0.0.0.0:31337"; | ||||
| pub const CERT_PATH: &str = "./tmp/brain-crt.pem"; | ||||
| pub const CERT_KEY_PATH: &str = "./tmp/brain-key.pem"; | ||||
| 
 | ||||
| pub const DB_ADDRESS: &str = "localhost:8000"; | ||||
| pub const DB_NS: &str = "brain"; | ||||
| pub const DB_NAME: &str = "migration"; | ||||
| 
 | ||||
| // TODO: read from .env
 | ||||
| pub const DB_USER: &str = "root"; | ||||
| pub const DB_PASS: &str = "root"; | ||||
| 
 | ||||
| pub const ADMIN_ACCOUNTS: &[&str] = &[ | ||||
|     "x52w7jARC5erhWWK65VZmjdGXzBK6ZDgfv1A283d8XK", | ||||
|     "FHuecMbeC1PfjkW2JKyoicJAuiU7khgQT16QUB3Q1XdL", | ||||
|     "H21Shi4iE7vgfjWEQNvzmpmBMJSaiZ17PYUcdNoAoKNc", | ||||
| ]; | ||||
| 
 | ||||
| pub const OLD_BRAIN_DATA_PATH: &str = "./saved_data.yaml"; | ||||
| 
 | ||||
| pub const ACCOUNT: &str = "account"; | ||||
| pub const VM_NODE: &str = "vm_node"; | ||||
| pub const ACTIVE_VM: &str = "active_vm"; | ||||
| pub const NEW_VM_REQ: &str = "new_vm_req"; | ||||
| pub const UPDATE_VM_REQ: &str = "update_vm_req"; | ||||
| pub const DELETED_VM: &str = "deleted_vm"; | ||||
| pub const VM_CONTRACT: &str = "vm_contract"; | ||||
| 
 | ||||
| pub const ID_ALPHABET: [char; 62] = [ | ||||
|     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', | ||||
|     'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', | ||||
|     'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', | ||||
|     'V', 'W', 'X', 'Y', 'Z', | ||||
| ]; | ||||
							
								
								
									
										30
									
								
								src/db.rs
									
									
									
									
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										30
									
								
								src/db.rs
									
									
									
									
									
								
							| @ -1,3 +1,8 @@ | ||||
| pub use crate::constants::{ | ||||
|     ACCOUNT, ACTIVE_VM, DB_ADDRESS, DB_NAME, DB_NS, DB_PASS, DB_USER, DELETED_VM, ID_ALPHABET, | ||||
|     NEW_VM_REQ, UPDATE_VM_REQ, VM_CONTRACT, VM_NODE, | ||||
| }; | ||||
| 
 | ||||
| use crate::old_brain; | ||||
| use serde::{Deserialize, Serialize}; | ||||
| use std::{str::FromStr, sync::LazyLock}; | ||||
| @ -11,19 +16,6 @@ use tokio::sync::mpsc::Sender; | ||||
| use tokio_stream::StreamExt as _; | ||||
| 
 | ||||
| static DB: LazyLock<Surreal<Client>> = LazyLock::new(Surreal::init); | ||||
| pub const ACCOUNT: &str = "account"; | ||||
| pub const VM_NODE: &str = "vm_node"; | ||||
| pub const ACTIVE_VM: &str = "active_vm"; | ||||
| pub const NEW_VM_REQ: &str = "new_vm_req"; | ||||
| pub const UPDATE_VM_REQ: &str = "update_vm_req"; | ||||
| pub const DELETED_VM: &str = "deleted_vm"; | ||||
| 
 | ||||
| pub const ID_ALPHABET: [char; 62] = [ | ||||
|     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', | ||||
|     'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', | ||||
|     'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', | ||||
|     'V', 'W', 'X', 'Y', 'Z', | ||||
| ]; | ||||
| 
 | ||||
| #[derive(thiserror::Error, Debug)] | ||||
| pub enum Error { | ||||
| @ -33,11 +25,11 @@ pub enum Error { | ||||
|     DaemonConnection(#[from] tokio::sync::mpsc::error::SendError<DaemonNotification>), | ||||
| } | ||||
| 
 | ||||
| pub async fn init() -> surrealdb::Result<()> { | ||||
|     DB.connect::<Ws>("localhost:8000").await?; | ||||
| pub async fn init(db_address: &str, ns: &str, db: &str) -> surrealdb::Result<()> { | ||||
|     DB.connect::<Ws>(db_address).await?; | ||||
|     // Sign in to the server
 | ||||
|     DB.signin(Root { username: "root", password: "root" }).await?; | ||||
|     DB.use_ns("brain").use_db("migration").await?; | ||||
|     DB.signin(Root { username: DB_USER, password: DB_PASS }).await?; | ||||
|     DB.use_ns(ns).use_db(db).await?; | ||||
|     Ok(()) | ||||
| } | ||||
| 
 | ||||
| @ -58,7 +50,7 @@ pub async fn migration0(old_data: &old_brain::BrainData) -> surrealdb::Result<() | ||||
|     let app_nodes: Vec<AppNode> = old_data.into(); | ||||
|     let vm_contracts: Vec<ActiveVm> = old_data.into(); | ||||
| 
 | ||||
|     init().await?; | ||||
|     init(DB_ADDRESS, DB_NS, DB_NAME).await?; | ||||
| 
 | ||||
|     println!("Inserting accounts..."); | ||||
|     let _: Vec<Account> = DB.insert(()).content(accounts).await?; | ||||
| @ -406,7 +398,7 @@ impl ActiveVm { | ||||
|         } | ||||
| 
 | ||||
|         let mut mapped_ports = Vec::new(); | ||||
|         let mut guest_ports= vec![ 22 ]; | ||||
|         let mut guest_ports = vec![22]; | ||||
|         guest_ports.append(&mut args.exposed_ports.clone()); | ||||
|         let mut i = 0; | ||||
|         while i < new_vm_req.extra_ports.len() && i < guest_ports.len() { | ||||
|  | ||||
							
								
								
									
										11
									
								
								src/grpc.rs
									
									
									
									
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										11
									
								
								src/grpc.rs
									
									
									
									
									
								
							| @ -1,4 +1,5 @@ | ||||
| #![allow(dead_code)] | ||||
| use crate::constants::{ACCOUNT, ADMIN_ACCOUNTS, VM_NODE}; | ||||
| use crate::db; | ||||
| use detee_shared::app_proto::{AppContract, AppNodeListResp}; | ||||
| use detee_shared::{ | ||||
| @ -250,8 +251,8 @@ impl BrainVmDaemon for BrainVmDaemonForReal { | ||||
|         let req = check_sig_from_req(req)?; | ||||
|         info!("Starting registration process for {:?}", req); | ||||
|         db::VmNode { | ||||
|             id: surrealdb::RecordId::from((db::VM_NODE, req.node_pubkey.clone())), | ||||
|             operator: surrealdb::RecordId::from((db::ACCOUNT, req.operator_wallet)), | ||||
|             id: surrealdb::RecordId::from((VM_NODE, req.node_pubkey.clone())), | ||||
|             operator: surrealdb::RecordId::from((ACCOUNT, req.operator_wallet)), | ||||
|             country: req.country, | ||||
|             region: req.region, | ||||
|             city: req.city, | ||||
| @ -843,12 +844,6 @@ fn check_sig_from_parts(pubkey: &str, time: &str, msg: &str, sig: &str) -> Resul | ||||
|     Ok(()) | ||||
| } | ||||
| 
 | ||||
| const ADMIN_ACCOUNTS: &[&str] = &[ | ||||
|     "x52w7jARC5erhWWK65VZmjdGXzBK6ZDgfv1A283d8XK", | ||||
|     "FHuecMbeC1PfjkW2JKyoicJAuiU7khgQT16QUB3Q1XdL", | ||||
|     "H21Shi4iE7vgfjWEQNvzmpmBMJSaiZ17PYUcdNoAoKNc", | ||||
| ]; | ||||
| 
 | ||||
| fn check_admin_key<T>(req: &Request<T>) -> Result<(), Status> { | ||||
|     let pubkey = match req.metadata().get("pubkey") { | ||||
|         Some(p) => p.clone(), | ||||
|  | ||||
| @ -1,3 +1,4 @@ | ||||
| pub mod grpc; | ||||
| pub mod constants; | ||||
| pub mod db; | ||||
| pub mod grpc; | ||||
| pub mod old_brain; | ||||
|  | ||||
| @ -5,6 +5,8 @@ use dashmap::DashMap; | ||||
| use serde::{Deserialize, Serialize}; | ||||
| use std::collections::{HashMap, HashSet}; | ||||
| 
 | ||||
| use crate::constants::OLD_BRAIN_DATA_PATH; | ||||
| 
 | ||||
| #[derive(Clone, Default, Serialize, Deserialize, Debug)] | ||||
| pub struct AccountData { | ||||
|     pub balance: u64, | ||||
| @ -124,7 +126,7 @@ pub struct BrainData { | ||||
| 
 | ||||
| impl BrainData { | ||||
|     pub fn load_from_disk() -> Result<Self, Box<dyn std::error::Error>> { | ||||
|         let content = std::fs::read_to_string("./saved_data.yaml")?; | ||||
|         let content = std::fs::read_to_string(OLD_BRAIN_DATA_PATH)?; | ||||
|         let data: Self = serde_yaml::from_str(&content)?; | ||||
|         Ok(data) | ||||
|     } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user