module

Shellmin::Utils

Class methods

find_executable_script(base_dir : String, action : String) : String | Nil
Source
find_script(module_dir : String, os : String, version : String, sub_modules : Array(String), action : String) : String | Nil

Find appropriate script for OS, version, sub-modules and action

Source
resolve_script_path(module_dir : String, os : String, version : String, sub_path : String, action : String) : String | Nil
Source
validate_module_structure(module_dir : String) : Array(String)

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)

Source

Instance methods

extract_host_info(host : YAML::Any) : NamedTuple(name: String, host: String, user: String, port: Int32, key_path: String | Nil, password: String | Nil)

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/alias
  • host: Host address/IP
  • user: SSH username
  • key_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"
Source
generate_context_yaml(host_info : NamedTuple, config : Hash(String, YAML::Any), state : Hash(String, JSON::Any)) : String

Generate a comprehensive context YAML for remote scripts Includes inventory, host context, and current state

Source
sanitize_command(command : String) : String

Sanitize command string for safe execution

Source
select_hosts(config : Hash(String, YAML::Any), host_names : String | Nil = nil, all : Bool = false) : Array(YAML::Any)

Select hosts from configuration based on criteria

Arguments

  • config: Configuration hash containing host definitions
  • host_name: Specific host name to select (optional)
  • all: Whether to select all hosts (default: false)

Returns

Array of YAML::Any representing selected hosts

Raises

  • ShellminError if 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)
Source
shell_quote(s : String) : String

Safely quotes a string for use in a shell command

Source
validate_command(command : String) : Bool

Validate command for security (prevents command injection)

Source
validate_host_config(host_data : Hash) : Hash

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

  • ShellminError for 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
Source

Nested types