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::general_proto::brain_general_cli_server::BrainGeneralCliServer;
|
||||||
use detee_shared::vm_proto::brain_vm_cli_server::BrainVmCliServer;
|
use detee_shared::vm_proto::brain_vm_cli_server::BrainVmCliServer;
|
||||||
use detee_shared::vm_proto::brain_vm_daemon_server::BrainVmDaemonServer;
|
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::db;
|
||||||
use surreal_brain::grpc::BrainGeneralCliForReal;
|
|
||||||
use surreal_brain::grpc::BrainVmCliForReal;
|
use surreal_brain::grpc::BrainVmCliForReal;
|
||||||
use surreal_brain::grpc::BrainVmDaemonForReal;
|
use surreal_brain::grpc::{BrainGeneralCliForReal, BrainVmDaemonForReal};
|
||||||
use tonic::transport::{Identity, Server, ServerTlsConfig};
|
use tonic::transport::{Identity, Server, ServerTlsConfig};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
env_logger::builder().filter_level(log::LevelFilter::Debug).init();
|
env_logger::builder().filter_level(log::LevelFilter::Debug).init();
|
||||||
db::init().await.unwrap();
|
db::init(DB_ADDRESS, DB_NS, DB_NAME).await.unwrap();
|
||||||
let addr = "0.0.0.0:31337".parse().unwrap();
|
let addr = BRAIN_GRPC_ADDR.parse().unwrap();
|
||||||
|
|
||||||
let snp_daemon_server = BrainVmDaemonServer::new(BrainVmDaemonForReal {});
|
let snp_daemon_server = BrainVmDaemonServer::new(BrainVmDaemonForReal {});
|
||||||
let snp_cli_server = BrainVmCliServer::new(BrainVmCliForReal {});
|
let snp_cli_server = BrainVmCliServer::new(BrainVmCliForReal {});
|
||||||
let general_service_server = BrainGeneralCliServer::new(BrainGeneralCliForReal {});
|
let general_service_server = BrainGeneralCliServer::new(BrainGeneralCliForReal {});
|
||||||
|
|
||||||
let cert = std::fs::read_to_string("/etc/detee/brain/brain-crt.pem").unwrap();
|
let cert = std::fs::read_to_string(CERT_PATH).unwrap();
|
||||||
let key = std::fs::read_to_string("/etc/detee/brain/brain-key.pem").unwrap();
|
let key = std::fs::read_to_string(CERT_KEY_PATH).unwrap();
|
||||||
|
|
||||||
let identity = Identity::from_pem(cert, key);
|
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 crate::old_brain;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{str::FromStr, sync::LazyLock};
|
use std::{str::FromStr, sync::LazyLock};
|
||||||
@ -11,19 +16,6 @@ use tokio::sync::mpsc::Sender;
|
|||||||
use tokio_stream::StreamExt as _;
|
use tokio_stream::StreamExt as _;
|
||||||
|
|
||||||
static DB: LazyLock<Surreal<Client>> = LazyLock::new(Surreal::init);
|
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)]
|
#[derive(thiserror::Error, Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
@ -33,11 +25,11 @@ pub enum Error {
|
|||||||
DaemonConnection(#[from] tokio::sync::mpsc::error::SendError<DaemonNotification>),
|
DaemonConnection(#[from] tokio::sync::mpsc::error::SendError<DaemonNotification>),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn init() -> surrealdb::Result<()> {
|
pub async fn init(db_address: &str, ns: &str, db: &str) -> surrealdb::Result<()> {
|
||||||
DB.connect::<Ws>("localhost:8000").await?;
|
DB.connect::<Ws>(db_address).await?;
|
||||||
// Sign in to the server
|
// Sign in to the server
|
||||||
DB.signin(Root { username: "root", password: "root" }).await?;
|
DB.signin(Root { username: DB_USER, password: DB_PASS }).await?;
|
||||||
DB.use_ns("brain").use_db("migration").await?;
|
DB.use_ns(ns).use_db(db).await?;
|
||||||
Ok(())
|
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 app_nodes: Vec<AppNode> = old_data.into();
|
||||||
let vm_contracts: Vec<ActiveVm> = old_data.into();
|
let vm_contracts: Vec<ActiveVm> = old_data.into();
|
||||||
|
|
||||||
init().await?;
|
init(DB_ADDRESS, DB_NS, DB_NAME).await?;
|
||||||
|
|
||||||
println!("Inserting accounts...");
|
println!("Inserting accounts...");
|
||||||
let _: Vec<Account> = DB.insert(()).content(accounts).await?;
|
let _: Vec<Account> = DB.insert(()).content(accounts).await?;
|
||||||
@ -406,7 +398,7 @@ impl ActiveVm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut mapped_ports = Vec::new();
|
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());
|
guest_ports.append(&mut args.exposed_ports.clone());
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while i < new_vm_req.extra_ports.len() && i < guest_ports.len() {
|
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)]
|
#![allow(dead_code)]
|
||||||
|
use crate::constants::{ACCOUNT, ADMIN_ACCOUNTS, VM_NODE};
|
||||||
use crate::db;
|
use crate::db;
|
||||||
use detee_shared::app_proto::{AppContract, AppNodeListResp};
|
use detee_shared::app_proto::{AppContract, AppNodeListResp};
|
||||||
use detee_shared::{
|
use detee_shared::{
|
||||||
@ -250,8 +251,8 @@ impl BrainVmDaemon for BrainVmDaemonForReal {
|
|||||||
let req = check_sig_from_req(req)?;
|
let req = check_sig_from_req(req)?;
|
||||||
info!("Starting registration process for {:?}", req);
|
info!("Starting registration process for {:?}", req);
|
||||||
db::VmNode {
|
db::VmNode {
|
||||||
id: surrealdb::RecordId::from((db::VM_NODE, req.node_pubkey.clone())),
|
id: surrealdb::RecordId::from((VM_NODE, req.node_pubkey.clone())),
|
||||||
operator: surrealdb::RecordId::from((db::ACCOUNT, req.operator_wallet)),
|
operator: surrealdb::RecordId::from((ACCOUNT, req.operator_wallet)),
|
||||||
country: req.country,
|
country: req.country,
|
||||||
region: req.region,
|
region: req.region,
|
||||||
city: req.city,
|
city: req.city,
|
||||||
@ -843,12 +844,6 @@ fn check_sig_from_parts(pubkey: &str, time: &str, msg: &str, sig: &str) -> Resul
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
const ADMIN_ACCOUNTS: &[&str] = &[
|
|
||||||
"x52w7jARC5erhWWK65VZmjdGXzBK6ZDgfv1A283d8XK",
|
|
||||||
"FHuecMbeC1PfjkW2JKyoicJAuiU7khgQT16QUB3Q1XdL",
|
|
||||||
"H21Shi4iE7vgfjWEQNvzmpmBMJSaiZ17PYUcdNoAoKNc",
|
|
||||||
];
|
|
||||||
|
|
||||||
fn check_admin_key<T>(req: &Request<T>) -> Result<(), Status> {
|
fn check_admin_key<T>(req: &Request<T>) -> Result<(), Status> {
|
||||||
let pubkey = match req.metadata().get("pubkey") {
|
let pubkey = match req.metadata().get("pubkey") {
|
||||||
Some(p) => p.clone(),
|
Some(p) => p.clone(),
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
pub mod grpc;
|
pub mod constants;
|
||||||
pub mod db;
|
pub mod db;
|
||||||
|
pub mod grpc;
|
||||||
pub mod old_brain;
|
pub mod old_brain;
|
||||||
|
@ -5,6 +5,8 @@ use dashmap::DashMap;
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
|
use crate::constants::OLD_BRAIN_DATA_PATH;
|
||||||
|
|
||||||
#[derive(Clone, Default, Serialize, Deserialize, Debug)]
|
#[derive(Clone, Default, Serialize, Deserialize, Debug)]
|
||||||
pub struct AccountData {
|
pub struct AccountData {
|
||||||
pub balance: u64,
|
pub balance: u64,
|
||||||
@ -124,7 +126,7 @@ pub struct BrainData {
|
|||||||
|
|
||||||
impl BrainData {
|
impl BrainData {
|
||||||
pub fn load_from_disk() -> Result<Self, Box<dyn std::error::Error>> {
|
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)?;
|
let data: Self = serde_yaml::from_str(&content)?;
|
||||||
Ok(data)
|
Ok(data)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user