Skip to content

Commit

Permalink
feat: add example to update log levels of all testnet nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed Mar 14, 2024
1 parent 414c188 commit 6b61280
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions sn_node_manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ assert_cmd = "2.0.12"
assert_fs = "1.0.13"
assert_matches = "1.5.0"
async-trait = "0.1"
# Do not specify the version field. Release process expects even the local dev deps to be published.
# Removing the version field is a workaround.
test_utils = { path = "../test_utils" }
futures = "~0.3.13"
mockall = "0.11.3"
predicates = "2.0"
reqwest = { version = "0.11", default-features = false, features = [
"json",
"rustls-tls",
] }
predicates = "2.0"
# Do not specify the version field. Release process expects even the local dev deps to be published.
# Removing the version field is a workaround.
test_utils = { path = "../test_utils" }
78 changes: 78 additions & 0 deletions sn_node_manager/examples/update_log_levels.rs
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)
}

0 comments on commit 6b61280

Please sign in to comment.