Shellmin::Utils
Class methods
Find appropriate script for OS, version, sub-modules and action
Validate module directory structure
Ensures modules follow the required hierarchical structure: /modules/module_name/os_name/(release/version|generic)/action.ext
Arguments
module_dir: Module directory path (e.g., modules/user)
Returns
Array of validation errors (empty if valid)
Instance methods
Extract host information from YAML configuration
Converts YAML host configuration into a structured format.
Arguments
host: YAML::Any containing host configuration
Returns
NamedTuple with host information:
name: Host name/aliashost: Host address/IPuser: SSH usernamekey_path: SSH key path (optional)password: SSH password (optional)
Example
host_yaml = YAML.parse("name: web-server\nhost: 192.168.1.100\nuser: admin")
host_info = Utils.extract_host_info(host_yaml)
puts host_info[:name] # "web-server"
puts host_info[:host] # "192.168.1.100"
Generate a comprehensive context YAML for remote scripts Includes inventory, host context, and current state
Select hosts from configuration based on criteria
Arguments
config: Configuration hash containing host definitionshost_name: Specific host name to select (optional)all: Whether to select all hosts (default: false)
Returns
Array of YAML::Any representing selected hosts
Raises
ShellminErrorif no hosts configured or specific host not found
Examples
# Select all hosts
hosts = Utils.select_hosts(config, all: true)
# Select specific host
host = Utils.select_hosts(config, host_name: "web-server")
# Select first host (default)
default_host = Utils.select_hosts(config)
Validate command for security (prevents command injection)
Validate host configuration data
Performs comprehensive validation of host configuration including:
- Required fields presence
- Host format validation (IPv4, IPv6, hostname)
- User name format validation
- SSH key path existence check
Arguments
host_data: Hash containing host configuration
Returns
Validated host data hash
Raises
ShellminErrorfor validation failures
Examples
# Valid configuration
valid_config = {"host" => "192.168.1.100", "user" => "admin"}
Utils.validate_host_config(valid_config) # Returns validated config
# Invalid configurations
Utils.validate_host_config({"host" => "", "user" => "admin"}) # Raises error
Utils.validate_host_config({"host" => "192.168.1.100", "user" => ""}) # Raises error