summaryrefslogtreecommitdiffstats
path: root/lib/puppet/indirector/facts
diff options
context:
space:
mode:
authorNick Lewis <nick@puppetlabs.com>2011-03-03 14:12:44 -0800
committerNick Lewis <nick@puppetlabs.com>2011-03-03 16:52:46 -0800
commitc3baa2899d88fadd4bbe94e008015e33f98132c7 (patch)
treec19576706966abf9a0dff04731a74ecc84735c04 /lib/puppet/indirector/facts
parent63d8a1f83f4ad109817990354f05879f44419137 (diff)
downloadpuppet-c3baa2899d88fadd4bbe94e008015e33f98132c7.tar.gz
puppet-c3baa2899d88fadd4bbe94e008015e33f98132c7.tar.xz
puppet-c3baa2899d88fadd4bbe94e008015e33f98132c7.zip
(#6338) Remove inventory indirection, and move to facts indirection
The inventory indirection was just providing the search method for facts. Because the route is now facts_search instead of inventory, it can just be implemented as the search method for facts. Reviewed-By: Daniel Pittman
Diffstat (limited to 'lib/puppet/indirector/facts')
-rw-r--r--lib/puppet/indirector/facts/yaml.rb75
1 files changed, 75 insertions, 0 deletions
diff --git a/lib/puppet/indirector/facts/yaml.rb b/lib/puppet/indirector/facts/yaml.rb
index 89feaf2ab..65bd78354 100644
--- a/lib/puppet/indirector/facts/yaml.rb
+++ b/lib/puppet/indirector/facts/yaml.rb
@@ -4,4 +4,79 @@ require 'puppet/indirector/yaml'
class Puppet::Node::Facts::Yaml < Puppet::Indirector::Yaml
desc "Store client facts as flat files, serialized using YAML, or
return deserialized facts from disk."
+
+ def search(request)
+ node_names = []
+ Dir.glob(yaml_dir_path).each do |file|
+ facts = YAML.load_file(file)
+ node_names << facts.name if node_matches?(facts, request.options)
+ end
+ node_names
+ end
+
+ private
+
+ # Return the path to a given node's file.
+ def yaml_dir_path
+ base = Puppet.run_mode.master? ? Puppet[:yamldir] : Puppet[:clientyamldir]
+ File.join(base, 'facts', '*.yaml')
+ end
+
+ def node_matches?(facts, options)
+ options.each do |key, value|
+ type, name, operator = key.to_s.split(".")
+ operator ||= 'eq'
+
+ return false unless node_matches_option?(type, name, operator, value, facts)
+ end
+ return true
+ end
+
+ def node_matches_option?(type, name, operator, value, facts)
+ case type
+ when "meta"
+ case name
+ when "timestamp"
+ compare_timestamp(operator, facts.timestamp, Time.parse(value))
+ end
+ when "facts"
+ compare_facts(operator, facts.values[name], value)
+ end
+ end
+
+ def compare_facts(operator, value1, value2)
+ return false unless value1
+
+ case operator
+ when "eq"
+ value1.to_s == value2.to_s
+ when "le"
+ value1.to_f <= value2.to_f
+ when "ge"
+ value1.to_f >= value2.to_f
+ when "lt"
+ value1.to_f < value2.to_f
+ when "gt"
+ value1.to_f > value2.to_f
+ when "ne"
+ value1.to_s != value2.to_s
+ end
+ end
+
+ def compare_timestamp(operator, value1, value2)
+ case operator
+ when "eq"
+ value1 == value2
+ when "le"
+ value1 <= value2
+ when "ge"
+ value1 >= value2
+ when "lt"
+ value1 < value2
+ when "gt"
+ value1 > value2
+ when "ne"
+ value1 != value2
+ end
+ end
end