change NewVmResp to allow the CLI to build params

This commit is contained in:
ghe0 2024-12-28 03:52:35 +02:00
parent 0dcacf6f98
commit 7b3ee9319f
Signed by: ghe0
GPG Key ID: 451028EE56A0FBB4
2 changed files with 46 additions and 17 deletions

@ -79,12 +79,21 @@ message ListVMContractsReq {
string node_pubkey = 2;
}
message NewVmRespIP {
uint32 nic_index = 1;
string address = 2;
string mask = 3;
string gateway = 4;
}
message NewVMResp {
string uuid = 1;
repeated uint32 exposed_ports = 2;
string public_ipv4 = 3;
string public_ipv6 = 4;
string error = 5;
string ovmf_hash = 5;
// This is needed to allow the CLI to build the kernel params from known data.
// The CLI will use the kernel params to get the measurement.
repeated NewVmRespIP ips = 6;
string error = 7;
}
message DeleteVMReq {

@ -3,6 +3,7 @@ use crate::grpc::brain as grpc;
use chrono::Utc;
use dashmap::DashMap;
use log::{debug, info, warn};
use std::str::FromStr;
use std::sync::RwLock;
use tokio::sync::mpsc::Sender;
use tokio::sync::oneshot::Sender as OneshotSender;
@ -126,7 +127,10 @@ impl BrainData {
let mut nodes = self.nodes.write().unwrap();
for n in nodes.iter_mut() {
if n.public_key == res.node_pubkey {
debug!("Found node {}. Updating resources to {:?}", n.public_key, res);
debug!(
"Found node {}. Updating resources to {:?}",
n.public_key, res
);
n.avail_ipv4 = res.avail_ipv4;
n.avail_ipv6 = res.avail_ipv6;
n.avail_vcpus = res.avail_vcpus;
@ -137,7 +141,10 @@ impl BrainData {
return;
}
}
debug!("Node {} not found when trying to update resources.", res.node_pubkey);
debug!(
"Node {} not found when trying to update resources.",
res.node_pubkey
);
debug!("Node list:\n{:?}", nodes);
}
@ -177,33 +184,46 @@ impl BrainData {
self.daemon_newvm_tx.insert(node_pubkey.to_string(), tx);
}
pub async fn submit_newvm_resp(&self, confirmation: grpc::NewVmResp) {
let newvmreq = match self.tmp_newvm_reqs.remove(&confirmation.uuid) {
pub async fn submit_newvm_resp(&self, new_vm_resp: grpc::NewVmResp) {
let newvmreq = match self.tmp_newvm_reqs.remove(&new_vm_resp.uuid) {
Some((_, r)) => r,
None => {
log::error!(
"Received confirmation for ghost NewVMReq {}",
confirmation.uuid
new_vm_resp.uuid
);
return;
}
};
if let Err(e) = newvmreq.1.send(confirmation.clone()) {
if let Err(e) = newvmreq.1.send(new_vm_resp.clone()) {
log::error!(
"CLI RX for {} dropped before receiving confirmation {:?}. Error is: {:?}",
&newvmreq.0.admin_pubkey,
confirmation,
new_vm_resp,
e
);
}
if confirmation.error != "" {
if new_vm_resp.error != "" {
return;
}
let mut public_ipv4 = String::new();
let mut public_ipv6 = String::new();
for ip in new_vm_resp.ips {
if let Ok(ipv4_addr) = std::net::Ipv4Addr::from_str(&ip.address) {
if !ipv4_addr.is_private() && !ipv4_addr.is_link_local() {
public_ipv4 = ipv4_addr.to_string();
}
continue;
}
if let Ok(ipv6_addr) = std::net::Ipv6Addr::from_str(&ip.address) {
public_ipv6 = ipv6_addr.to_string();
}
}
let contract = Contract {
uuid: confirmation.uuid,
exposed_ports: confirmation.exposed_ports,
public_ipv4: confirmation.public_ipv4,
public_ipv6: confirmation.public_ipv6,
uuid: new_vm_resp.uuid,
exposed_ports: new_vm_resp.exposed_ports,
public_ipv4,
public_ipv6,
created_at: Utc::now().to_rfc3339(),
updated_at: String::new(),
hostname: newvmreq.0.hostname,
@ -274,8 +294,8 @@ impl BrainData {
error: "Daemon is offline.".to_string(),
uuid,
exposed_ports: Vec::new(),
public_ipv4: "".to_string(),
public_ipv6: "".to_string(),
ovmf_hash: "".to_string(),
ips: Vec::new(),
})
.await;
}