1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright 2025 Lablup Inc. and Jeongkyu Shin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Node resolution and cluster management.
use anyhow::Result;
use crate::node::Node;
use crate::ssh::ssh_config::SshConfig;
use super::types::{Cluster, Config, JumpHostConfig, NodeConfig};
use super::utils::{expand_env_vars, get_current_username};
impl Config {
/// Get a cluster by name.
pub fn get_cluster(&self, name: &str) -> Option<&Cluster> {
self.clusters.get(name)
}
/// Resolve nodes for a cluster.
pub fn resolve_nodes(&self, cluster_name: &str) -> Result<Vec<Node>> {
let cluster = self
.get_cluster(cluster_name)
.ok_or_else(|| anyhow::anyhow!("Cluster '{}' not found in configuration.\nAvailable clusters: {}\nPlease check your configuration file or use 'bssh list' to see available clusters.", cluster_name, self.clusters.keys().cloned().collect::<Vec<_>>().join(", ")))?;
let mut nodes = Vec::new();
for node_config in &cluster.nodes {
let node = match node_config {
NodeConfig::Simple(host) => {
// Expand environment variables in host
let expanded_host = expand_env_vars(host);
let default_user = cluster
.defaults
.user
.as_ref()
.or(self.defaults.user.as_ref())
.map(|u| expand_env_vars(u));
let default_port = cluster.defaults.port.or(self.defaults.port).unwrap_or(22);
Node::parse(&expanded_host, default_user.as_deref()).map(|mut n| {
if !expanded_host.contains(':') {
n.port = default_port;
}
n
})?
}
NodeConfig::Detailed {
host, port, user, ..
} => {
// Expand environment variables
let expanded_host = expand_env_vars(host);
let username = user
.as_ref()
.map(|u| expand_env_vars(u))
.or_else(|| cluster.defaults.user.as_ref().map(|u| expand_env_vars(u)))
.or_else(|| self.defaults.user.as_ref().map(|u| expand_env_vars(u)))
.unwrap_or_else(get_current_username);
let port = port
.or(cluster.defaults.port)
.or(self.defaults.port)
.unwrap_or(22);
Node::new(expanded_host, port, username)
}
};
nodes.push(node);
}
Ok(nodes)
}
/// Get SSH key for a cluster.
pub fn get_ssh_key(&self, cluster_name: Option<&str>) -> Option<String> {
if let Some(cluster_name) = cluster_name {
if let Some(cluster) = self.get_cluster(cluster_name) {
if let Some(key) = &cluster.defaults.ssh_key {
return Some(key.clone());
}
}
}
self.defaults.ssh_key.clone()
}
/// Get timeout for a cluster.
pub fn get_timeout(&self, cluster_name: Option<&str>) -> Option<u64> {
if let Some(cluster_name) = cluster_name {
if let Some(cluster) = self.get_cluster(cluster_name) {
if let Some(timeout) = cluster.defaults.timeout {
return Some(timeout);
}
}
}
self.defaults.timeout
}
/// Get parallelism level for a cluster.
pub fn get_parallel(&self, cluster_name: Option<&str>) -> Option<usize> {
if let Some(cluster_name) = cluster_name {
if let Some(cluster) = self.get_cluster(cluster_name) {
if let Some(parallel) = cluster.defaults.parallel {
return Some(parallel);
}
}
}
self.defaults.parallel
}
/// Get jump host for a specific node in a cluster.
///
/// Resolution priority (highest to lowest):
/// 1. Node-level `jump_host` (in `NodeConfig::Detailed`)
/// 2. Cluster-level `jump_host` (in `ClusterDefaults`)
/// 3. Global default `jump_host` (in `Defaults`)
///
/// Empty string (`""`) explicitly disables jump host inheritance.
///
/// Note: This method does not resolve SSH config references (`@alias`).
/// Use `get_jump_host_with_key_and_ssh_config` for full resolution.
pub fn get_jump_host(&self, cluster_name: &str, node_index: usize) -> Option<String> {
self.get_jump_host_with_key(cluster_name, node_index)
.map(|(conn_str, _)| conn_str)
}
/// Get jump host with SSH key for a specific node in a cluster.
///
/// Resolution priority (highest to lowest):
/// 1. Node-level `jump_host` (in `NodeConfig::Detailed`)
/// 2. Cluster-level `jump_host` (in `ClusterDefaults`)
/// 3. Global default `jump_host` (in `Defaults`)
///
/// Empty string (`""`) explicitly disables jump host inheritance.
/// Returns tuple of (connection_string, optional_ssh_key_path)
///
/// Note: This method does not resolve SSH config references (`@alias`).
/// Use `get_jump_host_with_key_and_ssh_config` for full resolution.
pub fn get_jump_host_with_key(
&self,
cluster_name: &str,
node_index: usize,
) -> Option<(String, Option<String>)> {
self.get_jump_host_with_key_and_ssh_config(cluster_name, node_index, None)
}
/// Get jump host with SSH key for a specific node, with SSH config reference resolution.
///
/// This is the full-featured version that can resolve SSH config Host alias references
/// (`@alias` or `ssh_config_host` field) using the provided SSH config.
///
/// Resolution priority (highest to lowest):
/// 1. Node-level `jump_host` (in `NodeConfig::Detailed`)
/// 2. Cluster-level `jump_host` (in `ClusterDefaults`)
/// 3. Global default `jump_host` (in `Defaults`)
///
/// Empty string (`""`) explicitly disables jump host inheritance.
/// Returns tuple of (connection_string, optional_ssh_key_path)
pub fn get_jump_host_with_key_and_ssh_config(
&self,
cluster_name: &str,
node_index: usize,
ssh_config: Option<&SshConfig>,
) -> Option<(String, Option<String>)> {
if let Some(cluster) = self.get_cluster(cluster_name) {
// Check node-level first
if let Some(NodeConfig::Detailed {
jump_host: Some(jh),
..
}) = cluster.nodes.get(node_index)
{
return self.process_jump_host_config(jh, ssh_config);
}
// Check cluster-level
if let Some(jh) = &cluster.defaults.jump_host {
return self.process_jump_host_config(jh, ssh_config);
}
}
// Fall back to global default
self.defaults
.jump_host
.as_ref()
.and_then(|jh| self.process_jump_host_config(jh, ssh_config))
}
/// Process a JumpHostConfig and return (connection_string, optional_ssh_key_path)
///
/// If `ssh_config` is provided, SSH config references (`@alias` or `ssh_config_host`)
/// will be resolved using the SSH config. Otherwise, the reference string is returned as-is.
fn process_jump_host_config(
&self,
config: &JumpHostConfig,
ssh_config: Option<&SshConfig>,
) -> Option<(String, Option<String>)> {
match config {
JumpHostConfig::Simple(s) => {
if s.is_empty() {
None // Explicitly disabled
} else if let Some(alias) = s.strip_prefix('@') {
// SSH config reference with @ prefix
self.resolve_ssh_config_jump_host(alias, ssh_config)
} else {
Some((expand_env_vars(s), None))
}
}
JumpHostConfig::Detailed {
host,
user,
port,
ssh_key,
} => {
let mut conn_str = String::new();
if let Some(u) = user {
conn_str.push_str(&expand_env_vars(u));
conn_str.push('@');
}
conn_str.push_str(&expand_env_vars(host));
if let Some(p) = port {
conn_str.push(':');
conn_str.push_str(&p.to_string());
}
let key = ssh_key.as_ref().map(|k| expand_env_vars(k));
Some((conn_str, key))
}
JumpHostConfig::SshConfigHostRef { ssh_config_host } => {
self.resolve_ssh_config_jump_host(ssh_config_host, ssh_config)
}
}
}
/// Resolve an SSH config Host alias to connection string and SSH key.
///
/// If `ssh_config` is provided, looks up the alias and extracts:
/// - HostName (or uses the alias as hostname)
/// - User
/// - Port
/// - IdentityFile (first one, used as SSH key)
///
/// If `ssh_config` is None, returns the alias as the hostname with no SSH key.
fn resolve_ssh_config_jump_host(
&self,
alias: &str,
ssh_config: Option<&SshConfig>,
) -> Option<(String, Option<String>)> {
if let Some(ssh_cfg) = ssh_config {
// Try to resolve from SSH config
if let Some((conn_str, identity_file)) = ssh_cfg.resolve_jump_host_connection(alias) {
return Some((conn_str, identity_file));
}
}
// Fallback: use the alias as the hostname (SSH will resolve it)
// This allows the connection to proceed even without explicit SSH config resolution
tracing::debug!(
"SSH config reference '{}' could not be resolved, using as hostname",
alias
);
Some((alias.to_string(), None))
}
/// Get jump host for a cluster (cluster-level default).
///
/// Resolution priority (highest to lowest):
/// 1. Cluster-level `jump_host` (in `ClusterDefaults`)
/// 2. Global default `jump_host` (in `Defaults`)
///
/// Empty string (`""`) explicitly disables jump host inheritance.
pub fn get_cluster_jump_host(&self, cluster_name: Option<&str>) -> Option<String> {
self.get_cluster_jump_host_with_key(cluster_name)
.map(|(conn_str, _)| conn_str)
}
/// Get jump host with SSH key for a cluster (cluster-level default).
///
/// Resolution priority (highest to lowest):
/// 1. Cluster-level `jump_host` (in `ClusterDefaults`)
/// 2. Global default `jump_host` (in `Defaults`)
///
/// Empty string (`""`) explicitly disables jump host inheritance.
/// Returns tuple of (connection_string, optional_ssh_key_path)
pub fn get_cluster_jump_host_with_key(
&self,
cluster_name: Option<&str>,
) -> Option<(String, Option<String>)> {
self.get_cluster_jump_host_with_key_and_ssh_config(cluster_name, None)
}
/// Get jump host with SSH key for a cluster, with SSH config reference resolution.
///
/// Resolution priority (highest to lowest):
/// 1. Cluster-level `jump_host` (in `ClusterDefaults`)
/// 2. Global default `jump_host` (in `Defaults`)
///
/// Empty string (`""`) explicitly disables jump host inheritance.
/// Returns tuple of (connection_string, optional_ssh_key_path)
pub fn get_cluster_jump_host_with_key_and_ssh_config(
&self,
cluster_name: Option<&str>,
ssh_config: Option<&SshConfig>,
) -> Option<(String, Option<String>)> {
if let Some(cluster_name) = cluster_name {
if let Some(cluster) = self.get_cluster(cluster_name) {
if let Some(jh) = &cluster.defaults.jump_host {
return self.process_jump_host_config(jh, ssh_config);
}
}
}
// Fall back to global default
self.defaults
.jump_host
.as_ref()
.and_then(|jh| self.process_jump_host_config(jh, ssh_config))
}
/// Get SSH keepalive interval for a cluster.
///
/// Resolution priority (highest to lowest):
/// 1. Cluster-level `server_alive_interval`
/// 2. Global default `server_alive_interval`
///
/// Returns None if not specified (defaults will be applied at connection time).
pub fn get_server_alive_interval(&self, cluster_name: Option<&str>) -> Option<u64> {
if let Some(cluster_name) = cluster_name {
if let Some(cluster) = self.get_cluster(cluster_name) {
if let Some(interval) = cluster.defaults.server_alive_interval {
return Some(interval);
}
}
}
self.defaults.server_alive_interval
}
/// Get SSH keepalive count max for a cluster.
///
/// Resolution priority (highest to lowest):
/// 1. Cluster-level `server_alive_count_max`
/// 2. Global default `server_alive_count_max`
///
/// Returns None if not specified (defaults will be applied at connection time).
pub fn get_server_alive_count_max(&self, cluster_name: Option<&str>) -> Option<usize> {
if let Some(cluster_name) = cluster_name {
if let Some(cluster) = self.get_cluster(cluster_name) {
if let Some(count) = cluster.defaults.server_alive_count_max {
return Some(count);
}
}
}
self.defaults.server_alive_count_max
}
}