Module: Beaker::Options::HostsFileParser

Defined in:
lib/beaker/options/hosts_file_parser.rb

Overview

A set of functions to parse hosts files

Constant Summary collapse

PERMITTED_YAML_CLASSES =
[Beaker::Options::OptionsHash, Beaker::Platform, Symbol, Time]

Class Method Summary collapse

Class Method Details

.fix_roles_array(host_options) ⇒ Object

Make sure the roles array is present for all hosts



62
63
64
65
66
67
68
69
70
# File 'lib/beaker/options/hosts_file_parser.rb', line 62

def self.fix_roles_array( host_options )
  host_options['HOSTS'].each_key do |host|
    host_options['HOSTS'][host]['roles'] ||= []
  end
  if host_options.has_key?('CONFIG')
    host_options = host_options.merge(host_options.delete('CONFIG'))
  end
  host_options
end

.merge_hosts_yaml(host_options, error_message, &block) ⇒ OptionsHash

Merges YAML read in the passed block into given OptionsHash

Parameters:

  • host_options (OptionsHash)

    Host information hash

  • error_message (String)

    Message to print if Psych::SyntaxError is raised during block execution

Returns:

  • (OptionsHash)

    Updated host_options with host info merged



78
79
80
81
82
83
84
85
86
87
# File 'lib/beaker/options/hosts_file_parser.rb', line 78

def self.merge_hosts_yaml( host_options, error_message, &block )
  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

.new_host_optionsOptionsHash

Convenience method to create new OptionsHashes with a HOSTS section

Returns:



54
55
56
57
58
# File 'lib/beaker/options/hosts_file_parser.rb', line 54

def self.new_host_options
  host_options = Beaker::Options::OptionsHash.new
  host_options['HOSTS'] ||= {}
  host_options
end

.parse_hosts_file(hosts_file_path = nil) ⇒ OptionsHash

Read the contents of the hosts.cfg into an OptionsHash, merge the ‘CONFIG’ section into the OptionsHash, return OptionsHash

Examples:

hosts_hash = HostsFileParser.parse_hosts_file('sample.cfg')
hosts_hash == {:HOSTS=>{:"pe-ubuntu-lucid"=>{:roles=>["agent", "dashboard", "database", "master"], ... }

Parameters:

  • hosts_file_path (String) (defaults to: nil)

    The path to the hosts file

Returns:

  • (OptionsHash)

    The contents of the hosts file as an OptionsHash

Raises:

  • (ArgumentError)

    Raises if hosts_file_path is not a valid YAML file

  • (Errno::ENOENT)

    File not found error: hosts_file doesn’t exist



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/beaker/options/hosts_file_parser.rb', line 17

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 ) {
    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)
  }
  fix_roles_array( host_options )
end

.parse_hosts_string(hosts_def_yaml = nil) ⇒ OptionsHash

Read the contents of a host definition as a string into an OptionsHash

Parameters:

  • hosts_def_yaml (String) (defaults to: nil)

    YAML hosts definition

Returns:

  • (OptionsHash)

    Contents of the hosts file as an OptionsHash

Raises:

  • (ArgumentError)

    If hosts_def_yaml is not a valid YAML string



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/beaker/options/hosts_file_parser.rb', line 39

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 ) {
    process_yaml(hosts_def_yaml, binding)
  }
  fix_roles_array( host_options )
end

.process_yaml(template, b) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A helper to parse the YAML file and apply ERB templating

Parameters:

  • path (String)

    Path to the file to read

  • b (Binding)

    The binding to pass to ERB rendering



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/beaker/options/hosts_file_parser.rb', line 94

def self.process_yaml(template, b)
  erb_obj = if RUBY_VERSION >= '2.7'
               ERB.new(template, trim_mode: '-')
             else
               ERB.new(template, nil, '-')
             end
  if RUBY_VERSION >= '2.6'
    YAML.safe_load(erb_obj.result(b), permitted_classes: PERMITTED_YAML_CLASSES)
  else
    YAML.load(erb_obj.result(b))
  end
end