class Puppet::Settings::ConfigFile
@api private
Parses puppet configuration files
Constants
- Conf
- Meta
- NO_META
- Section
- Setting
Public Class Methods
new(value_converter)
click to toggle source
@param value_converter [Proc] a function that will convert strings into ruby types
# File lib/puppet/settings/config_file.rb 13 def initialize(value_converter) 14 @value_converter = value_converter 15 end
Public Instance Methods
parse_file(file, text, allowed_section_names = [])
click to toggle source
@param file [String, File] pointer to the file whose text we are parsing @param text [String] the actual text of the inifile to be parsed @param allowed_section_names [Array] an optional array of accepted section
names; if this list is non-empty, sections outside of it will raise an error.
@return A Struct with a sections array representing each configuration section
# File lib/puppet/settings/config_file.rb 23 def parse_file(file, text, allowed_section_names = []) 24 result = Conf.new 25 if !allowed_section_names.empty? 26 allowed_section_names << 'main' unless allowed_section_names.include?('main') 27 end 28 29 ini = Puppet::Settings::IniFile.parse(text.encode(Encoding::UTF_8)) 30 unique_sections_in(ini, file, allowed_section_names).each do |section_name| 31 section = Section.new(section_name.to_sym) 32 result.with_section(section) 33 34 ini.lines_in(section_name).each do |line| 35 if line.is_a?(Puppet::Settings::IniFile::SettingLine) 36 parse_setting(line, section) 37 elsif line.text !~ /^\s*#|^\s*$/ 38 raise Puppet::Settings::ParseError.new(_("Could not match line %{text}") % { text: line.text }, file, line.line_number) 39 end 40 end 41 end 42 43 result 44 end
Private Instance Methods
empty_section()
click to toggle source
# File lib/puppet/settings/config_file.rb 119 def empty_section 120 { :_meta => {} } 121 end
extract_fileinfo(string)
click to toggle source
# File lib/puppet/settings/config_file.rb 123 def extract_fileinfo(string) 124 result = {} 125 value = string.sub(/\{\s*([^}]+)\s*\}/) do 126 params = $1 127 params.split(/\s*,\s*/).each do |str| 128 if str =~ /^\s*(\w+)\s*=\s*([\w]+)\s*$/ 129 param, value = $1.intern, $2 130 result[param] = value 131 unless [:owner, :mode, :group].include?(param) 132 raise ArgumentError, _("Invalid file option '%{parameter}'") % { parameter: param } 133 end 134 135 if param == :mode and value !~ /^\d+$/ 136 raise ArgumentError, _("File modes must be numbers") 137 end 138 else 139 raise ArgumentError, _("Could not parse '%{string}'") % { string: string } 140 end 141 end 142 '' 143 end 144 result[:value] = value.sub(/\s*$/, '') 145 result 146 end
parse_setting(setting, section)
click to toggle source
# File lib/puppet/settings/config_file.rb 100 def parse_setting(setting, section) 101 var = setting.name.intern 102 value = @value_converter[setting.value] 103 104 # Check to see if this is a file argument and it has extra options 105 begin 106 options = extract_fileinfo(value) if value.is_a?(String) 107 if options 108 section.with_setting(var, options[:value], Meta.new(options[:owner], 109 options[:group], 110 options[:mode])) 111 else 112 section.with_setting(var, value, NO_META) 113 end 114 rescue Puppet::Error => detail 115 raise Puppet::Settings::ParseError.new(detail.message, file, setting.line_number, detail) 116 end 117 end
unique_sections_in(ini, file, allowed_section_names)
click to toggle source
# File lib/puppet/settings/config_file.rb 83 def unique_sections_in(ini, file, allowed_section_names) 84 ini.section_lines.collect do |section| 85 if !allowed_section_names.empty? && !allowed_section_names.include?(section.name) 86 error_location_str = Puppet::Util::Errors.error_location(file, section.line_number) 87 message = _("Illegal section '%{name}' in config file at %{error_location}.") % 88 { name: section.name, error_location: error_location_str } 89 #TRANSLATORS 'puppet.conf' is the name of the puppet configuration file and should not be translated. 90 message += ' ' + _("The only valid puppet.conf sections are: [%{allowed_sections_list}].") % 91 { allowed_sections_list: allowed_section_names.join(", ") } 92 message += ' ' + _("Please use the directory environments feature to specify environments.") 93 message += ' ' + _("(See https://puppet.com/docs/puppet/latest/environments_about.html)") 94 raise(Puppet::Error, message) 95 end 96 section.name 97 end.uniq 98 end