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
12 def initialize(value_converter)
13   @value_converter = value_converter
14 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
22 def parse_file(file, text, allowed_section_names = [])
23   result = Conf.new
24   if !allowed_section_names.empty?
25     allowed_section_names << 'main' unless allowed_section_names.include?('main')
26   end
27 
28   ini = Puppet::Settings::IniFile.parse(text.encode(Encoding::UTF_8))
29   unique_sections_in(ini, file, allowed_section_names).each do |section_name|
30     section = Section.new(section_name.to_sym)
31     result.with_section(section)
32 
33     ini.lines_in(section_name).each do |line|
34       if line.is_a?(Puppet::Settings::IniFile::SettingLine)
35         parse_setting(line, section)
36       elsif line.text !~ /^\s*#|^\s*$/
37         raise Puppet::Settings::ParseError.new(_("Could not match line %{text}") % { text: line.text }, file, line.line_number)
38       end
39     end
40   end
41 
42   result
43 end

Private Instance Methods

empty_section() click to toggle source
    # File lib/puppet/settings/config_file.rb
118 def empty_section
119   { :_meta => {} }
120 end
extract_fileinfo(string) click to toggle source
    # File lib/puppet/settings/config_file.rb
122 def extract_fileinfo(string)
123   result = {}
124   value = string.sub(/\{\s*([^}]+)\s*\}/) do
125     params = $1
126     params.split(/\s*,\s*/).each do |str|
127       if str =~ /^\s*(\w+)\s*=\s*([\w]+)\s*$/
128         param, value = $1.intern, $2
129         result[param] = value
130         unless [:owner, :mode, :group].include?(param)
131           raise ArgumentError, _("Invalid file option '%{parameter}'") % { parameter: param }
132         end
133 
134         if param == :mode and value !~ /^\d+$/
135           raise ArgumentError, _("File modes must be numbers")
136         end
137       else
138         raise ArgumentError, _("Could not parse '%{string}'") % { string: string }
139       end
140     end
141     ''
142   end
143   result[:value] = value.sub(/\s*$/, '')
144   result
145 end
parse_setting(setting, section) click to toggle source
    # File lib/puppet/settings/config_file.rb
 99 def parse_setting(setting, section)
100   var = setting.name.intern
101   value = @value_converter[setting.value]
102 
103   # Check to see if this is a file argument and it has extra options
104   begin
105     options = extract_fileinfo(value) if value.is_a?(String)
106     if options
107       section.with_setting(var, options[:value], Meta.new(options[:owner],
108                                                           options[:group],
109                                                           options[:mode]))
110     else
111       section.with_setting(var, value, NO_META)
112     end
113   rescue Puppet::Error => detail
114     raise Puppet::Settings::ParseError.new(detail.message, file, setting.line_number, detail)
115   end
116 end
unique_sections_in(ini, file, allowed_section_names) click to toggle source
   # File lib/puppet/settings/config_file.rb
82 def unique_sections_in(ini, file, allowed_section_names)
83   ini.section_lines.collect do |section|
84     if !allowed_section_names.empty? && !allowed_section_names.include?(section.name)
85       error_location_str = Puppet::Util::Errors.error_location(file, section.line_number)
86       message = _("Illegal section '%{name}' in config file at %{error_location}.") %
87           { name: section.name, error_location: error_location_str }
88       #TRANSLATORS 'puppet.conf' is the name of the puppet configuration file and should not be translated.
89       message += ' ' + _("The only valid puppet.conf sections are: [%{allowed_sections_list}].") %
90           { allowed_sections_list: allowed_section_names.join(", ") }
91       message += ' ' + _("Please use the directory environments feature to specify environments.")
92       message += ' ' + _("(See https://puppet.com/docs/puppet/latest/environments_about.html)")
93       raise(Puppet::Error, message)
94     end
95     section.name
96   end.uniq
97 end