summaryrefslogtreecommitdiffstats
path: root/lib/puppet/rails
diff options
context:
space:
mode:
Diffstat (limited to 'lib/puppet/rails')
-rw-r--r--lib/puppet/rails/database.rb17
-rw-r--r--lib/puppet/rails/host.rb54
-rw-r--r--lib/puppet/rails/rails_object.rb42
-rw-r--r--lib/puppet/rails/rails_parameter.rb10
-rw-r--r--lib/puppet/rails/rails_resource.rb34
5 files changed, 83 insertions, 74 deletions
diff --git a/lib/puppet/rails/database.rb b/lib/puppet/rails/database.rb
index caf87cbb8..8be05bd88 100644
--- a/lib/puppet/rails/database.rb
+++ b/lib/puppet/rails/database.rb
@@ -6,20 +6,23 @@ class Puppet::Rails::Database < ActiveRecord::Migration
ActiveRecord::Migration.verbose = false
end
- create_table :rails_objects do |table|
- table.column :name, :string, :null => false
- table.column :ptype, :string, :null => false
+ # 'type' cannot be a column name, apparently
+ create_table :rails_resources do |table|
+ table.column :title, :string, :null => false
+ table.column :restype, :string, :null => false
table.column :tags, :string
table.column :file, :string
table.column :line, :integer
table.column :host_id, :integer
- table.column :collectable, :boolean
+ table.column :exported, :boolean
end
create_table :rails_parameters do |table|
table.column :name, :string, :null => false
table.column :value, :string, :null => false
- table.column :rails_object_id, :integer
+ table.column :file, :string
+ table.column :line, :integer
+ table.column :rails_resource_id, :integer
end
create_table :hosts do |table|
@@ -33,8 +36,10 @@ class Puppet::Rails::Database < ActiveRecord::Migration
end
def self.down
- drop_table :rails_objects
+ drop_table :rails_resources
drop_table :rails_parameters
drop_table :hosts
end
end
+
+# $Id$
diff --git a/lib/puppet/rails/host.rb b/lib/puppet/rails/host.rb
index 77123c871..ccda1af64 100644
--- a/lib/puppet/rails/host.rb
+++ b/lib/puppet/rails/host.rb
@@ -1,11 +1,12 @@
-require 'puppet/rails/rails_object'
+require 'puppet/rails/rails_resource'
#RailsObject = Puppet::Rails::RailsObject
class Puppet::Rails::Host < ActiveRecord::Base
serialize :facts, Hash
serialize :classes, Array
- has_many :rails_objects, :dependent => :delete_all
+ has_many :rails_resources, :dependent => :delete_all,
+ :include => :rails_parameters
# If the host already exists, get rid of its objects
def self.clean(host)
@@ -19,40 +20,43 @@ class Puppet::Rails::Host < ActiveRecord::Base
# Store our host in the database.
def self.store(hash)
- name = hash[:host] || "localhost"
- ip = hash[:ip] || "127.0.0.1"
- facts = hash[:facts] || {}
- objects = hash[:objects]
+ unless hash[:name]
+ raise ArgumentError, "You must specify the hostname for storage"
+ end
- unless objects
- raise ArgumentError, "You must pass objects"
+ args = {}
+ [:name, :facts, :classes].each do |param|
+ if hash[param]
+ args[param] = hash[param]
+ end
end
- hostargs = {
- :name => name,
- :ip => ip,
- :facts => facts,
- :classes => objects.classes
- }
+ if hash[:facts].include?("ipaddress")
+ args[:ip] = hash[:facts]["ipaddress"]
+ end
- objects = objects.flatten
+ unless hash[:resources]
+ raise ArgumentError, "You must pass resources"
+ end
- host = nil
- if host = clean(name)
- [:name, :facts, :classes].each do |param|
- unless host[param] == hostargs[param]
- host[param] = hostargs[param]
+ if host = self.find_by_name(hash[:name])
+ args.each do |param, value|
+ unless host[param] == args[param]
+ host[param] = args[param]
end
end
- host.addobjects(objects)
else
- host = self.new(hostargs) do |hostobj|
- hostobj.addobjects(objects)
- end
+ # Create it anew
+ host = self.new(args)
end
+ hash[:resources].each do |res|
+ res.store(host)
+ end
- host.save
+ Puppet::Util.benchmark(:info, "Saved host to database") do
+ host.save
+ end
return host
end
diff --git a/lib/puppet/rails/rails_object.rb b/lib/puppet/rails/rails_object.rb
deleted file mode 100644
index d1e58e453..000000000
--- a/lib/puppet/rails/rails_object.rb
+++ /dev/null
@@ -1,42 +0,0 @@
-require 'puppet'
-require 'puppet/rails/rails_parameter'
-
-#RailsParameter = Puppet::Rails::RailsParameter
-class Puppet::Rails::RailsObject < ActiveRecord::Base
- has_many :rails_parameters, :dependent => :delete_all
- serialize :tags, Array
-
- belongs_to :host
-
- # Add a set of parameters.
- def addparams(params)
- params.each do |pname, pvalue|
- rails_parameters.build(
- :name => pname,
- :value => pvalue
- )
-
- #self.rails_parameters << pobj
- end
- end
-
- # Convert our object to a trans_object. Do not retain whether the object
- # is collectable, though, since that would cause it to get stripped
- # from the configuration.
- def to_trans
- obj = Puppet::TransObject.new(name(), ptype())
-
- [:file, :line, :tags].each do |method|
- if val = send(method)
- obj.send(method.to_s + "=", val)
- end
- end
- rails_parameters.each do |param|
- obj[param.name] = param.value
- end
-
- return obj
- end
-end
-
-# $Id$
diff --git a/lib/puppet/rails/rails_parameter.rb b/lib/puppet/rails/rails_parameter.rb
index a1966e3dc..295662146 100644
--- a/lib/puppet/rails/rails_parameter.rb
+++ b/lib/puppet/rails/rails_parameter.rb
@@ -1,5 +1,13 @@
class Puppet::Rails::RailsParameter < ActiveRecord::Base
- belongs_to :rails_objects
+ belongs_to :rails_resources
+
+ def to_resourceparam(source)
+ hash = self.attributes
+ hash[:source] = source
+ hash.delete("rails_resource_id")
+ hash.delete("id")
+ Puppet::Parser::Resource::Param.new hash
+ end
end
# $Id$
diff --git a/lib/puppet/rails/rails_resource.rb b/lib/puppet/rails/rails_resource.rb
new file mode 100644
index 000000000..caad1b460
--- /dev/null
+++ b/lib/puppet/rails/rails_resource.rb
@@ -0,0 +1,34 @@
+require 'puppet'
+require 'puppet/rails/rails_parameter'
+
+#RailsParameter = Puppet::Rails::RailsParameter
+class Puppet::Rails::RailsResource < ActiveRecord::Base
+ has_many :rails_parameters, :dependent => :delete_all
+ serialize :tags, Array
+
+ belongs_to :host
+
+ # Convert our object to a resource. Do not retain whether the object
+ # is collectable, 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")
+ hash.delete("host_id")
+ hash.delete("id")
+ hash.each do |p, v|
+ hash.delete(p) if v.nil?
+ end
+ hash[:scope] = scope
+ hash[:source] = scope.source
+ obj = Puppet::Parser::Resource.new(hash)
+ rails_parameters.each do |param|
+ obj.set(param.to_resourceparam(scope.source))
+ end
+
+ return obj
+ end
+end
+
+# $Id$