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', 'Beaker::Logger', 'Beaker::Options::OptionsHash', 'Beaker::Platform', 'Beaker::Result', 'File', 'IO', 'Logger', 'Logger::Formatter', 'Logger::LogDevice', 'Monitor', 'Net::SSH::Prompt', 'StringifyHash', 'StringIO', 'Symbol', 'Time', ]
Class Method Summary collapse
-
.fix_roles_array(host_options) ⇒ Object
Make sure the roles array is present for all hosts.
-
.merge_hosts_yaml(host_options, error_message) ⇒ OptionsHash
Merges YAML read in the passed block into given OptionsHash.
-
.new_host_options ⇒ OptionsHash
Convenience method to create new OptionsHashes with a HOSTS section.
-
.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.
-
.parse_hosts_string(hosts_def_yaml = nil) ⇒ OptionsHash
Read the contents of a host definition as a string into an OptionsHash.
-
.process_yaml(template, b) ⇒ Object
private
A helper to parse the YAML file and apply ERB templating.
Class Method Details
.fix_roles_array(host_options) ⇒ Object
Make sure the roles array is present for all hosts
79 80 81 82 83 84 85 86 87 |
# File 'lib/beaker/options/hosts_file_parser.rb', line 79 def self.fix_roles_array( ) ['HOSTS'].each_key do |host| ['HOSTS'][host]['roles'] ||= [] end if .has_key?('CONFIG') = .merge(.delete('CONFIG')) end end |
.merge_hosts_yaml(host_options, error_message) ⇒ OptionsHash
Merges YAML read in the passed block into given OptionsHash
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/beaker/options/hosts_file_parser.rb', line 95 def self.merge_hosts_yaml( , ) begin = yield rescue Psych::SyntaxError => e << e.to_s raise ArgumentError, end .merge( ) end |
.new_host_options ⇒ OptionsHash
Convenience method to create new OptionsHashes with a HOSTS section
71 72 73 74 75 |
# File 'lib/beaker/options/hosts_file_parser.rb', line 71 def self. = Beaker::Options::OptionsHash.new ['HOSTS'] ||= {} 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
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/beaker/options/hosts_file_parser.rb', line 34 def self.parse_hosts_file(hosts_file_path = nil) require 'erb' = return unless hosts_file_path = "#{hosts_file_path} is not a valid YAML file\n\t" = self.merge_hosts_yaml( , ) { hosts_file_path = File.( 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( ) end |
.parse_hosts_string(hosts_def_yaml = nil) ⇒ OptionsHash
Read the contents of a host definition as a string into an OptionsHash
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/beaker/options/hosts_file_parser.rb', line 56 def self.parse_hosts_string(hosts_def_yaml = nil) require 'erb' = return unless hosts_def_yaml = "#{hosts_def_yaml}\nis not a valid YAML string\n\t" = self.merge_hosts_yaml( , ) { process_yaml(hosts_def_yaml, binding) } fix_roles_array( ) 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
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/beaker/options/hosts_file_parser.rb', line 111 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, aliases: true) else YAML.load(erb_obj.result(b)) # rubocop:disable Security/YAMLLoad end end |