diff options
| author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-02-07 17:07:15 +0000 |
|---|---|---|
| committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-02-07 17:07:15 +0000 |
| commit | f1ffc34c0927840beeb21e1e2d864ce14de5d15e (patch) | |
| tree | b68a8795301d04393e56e540bb61ba73791a47d2 /lib/puppet | |
| parent | 6affe220db1248cee8489347dc7d7ac071a534e4 (diff) | |
| download | puppet-f1ffc34c0927840beeb21e1e2d864ce14de5d15e.tar.gz puppet-f1ffc34c0927840beeb21e1e2d864ce14de5d15e.tar.xz puppet-f1ffc34c0927840beeb21e1e2d864ce14de5d15e.zip | |
Configuration parameters now require (and have) descriptions, and a set of configuration parameters can be converted to a configuration file, a manifest, or a component. All I have to do now is integrate them into the executables.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@872 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/puppet')
| -rw-r--r-- | lib/puppet/client/master.rb | 4 | ||||
| -rw-r--r-- | lib/puppet/config.rb | 236 | ||||
| -rw-r--r-- | lib/puppet/parser/ast.rb | 6 | ||||
| -rw-r--r-- | lib/puppet/parser/ast/objectdef.rb | 2 | ||||
| -rwxr-xr-x | lib/puppet/server/fileserver.rb | 4 | ||||
| -rw-r--r-- | lib/puppet/sslcertificates/ca.rb | 119 | ||||
| -rw-r--r-- | lib/puppet/transportable.rb | 1 |
7 files changed, 220 insertions, 152 deletions
diff --git a/lib/puppet/client/master.rb b/lib/puppet/client/master.rb index 29e466224..9ea24d502 100644 --- a/lib/puppet/client/master.rb +++ b/lib/puppet/client/master.rb @@ -85,9 +85,9 @@ class Puppet::Client::MasterClient < Puppet::Client Puppet::Storage.init Puppet::Storage.load rescue => detail - Puppet.err "Corrupt state file %s: %s" % [Puppet[:checksumfile], detail] + Puppet.err "Corrupt state file %s: %s" % [Puppet[:statefile], detail] begin - File.unlink(Puppet[:checksumfile]) + File.unlink(Puppet[:statefile]) retry rescue => detail raise Puppet::Error.new("Cannot remove %s: %s" % diff --git a/lib/puppet/config.rb b/lib/puppet/config.rb index 84ebb6a55..295bf2035 100644 --- a/lib/puppet/config.rb +++ b/lib/puppet/config.rb @@ -1,6 +1,8 @@ module Puppet # The class for handling configuration files. class Config + include Enumerable + # Retrieve a config value def [](param) param = param.intern unless param.is_a? Symbol @@ -18,7 +20,8 @@ class Config def []=(param, value) param = param.intern unless param.is_a? Symbol unless @config.include?(param) - @config[param] = newelement(param, value) + raise Puppet::Error, "Unknown configuration parameter %s" % param + #@config[param] = newelement(param, value) end unless @order.include?(param) @order << param @@ -26,6 +29,17 @@ class Config @config[param].value = value end + # A simplified equality operator. + def ==(other) + self.each { |myname, myobj| + unless other[myname] == myobj.value + return false + end + } + + return true + end + # Remove all set values. def clear @config.each { |name, obj| @@ -43,6 +57,22 @@ class Config } end + # Iterate over each section name. + def eachsection + yielded = [] + @order.each { |name| + if @config.include?(name) + section = @config[name].section + unless yielded.include? section + yield section + yielded << section + end + else + raise Puppet::DevError, "%s is in the order but does not exist" % name + end + } + end + # Return an object by name. def element(param) param = param.intern unless param.is_a? Symbol @@ -55,6 +85,15 @@ class Config @config = {} end + # Return all of the parameters associated with a given section. + def params(section) + @config.find_all { |name, obj| + obj.section == section + }.collect { |name, obj| + name + } + end + # Parse a configuration file. def parse(file) text = nil @@ -76,7 +115,7 @@ class Config } section = "puppet" - metas = %w{user group mode} + metas = %w{owner group mode} values = Hash.new { |hash, key| hash[key] = {} } text.split(/\n/).each { |line| case line @@ -115,18 +154,18 @@ class Config # Create a new element. The value is passed in because it's used to determine # what kind of element we're creating, but the value itself might be either # a default or a value, so we can't actually assign it. - def newelement(param, value) + def newelement(param, desc, value) mod = nil case value when true, false, "true", "false": mod = CBoolean when /^\$/, /^\//: mod = CFile - when String: # nothing + when String, Integer, Float: # nothing else - raise Puppet::Error, "Invalid value '%s'" % value + raise Puppet::Error, "Invalid value '%s' for %s" % [value, param] end - element = CElement.new(param) + element = CElement.new(param, desc) element.parent = self if mod element.extend(mod) @@ -135,16 +174,78 @@ class Config return element end + def persection(section) + self.each { |name, obj| + if obj.section == section + yield obj + end + } + end + + # Get a list of objects per section + def sectionlist + sectionlist = [] + self.each { |name, obj| + section = obj.section || "puppet" + sections[section] ||= [] + unless sectionlist.include?(section) + sectionlist << section + end + sections[section] << obj + } + + return sectionlist, sections + end + + # Convert a single section into transportable objects. + def section_to_transportable(section, done) + objects = [] + persection(section) { |obj| + [:owner, :group].each { |type| + if obj.respond_to? type and val = obj.send(type) + # Skip owners and groups we've already done, but tag them with + # our section if necessary + if done[type].include?(val) + next unless defined? @section and @section + + tags = done[type][val].tags + unless tags.include?(@section) + done[type][val].tags = tags << @section + end + else + newobj = TransObject.new(val, type.to_s) + newobj[:ensure] = "exists" + done[type] << newobj + end + end + } + + if obj.respond_to? :to_transportable + objects << obj.to_transportable + end + } + + bucket = Puppet::TransBucket.new + bucket.autoname = true + bucket.name = "autosection-%s" % bucket.object_id + bucket.type = section + bucket.push(*objects) + bucket.keyword = "class" + + return bucket + end + # Set a bunch of defaults in a given section. The sections are actually pretty # pointless, but they help break things up a bit, anyway. - def setdefaults(section, hash) + def setdefaults(section, *defs) section = section.intern unless section.is_a? Symbol - hash.each { |param, value| + #hash.each { |param, value| + defs.each { |param, value, desc| if @config.include?(param) and @config[param].default raise Puppet::Error, "Default %s is already defined" % param end unless @config.include?(param) - @config[param] = newelement(param, value) + @config[param] = newelement(param, desc, value) end @config[param].default = value @config[param].section = section @@ -155,35 +256,27 @@ class Config def to_component transport = self.to_transportable return transport.to_type -# comp = Puppet.type(:component).create( -# :name => "PuppetConfig" -# ) -# self.to_objects.each { |hash| -# type = hash[:type] -# hash.delete(:name) -# comp.push Puppet.type(type).create(hash) -# } -# -# return comp + end + + # Convert our list of objects into a configuration file. + def to_config + str = "" + eachsection do |section| + str += "[#{section}]\n" + persection(section) do |obj| + str += obj.to_s + "\n" + end + end + + return str end # Convert our configuration into a list of transportable objects. def to_transportable - objects = [] done = { - :user => [], + :owner => [], :group => [], } - sections = {} - sectionlist = [] - self.each { |name, obj| - section = obj.section || "puppet" - sections[section] ||= [] - unless sectionlist.include?(section) - sectionlist << section - end - sections[section] << obj - } topbucket = Puppet::TransBucket.new if defined? @file and @file @@ -194,69 +287,11 @@ class Config topbucket.type = "puppetconfig" topbucket.top = true topbucket.autoname = true - sectionlist.each { |section| - objects = [] - sections[section].each { |obj| - Puppet.notice "changing %s" % obj.name - [:user, :group].each { |type| - if obj.respond_to? type and val = obj.send(type) - # Skip users and groups we've already done, but tag them with - # our section if necessary - if done[type].include?(val) - next unless defined? @section and @section - - tags = done[type][val].tags - unless tags.include?(@section) - done[type][val].tags = tags << @section - end - else - newobj = TransObject.new(val, type.to_s) - newobj[:ensure] = "exists" - done[type] << newobj - end - end - } - if obj.respond_to? :to_transportable - objects << obj.to_transportable - else - Puppet.notice "%s is not transportable" % obj.name - end - } - - bucket = Puppet::TransBucket.new - bucket.autoname = true - bucket.name = "autosection-%s" % bucket.object_id - bucket.type = section - bucket.push(*objects) - bucket.keyword = "class" - - topbucket.push bucket - } -# self.each { |name, obj| -# [:user, :group].each { |type| -# if obj.respond_to? type and val = obj.send(type) -# # Skip users and groups we've already done, but tag them with -# # our section if necessary -# if done[type].include?(val) -# next unless defined? @section and @section -# -# tags = done[type][val].tags -# unless tags.include?(@section) -# done[type][val].tags = tags << @section -# end -# else -# obj = TransObject.new(val, type.to_s) -# obj[:ensure] = "exists" -# done[type] << obj -# end -# end -# } -# -# if obj.respond_to? :to_transportable -# objects << obj.to_transportable -# end -# } + # Now iterate over each section + eachsection do |section| + topbucket.push section_to_transportable(section, done) + end topbucket end @@ -269,7 +304,7 @@ class Config # The base element type. class CElement - attr_accessor :name, :section, :default, :parent + attr_accessor :name, :section, :default, :parent, :desc # Unset any set value. def clear @@ -277,13 +312,19 @@ class Config end # Create the new element. Pretty much just sets the name. - def initialize(name, value = nil) + def initialize(name, desc, value = nil) @name = name + @desc = desc if value @value = value end end + def to_s + str = @desc.gsub(/^/, " # ") + + "\n %s = %s" % [@name, self.value] + end + # Retrieves the value, or if it's not set, retrieves the default. def value retval = nil @@ -317,7 +358,7 @@ class Config # A file. module CFile - attr_accessor :user, :group, :mode, :type + attr_accessor :owner, :group, :mode, :type def convert(value) unless value @@ -351,10 +392,9 @@ class Config end def to_transportable - Puppet.notice "transportabling %s" % self.name obj = Puppet::TransObject.new(self.value, "file") obj[:ensure] = self.type - [:user, :group, :mode].each { |var| + [:owner, :group, :mode].each { |var| if value = self.send(var) obj[var] = value end diff --git a/lib/puppet/parser/ast.rb b/lib/puppet/parser/ast.rb index c8cc74f04..5eb4ebaa2 100644 --- a/lib/puppet/parser/ast.rb +++ b/lib/puppet/parser/ast.rb @@ -11,8 +11,10 @@ module Puppet # Do this so I don't have to type the full path in all of the subclasses AST = Puppet::Parser::AST - Puppet.setdefault(:typecheck, true) - Puppet.setdefault(:paramcheck, true) + Puppet.setdefaults("ast", + [:typecheck, true, "Whether to validate types during parsing."], + [:paramcheck, true, "Whether to validate parameters during parsing."] + ) attr_accessor :line, :file, :parent # Just used for 'tree', which is only used in debugging. diff --git a/lib/puppet/parser/ast/objectdef.rb b/lib/puppet/parser/ast/objectdef.rb index dc355be77..8b40bda71 100644 --- a/lib/puppet/parser/ast/objectdef.rb +++ b/lib/puppet/parser/ast/objectdef.rb @@ -221,7 +221,7 @@ class Puppet::Parser::AST rescue => detail raise Puppet::DevError, detail.to_s end - next if pname == "name" # always allow these + return if pname == "name" # always allow these unless type.validattr?(pname) error = Puppet::ParseError.new( "Invalid parameter '%s' for type '%s'" % diff --git a/lib/puppet/server/fileserver.rb b/lib/puppet/server/fileserver.rb index 3349256ba..27e4d814a 100755 --- a/lib/puppet/server/fileserver.rb +++ b/lib/puppet/server/fileserver.rb @@ -8,7 +8,9 @@ class Server class FileServer < Handler attr_accessor :local - Puppet.setdefault(:fileserverconfig, [:puppetconf, "fileserver.conf"]) + Puppet.setdefaults("fileserver", + [:fileserverconfig, "$puppetconf/fileserver.conf", + "Where the fileserver configuration is stored."]) #CHECKPARAMS = %w{checksum type mode owner group} CHECKPARAMS = [:mode, :type, :owner, :group, :checksum] diff --git a/lib/puppet/sslcertificates/ca.rb b/lib/puppet/sslcertificates/ca.rb index 0137e15eb..40b34e1ee 100644 --- a/lib/puppet/sslcertificates/ca.rb +++ b/lib/puppet/sslcertificates/ca.rb @@ -2,52 +2,77 @@ class Puppet::SSLCertificates::CA Certificate = Puppet::SSLCertificates::Certificate attr_accessor :keyfile, :file, :config, :dir, :cert - @@params = [ - :certdir, - :publickeydir, - :privatekeydir, - :cadir, - :cakey, - :cacert, - :capass, - :capub, - :csrdir, - :signeddir, - :serial, - :privatedir, - :ca_crl_days, - :ca_days, - :ca_md, - :req_bits, - :keylength, - :autosign - ] - - @@defaults = { - :certdir => [:ssldir, "certs"], - :publickeydir => [:ssldir, "public_keys"], - :privatekeydir => [:ssldir, "private_keys"], - :cadir => [:ssldir, "ca"], - :cacert => [:cadir, "ca_crt.pem"], - :cakey => [:cadir, "ca_key.pem"], - :capub => [:cadir, "ca_pub.pem"], - :csrdir => [:cadir, "requests"], - :signeddir => [:cadir, "signed"], - :capass => [:cadir, "ca.pass"], - :serial => [:cadir, "serial"], - :privatedir => [:ssldir, "private"], - :passfile => [:privatedir, "password"], - :autosign => [:puppetconf, "autosign.conf"], - :ca_crl_days => 365, - :ca_days => 1825, - :ca_md => "md5", - :req_bits => 2048, - :keylength => 1024, - } - - @@params.each { |param| - Puppet.setdefault(param,@@defaults[param]) - } +# @@params = [ +# :certdir, +# :publickeydir, +# :privatekeydir, +# :cadir, +# :cakey, +# :cacert, +# :capass, +# :capub, +# :csrdir, +# :signeddir, +# :serial, +# :privatedir, +# :ca_crl_days, +# :ca_days, +# :ca_md, +# :req_bits, +# :keylength, +# :autosign +# ] +# :certdir => [:ssldir, "certs"], +# :publickeydir => [:ssldir, "public_keys"], +# :privatekeydir => [:ssldir, "private_keys"], +# :cadir => [:ssldir, "ca"], +# :cacert => [:cadir, "ca_crt.pem"], +# :cakey => [:cadir, "ca_key.pem"], +# :capub => [:cadir, "ca_pub.pem"], +# :csrdir => [:cadir, "requests"], +# :signeddir => [:cadir, "signed"], +# :capass => [:cadir, "ca.pass"], +# :serial => [:cadir, "serial"], +# :privatedir => [:ssldir, "private"], +# :passfile => [:privatedir, "password"], +# :autosign => [:puppetconf, "autosign.conf"], +# :ca_crl_days => 365, +# :ca_days => 1825, +# :ca_md => "md5", +# :req_bits => 2048, +# :keylength => 1024, + + Puppet.setdefaults("ca", + [:certdir, "$ssldir/certs", "The certificate directory."], + [:publickeydir, "$ssldir/public_keys", "The public key directory."], + [:privatekeydir, "$ssldir/private_keys", "The private key directory."], + [:cadir, "$ssldir/ca", + "The root directory for the certificate authority."], + [:cacert, "$cadir/ca_crt.pem", "The CA certificate."], + [:cakey, "$cadir/ca_key.pem", "The CA private key."], + [:capub, "$cadir/ca_pub.pem", "The CA public key."], + [:csrdir, "$cadir/requests", + "Where the CA stores certificate requests"], + [:signeddir, "$cadir/signed", + "Where the CA stores signed certificates."], + [:capass, "$cadir/ca.pass", + "Where the CA stores the password for the private key; usually not used."], + [:serial, "$cadir/serial", + "Where the serial number for certificates is stored."], + [:passfile, "$privatedir/password", + "Where puppetd stores the password for its private key. Generally + unused."], + [:autosign, "$puppetconf/autosign.conf", + "Where to look for the autosigning configuration file."], + [:ca_days, 1825, "How long a certificate should be valid."], + [:ca_md, "md5", "The type of hash used in certificates."], + [:req_bits, 2048, "The bit length of the certificates."], + [:keylength, 1024, "The bit length of keys."] + ) + + #@@params.each { |param| + # Puppet.setdefault(param,@@defaults[param]) + #} def certfile @config[:cacert] @@ -161,7 +186,7 @@ class Puppet::SSLCertificates::CA def setconfig(hash) @config = {} - @@params.each { |param| + Puppet.config.params("ca").each { |param| if hash.include?(param) begin @config[param] = hash[param] diff --git a/lib/puppet/transportable.rb b/lib/puppet/transportable.rb index 405179808..128a06a84 100644 --- a/lib/puppet/transportable.rb +++ b/lib/puppet/transportable.rb @@ -140,7 +140,6 @@ module Puppet str = "#{@keyword} #{@type} {\n%s\n}" end str % @children.collect { |child| - Puppet.info "manifesting %s" % child.name child.to_manifest }.collect { |str| if self.top |
