summaryrefslogtreecommitdiffstats
path: root/lib/puppet/rails
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-12-19 02:08:11 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-12-19 02:08:11 +0000
commitdc5f4dc0d01dc2ccb4679afbf3802a7ab0f3c126 (patch)
tree6082433e05ad445aa4323a05de7d5820c5c023d6 /lib/puppet/rails
parent5a52855c1da2cb4716587bf0223c6d20eddaf00a (diff)
downloadpuppet-dc5f4dc0d01dc2ccb4679afbf3802a7ab0f3c126.tar.gz
puppet-dc5f4dc0d01dc2ccb4679afbf3802a7ab0f3c126.tar.xz
puppet-dc5f4dc0d01dc2ccb4679afbf3802a7ab0f3c126.zip
Fixing most of the rails stuff. I think everything basically works now, and now I am just going through and making sure things get deleted when they are supposed (i.e., you remove a resource and it gets deleted from the host's config).
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1950 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/puppet/rails')
-rw-r--r--lib/puppet/rails/database/schema.rb11
-rw-r--r--lib/puppet/rails/fact_name.rb2
-rw-r--r--lib/puppet/rails/fact_value.rb2
-rw-r--r--lib/puppet/rails/host.rb65
-rw-r--r--lib/puppet/rails/param_name.rb10
-rw-r--r--lib/puppet/rails/param_value.rb4
-rw-r--r--lib/puppet/rails/resource.rb35
7 files changed, 64 insertions, 65 deletions
diff --git a/lib/puppet/rails/database/schema.rb b/lib/puppet/rails/database/schema.rb
index 546ad73fb..322e6f839 100644
--- a/lib/puppet/rails/database/schema.rb
+++ b/lib/puppet/rails/database/schema.rb
@@ -1,17 +1,18 @@
class Puppet::Rails::Schema
def self.init
oldout = nil
- Puppet::Util.benchmark(:notice, "Initialized database") do
+ Puppet::Util.benchmark(Puppet, :notice, "Initialized database") do
# We want to rewrite stdout, so we don't get migration messages.
oldout = $stdout
$stdout = File.open("/dev/null", "w")
ActiveRecord::Schema.define do
create_table :resources do |t|
t.column :title, :string, :null => false
- t.column :restype, :string
+ t.column :restype, :string, :null => false
t.column :host_id, :integer
t.column :source_file_id, :integer
t.column :exported, :boolean
+ t.column :line, :integer
end
create_table :source_files do |t|
@@ -52,6 +53,7 @@ class Puppet::Rails::Schema
create_table :param_names do |t|
t.column :name, :string, :null => false
t.column :resource_id, :integer
+ t.column :line, :integer
end
create_table :tags do |t|
@@ -64,9 +66,12 @@ class Puppet::Rails::Schema
t.column :taggable_type, :string
end
end
+ $stdout.close
+ $stdout = oldout
+ oldout = nil
end
ensure
- $stdout = oldout
+ $stdout = oldout if oldout
end
end
diff --git a/lib/puppet/rails/fact_name.rb b/lib/puppet/rails/fact_name.rb
index 886618ecb..85c951f87 100644
--- a/lib/puppet/rails/fact_name.rb
+++ b/lib/puppet/rails/fact_name.rb
@@ -1,3 +1,3 @@
class Puppet::Rails::FactName < ActiveRecord::Base
- has_many :fact_values
+ has_many :fact_values, :dependent => :destroy
end
diff --git a/lib/puppet/rails/fact_value.rb b/lib/puppet/rails/fact_value.rb
index 4da74b713..09be5d265 100644
--- a/lib/puppet/rails/fact_value.rb
+++ b/lib/puppet/rails/fact_value.rb
@@ -1,3 +1,3 @@
class Puppet::Rails::FactValue < ActiveRecord::Base
- belongs_to :fact_names
+ belongs_to :fact_name
end
diff --git a/lib/puppet/rails/host.rb b/lib/puppet/rails/host.rb
index fd0642722..a46fa92a5 100644
--- a/lib/puppet/rails/host.rb
+++ b/lib/puppet/rails/host.rb
@@ -2,10 +2,12 @@ require 'puppet/rails/resource'
class Puppet::Rails::Host < ActiveRecord::Base
has_many :fact_values, :through => :fact_names
- has_many :fact_names
+ has_many :fact_names, :dependent => :destroy
belongs_to :puppet_classes
has_many :source_files
- has_many :resources, :include => [ :param_names, :param_values ]
+ has_many :resources,
+ :include => [ :param_names, :param_values ],
+ :dependent => :destroy
acts_as_taggable
@@ -33,30 +35,20 @@ class Puppet::Rails::Host < ActiveRecord::Base
raise ArgumentError, "You must specify the hostname for storage"
end
- create = true
-
args = {}
if hash[:facts].include?("ipaddress")
args[:ip] = hash[:facts]["ipaddress"]
end
- host = nil
- Puppet::Util.benchmark(:info, "Found/created host") do
- host = self.find_or_create_by_name(hash[:facts]["hostname"], args)
+ unless host = find_by_name(hash[:facts]["hostname"])
+ host = new(:name => hash[:facts]["hostname"])
end
- Puppet::Util.benchmark(:info, "Converted facts") do
- hash[:facts].each do |name, value|
- if create
- fn = host.fact_names.find_or_create_by_name(name)
- fv = fn.fact_values.find_or_create_by_value(value)
- else
- fn = host.fact_names.find_by_name(name) || host.fact_names.new(:name => name)
- unless fv = fn.fact_values.find_by_value(value)
- fn.fact_values << fn.fact_values.new(:value => value)
- end
- end
- host.fact_names << fn
+ # Store the facts into the
+ hash[:facts].each do |name, value|
+ fn = host.fact_names.find_by_name(name) || host.fact_names.build(:name => name)
+ unless fn.fact_values.find_by_value(value)
+ fn.fact_values.build(:value => value)
end
end
@@ -64,39 +56,12 @@ class Puppet::Rails::Host < ActiveRecord::Base
raise ArgumentError, "You must pass resources"
end
- Puppet::Util.benchmark(:info, "Converted resources") do
- hash[:resources].each do |resource|
- resargs = resource.to_hash.stringify_keys
-
- if create
- res = host.resources.find_or_create_by_restype_and_title(resource[:type], resource[:title])
- else
- unless res = host.resources.find_by_restype_and_title(resource[:type], resource[:title])
- res = host.resources.new(:restype => resource[:type], :title => resource[:title])
- host.resources << res
- end
- end
-
- resargs.each do |param, value|
- if create
- pn = res.param_names.find_or_create_by_name(param)
- pv = pn.param_values.find_or_create_by_value(value)
- else
- unless pn = res.param_names.find_by_name(param)
- pn = res.param_names.new(:name => param)
- end
- unless pn.param_values.find_by_value(value)
- pn.param_values << pn.param_values.new(:value => value)
- end
- end
- res.param_names << pn
- end
- end
+ resources = []
+ hash[:resources].each do |resource|
+ resources << resource.to_rails(host)
end
- Puppet::Util.benchmark(:info, "Saved host to database") do
- host.save
- end
+ host.save
return host
end
diff --git a/lib/puppet/rails/param_name.rb b/lib/puppet/rails/param_name.rb
index 928838f5c..dba6960da 100644
--- a/lib/puppet/rails/param_name.rb
+++ b/lib/puppet/rails/param_name.rb
@@ -1,13 +1,17 @@
class Puppet::Rails::ParamName < ActiveRecord::Base
- has_many :param_values
- belongs_to :resources
+ has_many :param_values, :dependent => :destroy
+ belongs_to :resource
def to_resourceparam(source)
hash = {}
hash[:name] = self.name.to_sym
hash[:source] = source
- hash[:value] = self.param_values.find(:first).value
+ hash[:value] = self.param_values.find(:all).collect { |v| v.value }
+ if hash[:value].empty?
+ hash[:value] = nil
+ end
Puppet::Parser::Resource::Param.new hash
end
end
+# $Id$
diff --git a/lib/puppet/rails/param_value.rb b/lib/puppet/rails/param_value.rb
index b01add4a7..d988559af 100644
--- a/lib/puppet/rails/param_value.rb
+++ b/lib/puppet/rails/param_value.rb
@@ -1,5 +1,5 @@
class Puppet::Rails::ParamValue < ActiveRecord::Base
- belongs_to :param_names
-
+ belongs_to :param_name
end
+# $Id$
diff --git a/lib/puppet/rails/resource.rb b/lib/puppet/rails/resource.rb
index 423b227ad..c0a7fbd8c 100644
--- a/lib/puppet/rails/resource.rb
+++ b/lib/puppet/rails/resource.rb
@@ -4,29 +4,51 @@ require 'puppet/rails/param_name'
class Puppet::Rails::Resource < ActiveRecord::Base
has_many :param_values, :through => :param_names
- has_many :param_names
+ has_many :param_names, :dependent => :destroy
has_many :source_files
- belongs_to :hosts
+ belongs_to :host
acts_as_taggable
+ def [](param)
+ return super || parameter(param)
+ end
+
+ def parameter(param)
+ if pn = param_names.find_by_name(param)
+ if pv = pn.param_values.find(:first)
+ return pv.value
+ else
+ return nil
+ end
+ end
+ end
+
def parameters
hash = {}
self.param_values.find(:all).each do |pvalue|
- pname = self.param_names.find(:first)
- hash.store(pname.name, pvalue.value)
+ pname = pvalue.param_name.name
+ hash.store(pname, pvalue.value)
end
return hash
end
+ def ref
+ "%s[%s]" % [self[:restype], self[:title]]
+ end
+
# Convert our object to a resource. Do not retain whether the object
- # is collectable, though, since that would cause it to get stripped
+ # is exported, though, since that would cause it to get stripped
# from the configuration.
def to_resource(scope)
hash = self.attributes
hash["type"] = hash["restype"]
hash.delete("restype")
+
+ # FIXME At some point, we're going to want to retain this information
+ # for logging and auditing.
hash.delete("host_id")
+
hash.delete("source_file_id")
hash.delete("id")
hash.each do |p, v|
@@ -39,6 +61,9 @@ class Puppet::Rails::Resource < ActiveRecord::Base
obj.set(pname.to_resourceparam(scope.source))
end
+ # Store the ID, so we can check if we're re-collecting the same resource.
+ obj.rails_id = self.id
+
return obj
end
end