bssh 2.1.2

Parallel SSH command execution tool for cluster management
Documentation
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// 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.

//! Hostlist expansion implementation
//!
//! This module expands parsed host patterns into lists of hostnames
//! using cartesian product for multiple range expressions.

use super::error::HostlistError;
use super::parser::{PatternSegment, parse_host_pattern};

/// Maximum number of hosts that can be generated from a single pattern
const MAX_EXPANSION_SIZE: usize = 100_000;

/// Expand a hostlist expression into a list of hostnames
///
/// # Arguments
///
/// * `expr` - The hostlist expression to expand
///
/// # Returns
///
/// A vector of expanded hostnames.
///
/// # Examples
///
/// ```rust
/// use bssh::hostlist::expand_hostlist;
///
/// // Simple range
/// let hosts = expand_hostlist("node[1-3]").unwrap();
/// assert_eq!(hosts, vec!["node1", "node2", "node3"]);
///
/// // Zero-padded
/// let hosts = expand_hostlist("server[01-03]").unwrap();
/// assert_eq!(hosts, vec!["server01", "server02", "server03"]);
///
/// // Cartesian product
/// let hosts = expand_hostlist("rack[1-2]-node[1-2]").unwrap();
/// assert_eq!(hosts, vec!["rack1-node1", "rack1-node2", "rack2-node1", "rack2-node2"]);
/// ```
pub fn expand_hostlist(expr: &str) -> Result<Vec<String>, HostlistError> {
    if expr.is_empty() {
        return Ok(Vec::new());
    }

    let pattern = parse_host_pattern(expr)?;

    // Check expansion size before generating
    let expansion_count = pattern.expansion_count();
    if expansion_count > MAX_EXPANSION_SIZE {
        return Err(HostlistError::RangeTooLarge {
            expression: expr.to_string(),
            count: expansion_count,
            limit: MAX_EXPANSION_SIZE,
        });
    }

    // If no ranges, just concatenate literals
    if !pattern.has_ranges() {
        let host: String = pattern
            .segments
            .iter()
            .filter_map(|s| match s {
                PatternSegment::Literal(lit) => Some(lit.as_str()),
                PatternSegment::Range(_) => None,
            })
            .collect();
        return Ok(if host.is_empty() {
            Vec::new()
        } else {
            vec![host]
        });
    }

    // Expand using cartesian product
    expand_segments(&pattern.segments)
}

/// Expand pattern segments into a list of hostnames using cartesian product
fn expand_segments(segments: &[PatternSegment]) -> Result<Vec<String>, HostlistError> {
    if segments.is_empty() {
        return Ok(vec![String::new()]);
    }

    // Start with a single empty string
    let mut results = vec![String::new()];

    for segment in segments {
        match segment {
            PatternSegment::Literal(lit) => {
                // Append literal to all current results
                for result in &mut results {
                    result.push_str(lit);
                }
            }
            PatternSegment::Range(range_expr) => {
                // Expand with all values from the range
                let values = range_expr.values();

                // Use checked multiplication to prevent integer overflow
                let new_capacity = results.len().checked_mul(values.len()).ok_or_else(|| {
                    HostlistError::RangeTooLarge {
                        expression: "cartesian product".to_string(),
                        count: usize::MAX,
                        limit: MAX_EXPANSION_SIZE,
                    }
                })?;

                if new_capacity > MAX_EXPANSION_SIZE {
                    return Err(HostlistError::RangeTooLarge {
                        expression: "cartesian product".to_string(),
                        count: new_capacity,
                        limit: MAX_EXPANSION_SIZE,
                    });
                }

                let mut new_results = Vec::with_capacity(new_capacity);

                for result in &results {
                    for value in &values {
                        let formatted = range_expr.format_value(*value);
                        let mut new_result = result.clone();
                        new_result.push_str(&formatted);
                        new_results.push(new_result);
                    }
                }

                results = new_results;
            }
        }
    }

    Ok(results)
}

/// Expand a host specification that may include user@ prefix and :port suffix
///
/// This function handles the full host specification format:
/// `[user@]hostpattern[:port]`
///
/// # Arguments
///
/// * `spec` - The full host specification
///
/// # Returns
///
/// A vector of expanded host specifications preserving user and port.
///
/// # Examples
///
/// ```rust
/// use bssh::hostlist::expander::expand_host_spec;
///
/// let hosts = expand_host_spec("admin@web[1-2].example.com:22").unwrap();
/// assert_eq!(hosts, vec![
///     "admin@web1.example.com:22",
///     "admin@web2.example.com:22"
/// ]);
/// ```
pub fn expand_host_spec(spec: &str) -> Result<Vec<String>, HostlistError> {
    if spec.is_empty() {
        return Ok(Vec::new());
    }

    // Parse user prefix
    let (user_prefix, rest) = if let Some(at_pos) = spec.find('@') {
        // Check if @ is before any [ to avoid matching @ in expressions
        let bracket_pos = spec.find('[');
        if bracket_pos.is_none() || at_pos < bracket_pos.unwrap() {
            let user = &spec[..=at_pos]; // includes @
            let rest = &spec[at_pos + 1..];
            (Some(user.to_string()), rest)
        } else {
            (None, spec)
        }
    } else {
        (None, spec)
    };

    // Parse port suffix (find last : that's not inside brackets)
    let (host_pattern, port_suffix) = parse_port_suffix(rest)?;

    // Expand the host pattern
    let expanded_hosts = expand_hostlist(host_pattern)?;

    // Reconstruct with user and port
    let results: Vec<String> = expanded_hosts
        .into_iter()
        .map(|host| {
            let mut result = String::new();
            if let Some(ref user) = user_prefix {
                result.push_str(user);
            }
            result.push_str(&host);
            if let Some(ref port) = port_suffix {
                result.push_str(port);
            }
            result
        })
        .collect();

    Ok(results)
}

/// Parse port suffix from a host pattern, being careful about brackets
fn parse_port_suffix(spec: &str) -> Result<(&str, Option<String>), HostlistError> {
    // Find the last : that's not inside brackets
    let mut bracket_depth = 0;
    let mut last_colon_outside = None;

    for (i, ch) in spec.char_indices() {
        match ch {
            '[' => bracket_depth += 1,
            ']' => {
                if bracket_depth > 0 {
                    bracket_depth -= 1;
                }
            }
            ':' if bracket_depth == 0 => {
                last_colon_outside = Some(i);
            }
            _ => {}
        }
    }

    if let Some(colon_pos) = last_colon_outside {
        let potential_port = &spec[colon_pos + 1..];
        // Check if it looks like a port (all digits)
        if !potential_port.is_empty() && potential_port.chars().all(|c| c.is_ascii_digit()) {
            let host_pattern = &spec[..colon_pos];
            let port_suffix = Some(format!(":{}", potential_port));
            return Ok((host_pattern, port_suffix));
        }
    }

    Ok((spec, None))
}

/// Expand multiple comma-separated host specifications
///
/// # Arguments
///
/// * `specs` - Comma-separated host specifications
///
/// # Returns
///
/// A vector of all expanded host specifications, deduplicated.
pub fn expand_host_specs(specs: &str) -> Result<Vec<String>, HostlistError> {
    use super::split_patterns;

    if specs.is_empty() {
        return Ok(Vec::new());
    }

    let patterns = split_patterns(specs)?;
    let mut all_hosts = Vec::new();

    for pattern in patterns {
        let pattern = pattern.trim();
        if pattern.is_empty() {
            continue;
        }

        let expanded = expand_host_spec(pattern)?;
        all_hosts.extend(expanded);
    }

    // Deduplicate while preserving order
    let mut seen = std::collections::HashSet::new();
    let mut result = Vec::new();
    for host in all_hosts {
        if seen.insert(host.clone()) {
            result.push(host);
        }
    }

    Ok(result)
}

#[cfg(test)]
mod tests {
    use super::*;

    // Basic expansion tests
    #[test]
    fn test_expand_simple_range() {
        let hosts = expand_hostlist("node[1-3]").unwrap();
        assert_eq!(hosts, vec!["node1", "node2", "node3"]);
    }

    #[test]
    fn test_expand_zero_padded_range() {
        let hosts = expand_hostlist("node[01-05]").unwrap();
        assert_eq!(
            hosts,
            vec!["node01", "node02", "node03", "node04", "node05"]
        );
    }

    #[test]
    fn test_expand_comma_separated() {
        let hosts = expand_hostlist("node[1,3,5]").unwrap();
        assert_eq!(hosts, vec!["node1", "node3", "node5"]);
    }

    #[test]
    fn test_expand_mixed_range() {
        let hosts = expand_hostlist("node[1-3,7,9-10]").unwrap();
        assert_eq!(
            hosts,
            vec!["node1", "node2", "node3", "node7", "node9", "node10"]
        );
    }

    #[test]
    fn test_expand_cartesian_product() {
        let hosts = expand_hostlist("rack[1-2]-node[1-3]").unwrap();
        assert_eq!(
            hosts,
            vec![
                "rack1-node1",
                "rack1-node2",
                "rack1-node3",
                "rack2-node1",
                "rack2-node2",
                "rack2-node3"
            ]
        );
    }

    #[test]
    fn test_expand_with_domain() {
        let hosts = expand_hostlist("web[1-3].example.com").unwrap();
        assert_eq!(
            hosts,
            vec!["web1.example.com", "web2.example.com", "web3.example.com"]
        );
    }

    #[test]
    fn test_expand_no_range() {
        let hosts = expand_hostlist("simple.host.com").unwrap();
        assert_eq!(hosts, vec!["simple.host.com"]);
    }

    #[test]
    fn test_expand_empty() {
        let hosts = expand_hostlist("").unwrap();
        assert!(hosts.is_empty());
    }

    #[test]
    fn test_expand_single_value_range() {
        let hosts = expand_hostlist("node[5]").unwrap();
        assert_eq!(hosts, vec!["node5"]);
    }

    #[test]
    fn test_expand_three_digit_padding() {
        let hosts = expand_hostlist("node[001-003]").unwrap();
        assert_eq!(hosts, vec!["node001", "node002", "node003"]);
    }

    // Host spec expansion tests
    #[test]
    fn test_expand_host_spec_with_user() {
        let hosts = expand_host_spec("admin@node[1-2]").unwrap();
        assert_eq!(hosts, vec!["admin@node1", "admin@node2"]);
    }

    #[test]
    fn test_expand_host_spec_with_port() {
        let hosts = expand_host_spec("node[1-2]:22").unwrap();
        assert_eq!(hosts, vec!["node1:22", "node2:22"]);
    }

    #[test]
    fn test_expand_host_spec_full() {
        let hosts = expand_host_spec("admin@web[1-2].example.com:22").unwrap();
        assert_eq!(
            hosts,
            vec!["admin@web1.example.com:22", "admin@web2.example.com:22"]
        );
    }

    #[test]
    fn test_expand_host_spec_no_expansion() {
        let hosts = expand_host_spec("user@host.com:2222").unwrap();
        assert_eq!(hosts, vec!["user@host.com:2222"]);
    }

    #[test]
    fn test_expand_host_specs_multiple() {
        let hosts = expand_host_specs("web[1-2],db[1-2]").unwrap();
        assert_eq!(hosts, vec!["web1", "web2", "db1", "db2"]);
    }

    #[test]
    fn test_expand_host_specs_with_user_port() {
        let hosts = expand_host_specs("admin@web[1-2]:22,root@db[1-2]:3306").unwrap();
        assert_eq!(
            hosts,
            vec![
                "admin@web1:22",
                "admin@web2:22",
                "root@db1:3306",
                "root@db2:3306"
            ]
        );
    }

    #[test]
    fn test_expand_host_specs_deduplication() {
        let hosts = expand_host_specs("node[1-3],node[2-4]").unwrap();
        assert_eq!(hosts, vec!["node1", "node2", "node3", "node4"]);
    }

    // Error cases
    #[test]
    fn test_expand_too_large() {
        // This would produce 1000 * 1000 = 1,000,000 hosts
        let result = expand_hostlist("a[1-1000]-b[1-1000]");
        assert!(matches!(result, Err(HostlistError::RangeTooLarge { .. })));
    }

    #[test]
    fn test_expand_empty_bracket() {
        let result = expand_hostlist("node[]");
        assert!(matches!(result, Err(HostlistError::EmptyBracket { .. })));
    }

    #[test]
    fn test_expand_reversed_range() {
        let result = expand_hostlist("node[5-1]");
        assert!(matches!(result, Err(HostlistError::ReversedRange { .. })));
    }

    #[test]
    fn test_expand_invalid_number() {
        let result = expand_hostlist("node[a-z]");
        assert!(matches!(result, Err(HostlistError::InvalidNumber { .. })));
    }

    // Edge cases
    #[test]
    fn test_expand_large_but_valid_range() {
        let hosts = expand_hostlist("node[1-1000]").unwrap();
        assert_eq!(hosts.len(), 1000);
        assert_eq!(hosts[0], "node1");
        assert_eq!(hosts[999], "node1000");
    }

    #[test]
    fn test_expand_prefix_only() {
        let hosts = expand_hostlist("prefix-[1-2]").unwrap();
        assert_eq!(hosts, vec!["prefix-1", "prefix-2"]);
    }

    #[test]
    fn test_expand_suffix_only() {
        let hosts = expand_hostlist("[1-2]-suffix").unwrap();
        assert_eq!(hosts, vec!["1-suffix", "2-suffix"]);
    }

    #[test]
    fn test_expand_range_only() {
        let hosts = expand_hostlist("[1-3]").unwrap();
        assert_eq!(hosts, vec!["1", "2", "3"]);
    }

    #[test]
    fn test_expand_complex_domain() {
        let hosts = expand_hostlist("app[1-2].prod.us-east-1.example.com").unwrap();
        assert_eq!(
            hosts,
            vec![
                "app1.prod.us-east-1.example.com",
                "app2.prod.us-east-1.example.com"
            ]
        );
    }

    #[test]
    fn test_port_suffix_parsing() {
        // Test that port suffix is correctly separated
        let (host, port) = parse_port_suffix("node[1-3]:22").unwrap();
        assert_eq!(host, "node[1-3]");
        assert_eq!(port, Some(":22".to_string()));

        // No port
        let (host, port) = parse_port_suffix("node[1-3]").unwrap();
        assert_eq!(host, "node[1-3]");
        assert_eq!(port, None);

        // Domain with port
        let (host, port) = parse_port_suffix("node[1-3].example.com:2222").unwrap();
        assert_eq!(host, "node[1-3].example.com");
        assert_eq!(port, Some(":2222".to_string()));
    }
}