module Beaker::Options::HostsFileParser

def self.fix_roles_array(host_options)


Make sure the roles array is present for all hosts
def self.fix_roles_array(host_options)
  host_options['HOSTS'].each_key do |host|
    host_options['HOSTS'][host]['roles'] ||= []
  end
  host_options = host_options.merge(host_options.delete('CONFIG')) if host_options.has_key?('CONFIG')
  host_options
end

def self.merge_hosts_yaml(host_options, error_message)

Returns:
  • (OptionsHash) - Updated host_options with host info merged

Parameters:
  • error_message (String) -- Message to print if {::Psych::SyntaxError}
  • host_options (OptionsHash) -- Host information hash
def self.merge_hosts_yaml(host_options, error_message)
  begin
    loaded_host_options = yield
  rescue Psych::SyntaxError => e
    error_message << e.to_s
    raise ArgumentError, error_message
  end
  host_options.merge(loaded_host_options)
end

def self.new_host_options

Returns:
  • (OptionsHash) - Hash with HOSTS section
def self.new_host_options
  host_options = Beaker::Options::OptionsHash.new
  host_options['HOSTS'] ||= {}
  host_options
end

def self.parse_hosts_file(hosts_file_path = nil)

Raises:
  • (Errno::ENOENT) - File not found error: hosts_file doesn't exist
  • (ArgumentError) - Raises if hosts_file_path is not a valid YAML file

Returns:
  • (OptionsHash) - The contents of the hosts file as an OptionsHash

Parameters:
  • hosts_file_path (String) -- The path to the hosts file
def self.parse_hosts_file(hosts_file_path = nil)
  require 'erb'
  host_options = new_host_options
  return host_options unless hosts_file_path
  error_message = "#{hosts_file_path} is not a valid YAML file\n\t"
  host_options = self.merge_hosts_yaml(host_options, error_message) do
    hosts_file_path = File.expand_path(hosts_file_path)
    raise "#{hosts_file_path} is not a valid path" unless File.exist?(hosts_file_path)
    process_yaml(File.read(hosts_file_path), binding)
  end
  fix_roles_array(host_options)
end

def self.parse_hosts_string(hosts_def_yaml = nil)

Raises:
  • (ArgumentError) - If hosts_def_yaml is not a valid YAML string

Returns:
  • (OptionsHash) - Contents of the hosts file as an OptionsHash

Parameters:
  • hosts_def_yaml (String) -- YAML hosts definition
def self.parse_hosts_string(hosts_def_yaml = nil)
  require 'erb'
  host_options = new_host_options
  return host_options unless hosts_def_yaml
  error_message = "#{hosts_def_yaml}\nis not a valid YAML string\n\t"
  host_options = self.merge_hosts_yaml(host_options, error_message) do
    process_yaml(hosts_def_yaml, binding)
  end
  fix_roles_array(host_options)
end

def self.process_yaml(template, b)

Other tags:
    Api: - private

Parameters:
  • b (Binding) -- The binding to pass to ERB rendering
  • path (String) -- Path to the file to read
def self.process_yaml(template, b)
  erb_obj = ERB.new(template, trim_mode: '-')
  YAML.safe_load(erb_obj.result(b), permitted_classes: PERMITTED_YAML_CLASSES, aliases: true)
end