summaryrefslogtreecommitdiffstats
path: root/lib/puppet/rails
diff options
context:
space:
mode:
authorballman <ballman@980ebf18-57e1-0310-9a29-db15c13687c0>2007-06-12 00:31:16 +0000
committerballman <ballman@980ebf18-57e1-0310-9a29-db15c13687c0>2007-06-12 00:31:16 +0000
commit68e37a99d5357f022662f9ba7cc564c48aed21a9 (patch)
treee8d94cc9ea44d312e8b96f94c887b88fc1b04e0f /lib/puppet/rails
parentc26f678178d173bd7360362b2959af51c4d39762 (diff)
downloadpuppet-68e37a99d5357f022662f9ba7cc564c48aed21a9.tar.gz
puppet-68e37a99d5357f022662f9ba7cc564c48aed21a9.tar.xz
puppet-68e37a99d5357f022662f9ba7cc564c48aed21a9.zip
Major rework of the rails feature. Changed the relationship between
host and facts (now many-to-many with fact_name through fact_values). Also changed the relationship between resource and params (similarly many-to-many with param_names through param_values). Added the resource_tags and puppet_tags. The latter has the tag names and the former is the man-to-many link with resources. There is a little clean up left but the schema is in order. Also a test for the tags stuff is required. git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2565 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/puppet/rails')
-rw-r--r--lib/puppet/rails/database/schema.rb35
-rw-r--r--lib/puppet/rails/fact_value.rb1
-rw-r--r--lib/puppet/rails/host.rb92
-rw-r--r--lib/puppet/rails/param_name.rb9
-rw-r--r--lib/puppet/rails/param_value.rb1
-rw-r--r--lib/puppet/rails/puppet_tag.rb5
-rw-r--r--lib/puppet/rails/resource.rb49
-rw-r--r--lib/puppet/rails/resource_tag.rb4
8 files changed, 127 insertions, 69 deletions
diff --git a/lib/puppet/rails/database/schema.rb b/lib/puppet/rails/database/schema.rb
index f8960f2d0..d66d9f56e 100644
--- a/lib/puppet/rails/database/schema.rb
+++ b/lib/puppet/rails/database/schema.rb
@@ -22,10 +22,14 @@ class Puppet::Rails::Schema
t.column :updated_at, :datetime
end
- create_table :puppet_classes do |t|
+ create_table :resource_tags do |t|
+ t.column :resource_id, :integer
+ t.column :puppet_tag_id, :integer
+ t.column :updated_at, :datetime
+ end
+
+ create_table :puppet_tags do |t|
t.column :name, :string
- t.column :host_id, :integer
- t.column :source_file_id, :integer
t.column :updated_at, :datetime
end
@@ -42,47 +46,34 @@ class Puppet::Rails::Schema
create_table :fact_names do |t|
t.column :name, :string, :null => false
- t.column :host_id, :integer, :null => false
t.column :updated_at, :datetime
end
create_table :fact_values do |t|
t.column :value, :text, :null => false
t.column :fact_name_id, :integer, :null => false
+ t.column :host_id, :integer, :null => false
t.column :updated_at, :datetime
end
create_table :param_values do |t|
t.column :value, :text, :null => false
t.column :param_name_id, :integer, :null => false
+ t.column :line, :integer
+ t.column :resource_id, :integer
t.column :updated_at, :datetime
end
create_table :param_names do |t|
t.column :name, :string, :null => false
- t.column :resource_id, :integer
- t.column :line, :integer
- t.column :updated_at, :datetime
- end
-
- create_table :tags do |t|
- t.column :name, :string
- t.column :updated_at, :datetime
- end
-
- create_table :taggings do |t|
- t.column :tag_id, :integer
- t.column :taggable_id, :integer
- t.column :taggable_type, :string
t.column :updated_at, :datetime
end
- end
- $stdout.close
- $stdout = oldout
- oldout = nil
+ end
end
ensure
+ $stdout.close
$stdout = oldout if oldout
+ oldout = nil
end
end
diff --git a/lib/puppet/rails/fact_value.rb b/lib/puppet/rails/fact_value.rb
index a800ad597..0eb70be72 100644
--- a/lib/puppet/rails/fact_value.rb
+++ b/lib/puppet/rails/fact_value.rb
@@ -1,5 +1,6 @@
class Puppet::Rails::FactValue < ActiveRecord::Base
belongs_to :fact_name
+ belongs_to :host
end
# $Id: fact_value.rb 1952 2006-12-19 05:47:57Z luke $
diff --git a/lib/puppet/rails/host.rb b/lib/puppet/rails/host.rb
index ff6fb8f83..43cc9a367 100644
--- a/lib/puppet/rails/host.rb
+++ b/lib/puppet/rails/host.rb
@@ -9,12 +9,12 @@ class Puppet::Rails::Host < ActiveRecord::Base
include Puppet::Util
include Puppet::Util::CollectionMerger
- has_many :fact_values, :through => :fact_names
- has_many :fact_names, :dependent => :destroy
+ has_many :fact_values, :dependent => :destroy
+ has_many :fact_names, :through => :fact_values
belongs_to :puppet_classes
has_many :source_files
has_many :resources,
- :include => [ :param_names, :param_values ],
+ :include => :param_values,
:dependent => :destroy
# If the host already exists, get rid of its objects
@@ -39,9 +39,7 @@ class Puppet::Rails::Host < ActiveRecord::Base
transaction do
#unless host = find_by_name(name)
seconds = Benchmark.realtime {
- #unless host = find_by_name(name, :include => {:resources => {:param_names => :param_values}, :fact_names => :fact_values})
- unless host = find_by_name(name, :include => {:fact_names => :fact_values})
- #unless host = find_by_name(name)
+ unless host = find_by_name(name)
host = new(:name => name)
end
}
@@ -57,7 +55,6 @@ class Puppet::Rails::Host < ActiveRecord::Base
raise ArgumentError, "You must pass resources"
end
-
seconds = Benchmark.realtime {
host.setresources(hash[:resources])
}
@@ -73,46 +70,81 @@ class Puppet::Rails::Host < ActiveRecord::Base
# Return the value of a fact.
def fact(name)
- if fv = self.fact_values.find(:first, :conditions => "fact_names.name = '#{name}'")
- return fv.value
+ if fv = self.fact_values.find(:all, :include => :fact_name,
+ :conditions => "fact_names.name = '#{name}'")
+ return fv
else
return nil
end
end
+
+ # returns a hash of fact_names.name => [ fact_values ] for this host.
+ def get_facts_hash
+ fact_values = self.fact_values.find(:all, :include => :fact_name)
+ return fact_values.inject({}) do | hash, value |
+ hash[value.fact_name.name] ||= []
+ hash[value.fact_name.name] << value
+ hash
+ end
+ end
+
def setfacts(facts)
facts = facts.dup
- remove = []
-
- collection_merge :fact_names, :updates => facts, :modify => Proc.new { |fn, name, value|
- fn.fact_values.each do |fv|
- unless value == fv.value
- fv.value = value
- end
- break
- end
- }, :create => Proc.new { |name, value|
- fn = fact_names.build(:name => name)
- fn.fact_values = [fn.fact_values.build(:value => value)]
- }
+
+ ar_hash_merge(get_facts_hash(), facts,
+ :create => Proc.new { |name, values|
+ fact_name = Puppet::Rails::FactName.find_or_create_by_name(name)
+ values.each do |value|
+ fact_values.build(:value => value,
+ :fact_name => fact_name)
+ end
+ }, :delete => Proc.new { |values|
+ values.each { |value| self.fact_values.delete(value) }
+ }, :modify => Proc.new { |db, mem|
+ mem = [mem].flatten
+ fact_name = db[0].fact_name
+ db_values = db.collect { |fact_value| fact_value.value }
+ (db_values - (db_values & mem)).each do |value|
+ db.find_all { |fact_value|
+ fact_value.value == value
+ }.each { |fact_value|
+ fact_values.delete(fact_value)
+ }
+ end
+ (mem - (db_values & mem)).each do |value|
+ fact_values.build(:value => value,
+ :fact_name => fact_name)
+ end
+ })
end
# Set our resources.
def setresources(list)
- compiled = {}
- remove = []
existing = nil
seconds = Benchmark.realtime {
#existing = resources.find(:all)
- existing = resources.find(:all, :include => {:param_names => :param_values})
- #existing = resources
+ existing = resources.find(:all, :include => {:param_names => :param_values}).inject({}) do | hash, resource |
+ hash[resource.ref] = resource
+ hash
+ end
}
+
Puppet.notice("Searched for resources in %0.2f seconds" % seconds) if defined?(Puppet::TIME_DEBUG)
- list.each do |resource|
- compiled[resource.ref] = resource
- end
- collection_merge :resources, :existing => existing, :updates => compiled
+ compiled = list.inject({}) do |hash, resource|
+ hash[resource.ref] = resource
+ hash
+ end
+
+ ar_hash_merge(existing, compiled,
+ :create => Proc.new { |ref, resource|
+ resource.to_rails(self)
+ }, :delete => Proc.new { |resource|
+ self.resources.delete(resource)
+ }, :modify => Proc.new { |db, mem|
+ mem.modify_rails(db)
+ })
end
def update_connect_time
diff --git a/lib/puppet/rails/param_name.rb b/lib/puppet/rails/param_name.rb
index 1b708cacb..1f633638e 100644
--- a/lib/puppet/rails/param_name.rb
+++ b/lib/puppet/rails/param_name.rb
@@ -4,17 +4,12 @@ require 'puppet/rails/param_value'
class Puppet::Rails::ParamName < ActiveRecord::Base
include Puppet::Util::CollectionMerger
has_many :param_values, :dependent => :destroy
-# def <<(value)
-# ParamValue.with_scope(:create => {:value => value})
-# end
-## end
- belongs_to :resource
- def to_resourceparam(source)
+ def to_resourceparam(resource, source)
hash = {}
hash[:name] = self.name.to_sym
hash[:source] = source
- hash[:value] = self.param_values.find(:all).collect { |v| v.value }
+ hash[:value] = resource.param_values.find(:all, :conditions => [ "param_name_id = ?", self]).collect { |v| v.value }
if hash[:value].length == 1
hash[:value] = hash[:value].shift
end
diff --git a/lib/puppet/rails/param_value.rb b/lib/puppet/rails/param_value.rb
index d988559af..f463608a6 100644
--- a/lib/puppet/rails/param_value.rb
+++ b/lib/puppet/rails/param_value.rb
@@ -1,5 +1,6 @@
class Puppet::Rails::ParamValue < ActiveRecord::Base
belongs_to :param_name
+ belongs_to :resource
end
# $Id$
diff --git a/lib/puppet/rails/puppet_tag.rb b/lib/puppet/rails/puppet_tag.rb
new file mode 100644
index 000000000..279eefc4e
--- /dev/null
+++ b/lib/puppet/rails/puppet_tag.rb
@@ -0,0 +1,5 @@
+require 'puppet/rails/resource_tag'
+class Puppet::Rails::PuppetTag < ActiveRecord::Base
+ has_many :resource_tags, :dependent => :destroy
+ has_many :resources, :through => :resource_tags
+end
diff --git a/lib/puppet/rails/resource.rb b/lib/puppet/rails/resource.rb
index 49bd1dacc..d2d4ec21f 100644
--- a/lib/puppet/rails/resource.rb
+++ b/lib/puppet/rails/resource.rb
@@ -1,15 +1,25 @@
require 'puppet'
require 'puppet/rails/param_name'
+require 'puppet/rails/puppet_tag'
require 'puppet/util/rails/collection_merger'
class Puppet::Rails::Resource < ActiveRecord::Base
include Puppet::Util::CollectionMerger
- has_many :param_values, :through => :param_names
- has_many :param_names, :dependent => :destroy
+ has_many :param_values, :dependent => :destroy
+ has_many :param_names, :through => :param_values
+
+ has_many :resource_tags, :dependent => :destroy
+ has_many :puppet_tags, :through => :resource_tags
+
belongs_to :source_file
belongs_to :host
+ def add_resource_tag(tag)
+ pt = Puppet::Rails::PuppetTag.find_or_create_by_name(tag)
+ resource_tags.create(:puppet_tag => pt)
+ end
+
def file
if f = self.source_file
return f.filename
@@ -19,7 +29,25 @@ class Puppet::Rails::Resource < ActiveRecord::Base
end
def file=(file)
- self.source_file = Puppet::Rails::SourceFile.new(:filename => file)
+ self.source_file = Puppet::Rails::SourceFile.find_or_create_by_filename(file)
+ end
+
+ # returns a hash of param_names.name => [param_values]
+ def get_params_hash
+ param_values = self.param_values.find(:all, :include => :param_name)
+ return param_values.inject({}) do | hash, value |
+ hash[value.param_name.name] ||= []
+ hash[value.param_name.name] << value
+ hash
+ end
+ end
+
+ def get_tag_hash
+ tags = self.resource_tags.find(:all, :include => :puppet_tag)
+ return tags.inject({}) do |hash, tag|
+ hash[tag.puppet_tag.name] = tag.puppet_tag.name
+ hash
+ end
end
def [](param)
@@ -32,7 +60,7 @@ class Puppet::Rails::Resource < ActiveRecord::Base
def parameter(param)
if pn = param_names.find_by_name(param)
- if pv = pn.param_values.find(:first)
+ if pv = param_values.find(:first, :conditions => [ 'param_name_id = ?', pn] )
return pv.value
else
return nil
@@ -41,12 +69,12 @@ class Puppet::Rails::Resource < ActiveRecord::Base
end
def parameters
- hash = {}
- self.param_values.find(:all).each do |pvalue|
- pname = pvalue.param_name.name
- hash.store(pname, pvalue.value)
+ return self.param_values.find(:all,
+ :include => :param_name).inject({}) do |hash, pvalue|
+ hash[pvalue.param_name.name] ||= []
+ hash[pvalue.param_name.name] << pvalue.value
+ hash
end
- return hash
end
def ref
@@ -73,8 +101,9 @@ class Puppet::Rails::Resource < ActiveRecord::Base
hash[:scope] = scope
hash[:source] = scope.source
obj = Puppet::Parser::Resource.new(hash)
+
self.param_names.each do |pname|
- obj.set(pname.to_resourceparam(scope.source))
+ obj.set(pname.to_resourceparam(self, scope.source))
end
# Store the ID, so we can check if we're re-collecting the same resource.
diff --git a/lib/puppet/rails/resource_tag.rb b/lib/puppet/rails/resource_tag.rb
new file mode 100644
index 000000000..d06711877
--- /dev/null
+++ b/lib/puppet/rails/resource_tag.rb
@@ -0,0 +1,4 @@
+class Puppet::Rails::ResourceTag < ActiveRecord::Base
+ belongs_to :puppet_tag
+ belongs_to :resource
+end