Does this use a different db than storeconfigs?

Aren't the facts already available in storeconfigs?

This seems an odd choice, if the answer to both of those questions is yes.

On Mar 8, 2011, at 4:48 PM, Nick Lewis wrote:

> 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
> 
> Signed-off-by: Nick Lewis <[email protected]>
> ---
> .../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 ++
> .../facts/inventory_active_record_spec.rb          |   99 ++++++++++++++++++++
> 6 files changed, 202 insertions(+), 0 deletions(-)
> 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
> create mode 100644 spec/unit/indirector/facts/inventory_active_record_spec.rb
> 
> 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 0000000..6cd63ab
> --- /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 0000000..2229843
> --- /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 8b389d7..5e455d6 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 0000000..ecb6e41
> --- /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 0000000..433e543
> --- /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
> diff --git a/spec/unit/indirector/facts/inventory_active_record_spec.rb 
> b/spec/unit/indirector/facts/inventory_active_record_spec.rb
> new file mode 100644
> index 0000000..b97bada
> --- /dev/null
> +++ b/spec/unit/indirector/facts/inventory_active_record_spec.rb
> @@ -0,0 +1,99 @@
> +#!/usr/bin/env ruby
> +
> +require File.dirname(__FILE__) + '/../../../spec_helper'
> +require 'sqlite3' rescue nil
> +require 'tempfile'
> +require 'puppet/rails'
> +
> +describe "Puppet::Node::Facts::InventoryActiveRecord", :if => 
> (Puppet.features.rails? and defined? SQLite3) do
> +  let(:terminus) { Puppet::Node::Facts::InventoryActiveRecord.new }
> +
> +  before :all do
> +    require 'puppet/indirector/facts/inventory_active_record'
> +    @dbfile = Tempfile.new("testdb")
> +    @dbfile.close
> +  end
> +
> +  after :all do
> +    Puppet::Node::Facts.indirection.reset_terminus_class
> +    @dbfile.unlink
> +  end
> +
> +  before :each do
> +    Puppet::Node::Facts.terminus_class = :inventory_active_record
> +    Puppet[:dbadapter]  = 'sqlite3'
> +    Puppet[:dblocation] = @dbfile.path
> +    Puppet[:railslog] = "/dev/null"
> +    Puppet::Rails.init
> +  end
> +
> +  after :each do
> +    Puppet::Rails.teardown
> +  end
> +
> +  describe "#save" do
> +    it "should use an existing host if possible" do
> +      host = Puppet::Rails::InventoryHost.new(:name => "foo", :timestamp => 
> Time.now)
> +      host.save
> +      Puppet::Node::Facts.new("foo", "uptime_days" => "60", "kernel" => 
> "Darwin").save
> +
> +      Puppet::Rails::InventoryHost.count.should == 1
> +      Puppet::Rails::InventoryHost.first.should == host
> +    end
> +
> +    it "should create a new host if one can't be found" do
> +      # This test isn't valid if there are hosts to begin with
> +      Puppet::Rails::InventoryHost.count.should == 0
> +
> +      Puppet::Node::Facts.new("foo", "uptime_days" => "60", "kernel" => 
> "Darwin").save
> +
> +      Puppet::Rails::InventoryHost.count.should == 1
> +      Puppet::Rails::InventoryHost.first.name.should == "foo"
> +    end
> +
> +    it "should save the facts" do
> +      Puppet::Node::Facts.new("foo", "uptime_days" => "60", "kernel" => 
> "Darwin").save
> +
> +      Puppet::Rails::InventoryFact.all.map{|f| [f.name,f.value]}.should =~ 
> [["uptime_days","60"],["kernel","Darwin"]]
> +    end
> +
> +    it "should remove the previous facts for an existing host" do
> +      Puppet::Node::Facts.new("foo", "uptime_days" => "30", "kernel" => 
> "Darwin").save
> +      bar_facts = Puppet::Node::Facts.new("bar", "uptime_days" => "35", 
> "kernel" => "Linux")
> +      foo_facts = Puppet::Node::Facts.new("foo", "uptime_days" => "60", 
> "is_virtual" => "false")
> +      bar_facts.save
> +      foo_facts.save
> +
> +      Puppet::Node::Facts.find("bar").should == bar_facts
> +      Puppet::Node::Facts.find("foo").should == foo_facts
> +      Puppet::Rails::InventoryFact.all.map{|f| [f.name,f.value]}.should_not 
> include(["uptime_days", "30"], ["kernel", "Darwin"])
> +    end
> +
> +    it "should not replace the node's facts if something goes wrong" do
> +    end
> +  end
> +
> +  describe "#find" do
> +    before do
> +      @foo_facts = Puppet::Node::Facts.new("foo", "uptime_days" => "60", 
> "kernel" => "Darwin")
> +      @bar_facts = Puppet::Node::Facts.new("bar", "uptime_days" => "30", 
> "kernel" => "Linux")
> +      @foo_facts.save
> +      @bar_facts.save
> +    end
> +
> +    it "should identify facts by host name" do
> +      Puppet::Node::Facts.find("foo").should == @foo_facts
> +    end
> +
> +    it "should return nil if no host instance can be found" do
> +      Puppet::Node::Facts.find("non-existent host").should == nil
> +    end
> +
> +    it "should convert all single-member arrays into non-arrays" do
> +      Puppet::Node::Facts.new("array", "fact1" => ["value1"]).save
> +
> +      Puppet::Node::Facts.find("array").values["fact1"].should == "value1"
> +    end
> +  end
> +end
> +
> -- 
> 1.7.4.1
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Puppet Developers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/puppet-dev?hl=en.
> 


-- 
I think that all good, right thinking people in this country are sick
and tired of being told that all good, right thinking people in this
country are fed up with being told that all good, right thinking people
in this country are fed up with being sick and tired.  I'm certainly
not, and I'm sick and tired of being told that I am.
                -- Monty Python
---------------------------------------------------------------------
Luke Kanies  -|-   http://puppetlabs.com   -|-   +1(615)594-8199




-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to