diff options
author | Nick Lewis <nick@puppetlabs.com> | 2011-03-08 16:06:03 -0800 |
---|---|---|
committer | Nick Lewis <nick@puppetlabs.com> | 2011-03-08 16:06:03 -0800 |
commit | e8763415627cf41cefece00bf4dbc48e9be81d1d (patch) | |
tree | f23dfd8c60020ad511e5fd5c49cf6fae973710ab /lib | |
parent | 7c768c4a6b85788aabc431e01febee00d4c3d3e5 (diff) | |
parent | 880d0c6cbd20758e02848d1fa61966402dc44dc0 (diff) | |
download | puppet-e8763415627cf41cefece00bf4dbc48e9be81d1d.tar.gz puppet-e8763415627cf41cefece00bf4dbc48e9be81d1d.tar.xz puppet-e8763415627cf41cefece00bf4dbc48e9be81d1d.zip |
Merge branch 'ticket/2.6.next/6338' into 2.6.next
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet/indirector/facts/inventory_active_record.rb | 95 | ||||
-rw-r--r-- | lib/puppet/rails/database/004_add_inventory_service_tables.rb | 36 | ||||
-rw-r--r-- | lib/puppet/rails/database/schema.rb | 17 | ||||
-rw-r--r-- | lib/puppet/rails/inventory_fact.rb | 6 | ||||
-rw-r--r-- | lib/puppet/rails/inventory_host.rb | 37 |
5 files changed, 191 insertions, 0 deletions
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..5b8606839 --- /dev/null +++ b/lib/puppet/indirector/facts/inventory_active_record.rb @@ -0,0 +1,95 @@ +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 + + def search(request) + return [] unless request.options + fact_names = [] + 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" + 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 = [] + fact_filters['eq'].each do |name,value| + host_sets << Puppet::Rails::InventoryHost.has_fact_with_value(name,value).map {|host| host.name} + end + fact_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| + 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 + + 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 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..10dd62083 --- /dev/null +++ b/lib/puppet/rails/inventory_host.rb @@ -0,0 +1,37 @@ +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) + end + end +end |