added code to list and delete contracts
This commit is contained in:
parent
6ee235a2ec
commit
8cf9f21d09
@ -5,7 +5,8 @@ pub mod brain {
|
|||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use brain::{
|
use brain::{
|
||||||
brain_cli_service_client::BrainCliServiceClient, NewVmRequest, NodeFilters, NodeListResp,
|
brain_cli_service_client::BrainCliServiceClient, DeletedVmUpdate, ListVmContractsReq,
|
||||||
|
NewVmRequest, NodeFilters, NodeListResp, VmContract,
|
||||||
};
|
};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use log::{debug, info, warn};
|
use log::{debug, info, warn};
|
||||||
@ -15,7 +16,11 @@ use tokio_stream::StreamExt;
|
|||||||
use tonic::transport::Channel;
|
use tonic::transport::Channel;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref SECURE_PUBLIC_KEY: String = generate_random_string();
|
static ref SECURE_PUBLIC_KEY: String = use_default_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn use_default_string() -> String {
|
||||||
|
"ThisIsMyEternalClient".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_random_string() -> String {
|
fn generate_random_string() -> String {
|
||||||
@ -92,6 +97,44 @@ async fn submit_vm_request(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn list_contracts(mut client: BrainCliServiceClient<Channel>) -> Result<Vec<VmContract>> {
|
||||||
|
info!("Getting contracts from brain...");
|
||||||
|
let mut contracts = Vec::new();
|
||||||
|
let mut grpc_stream = client
|
||||||
|
.list_vm_contracts(ListVmContractsReq {
|
||||||
|
admin_pubkey: SECURE_PUBLIC_KEY.to_string(),
|
||||||
|
node_pubkey: String::new(),
|
||||||
|
})
|
||||||
|
.await?
|
||||||
|
.into_inner();
|
||||||
|
while let Some(stream_update) = grpc_stream.next().await {
|
||||||
|
match stream_update {
|
||||||
|
Ok(node) => {
|
||||||
|
debug!("Received contract from brain: {node:?}");
|
||||||
|
contracts.push(node);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
warn!("Received error instead of contracts: {e:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
info!("Brain terminated list_contracts stream.");
|
||||||
|
Ok(contracts)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete_vm(mut client: BrainCliServiceClient<Channel>, uuid: &str) -> Result<()> {
|
||||||
|
let req = DeletedVmUpdate {
|
||||||
|
uuid: uuid.to_string(),
|
||||||
|
};
|
||||||
|
info!("Creating VM {req:?}");
|
||||||
|
let result = client.delete_vm(req).await;
|
||||||
|
match result {
|
||||||
|
Ok(confirmation) => info!("VM deleted: {confirmation:?}"),
|
||||||
|
Err(e) => log::error!("Could not delete vm: {e:?}"),
|
||||||
|
};
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
env_logger::builder()
|
env_logger::builder()
|
||||||
@ -110,5 +153,16 @@ async fn main() -> Result<()> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if std::env::var("DELETE_VMS").is_err() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let contracts = list_contracts(client.clone()).await?;
|
||||||
|
for contract in contracts {
|
||||||
|
if let Err(e) = delete_vm(client.clone(), &contract.uuid).await {
|
||||||
|
log::error!("Received error when deleting VM {}: {e:?}", &contract.uuid);
|
||||||
|
}
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user