From 959b5c32c96c07d02d36ca8714fda59a6a964b97 Mon Sep 17 00:00:00 2001 From: Ramil_Algayev Date: Mon, 23 Dec 2024 17:39:54 +0400 Subject: [PATCH] created update functionality --- cli-mock/brain.proto | 22 +++++- cli-mock/src/main.rs | 27 ++++++- daemon-mock/Cargo.lock | 162 ++++++++++++++++++++++++++++++++++++++++ daemon-mock/Cargo.toml | 1 + daemon-mock/brain.proto | 22 +++++- daemon-mock/src/main.rs | 56 +++++++++++++- 6 files changed, 283 insertions(+), 7 deletions(-) diff --git a/cli-mock/brain.proto b/cli-mock/brain.proto index 05d8e4e..62224e3 100644 --- a/cli-mock/brain.proto +++ b/cli-mock/brain.proto @@ -40,6 +40,23 @@ message NewVMRequest { string dtrfs_sha = 14; } +message UpdateVMRequest { + string uuid = 1; + uint32 disk_size_gb = 2; + uint32 vcpus = 3; + uint32 memory_mb = 4; + string kernel_url = 5; + string kernel_sha = 6; + string dtrfs_url = 7; + string dtrfs_sha = 8; +} + +message UpdateVMResp { + string uuid = 1; + string timestamp = 2; + string error = 3; +} + message VMContract { string uuid = 1; string hostname = 2; @@ -79,6 +96,7 @@ service BrainDaemonService { rpc SendVMConfirmations (stream NewVMConfirmation) returns (Empty); rpc DeletedVMUpdates (NodePubkey) returns (stream DeletedVMUpdate); rpc ListVMContracts (ListVMContractsReq) returns (stream VMContract); + rpc UpdateVMs (NodePubkey) returns (stream UpdateVMRequest); } message NodeFilters { @@ -105,5 +123,5 @@ service BrainCliService { rpc ListVMContracts (ListVMContractsReq) returns (stream VMContract); rpc ListNodes (NodeFilters) returns (stream NodeListResp); rpc DeleteVM (DeletedVMUpdate) returns (Empty); -} - + rpc UpdateVM (UpdateVMRequest) returns (UpdateVMResp); +} \ No newline at end of file diff --git a/cli-mock/src/main.rs b/cli-mock/src/main.rs index 0e51c7f..1e73b7b 100644 --- a/cli-mock/src/main.rs +++ b/cli-mock/src/main.rs @@ -6,7 +6,7 @@ pub mod brain { use anyhow::Result; use brain::{ brain_cli_service_client::BrainCliServiceClient, DeletedVmUpdate, ListVmContractsReq, - NewVmRequest, NodeFilters, NodeListResp, VmContract, + NewVmRequest, NodeFilters, NodeListResp, VmContract, UpdateVmRequest, UpdateVmResp, }; use lazy_static::lazy_static; use log::{debug, info, warn}; @@ -135,6 +135,24 @@ async fn delete_vm(mut client: BrainCliServiceClient, uuid: &str) -> Re Ok(()) } +async fn update_vm(mut client: BrainCliServiceClient, uuid: &str) -> Result { + info!("Updatind VM {uuid}"); + let req = UpdateVmRequest { + uuid: String::new(), + vcpus: 4, + memory_mb: 4096, + disk_size_gb: 40, + kernel_url: "thisIsMyNewURL".to_string(), + kernel_sha: "thisIsMyNewSha".to_string(), + dtrfs_url: "thisIsMyNewURL".to_string(), + dtrfs_sha: "thisIsMyNewSha".to_string(), + }; + + let result = client.update_vm(req).await; + + result.map(|msg| msg.into_inner()).map_err(|e| anyhow::Error::new(e)) +} + #[tokio::main] async fn main() -> Result<()> { env_logger::builder() @@ -154,6 +172,13 @@ async fn main() -> Result<()> { } } + let contracts = list_contracts(client.clone()).await?; + for contract in contracts { + if let Err(e) = update_vm(client.clone(), &contract.uuid).await { + log::error!("Received error when updating VM {}: {e:?}", &contract.uuid); + } + } + if std::env::var("DELETE_VMS").is_err() { return Ok(()); } diff --git a/daemon-mock/Cargo.lock b/daemon-mock/Cargo.lock index 572553c..db2be90 100644 --- a/daemon-mock/Cargo.lock +++ b/daemon-mock/Cargo.lock @@ -26,6 +26,21 @@ dependencies = [ "memchr", ] +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "0.6.18" @@ -200,6 +215,12 @@ version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + [[package]] name = "byteorder" version = "1.5.0" @@ -212,23 +233,53 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b" +[[package]] +name = "cc" +version = "1.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c31a0499c1dc64f458ad13872de75c0eb7e3fdb0e67964610c914b034fc5956e" +dependencies = [ + "shlex", +] + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-targets", +] + [[package]] name = "colorchoice" version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + [[package]] name = "daemon-mock" version = "0.1.0" dependencies = [ "anyhow", + "chrono", "env_logger", "lazy_static", "log", @@ -502,6 +553,29 @@ dependencies = [ "tracing", ] +[[package]] +name = "iana-time-zone" +version = "0.1.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "indexmap" version = "1.9.3" @@ -543,6 +617,16 @@ version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" +[[package]] +name = "js-sys" +version = "0.3.76" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6717b6b5b077764fb5966237269cb3c64edddde4b14ce42647430a78ced9e7b7" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + [[package]] name = "lazy_static" version = "1.5.0" @@ -611,6 +695,15 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "defc4c55412d89136f966bbb339008b474350e5e6e78d2714439c386b3137a03" +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "object" version = "0.36.7" @@ -867,6 +960,12 @@ dependencies = [ "syn", ] +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + [[package]] name = "slab" version = "0.4.9" @@ -1127,6 +1226,69 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasm-bindgen" +version = "0.2.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a474f6281d1d70c17ae7aa6a613c87fce69a127e2624002df63dcb39d6cf6396" +dependencies = [ + "cfg-if", + "once_cell", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f89bb38646b4f81674e8f5c3fb81b562be1fd936d84320f3264486418519c79" +dependencies = [ + "bumpalo", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2cc6181fd9a7492eef6fef1f33961e3695e4579b9872a6f7c83aee556666d4fe" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6" + +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.52.0" diff --git a/daemon-mock/Cargo.toml b/daemon-mock/Cargo.toml index 955420f..938d7b0 100644 --- a/daemon-mock/Cargo.toml +++ b/daemon-mock/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] anyhow = "1.0.94" +chrono = "0.4.39" env_logger = "0.11.6" lazy_static = "1.5.0" log = "0.4.22" diff --git a/daemon-mock/brain.proto b/daemon-mock/brain.proto index 05d8e4e..62224e3 100644 --- a/daemon-mock/brain.proto +++ b/daemon-mock/brain.proto @@ -40,6 +40,23 @@ message NewVMRequest { string dtrfs_sha = 14; } +message UpdateVMRequest { + string uuid = 1; + uint32 disk_size_gb = 2; + uint32 vcpus = 3; + uint32 memory_mb = 4; + string kernel_url = 5; + string kernel_sha = 6; + string dtrfs_url = 7; + string dtrfs_sha = 8; +} + +message UpdateVMResp { + string uuid = 1; + string timestamp = 2; + string error = 3; +} + message VMContract { string uuid = 1; string hostname = 2; @@ -79,6 +96,7 @@ service BrainDaemonService { rpc SendVMConfirmations (stream NewVMConfirmation) returns (Empty); rpc DeletedVMUpdates (NodePubkey) returns (stream DeletedVMUpdate); rpc ListVMContracts (ListVMContractsReq) returns (stream VMContract); + rpc UpdateVMs (NodePubkey) returns (stream UpdateVMRequest); } message NodeFilters { @@ -105,5 +123,5 @@ service BrainCliService { rpc ListVMContracts (ListVMContractsReq) returns (stream VMContract); rpc ListNodes (NodeFilters) returns (stream NodeListResp); rpc DeleteVM (DeletedVMUpdate) returns (Empty); -} - + rpc UpdateVM (UpdateVMRequest) returns (UpdateVMResp); +} \ No newline at end of file diff --git a/daemon-mock/src/main.rs b/daemon-mock/src/main.rs index a07da74..d8a5914 100644 --- a/daemon-mock/src/main.rs +++ b/daemon-mock/src/main.rs @@ -6,7 +6,7 @@ pub mod brain { use anyhow::Result; use brain::{ brain_daemon_service_client::BrainDaemonServiceClient, DeletedVmUpdate, NewVmConfirmation, - NewVmRequest, NodePubkey, RegisterNodeRequest, + NewVmRequest, NodePubkey, RegisterNodeRequest, UpdateVmRequest, UpdateVmResp }; use lazy_static::lazy_static; use log::{debug, error, info, warn}; @@ -16,6 +16,7 @@ use tokio::{sync::mpsc::Receiver, sync::mpsc::Sender, task::JoinSet}; use tokio_stream::wrappers::ReceiverStream; use tokio_stream::StreamExt; use tonic::transport::Channel; +use chrono; lazy_static! { static ref SECURE_PUBLIC_KEY: String = generate_random_string(); @@ -115,6 +116,48 @@ async fn listen_for_deleted_vms( Ok(()) } +async fn listen_for_update_vm_reqs( + mut client: BrainDaemonServiceClient, + tx: Sender, +) -> Result<()> { + debug!("starting listen_for_update_vm_reqs"); + let node_pubkey = SECURE_PUBLIC_KEY.clone(); + let mut grpc_stream = client + .update_v_ms(NodePubkey { node_pubkey }) + .await? + .into_inner(); + while let Some(stream_update) = grpc_stream.next().await { + match stream_update { + Ok(req) => { + info!("Received update vm request: {req:?}"); + let _ = tx.send(req).await; + } + Err(e) => { + warn!("Brain disconnected from listen_for_update_vm_reqs: {e}"); + } + } + } + debug!("listen_for_update_vm_reqs is about to exit"); + Ok(()) +} + +async fn handle_update_vm_requests( + mut req: Receiver, + resp: Sender, +) { + info!("Started to handle update vm requests."); + while let Some(update_vm) = req.recv().await { + let response = UpdateVmResp { + uuid: update_vm.uuid.clone(), + timestamp: chrono::Utc::now().to_rfc3339(), + error: String::new(), + }; + info!("Sending UpdateVmResp: {response:?}"); + let _ = resp.send(response).await; + } + warn!("update vm request handler is ending"); +} + async fn handle_vm_requests(mut req: Receiver, resp: Sender) { info!("Started to handle vm requests. 1 out of 5 requests will return error."); let mut i = 0; @@ -178,6 +221,15 @@ async fn connect_and_run() -> Result<()> { handle_vm_requests(newvm_rx, confirm_tx).await; }); + let updatevm_client = client.clone(); + let (update_tx, update_rx) = tokio::sync::mpsc::channel(6); + streaming_tasks.spawn(listen_for_update_vm_reqs(updatevm_client, update_tx)); + + let (update_resp_tx, _) = tokio::sync::mpsc::channel(6); + tokio::spawn(async move { + handle_update_vm_requests(update_rx, update_resp_tx).await; + }); + let deletevms_client = client.clone(); let (tx, _deletevm_rx) = tokio::sync::mpsc::channel(6); streaming_tasks.spawn(listen_for_deleted_vms(deletevms_client, tx)); @@ -204,4 +256,4 @@ async fn main() { .init(); info!("Hello! My name is {}", SECURE_PUBLIC_KEY.clone()); connection_wrapper().await; -} +} \ No newline at end of file