-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add example to update log levels of all testnet nodes
- Loading branch information
1 parent
414c188
commit 6b61280
Showing
3 changed files
with
84 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (C) 2024 MaidSafe.net limited. | ||
// | ||
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3. | ||
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed | ||
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. Please review the Licences for the specific language governing | ||
// permissions and limitations relating to use of the SAFE Network Software. | ||
|
||
use clap::Parser; | ||
use color_eyre::{eyre::eyre, Result}; | ||
use futures::StreamExt; | ||
use sn_service_management::rpc::{RpcActions, RpcClient}; | ||
use std::net::SocketAddr; | ||
use test_utils::testnet::DeploymentInventory; | ||
|
||
#[derive(Parser, Debug)] | ||
#[clap(name = "Update Log level")] | ||
struct Opt { | ||
/// Provide the path to the DeploymentInventory file or the name of the deployment. | ||
/// If the name is provided, we search for the inventory file in the default location. | ||
#[clap(long)] | ||
inventory: String, | ||
/// Change the log level of the safenode. | ||
/// | ||
/// Example: --log-level SN_LOG=all,RUST_LOG=libp2p=debug | ||
#[clap(long)] | ||
log_level: String, | ||
/// The number of nodes to update concurrently. | ||
#[clap(long, short = 'c', default_value_t = 10)] | ||
concurrent_updates: usize, | ||
} | ||
|
||
// Run using `cargo run --release --example update_log_levels -- --inventory <inventory name/path> --log-level safenode=trace,RUST_LOG=libp2p=debug` | ||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let opt = Opt::parse(); | ||
let inventory = DeploymentInventory::load_from_str(&opt.inventory)?; | ||
|
||
let node_endpoints = inventory | ||
.rpc_endpoints | ||
.values() | ||
.cloned() | ||
.collect::<Vec<_>>(); | ||
let mut stream = futures::stream::iter(node_endpoints.iter()) | ||
.map(|endpoint| update_log_level(*endpoint, opt.log_level.clone())) | ||
.buffer_unordered(opt.concurrent_updates); | ||
|
||
let mut failed_list = vec![]; | ||
let mut last_error = Ok(()); | ||
let mut success_counter = 0; | ||
while let Some((endpoint, result)) = stream.next().await { | ||
match result { | ||
Ok(_) => success_counter += 1, | ||
Err(err) => { | ||
failed_list.push(endpoint); | ||
last_error = Err(err); | ||
} | ||
} | ||
} | ||
|
||
println!("==== Update Log Levels Summary ===="); | ||
println!("Successfully updated: {success_counter} nodes"); | ||
if !failed_list.is_empty() { | ||
println!("Failed to update: {} nodes", failed_list.len()); | ||
println!("Last error: {last_error:?}") | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn update_log_level(endpoint: SocketAddr, log_levels: String) -> (SocketAddr, Result<()>) { | ||
let client = RpcClient::from_socket_addr(endpoint); | ||
let res = client | ||
.update_log_level(log_levels) | ||
.await | ||
.map_err(|err| eyre!("{err:?}")); | ||
(endpoint, res) | ||
} |