hostlist

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 18, 2025 License: MIT Imports: 5 Imported by: 0

README

Hostlist

The hostlist package provides functionality to expand compact hostlist specifications into individual host names. This is particularly useful for cluster computing environments where hosts are often specified using range notation.

Features

  • Compact notation: Express multiple hosts concisely using range syntax
  • Zero-padding preservation: Maintains leading zeros in numeric ranges
  • Automatic sorting: Results are alphabetically sorted
  • Deduplication: Automatically removes duplicate host names
  • Flexible syntax: Supports multiple ranges, indices, and optional suffixes

Installation

import "github.com/ClusterCockpit/cc-lib/hostlist"

Quick Start

package main

import (
    "fmt"
    "log"
    
    "github.com/ClusterCockpit/cc-lib/hostlist"
)

func main() {
    // Expand a simple range
    hosts, err := hostlist.Expand("node[1-3]")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(hosts)
    // Output: [node1 node2 node3]
}

Syntax

Basic Patterns
Pattern Expands to Description
n1 [n1] Single node
n[1-3] [n1, n2, n3] Numeric range
n[01-03] [n01, n02, n03] Zero-padded range
n[1,3,5] [n1, n3, n5] Specific indices
n[1-2,5] [n1, n2, n5] Mixed ranges and indices
Advanced Patterns
Pattern Expands to Description
n[1-2]-ib [n1-ib, n2-ib] Range with suffix
n[1-2],m[3-4] [m3, m4, n1, n2] Multiple host groups (sorted)
n1,n1,n2 [n1, n2] Duplicates removed
n1 , n2 [n1, n2] Whitespace trimmed
Syntax Rules
  • Brackets: Ranges must be enclosed in square brackets []
  • Range format: Use hyphen for ranges: [start-end]
  • Multiple items: Separate with commas: [1,2,5-7]
  • One range per host: Only one [] specification allowed per host expression
  • Valid characters: Host names can contain: a-z, A-Z, 0-9, -
  • Range order: Start value must be ≤ end value
  • Zero-padding: Preserved when start and end have the same width

Usage Examples

Example 1: Expanding Compute Nodes
hosts, err := hostlist.Expand("compute[001-128]")
if err != nil {
    log.Fatal(err)
}
// Returns: [compute001, compute002, ..., compute128]
fmt.Printf("Total hosts: %d\n", len(hosts))
Example 2: Multiple Node Types
hosts, err := hostlist.Expand("login[1-2],compute[1-4],gpu[1-2]")
if err != nil {
    log.Fatal(err)
}
// Returns: [compute1, compute2, compute3, compute4, gpu1, gpu2, login1, login2]
// Note: Results are sorted alphabetically
Example 3: Network Interfaces
// InfiniBand interfaces
ibHosts, _ := hostlist.Expand("node[1-4]-ib")
// Returns: [node1-ib, node2-ib, node3-ib, node4-ib]

// Ethernet interfaces  
ethHosts, _ := hostlist.Expand("node[1-4]-eth0")
// Returns: [node1-eth0, node2-eth0, node3-eth0, node4-eth0]
Example 4: Error Handling
// Invalid: decreasing range
_, err := hostlist.Expand("node[5-1]")
if err != nil {
    fmt.Println(err)
    // Output: single range start is greater than end: 5-1
}

// Invalid: forbidden character
_, err = hostlist.Expand("node@1")
if err != nil {
    fmt.Println(err)
    // Output: not a hostlist: @1
}

// Invalid: malformed range
_, err = hostlist.Expand("node[1-2-3]")
if err != nil {
    fmt.Println(err)
    // Output: not at hostlist range: [1-2-3]
}

Invalid Specifications

The following patterns will return an error:

  • Multiple hyphens in range: [1-2-3]
  • Decreasing ranges: [5-1]
  • Invalid characters: node@1, node$1
  • Nested brackets: node[[1-2]]

Performance Considerations

  • The function uses in-place deduplication for memory efficiency
  • Results are sorted using Go's standard sort.Strings() (O(n log n))
  • Zero-padding detection is performed per range for optimal formatting
  • Large ranges (e.g., [1-10000]) are expanded efficiently

Common Use Cases

Configuration Files
{
  "host_list": "node[01-16]",
  "username": "admin",
  "endpoint": "https://%h-bmc.example.com"
}

The %h placeholder is typically replaced with each expanded hostname by the calling application.

SLURM/PBS Job Scripts
# Expand node list for parallel jobs
NODES=$(echo "node[1-8]" | your-expand-tool)
Monitoring Systems

Used in ClusterCockpit receivers (IPMI, Redfish) to specify which hosts to monitor:

hostList, err := hostlist.Expand(clientConfigJSON.HostList)
if err != nil {
    return fmt.Errorf("failed to parse host list: %v", err)
}
for _, host := range hostList {
    // Monitor each host
}

API Reference

For detailed API documentation, see the godoc.

Main Function
func Expand(in string) (result []string, err error)

Converts a compact hostlist specification into a slice of individual host names. Results are sorted alphabetically and deduplicated.

Testing

Run the test suite:

go test -v github.com/ClusterCockpit/cc-lib/hostlist

Check test coverage:

go test -cover github.com/ClusterCockpit/cc-lib/hostlist

License

Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
Licensed under the MIT License. See LICENSE file for details.

Contributors

  • Holger Obermaier (NHR@KIT)

Documentation

Overview

Package hostlist provides functionality to expand compact hostlist specifications into individual host names. This is particularly useful for cluster computing environments where hosts are often specified using range notation.

The package supports expanding hostlist expressions like:

  • "n[1-3]" expands to ["n1", "n2", "n3"]
  • "n[01-03]" expands to ["n01", "n02", "n03"] (preserves zero-padding)
  • "n[1-2],m[3-4]" expands to ["m3", "m4", "n1", "n2"] (sorted and deduplicated)
  • "n[1-2]-suffix" expands to ["n1-suffix", "n2-suffix"]

The expansion automatically sorts results alphabetically and removes duplicates.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Expand

func Expand(in string) (result []string, err error)

Expand converts a compact hostlist specification into a slice of individual host names.

The function accepts hostlist expressions containing:

  • Single hosts: "node1"
  • Numeric ranges in brackets: "node[1-5]"
  • Multiple ranges or indices: "node[1-3,5,7-9]"
  • Optional suffixes after ranges: "node[1-3]-ib" for InfiniBand interfaces
  • Multiple comma-separated expressions: "n[1-2],m[3-4]"

Syntax rules:

  • Ranges must be specified in brackets using the format [start-end]
  • Multiple ranges or indices within brackets must be comma-separated
  • Only one bracketed range specification is allowed per host expression
  • Range start must be less than or equal to range end
  • Zero-padding is preserved when start and end have the same width
  • Valid DNS characters: a-z, A-Z, 0-9, and hyphen (-)

The function automatically:

  • Sorts the resulting host names alphabetically
  • Removes duplicate entries
  • Trims leading/trailing spaces and commas from the input

Parameters:

  • in: The hostlist specification string to expand

Returns:

  • result: A sorted slice of unique host names
  • err: An error if the input is malformed or contains invalid syntax

Examples:

// Simple range
hosts, _ := Expand("n[1-3]")
// Returns: []string{"n1", "n2", "n3"}

// Zero-padded range
hosts, _ := Expand("node[01-03]")
// Returns: []string{"node01", "node02", "node03"}

// Multiple ranges and indices
hosts, _ := Expand("n[1-2,5,7-8]")
// Returns: []string{"n1", "n2", "n5", "n7", "n8"}

// With suffix
hosts, _ := Expand("n[1-2]-ib")
// Returns: []string{"n1-ib", "n2-ib"}

// Multiple host groups
hosts, _ := Expand("n[1-2],m[3-4]")
// Returns: []string{"m3", "m4", "n1", "n2"}

// Duplicates are removed
hosts, _ := Expand("n1,n1,n2")
// Returns: []string{"n1", "n2"}

Error conditions:

  • Invalid characters (e.g., "@", "$")
  • Malformed range syntax (e.g., "[1-2-3]")
  • Decreasing ranges (e.g., "[5-1]")
  • Invalid bracket nesting or missing brackets

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL