diff options
| author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-05-06 05:42:53 +0000 |
|---|---|---|
| committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-05-06 05:42:53 +0000 |
| commit | 494675b1dbf2f7141c201d203b197e03a30b72e3 (patch) | |
| tree | 0fc130b012161966829f907eb19f9b149ef5afd0 /lib/puppet | |
| parent | 1f8de9d0c211ac5641f5ad827a0f478fff6bd223 (diff) | |
Fixing #206 and #422. Executables will still look for the deprecated config files and load them using the old parse method, but they now prefer a single configuration file, and files can set parameters (owner, mode, group) in brackets on the same line.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2464 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/puppet')
| -rw-r--r-- | lib/puppet/configuration.rb | 2 | ||||
| -rw-r--r-- | lib/puppet/util/config.rb | 176 |
2 files changed, 160 insertions, 18 deletions
diff --git a/lib/puppet/configuration.rb b/lib/puppet/configuration.rb index 9489b9737..c6ca53d98 100644 --- a/lib/puppet/configuration.rb +++ b/lib/puppet/configuration.rb @@ -259,7 +259,7 @@ module Puppet # Define the config default. self.setdefaults(self.config[:name], - :config => ["$confdir/#{Puppet[:name]}.conf", + :config => ["$confdir/puppet.conf", "The configuration file for #{Puppet[:name]}."], :pidfile => ["", "The pid file"], :bindaddress => ["", "The address to bind to. Mongrel servers diff --git a/lib/puppet/util/config.rb b/lib/puppet/util/config.rb index 7731fdc5b..1bdff5eff 100644 --- a/lib/puppet/util/config.rb +++ b/lib/puppet/util/config.rb @@ -167,7 +167,7 @@ class Puppet::Util::Config # Handle a command-line argument. def handlearg(opt, value = nil) - value = mungearg(value) if value + value = munge_value(value) if value str = opt.sub(/^--/,'') bool = true newstr = str.sub(/^no-/, '') @@ -221,18 +221,6 @@ class Puppet::Util::Config end end - # Convert arguments appropriately. - def mungearg(value) - # Handle different data types correctly - return case value - when /^false$/i: false - when /^true$/i: true - when /^\d+$/i: Integer(value) - else - value.gsub(/^["']|["']$/,'').sub(/\s+$/, '') - end - end - # Return all of the parameters associated with a given section. def params(section) section = section.intern if section.is_a? String @@ -243,8 +231,24 @@ class Puppet::Util::Config } end - # Parse a configuration file. + # Parse the configuration file. def parse(file) + configmap = parse_file(file) + + # We know we want the 'main' section + if main = configmap[:main] + set_parameter_hash(main) + end + + # Otherwise, we only want our named section + if @config.include?(:name) and named = configmap[symbolize(self[:name])] + set_parameter_hash(named) + end + end + + # Parse the configuration file. As of May 2007, this is a backward-compatibility method and + # will be deprecated soon. + def old_parse(file) text = nil if file.is_a? Puppet::Util::LoadedFile @@ -253,8 +257,8 @@ class Puppet::Util::Config @file = Puppet::Util::LoadedFile.new(file) end - # Create a timer so that this. - settimer() + # Don't create a timer for the old style parsing. + # settimer() begin text = File.read(@file.file) @@ -284,7 +288,7 @@ class Puppet::Util::Config if var == :mode value = $2 else - value = mungearg($2) + value = munge_value($2) end # Only warn if we don't know what this config var is. This @@ -706,6 +710,144 @@ Generated on #{Time.now}. end end + private + + # Extra extra setting information for files. + def extract_fileinfo(string) + paramregex = %r{(\w+)\s*=\s*([\w\d]+)} + result = {} + string.scan(/\{\s*([^}]+)\s*\}/) do + params = $1 + params.split(/\s*,\s*/).each do |str| + if str =~ /^\s*(\w+)\s*=\s*([\w\w]+)\s*$/ + param, value = $1.intern, $2 + result[param] = value + unless [:owner, :mode, :group].include?(param) + raise Puppet::Error, "Invalid file option '%s'" % param + end + + if param == :mode and value !~ /^\d+$/ + raise Puppet::Error, "File modes must be numbers" + end + else + raise Puppet::Error, "Could not parse '%s'" % string + end + end + + return result + end + + return nil + end + + # Convert arguments into booleans, integers, or whatever. + def munge_value(value) + # Handle different data types correctly + return case value + when /^false$/i: false + when /^true$/i: true + when /^\d+$/i: Integer(value) + else + value.gsub(/^["']|["']$/,'').sub(/\s+$/, '') + end + end + + # This is an abstract method that just turns a file in to a hash of hashes. + # We mostly need this for backward compatibility -- as of May 2007 we need to + # support parsing old files with any section, or new files with just two + # valid sections. + def parse_file(file) + text = nil + + if file.is_a? Puppet::Util::LoadedFile + @file = file + else + @file = Puppet::Util::LoadedFile.new(file) + end + + # Create a timer so that this file will get checked automatically + # and reparsed if necessary. + settimer() + + begin + text = File.read(@file.file) + rescue Errno::ENOENT + raise Puppet::Error, "No such file %s" % file + rescue Errno::EACCES + raise Puppet::Error, "Permission denied to file %s" % file + end + + result = Hash.new { |names, name| + names[name] = {} + } + + count = 0 + + # Default to 'main' for the section. + section = :main + result[section][:_meta] = {} + text.split(/\n/).each { |line| + count += 1 + case line + when /^\[(\w+)\]$/: + section = $1.intern # Section names + # Add a meta section + result[section][:_meta] ||= {} + when /^\s*#/: next # Skip comments + when /^\s*$/: next # Skip blanks + when /^\s*(\w+)\s*=\s*(.+)$/: # settings + var = $1.intern + + # We don't want to munge modes, because they're specified in octal, so we'll + # just leave them as a String, since Puppet handles that case correctly. + if var == :mode + value = $2 + else + value = munge_value($2) + end + + # Check to see if this is a file argument and it has extra options + begin + if value.is_a?(String) and options = extract_fileinfo(value) + result[section][:_meta][var] = options + end + result[section][var] = value + rescue Puppet::Error => detail + detail.file = file + detail.line = line + raise + end + else + error = Puppet::Error.new("Could not match line %s" % line) + error.file = file + error.line = line + raise error + end + } + + return result + end + + # Take all members of a hash and assign their values appropriately. + def set_parameter_hash(params) + params.each do |param, value| + next if param == :_meta + unless @config.include?(param) + Puppet.warning "Discarded unknown configuration parameter %s" % param + next + end + self[param] = value + end + + if meta = params[:_meta] + meta.each do |var, values| + values.each do |param, value| + @config[var].send(param.to_s + "=", value) + end + end + end + end + # The base element type. class CElement attr_accessor :name, :section, :default, :parent, :setbycli |
