diff options
| author | Matt Robinson <matt@puppetlabs.com> | 2010-10-28 14:49:37 -0700 |
|---|---|---|
| committer | Matt Robinson <matt@puppetlabs.com> | 2010-10-28 15:09:15 -0700 |
| commit | 45a9a97285d99db524d5330c236352b29e5884ed (patch) | |
| tree | 65d227ec46a7259a2a06221889cb48d1698dff3a /lib/puppet | |
| parent | e559b48d51c53c4a345353de1e196634c7430195 (diff) | |
| download | puppet-45a9a97285d99db524d5330c236352b29e5884ed.tar.gz puppet-45a9a97285d99db524d5330c236352b29e5884ed.tar.xz puppet-45a9a97285d99db524d5330c236352b29e5884ed.zip | |
(#5132) Provide a query REST interface for inventory
This REST interface returns a list of nodes that match a fact query.
Fact queries can use (in)equality testing as a string comparison, and >,
<, >=, <= numerical comparisons. Multiple tests can be done as AND
comparisons, not OR.
The fact queries need to be prefixed by facts, and the comparisons other
than equality are specified with a .comparison_type after the fact name.
This will be better explained in the REST documentation on the website.
Searches that don't match anything now return empty array instead of a
404 error.
Diffstat (limited to 'lib/puppet')
| -rw-r--r-- | lib/puppet/defaults.rb | 1 | ||||
| -rw-r--r-- | lib/puppet/indirector/indirection.rb | 1 | ||||
| -rw-r--r-- | lib/puppet/indirector/inventory/yaml.rb | 47 | ||||
| -rw-r--r-- | lib/puppet/network/http/api/v1.rb | 3 | ||||
| -rw-r--r-- | lib/puppet/network/http/handler.rb | 2 | ||||
| -rw-r--r-- | lib/puppet/node.rb | 1 | ||||
| -rw-r--r-- | lib/puppet/node/inventory.rb | 7 |
7 files changed, 59 insertions, 3 deletions
diff --git a/lib/puppet/defaults.rb b/lib/puppet/defaults.rb index ab127602b..c7bebf8f5 100644 --- a/lib/puppet/defaults.rb +++ b/lib/puppet/defaults.rb @@ -116,6 +116,7 @@ module Puppet :catalog_terminus => ["compiler", "Where to get node catalogs. This is useful to change if, for instance, you'd like to pre-compile catalogs and store them in memcached or some other easily-accessed store."], :facts_terminus => [Puppet.application_name.to_s == "master" ? 'yaml' : 'facter', "The node facts terminus."], + :inventory_terminus => [ "$facts_terminus", "Should usually be the same as the facts terminus" ], :httplog => { :default => "$logdir/http.log", :owner => "root", :mode => 0640, diff --git a/lib/puppet/indirector/indirection.rb b/lib/puppet/indirector/indirection.rb index 309eed7b6..9095e48f8 100644 --- a/lib/puppet/indirector/indirection.rb +++ b/lib/puppet/indirector/indirection.rb @@ -238,6 +238,7 @@ class Puppet::Indirector::Indirection if result = terminus.search(request) raise Puppet::DevError, "Search results from terminus #{terminus.name} are not an array" unless result.is_a?(Array) result.each do |instance| + next unless instance.respond_to? :expiration instance.expiration ||= self.expiration end return result diff --git a/lib/puppet/indirector/inventory/yaml.rb b/lib/puppet/indirector/inventory/yaml.rb new file mode 100644 index 000000000..c6b1a14aa --- /dev/null +++ b/lib/puppet/indirector/inventory/yaml.rb @@ -0,0 +1,47 @@ +require 'puppet/node/inventory' +require 'puppet/indirector/yaml' + +class Puppet::Node::Inventory::Yaml < Puppet::Indirector::Yaml + desc "Return node names matching the fact query" + + # 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' + + next unless type == "facts" + return false unless facts.values[name] + + return false unless case operator + when "eq" + facts.values[name].to_s == value.to_s + when "le" + facts.values[name].to_f <= value.to_f + when "ge" + facts.values[name].to_f >= value.to_f + when "lt" + facts.values[name].to_f < value.to_f + when "gt" + facts.values[name].to_f > value.to_f + when "ne" + facts.values[name].to_s != value.to_s + end + end + return true + end + + 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 +end diff --git a/lib/puppet/network/http/api/v1.rb b/lib/puppet/network/http/api/v1.rb index dd4612a14..2a4736365 100644 --- a/lib/puppet/network/http/api/v1.rb +++ b/lib/puppet/network/http/api/v1.rb @@ -57,9 +57,8 @@ module Puppet::Network::HTTP::API::V1 # fix to not need this, and our goal is to move away from the complication # that leads to the fix being too long. return :singular if indirection == "facts" - - # "status" really is singular return :singular if indirection == "status" + return :plural if indirection == "inventory" result = (indirection =~ /s$/) ? :plural : :singular diff --git a/lib/puppet/network/http/handler.rb b/lib/puppet/network/http/handler.rb index 61ae2d2fc..e5aa1c56f 100644 --- a/lib/puppet/network/http/handler.rb +++ b/lib/puppet/network/http/handler.rb @@ -116,7 +116,7 @@ module Puppet::Network::HTTP::Handler def do_search(indirection_request, request, response) result = indirection_request.model.search(indirection_request.key, indirection_request.to_hash) - if result.nil? or (result.is_a?(Array) and result.empty?) + if result.nil? return do_exception(response, "Could not find instances in #{indirection_request.indirection_name} with '#{indirection_request.key}'", 404) end diff --git a/lib/puppet/node.rb b/lib/puppet/node.rb index 2453cd1d5..e8d58e6be 100644 --- a/lib/puppet/node.rb +++ b/lib/puppet/node.rb @@ -3,6 +3,7 @@ require 'puppet/indirector' # A class for managing nodes, including their facts and environment. class Puppet::Node require 'puppet/node/facts' + require 'puppet/node/inventory' require 'puppet/node/environment' # Set up indirection, so that nodes can be looked for in diff --git a/lib/puppet/node/inventory.rb b/lib/puppet/node/inventory.rb new file mode 100644 index 000000000..fd99163b0 --- /dev/null +++ b/lib/puppet/node/inventory.rb @@ -0,0 +1,7 @@ +require 'puppet/node' +require 'puppet/indirector' + +class Puppet::Node::Inventory + extend Puppet::Indirector + indirects :inventory, :terminus_setting => :inventory_terminus +end |
