bssh/commands/
ping.rs

1// Copyright 2025 Lablup Inc. and Jeongkyu Shin
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use anyhow::Result;
16use owo_colors::OwoColorize;
17use std::path::Path;
18
19use crate::executor::ParallelExecutor;
20use crate::node::Node;
21use crate::ssh::known_hosts::StrictHostKeyChecking;
22use crate::ui::OutputFormatter;
23
24pub async fn ping_nodes(
25    nodes: Vec<Node>,
26    max_parallel: usize,
27    key_path: Option<&Path>,
28    strict_mode: StrictHostKeyChecking,
29    use_agent: bool,
30    use_password: bool,
31) -> Result<()> {
32    println!(
33        "{}",
34        OutputFormatter::format_command_header("ping", nodes.len())
35    );
36
37    let key_path = key_path.map(|p| p.to_string_lossy().to_string());
38    let executor = ParallelExecutor::new_with_all_options(
39        nodes.clone(),
40        max_parallel,
41        key_path,
42        strict_mode,
43        use_agent,
44        use_password,
45    );
46
47    let results = executor.execute("echo 'pong'").await?;
48
49    let mut success_count = 0;
50    let mut failed_count = 0;
51
52    println!("\n{} {}\n", "▶".cyan(), "Connection Test Results".bold());
53
54    for result in &results {
55        if result.is_success() {
56            success_count += 1;
57            println!(
58                "  {} {} - {}",
59                "●".green(),
60                result.node.to_string().bold(),
61                "Connected".green()
62            );
63        } else {
64            failed_count += 1;
65            println!(
66                "  {} {} - {}",
67                "●".red(),
68                result.node.to_string().bold(),
69                "Failed".red()
70            );
71            if let Err(e) = &result.result {
72                println!("    {} {}", "└".dimmed(), e.to_string().dimmed());
73            }
74        }
75    }
76
77    println!(
78        "{}",
79        OutputFormatter::format_summary(nodes.len(), success_count, failed_count)
80    );
81
82    Ok(())
83}