From 8ce30c83ddba87ba7e2622a46f27143159132789 Mon Sep 17 00:00:00 2001 From: Nick Lewis Date: Mon, 7 Mar 2011 15:19:20 -0800 Subject: (#6338) Add an InventoryActiveRecord terminus for Facts So far this terminus only supports find and save. Search is forthcoming. It uses two new tables (inventory_host and inventory_facts) so that it won't interact with storedconfigs. Paired-With: Jacob Helwig --- .../indirector/facts/inventory_active_record.rb | 33 ++++++++++++++++++++ .../database/004_add_inventory_service_tables.rb | 36 ++++++++++++++++++++++ lib/puppet/rails/database/schema.rb | 17 ++++++++++ lib/puppet/rails/inventory_fact.rb | 6 ++++ lib/puppet/rails/inventory_host.rb | 11 +++++++ 5 files changed, 103 insertions(+) create mode 100644 lib/puppet/indirector/facts/inventory_active_record.rb create mode 100644 lib/puppet/rails/database/004_add_inventory_service_tables.rb create mode 100644 lib/puppet/rails/inventory_fact.rb create mode 100644 lib/puppet/rails/inventory_host.rb (limited to 'lib') diff --git a/lib/puppet/indirector/facts/inventory_active_record.rb b/lib/puppet/indirector/facts/inventory_active_record.rb new file mode 100644 index 000000000..6cd63ab1a --- /dev/null +++ b/lib/puppet/indirector/facts/inventory_active_record.rb @@ -0,0 +1,33 @@ +require 'puppet/rails/inventory_host' +require 'puppet/rails/inventory_fact' +require 'puppet/indirector/active_record' + +class Puppet::Node::Facts::InventoryActiveRecord < Puppet::Indirector::ActiveRecord + def find(request) + host = Puppet::Rails::InventoryHost.find_by_name(request.key) + return nil unless host + facts = Puppet::Node::Facts.new(host.name, host.facts_to_hash) + facts.timestamp = host.timestamp + facts.values.each do |key,value| + facts.values[key] = value.first if value.is_a?(Array) && value.length == 1 + end + facts + end + + def save(request) + facts = request.instance + host = Puppet::Rails::InventoryHost.find_by_name(request.key) || Puppet::Rails::InventoryHost.create(:name => request.key, :timestamp => facts.timestamp) + host.timestamp = facts.timestamp + + ActiveRecord::Base.transaction do + Puppet::Rails::InventoryFact.delete_all(:inventory_host_id => host.id) + # We don't want to save internal values as facts, because those are + # metadata that belong on the host + facts.values.each do |name,value| + next if name.to_s =~ /^_/ + host.facts.build(:name => name, :value => value) + end + host.save + end + end +end diff --git a/lib/puppet/rails/database/004_add_inventory_service_tables.rb b/lib/puppet/rails/database/004_add_inventory_service_tables.rb new file mode 100644 index 000000000..22298437a --- /dev/null +++ b/lib/puppet/rails/database/004_add_inventory_service_tables.rb @@ -0,0 +1,36 @@ +class AddInventoryServiceTables < ActiveRecord::Migration + def self.up + unless ActiveRecord::Base.connection.tables.include?("inventory_hosts") + create_table :inventory_hosts do |t| + t.column :name, :string, :null => false + t.column :timestamp, :datetime, :null => false + t.column :updated_at, :datetime + t.column :created_at, :datetime + end + + add_index :inventory_hosts, :name, :unique => true + end + + unless ActiveRecord::Base.connection.tables.include?("inventory_facts") + create_table :inventory_facts, :id => false do |t| + t.column :inventory_host_id, :integer, :null => false + t.column :name, :string, :null => false + t.column :value, :text, :null => false + end + + add_index :inventory_facts, [:inventory_host_id, :name], :unique => true + end + end + + def self.down + unless ActiveRecord::Base.connection.tables.include?("inventory_hosts") + remove_index :inventory_hosts, :name + drop_table :inventory_hosts + end + + if ActiveRecord::Base.connection.tables.include?("inventory_facts") + remove_index :inventory_facts, [:inventory_host_id, :name] + drop_table :inventory_facts + end + end +end diff --git a/lib/puppet/rails/database/schema.rb b/lib/puppet/rails/database/schema.rb index 8b389d773..5e455d6c0 100644 --- a/lib/puppet/rails/database/schema.rb +++ b/lib/puppet/rails/database/schema.rb @@ -103,6 +103,23 @@ class Puppet::Rails::Schema t.column :created_at, :datetime end add_index :param_names, :name + + create_table :inventory_hosts do |t| + t.column :name, :string, :null => false + t.column :timestamp, :datetime, :null => false + t.column :updated_at, :datetime + t.column :created_at, :datetime + end + + add_index :inventory_hosts, :name, :unique => true + + create_table :inventory_facts, :id => false do |t| + t.column :inventory_host_id, :integer, :null => false + t.column :name, :string, :null => false + t.column :value, :text, :null => false + end + + add_index :inventory_facts, [:inventory_host_id, :name], :unique => true end end ensure diff --git a/lib/puppet/rails/inventory_fact.rb b/lib/puppet/rails/inventory_fact.rb new file mode 100644 index 000000000..ecb6e41ee --- /dev/null +++ b/lib/puppet/rails/inventory_fact.rb @@ -0,0 +1,6 @@ +require 'puppet/rails/inventory_host' + +class Puppet::Rails::InventoryFact < ::ActiveRecord::Base + belongs_to :host, :class_name => "Puppet::Rails::InventoryHost" + serialize :value +end diff --git a/lib/puppet/rails/inventory_host.rb b/lib/puppet/rails/inventory_host.rb new file mode 100644 index 000000000..433e54389 --- /dev/null +++ b/lib/puppet/rails/inventory_host.rb @@ -0,0 +1,11 @@ +require 'puppet/rails/inventory_fact' + +class Puppet::Rails::InventoryHost < ::ActiveRecord::Base + has_many :facts, :class_name => "Puppet::Rails::InventoryFact", :dependent => :delete_all + + def facts_to_hash + facts.inject({}) do |fact_hash,fact| + fact_hash.merge(fact.name => fact.value) + end + end +end -- cgit From f83636698229241b2ab35849437f3e515f6ac5c1 Mon Sep 17 00:00:00 2001 From: Nick Lewis Date: Mon, 7 Mar 2011 16:42:53 -0800 Subject: (#6338) Implement search for InventoryActiveRecord facts terminus Paired-With: Max Martin Reviewed-By: Jacob Helwig --- .../indirector/facts/inventory_active_record.rb | 34 ++++++++++++++++++++++ lib/puppet/rails/inventory_host.rb | 26 +++++++++++++++++ 2 files changed, 60 insertions(+) (limited to 'lib') diff --git a/lib/puppet/indirector/facts/inventory_active_record.rb b/lib/puppet/indirector/facts/inventory_active_record.rb index 6cd63ab1a..30cb88ea0 100644 --- a/lib/puppet/indirector/facts/inventory_active_record.rb +++ b/lib/puppet/indirector/facts/inventory_active_record.rb @@ -30,4 +30,38 @@ class Puppet::Node::Facts::InventoryActiveRecord < Puppet::Indirector::ActiveRec host.save end end + + def search(request) + return [] unless request.options + fact_names = [] + filters = Hash.new {|h,k| h[k] = []} + request.options.each do |key,value| + type, name, operator = key.to_s.split(".") + operator ||= "eq" + filters[operator] << [name,value] + end + + + host_sets = [] + filters['eq'].each do |name,value| + host_sets << Puppet::Rails::InventoryHost.has_fact_with_value(name,value).map {|host| host.name} + end + filters['ne'].each do |name,value| + host_sets << Puppet::Rails::InventoryHost.has_fact_without_value(name,value).map {|host| host.name} + end + { + 'gt' => '>', + 'lt' => '<', + 'ge' => '>=', + 'le' => '<=' + }.each do |operator_name,operator| + filters[operator_name].each do |name,value| + hosts_with_fact = Puppet::Rails::InventoryHost.has_fact(name) + host_sets << hosts_with_fact.select {|h| h.value_for(name).to_f.send(operator, value.to_f)}.map {|host| host.name} + end + end + + # to_a because [].inject == nil + host_sets.inject {|hosts,this_set| hosts & this_set}.to_a + end end diff --git a/lib/puppet/rails/inventory_host.rb b/lib/puppet/rails/inventory_host.rb index 433e54389..10dd62083 100644 --- a/lib/puppet/rails/inventory_host.rb +++ b/lib/puppet/rails/inventory_host.rb @@ -3,6 +3,32 @@ require 'puppet/rails/inventory_fact' class Puppet::Rails::InventoryHost < ::ActiveRecord::Base has_many :facts, :class_name => "Puppet::Rails::InventoryFact", :dependent => :delete_all + named_scope :has_fact_with_value, lambda { |name,value| + { + :conditions => ["inventory_facts.name = ? AND inventory_facts.value = ?", name, value], + :joins => :facts + } + } + + named_scope :has_fact_without_value, lambda { |name,value| + { + :conditions => ["inventory_facts.name = ? AND inventory_facts.value != ?", name, value], + :joins => :facts + } + } + + named_scope :has_fact, lambda { |name| + { + :conditions => ["inventory_facts.name = ?", name], + :joins => :facts + } + } + + def value_for(fact_name) + fact = facts.find_by_name(fact_name) + fact ? fact.value : nil + end + def facts_to_hash facts.inject({}) do |fact_hash,fact| fact_hash.merge(fact.name => fact.value) -- cgit From 880d0c6cbd20758e02848d1fa61966402dc44dc0 Mon Sep 17 00:00:00 2001 From: Nick Lewis Date: Mon, 7 Mar 2011 17:28:43 -0800 Subject: (#6338) Support searching on metadata in InventoryActiveRecord terminus Timestamps are currently the only supported metadata for searching. Paired-With: Max Martin Reviewed-By: Jacob Helwig --- .../indirector/facts/inventory_active_record.rb | 42 ++++++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/puppet/indirector/facts/inventory_active_record.rb b/lib/puppet/indirector/facts/inventory_active_record.rb index 30cb88ea0..5b8606839 100644 --- a/lib/puppet/indirector/facts/inventory_active_record.rb +++ b/lib/puppet/indirector/facts/inventory_active_record.rb @@ -34,19 +34,32 @@ class Puppet::Node::Facts::InventoryActiveRecord < Puppet::Indirector::ActiveRec def search(request) return [] unless request.options fact_names = [] - filters = Hash.new {|h,k| h[k] = []} + fact_filters = Hash.new {|h,k| h[k] = []} + meta_filters = Hash.new {|h,k| h[k] = []} request.options.each do |key,value| type, name, operator = key.to_s.split(".") operator ||= "eq" - filters[operator] << [name,value] + if type == "facts" + fact_filters[operator] << [name,value] + elsif type == "meta" and name == "timestamp" + meta_filters[operator] << [name,value] + end end + matching_hosts = hosts_matching_fact_filters(fact_filters) + hosts_matching_meta_filters(meta_filters) + + # to_a because [].inject == nil + matching_hosts.inject {|hosts,this_set| hosts & this_set}.to_a.sort + end + + private + def hosts_matching_fact_filters(fact_filters) host_sets = [] - filters['eq'].each do |name,value| + fact_filters['eq'].each do |name,value| host_sets << Puppet::Rails::InventoryHost.has_fact_with_value(name,value).map {|host| host.name} end - filters['ne'].each do |name,value| + fact_filters['ne'].each do |name,value| host_sets << Puppet::Rails::InventoryHost.has_fact_without_value(name,value).map {|host| host.name} end { @@ -55,13 +68,28 @@ class Puppet::Node::Facts::InventoryActiveRecord < Puppet::Indirector::ActiveRec 'ge' => '>=', 'le' => '<=' }.each do |operator_name,operator| - filters[operator_name].each do |name,value| + fact_filters[operator_name].each do |name,value| hosts_with_fact = Puppet::Rails::InventoryHost.has_fact(name) host_sets << hosts_with_fact.select {|h| h.value_for(name).to_f.send(operator, value.to_f)}.map {|host| host.name} end end + host_sets + end - # to_a because [].inject == nil - host_sets.inject {|hosts,this_set| hosts & this_set}.to_a + def hosts_matching_meta_filters(meta_filters) + host_sets = [] + { + 'eq' => '=', + 'ne' => '!=', + 'gt' => '>', + 'lt' => '<', + 'ge' => '>=', + 'le' => '<=' + }.each do |operator_name,operator| + meta_filters[operator_name].each do |name,value| + host_sets << Puppet::Rails::InventoryHost.find(:all, :conditions => ["timestamp #{operator} ?", value]).map {|host| host.name} + end + end + host_sets end end -- cgit