diff options
| author | Michael V. O'Brien <michael@reductivelabs.com> | 2007-09-25 17:23:36 -0500 |
|---|---|---|
| committer | Michael V. O'Brien <michael@reductivelabs.com> | 2007-09-25 17:23:36 -0500 |
| commit | 93f64885100eecb4c235d08e1f9cd266e6d789ad (patch) | |
| tree | a5ab7d8236ffc84ef05124d03c1cb9db89baf4a9 /lib | |
| parent | df1879b814c25cd3564abaa3064e0cdd6ef50eb4 (diff) | |
| parent | fa643e61c7451c2c46623d2c801a42c6c7640e1e (diff) | |
| download | puppet-93f64885100eecb4c235d08e1f9cd266e6d789ad.tar.gz puppet-93f64885100eecb4c235d08e1f9cd266e6d789ad.tar.xz puppet-93f64885100eecb4c235d08e1f9cd266e6d789ad.zip | |
Merge branch 'master' of git://reductivelabs.com/puppet
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/puppet/fact_stores/yaml.rb | 40 | ||||
| -rw-r--r-- | lib/puppet/indirector/code/facts.rb | 21 | ||||
| -rw-r--r-- | lib/puppet/indirector/indirection.rb | 32 | ||||
| -rw-r--r-- | lib/puppet/indirector/terminus.rb | 6 | ||||
| -rw-r--r-- | lib/puppet/indirector/yaml/configuration.rb | 5 | ||||
| -rw-r--r-- | lib/puppet/indirector/yaml/node.rb | 5 | ||||
| -rw-r--r-- | lib/puppet/node_source/external.rb | 51 | ||||
| -rw-r--r-- | lib/puppet/node_source/ldap.rb | 138 | ||||
| -rw-r--r-- | lib/puppet/node_source/none.rb | 10 |
9 files changed, 66 insertions, 242 deletions
diff --git a/lib/puppet/fact_stores/yaml.rb b/lib/puppet/fact_stores/yaml.rb deleted file mode 100644 index b33e162ba..000000000 --- a/lib/puppet/fact_stores/yaml.rb +++ /dev/null @@ -1,40 +0,0 @@ -Puppet::Util::FactStore.newstore(:yaml) do - desc "Store client facts as flat files, serialized using YAML." - - # Get a client's facts. - def get(node) - file = path(node) - - return nil unless FileTest.exists?(file) - - begin - facts = YAML::load(File.read(file)) - rescue => detail - Puppet.err "Could not load facts for %s: %s" % [node, detail] - end - facts - end - - def initialize - Puppet.settings.use(:yamlfacts) - end - - # Store the facts to disk. - def set(node, facts) - File.open(path(node), "w", 0600) do |f| - begin - f.print YAML::dump(facts) - rescue => detail - Puppet.err "Could not write facts for %s: %s" % [node, detail] - end - end - nil - end - - private - - # Return the path to a given node's file. - def path(node) - File.join(Puppet[:yamlfactdir], node + ".yaml") - end -end diff --git a/lib/puppet/indirector/code/facts.rb b/lib/puppet/indirector/code/facts.rb new file mode 100644 index 000000000..b64e9854e --- /dev/null +++ b/lib/puppet/indirector/code/facts.rb @@ -0,0 +1,21 @@ +require 'puppet/node/facts' +require 'puppet/indirector/code' + +class Puppet::Indirector::Code::Facts < Puppet::Indirector::Code + desc "Retrieve facts from Facter. This provides a somewhat abstract interface + between Puppet and Facter. It's only 'somewhat' abstract because it always + returns the local host's facts, regardless of what you attempt to find." + + def destroy(facts) + raise Puppet::DevError, "You cannot destroy facts in the code store; it is only used for getting facts from Facter" + end + + # Look a host's facts up in Facter. + def find(key) + Puppet::Node::Facts.new(key, Facter.to_hash) + end + + def save(facts) + raise Puppet::DevError, "You cannot save facts to the code store; it is only used for getting facts from Facter" + end +end diff --git a/lib/puppet/indirector/indirection.rb b/lib/puppet/indirector/indirection.rb index 8afe0012d..9cc116e40 100644 --- a/lib/puppet/indirector/indirection.rb +++ b/lib/puppet/indirector/indirection.rb @@ -15,9 +15,36 @@ class Puppet::Indirector::Indirection attr_accessor :name, :model - # Clear our cached list of termini. + # Create and return our cache terminus. + def cache + terminus(cache_name) + end + + # Should we use a cache? + def cache? + cache_name ? true : false + end + + # Figure out the cache name, if there is one. If there's no name, then + # caching is disabled. + def cache_name + unless @cache_name + setting_name = "%s_cache" % self.name + if ! Puppet.settings.valid?(setting_name) or (value = Puppet.settings[setting_name] and value == "none") + @cache_name = nil + else + @cache_name = value + @cache_name = @cache_name.intern if @cache_name.is_a?(String) + end + end + @cache_name + end + + # Clear our cached list of termini, and reset the cache name + # so it's looked up again. # This is only used for testing. def clear_cache + @cache_name = nil @termini.clear end @@ -38,6 +65,7 @@ class Puppet::Indirector::Indirection end @termini = {} @terminus_types = {} + @cache_name = nil raise(ArgumentError, "Indirection %s is already defined" % @name) if @@indirections.find { |i| i.name == @name } @@indirections << self end @@ -66,6 +94,7 @@ class Puppet::Indirector::Indirection end def destroy(*args) + cache.destroy(*args) if cache? terminus.destroy(*args) end @@ -75,6 +104,7 @@ class Puppet::Indirector::Indirection # these become instance methods def save(*args) + cache.save(*args) if cache? terminus.save(*args) end diff --git a/lib/puppet/indirector/terminus.rb b/lib/puppet/indirector/terminus.rb index bcff08d79..3e0ea447c 100644 --- a/lib/puppet/indirector/terminus.rb +++ b/lib/puppet/indirector/terminus.rb @@ -26,7 +26,7 @@ class Puppet::Indirector::Terminus elsif ind = Puppet::Indirector::Indirection.instance(name) @indirection = ind else - raise ArgumentError, "Could not find indirection instance %s" % name + raise ArgumentError, "Could not find indirection instance %s for %s" % [name, self.name] end end @@ -39,7 +39,9 @@ class Puppet::Indirector::Terminus raise ArgumentError, "Terminus subclasses must have associated constants" end names = longname.split("::") - name = names.pop.downcase.intern + + # Convert everything to a lower-case symbol, converting camelcase to underscore word separation. + name = names.pop.sub(/^[A-Z]/) { |i| i.downcase }.gsub(/[A-Z]/) { |i| "_" + i.downcase }.intern subclass.name = name diff --git a/lib/puppet/indirector/yaml/configuration.rb b/lib/puppet/indirector/yaml/configuration.rb new file mode 100644 index 000000000..691f0e343 --- /dev/null +++ b/lib/puppet/indirector/yaml/configuration.rb @@ -0,0 +1,5 @@ +require 'puppet/indirector/yaml' + +class Puppet::Indirector::Yaml::Configuration < Puppet::Indirector::Yaml + desc "Store configurations as flat files, serialized using YAML." +end diff --git a/lib/puppet/indirector/yaml/node.rb b/lib/puppet/indirector/yaml/node.rb new file mode 100644 index 000000000..62fef58ac --- /dev/null +++ b/lib/puppet/indirector/yaml/node.rb @@ -0,0 +1,5 @@ +require 'puppet/indirector/yaml' + +class Puppet::Indirector::Yaml::Node < Puppet::Indirector::Yaml + desc "Store node information as flat files, serialized using YAML." +end diff --git a/lib/puppet/node_source/external.rb b/lib/puppet/node_source/external.rb deleted file mode 100644 index 54111d924..000000000 --- a/lib/puppet/node_source/external.rb +++ /dev/null @@ -1,51 +0,0 @@ -Puppet::Network::Handler::Node.newnode_source(:external, :fact_merge => true) do - desc "Call an external program to get node information." - - include Puppet::Util - # Look for external node definitions. - def nodesearch(name) - return nil unless Puppet[:external_nodes] != "none" - - # This is a very cheap way to do this, since it will break on - # commands that have spaces in the arguments. But it's good - # enough for most cases. - external_node_command = Puppet[:external_nodes].split - external_node_command << name - begin - output = Puppet::Util.execute(external_node_command) - rescue Puppet::ExecutionFailure => detail - if $?.exitstatus == 1 - return nil - else - Puppet.err "Could not retrieve external node information for %s: %s" % [name, detail] - end - return nil - end - - if output =~ /\A\s*\Z/ # all whitespace - Puppet.debug "Empty response for %s from external node source" % name - return nil - end - - begin - result = YAML.load(output).inject({}) { |hash, data| hash[symbolize(data[0])] = data[1]; hash } - rescue => detail - raise Puppet::Error, "Could not load external node results for %s: %s" % [name, detail] - end - - node = newnode(name) - set = false - [:parameters, :classes].each do |param| - if value = result[param] - node.send(param.to_s + "=", value) - set = true - end - end - - if set - return node - else - return nil - end - end -end diff --git a/lib/puppet/node_source/ldap.rb b/lib/puppet/node_source/ldap.rb deleted file mode 100644 index 7b60a3c62..000000000 --- a/lib/puppet/node_source/ldap.rb +++ /dev/null @@ -1,138 +0,0 @@ -Puppet::Network::Handler::Node.newnode_source(:ldap, :fact_merge => true) do - desc "Search in LDAP for node configuration information." - - # Find the ldap node, return the class list and parent node specially, - # and everything else in a parameter hash. - def ldapsearch(node) - filter = Puppet[:ldapstring] - classattrs = Puppet[:ldapclassattrs].split("\s*,\s*") - if Puppet[:ldapattrs] == "all" - # A nil value here causes all attributes to be returned. - search_attrs = nil - else - search_attrs = classattrs + Puppet[:ldapattrs].split("\s*,\s*") - end - pattr = nil - if pattr = Puppet[:ldapparentattr] - if pattr == "" - pattr = nil - else - search_attrs << pattr unless search_attrs.nil? - end - end - - if filter =~ /%s/ - filter = filter.gsub(/%s/, node) - end - - parent = nil - classes = [] - parameters = nil - - found = false - count = 0 - - begin - # We're always doing a sub here; oh well. - ldap.search(Puppet[:ldapbase], 2, filter, search_attrs) do |entry| - found = true - if pattr - if values = entry.vals(pattr) - if values.length > 1 - raise Puppet::Error, - "Node %s has more than one parent: %s" % - [node, values.inspect] - end - unless values.empty? - parent = values.shift - end - end - end - - classattrs.each { |attr| - if values = entry.vals(attr) - values.each do |v| classes << v end - end - } - - parameters = entry.to_hash.inject({}) do |hash, ary| - if ary[1].length == 1 - hash[ary[0]] = ary[1].shift - else - hash[ary[0]] = ary[1] - end - hash - end - end - rescue => detail - if count == 0 - # Try reconnecting to ldap - @ldap = nil - retry - else - raise Puppet::Error, "LDAP Search failed: %s" % detail - end - end - - classes.flatten! - - if classes.empty? - classes = nil - end - - if parent or classes or parameters - return parent, classes, parameters - else - return nil - end - end - - # Look for our node in ldap. - def nodesearch(node) - unless ary = ldapsearch(node) - return nil - end - parent, classes, parameters = ary - - while parent - parent, tmpclasses, tmpparams = ldapsearch(parent) - classes += tmpclasses if tmpclasses - tmpparams.each do |param, value| - # Specifically test for whether it's set, so false values are handled - # correctly. - parameters[param] = value unless parameters.include?(param) - end - end - - return newnode(node, :classes => classes, :source => "ldap", :parameters => parameters) - end - - private - - # Create an ldap connection. - def ldap - unless defined? @ldap and @ldap - unless Puppet.features.ldap? - raise Puppet::Error, "Could not set up LDAP Connection: Missing ruby/ldap libraries" - end - begin - if Puppet[:ldapssl] - @ldap = LDAP::SSLConn.new(Puppet[:ldapserver], Puppet[:ldapport]) - elsif Puppet[:ldaptls] - @ldap = LDAP::SSLConn.new( - Puppet[:ldapserver], Puppet[:ldapport], true - ) - else - @ldap = LDAP::Conn.new(Puppet[:ldapserver], Puppet[:ldapport]) - end - @ldap.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3) - @ldap.set_option(LDAP::LDAP_OPT_REFERRALS, LDAP::LDAP_OPT_ON) - @ldap.simple_bind(Puppet[:ldapuser], Puppet[:ldappassword]) - rescue => detail - raise Puppet::Error, "Could not connect to LDAP: %s" % detail - end - end - - return @ldap - end -end diff --git a/lib/puppet/node_source/none.rb b/lib/puppet/node_source/none.rb deleted file mode 100644 index ce188add5..000000000 --- a/lib/puppet/node_source/none.rb +++ /dev/null @@ -1,10 +0,0 @@ -Puppet::Network::Handler::Node.newnode_source(:none, :fact_merge => true) do - desc "Always return an empty node object. This is the node source you should - use when you don't have some other, functional source you want to use, - as the compiler will not work without this node information." - - # Just return an empty node. - def nodesearch(name) - newnode(name) - end -end |
