summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-11-13 07:49:48 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-11-13 07:49:48 +0000
commit35de0e3bd74e94de92ac0e9209a4884cf07c88a0 (patch)
treebe7119181bc15dad6575cc5b7087c92cfd7fbac8
parent26b32b953d8e097785b38aa90e5e4bff6ae9247e (diff)
downloadpuppet-35de0e3bd74e94de92ac0e9209a4884cf07c88a0.tar.gz
puppet-35de0e3bd74e94de92ac0e9209a4884cf07c88a0.tar.xz
puppet-35de0e3bd74e94de92ac0e9209a4884cf07c88a0.zip
Temporarily reverting all of the recent rails work so that I can release 0.20.1
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1873 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r--lib/puppet.rb3
-rw-r--r--lib/puppet/parser/collector.rb2
-rw-r--r--lib/puppet/parser/resource.rb12
-rw-r--r--lib/puppet/parser/resource/param.rb10
-rw-r--r--lib/puppet/rails.rb37
-rw-r--r--lib/puppet/rails/database/01_puppet_initialize.rb45
-rw-r--r--lib/puppet/rails/database/schema_init.rb64
-rw-r--r--lib/puppet/rails/fact_name.rb3
-rw-r--r--lib/puppet/rails/fact_value.rb3
-rw-r--r--lib/puppet/rails/host.rb59
-rw-r--r--lib/puppet/rails/lib/README4
-rw-r--r--lib/puppet/rails/lib/acts_as_taggable.rb58
-rw-r--r--lib/puppet/rails/lib/init.rb5
-rw-r--r--lib/puppet/rails/lib/tag.rb40
-rw-r--r--lib/puppet/rails/lib/tagging.rb12
-rw-r--r--lib/puppet/rails/param_name.rb13
-rw-r--r--lib/puppet/rails/param_value.rb5
-rw-r--r--lib/puppet/rails/puppet_class.rb7
-rw-r--r--lib/puppet/rails/rails_parameter.rb13
-rw-r--r--lib/puppet/rails/rails_resource.rb34
-rw-r--r--lib/puppet/rails/resource.rb49
-rw-r--r--lib/puppet/rails/source_file.rb3
-rw-r--r--test/lib/puppettest/railstesting.rb10
-rwxr-xr-xtest/rails/rails.rb11
-rwxr-xr-xtest/rails/railsparameter.rb31
-rwxr-xr-xtest/rails/railsresource.rb27
26 files changed, 178 insertions, 382 deletions
diff --git a/lib/puppet.rb b/lib/puppet.rb
index d621edb93..a6e8aef1a 100644
--- a/lib/puppet.rb
+++ b/lib/puppet.rb
@@ -391,8 +391,5 @@ end
require 'puppet/server'
require 'puppet/type'
require 'puppet/storage'
-if Puppet[:storeconfigs]
- require 'puppet/rails'
-end
# $Id$
diff --git a/lib/puppet/parser/collector.rb b/lib/puppet/parser/collector.rb
index 2b4e71d77..61807717d 100644
--- a/lib/puppet/parser/collector.rb
+++ b/lib/puppet/parser/collector.rb
@@ -27,7 +27,7 @@ class Puppet::Parser::Collector
# and such, we'll need to vary the conditions, but this works with no
# attributes, anyway.
Puppet::Util.benchmark(:debug, "Collected #{self.type} resources") do
- Puppet::Rails::Resource.find_all_by_restype_and_exported(@type, true,
+ Puppet::Rails::RailsResource.find_all_by_restype_and_exported(@type, true,
args
).each do |obj|
count += 1
diff --git a/lib/puppet/parser/resource.rb b/lib/puppet/parser/resource.rb
index 8c1153929..cb339e38d 100644
--- a/lib/puppet/parser/resource.rb
+++ b/lib/puppet/parser/resource.rb
@@ -256,25 +256,27 @@ class Puppet::Parser::Resource
# with.
def store(host)
args = {}
- #FIXME: support files/lines, etc.
- #%w{type title tags file line exported}.each do |param|
- %w{type title tags exported}.each do |param|
+ %w{type title tags file line exported}.each do |param|
if value = self.send(param)
args[param] = value
end
end
+ # 'type' isn't a valid column name, so we have to use something else.
args = symbolize_options(args)
+ args[:restype] = args[:type]
+ args.delete(:type)
# Let's see if the object exists
- if obj = host.resources.find_by_type_and_title(self.type, self.title)
+ #if obj = host.rails_resources.find_by_type_and_title(self.type, self.title)
+ if obj = host.rails_resources.find_by_restype_and_title(self.type, self.title)
# We exist
args.each do |param, value|
obj[param] = value
end
else
# Else create it anew
- obj = host.resources.build(args)
+ obj = host.rails_resources.build(args)
end
# Either way, now add our parameters
diff --git a/lib/puppet/parser/resource/param.rb b/lib/puppet/parser/resource/param.rb
index bcd59573a..600e44781 100644
--- a/lib/puppet/parser/resource/param.rb
+++ b/lib/puppet/parser/resource/param.rb
@@ -18,22 +18,20 @@ class Puppet::Parser::Resource::Param
# Store this parameter in a Rails db.
def store(resource)
args = {}
- #FIXME: re-add line/file support
- #[:name, :value, :line, :file].each do |var|
- [:name, :value ].each do |var|
+ [:name, :value, :line, :file].each do |var|
if val = self.send(var)
args[var] = val
end
end
args[:name] = args[:name].to_s
- if pname = resource.param_names.find_by_name(self.name)
+ if obj = resource.rails_parameters.find_by_name(self.name)
# We exist
args.each do |p, v|
- pname.param_values.build(v)
+ obj[p] = v
end
else
# Else create it anew
- obj = resource.param_names.build(:name => self.class)
+ obj = resource.rails_parameters.build(args)
end
return obj
diff --git a/lib/puppet/rails.rb b/lib/puppet/rails.rb
index ce1e3ebe9..959ea5628 100644
--- a/lib/puppet/rails.rb
+++ b/lib/puppet/rails.rb
@@ -34,10 +34,7 @@ unless defined? ActiveRecord
end
module Puppet::Rails
-require 'puppet/rails/database/schema_init'
-
Puppet.config.setdefaults(:puppetmaster,
- #this should be changed to use $statedir, but for now it only works this way.
:dblocation => { :default => "$statedir/clientconfigs.sqlite3",
:mode => 0600,
:owner => "$user",
@@ -47,13 +44,12 @@ require 'puppet/rails/database/schema_init'
},
:dbadapter => [ "sqlite3", "The type of database to use." ],
:dbname => [ "puppet", "The name of the database to use." ],
- :dbserver => [ "localhost", "The database server for Client caching. Only
+ :dbserver => [ "puppet", "The database server for Client caching. Only
used when networked databases are used."],
:dbuser => [ "puppet", "The database user for Client caching. Only
used when networked databases are used."],
:dbpassword => [ "puppet", "The database password for Client caching. Only
used when networked databases are used."],
- #this should be changed to use $logdir, but for now it only works this way.
:railslog => {:default => "$logdir/puppetrails.log",
:mode => 0600,
:owner => "$user",
@@ -74,25 +70,25 @@ require 'puppet/rails/database/schema_init'
end
- ActiveRecord::Base.logger = Logger.new(Puppet[:railslog])
# This global init does not work for testing, because we remove
# the state dir on every test.
#unless (defined? @inited and @inited) or defined? Test::Unit::TestCase
unless (defined? @inited and @inited)
- Puppet.config.use(:puppetmaster)
+ Puppet.config.use(:puppet)
+ ActiveRecord::Base.logger = Logger.new(Puppet[:railslog])
args = {:adapter => Puppet[:dbadapter]}
case Puppet[:dbadapter]
when "sqlite3":
args[:database] = Puppet[:dblocation]
- unless FileTest.exists?(Puppet[:dblocation])
- Puppet.config.use(:puppet)
- Puppet.config.write(:dblocation) do |f|
- f.print ""
- end
- end
+ #unless FileTest.exists?(Puppet[:dblocation])
+ # Puppet.config.use(:puppet)
+ # Puppet.config.write(:dblocation) do |f|
+ # f.print ""
+ # end
+ #end
when "mysql":
args[:host] = Puppet[:dbserver]
@@ -109,20 +105,17 @@ require 'puppet/rails/database/schema_init'
end
raise Puppet::Error, "Could not connect to database: %s" % detail
end
- begin
- @inited = true if ActiveRecord::Base.connection.tables.include? "resources"
- rescue SQLite3::CantOpenException => detail
- @inited = false
- end
#puts "Database initialized: #{@inited.inspect} "
end
+ ActiveRecord::Base.logger = Logger.new(Puppet[:railslog])
+
+ if Puppet[:dbadapter] == "sqlite3" and ! FileTest.exists?(Puppet[:dblocation])
- if @inited
dbdir = nil
$:.each { |d|
tmp = File.join(d, "puppet/rails/database")
if FileTest.directory?(tmp)
- dbdir = tmp
+ dbdir = tmp
end
}
@@ -138,10 +131,8 @@ require 'puppet/rails/database/schema_init'
end
raise Puppet::Error, "Could not initialize database: %s" % detail
end
- else
- Puppet::Rails::Schema.init
end
- Puppet.config.use(:puppet)
+ Puppet.config.use(:puppetmaster)
ActiveRecord::Base.logger = Logger.new(Puppet[:railslog])
end
end
diff --git a/lib/puppet/rails/database/01_puppet_initialize.rb b/lib/puppet/rails/database/01_puppet_initialize.rb
new file mode 100644
index 000000000..485634004
--- /dev/null
+++ b/lib/puppet/rails/database/01_puppet_initialize.rb
@@ -0,0 +1,45 @@
+class PuppetInitialize < ActiveRecord::Migration
+ require 'sqlite3'
+
+ def self.up
+ if ActiveRecord::Migration.respond_to?(:verbose)
+ ActiveRecord::Migration.verbose = false
+ end
+
+ # '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 :exported, :boolean
+ end
+
+ create_table :rails_parameters do |table|
+ table.column :name, :string, :null => false
+ table.column :value, :string, :null => false
+ table.column :file, :string
+ table.column :line, :integer
+ table.column :rails_resource_id, :integer
+ end
+
+ create_table :hosts do |table|
+ table.column :name, :string, :null => false
+ table.column :ip, :string
+ table.column :facts, :string
+ table.column :connect, :date
+ table.column :success, :date
+ table.column :classes, :string
+ end
+ end
+
+ def self.down
+ drop_table :rails_resources
+ drop_table :rails_parameters
+ drop_table :hosts
+ end
+end
+
+# $Id$
diff --git a/lib/puppet/rails/database/schema_init.rb b/lib/puppet/rails/database/schema_init.rb
deleted file mode 100644
index 9151bee46..000000000
--- a/lib/puppet/rails/database/schema_init.rb
+++ /dev/null
@@ -1,64 +0,0 @@
-class Puppet::Rails::Schema
-
-def self.init
- ActiveRecord::Schema.define do
- create_table :resources do |t|
- t.column :title, :string, :null => false
- t.column :type, :string
- t.column :host_id, :integer
- t.column :source_file_id, :integer
- t.column :exported, :boolean
- end
-
- create_table :source_files do |t|
- t.column :filename, :string
- t.column :path, :string
- end
-
- create_table :puppet_classes do |t|
- t.column :name, :string
- t.column :host_id, :integer
- t.column :source_file_id, :integer
- end
-
- create_table :hosts do |t|
- t.column :name, :string, :null => false
- t.column :ip, :string
- t.column :connect, :date
- #Use updated_at to automatically add timestamp on save.
- t.column :updated_at, :date
- t.column :source_file_id, :integer
- end
-
- create_table :fact_names do |t|
- t.column :name, :string, :null => false
- t.column :host_id, :integer, :null => false
- end
-
- create_table :fact_values do |t|
- t.column :value, :string, :null => false
- t.column :fact_name_id, :integer, :null => false
- end
-
- create_table :param_values do |t|
- t.column :value, :string, :null => false
- t.column :param_name_id, :integer, :null => false
- end
-
- create_table :param_names do |t|
- t.column :name, :string, :null => false
- t.column :resource_id, :integer
- end
-
- create_table :tags do |t|
- t.column :name, :string
- end
-
- create_table :taggings do |t|
- t.column :tag_id, :integer
- t.column :taggable_id, :integer
- t.column :taggable_type, :string
- end
- end
-end
-end
diff --git a/lib/puppet/rails/fact_name.rb b/lib/puppet/rails/fact_name.rb
deleted file mode 100644
index 886618ecb..000000000
--- a/lib/puppet/rails/fact_name.rb
+++ /dev/null
@@ -1,3 +0,0 @@
-class Puppet::Rails::FactName < ActiveRecord::Base
- has_many :fact_values
-end
diff --git a/lib/puppet/rails/fact_value.rb b/lib/puppet/rails/fact_value.rb
deleted file mode 100644
index 4da74b713..000000000
--- a/lib/puppet/rails/fact_value.rb
+++ /dev/null
@@ -1,3 +0,0 @@
-class Puppet::Rails::FactValue < ActiveRecord::Base
- belongs_to :fact_names
-end
diff --git a/lib/puppet/rails/host.rb b/lib/puppet/rails/host.rb
index 72ec64815..ccda1af64 100644
--- a/lib/puppet/rails/host.rb
+++ b/lib/puppet/rails/host.rb
@@ -1,18 +1,12 @@
-require 'puppet/rails/resource'
+require 'puppet/rails/rails_resource'
+#RailsObject = Puppet::Rails::RailsObject
class Puppet::Rails::Host < ActiveRecord::Base
- has_many :fact_values, :through => :fact_names
- has_many :fact_names
- belongs_to :puppet_classes
- has_many :source_files
- has_many :resources, :include => [ :param_names, :param_values ]
+ serialize :facts, Hash
+ serialize :classes, Array
- acts_as_taggable
-
- def facts(name)
- fv = self.fact_values.find(:first, :conditions => "fact_names.name = '#{name}'")
- return fv.value
- end
+ has_many :rails_resources, :dependent => :delete_all,
+ :include => :rails_parameters
# If the host already exists, get rid of its objects
def self.clean(host)
@@ -31,42 +25,33 @@ class Puppet::Rails::Host < ActiveRecord::Base
end
args = {}
+ [:name, :facts, :classes].each do |param|
+ if hash[param]
+ args[param] = hash[param]
+ end
+ end
if hash[:facts].include?("ipaddress")
args[:ip] = hash[:facts]["ipaddress"]
end
- host = self.find_or_create_by_name(hash[:facts]["hostname"], args)
-
- hash[:facts].each do |name, value|
- fn = host.fact_names.find_or_create_by_name(name)
- fv = fn.fact_values.find_or_create_by_value(value)
- host.fact_names << fn
- end
unless hash[:resources]
raise ArgumentError, "You must pass resources"
end
- typenames = []
- Puppet::Type.eachtype do |type|
- typenames << type.name.to_s
- end
-
- hash[:resources].each do |resource|
- resargs = resource.to_hash.stringify_keys
-
- if typenames.include?(resource.instance_eval("type"))
- rtype = "Puppet#{resource.instance_eval("type").capitalize}"
+ if host = self.find_by_name(hash[:name])
+ args.each do |param, value|
+ unless host[param] == args[param]
+ host[param] = args[param]
+ end
end
+ else
+ # Create it anew
+ host = self.new(args)
+ end
- res = host.resources.find_or_create_by_title(resource[:title])
- res.type = rtype
- res.save
- resargs.each do |param, value|
- pn = res.param_names.find_or_create_by_name(param)
- pv = pn.param_values.find_or_create_by_value(value)
- res.param_names << pn
- end
+ hash[:resources].each do |res|
+ res.store(host)
end
Puppet::Util.benchmark(:info, "Saved host to database") do
diff --git a/lib/puppet/rails/lib/README b/lib/puppet/rails/lib/README
deleted file mode 100644
index 8d2f90822..000000000
--- a/lib/puppet/rails/lib/README
+++ /dev/null
@@ -1,4 +0,0 @@
-Acts As Taggable
-=================
-
-Allows for tags to be added to multiple classes. \ No newline at end of file
diff --git a/lib/puppet/rails/lib/acts_as_taggable.rb b/lib/puppet/rails/lib/acts_as_taggable.rb
deleted file mode 100644
index e9bd03696..000000000
--- a/lib/puppet/rails/lib/acts_as_taggable.rb
+++ /dev/null
@@ -1,58 +0,0 @@
-module ActiveRecord
- module Acts #:nodoc:
- module Taggable #:nodoc:
- def self.included(base)
- base.extend(ClassMethods)
- end
-
- module ClassMethods
- def acts_as_taggable(options = {})
- write_inheritable_attribute(:acts_as_taggable_options, {
- :taggable_type => ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s,
- :from => options[:from]
- })
-
- class_inheritable_reader :acts_as_taggable_options
-
- has_many :taggings, :as => :taggable, :dependent => true
- has_many :tags, :through => :taggings
-
- include ActiveRecord::Acts::Taggable::InstanceMethods
- extend ActiveRecord::Acts::Taggable::SingletonMethods
- end
- end
-
- module SingletonMethods
- def find_tagged_with(list)
- find_by_sql([
- "SELECT #{table_name}.* FROM #{table_name}, tags, taggings " +
- "WHERE #{table_name}.#{primary_key} = taggings.taggable_id " +
- "AND taggings.taggable_type = ? " +
- "AND taggings.tag_id = tags.id AND tags.name IN (?)",
- acts_as_taggable_options[:taggable_type], list
- ])
- end
- end
-
- module InstanceMethods
- def tag_with(list)
- Tag.transaction do
- taggings.destroy_all
-
- Tag.parse(list).each do |name|
- if acts_as_taggable_options[:from]
- send(acts_as_taggable_options[:from]).tags.find_or_create_by_name(name).on(self)
- else
- Tag.find_or_create_by_name(name).on(self)
- end
- end
- end
- end
-
- def tag_list
- tags.collect { |tag| tag.name.include?(" ") ? "'#{tag.name}'" : tag.name }.join(" ")
- end
- end
- end
- end
-end \ No newline at end of file
diff --git a/lib/puppet/rails/lib/init.rb b/lib/puppet/rails/lib/init.rb
deleted file mode 100644
index 0a07e5c8e..000000000
--- a/lib/puppet/rails/lib/init.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require 'puppet/rails/lib/acts_as_taggable'
-ActiveRecord::Base.send(:include, ActiveRecord::Acts::Taggable)
-
-require 'puppet/rails/lib/tagging'
-require 'puppet/rails/lib/tag'
diff --git a/lib/puppet/rails/lib/tag.rb b/lib/puppet/rails/lib/tag.rb
deleted file mode 100644
index ca31171b2..000000000
--- a/lib/puppet/rails/lib/tag.rb
+++ /dev/null
@@ -1,40 +0,0 @@
-class Tag < ActiveRecord::Base
- has_many :taggings
-
- def self.parse(list)
- tag_names = []
-
- # first, pull out the quoted tags
- list.gsub!(/\"(.*?)\"\s*/ ) { tag_names << $1; "" }
-
- # then, replace all commas with a space
- list.gsub!(/,/, " ")
-
- # then, get whatever's left
- tag_names.concat list.split(/\s/)
-
- # strip whitespace from the names
- tag_names = tag_names.map { |t| t.strip }
-
- # delete any blank tag names
- tag_names = tag_names.delete_if { |t| t.empty? }
-
- return tag_names
- end
-
- def tagged
- @tagged ||= taggings.collect { |tagging| tagging.taggable }
- end
-
- def on(taggable)
- taggings.create :taggable => taggable
- end
-
- def ==(comparison_object)
- super || name == comparison_object.to_s
- end
-
- def to_s
- name
- end
-end \ No newline at end of file
diff --git a/lib/puppet/rails/lib/tagging.rb b/lib/puppet/rails/lib/tagging.rb
deleted file mode 100644
index e06e88a14..000000000
--- a/lib/puppet/rails/lib/tagging.rb
+++ /dev/null
@@ -1,12 +0,0 @@
-class Tagging < ActiveRecord::Base
- belongs_to :tag
- belongs_to :taggable, :polymorphic => true
-
- def self.tagged_class(taggable)
- ActiveRecord::Base.send(:class_name_of_active_record_descendant, taggable.class).to_s
- end
-
- def self.find_taggable(tagged_class, tagged_id)
- tagged_class.constantize.find(tagged_id)
- end
-end \ No newline at end of file
diff --git a/lib/puppet/rails/param_name.rb b/lib/puppet/rails/param_name.rb
deleted file mode 100644
index 928838f5c..000000000
--- a/lib/puppet/rails/param_name.rb
+++ /dev/null
@@ -1,13 +0,0 @@
-class Puppet::Rails::ParamName < ActiveRecord::Base
- has_many :param_values
- belongs_to :resources
-
- def to_resourceparam(source)
- hash = {}
- hash[:name] = self.name.to_sym
- hash[:source] = source
- hash[:value] = self.param_values.find(:first).value
- Puppet::Parser::Resource::Param.new hash
- end
-end
-
diff --git a/lib/puppet/rails/param_value.rb b/lib/puppet/rails/param_value.rb
deleted file mode 100644
index b01add4a7..000000000
--- a/lib/puppet/rails/param_value.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-class Puppet::Rails::ParamValue < ActiveRecord::Base
- belongs_to :param_names
-
-end
-
diff --git a/lib/puppet/rails/puppet_class.rb b/lib/puppet/rails/puppet_class.rb
deleted file mode 100644
index de54a31e9..000000000
--- a/lib/puppet/rails/puppet_class.rb
+++ /dev/null
@@ -1,7 +0,0 @@
-class Puppet::Rails::PuppetClass < ActiveRecord::Base
- has_many :resources
- has_many :source_files
- has_many :hosts
-
- acts_as_taggable
-end
diff --git a/lib/puppet/rails/rails_parameter.rb b/lib/puppet/rails/rails_parameter.rb
new file mode 100644
index 000000000..295662146
--- /dev/null
+++ b/lib/puppet/rails/rails_parameter.rb
@@ -0,0 +1,13 @@
+class Puppet::Rails::RailsParameter < ActiveRecord::Base
+ 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$
diff --git a/lib/puppet/rails/resource.rb b/lib/puppet/rails/resource.rb
deleted file mode 100644
index bbe71305c..000000000
--- a/lib/puppet/rails/resource.rb
+++ /dev/null
@@ -1,49 +0,0 @@
-require 'puppet'
-require 'puppet/rails/lib/init'
-require 'puppet/rails/param_name'
-
-class Puppet::Rails::Resource < ActiveRecord::Base
- has_many :param_values, :through => :param_names
- has_many :param_names
- has_many :source_files
- belongs_to :hosts
-
- acts_as_taggable
-
- Puppet::Type.eachtype do |type|
- klass = Class.new(Puppet::Rails::Resource)
- Object.const_set("Puppet%s" % type.name.to_s.capitalize, klass)
- end
-
- def parameters
- hash = {}
- self.param_values.find(:all).each do |pvalue|
- pname = self.param_names.find(:first)
- hash.store(pname.name.to_sym, pvalue.value)
- end
- return hash
- end
-
- # 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.delete("type")
- hash.delete("host_id")
- hash.delete("source_file_id")
- hash.delete("id")
- hash.each do |p, v|
- hash.delete(p) if v.nil?
- end
- hash[:type] = self.class.to_s.gsub(/Puppet/,'').downcase
- 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))
- end
-
- return obj
- end
-end
diff --git a/lib/puppet/rails/source_file.rb b/lib/puppet/rails/source_file.rb
deleted file mode 100644
index d300ec74c..000000000
--- a/lib/puppet/rails/source_file.rb
+++ /dev/null
@@ -1,3 +0,0 @@
-class Puppet::Rails::SourceFile < ActiveRecord::Base
- has_many :hosts, :puppet_classes, :resources
-end
diff --git a/test/lib/puppettest/railstesting.rb b/test/lib/puppettest/railstesting.rb
index e3b472b4f..3a32d0c9e 100644
--- a/test/lib/puppettest/railstesting.rb
+++ b/test/lib/puppettest/railstesting.rb
@@ -14,14 +14,16 @@ module PuppetTest::RailsTesting
host = Puppet::Rails::Host.new(:name => Facter.value("hostname"))
# Now build a resource
- resource = host.resources.build(
- :title => title, :exported => true
+ resource = host.rails_resources.build(
+ :title => title, :restype => type,
+ :exported => true
)
# Now add some params
params.each do |param, value|
- pvalue = ParamValue.new(:value => value)
- resource.param_name.find_or_create_by_name(param).param_values << pvalue
+ resource.rails_parameters.build(
+ :name => param, :value => value
+ )
end
# Now save the whole thing
diff --git a/test/rails/rails.rb b/test/rails/rails.rb
index 699d132a2..8be1bc66d 100755
--- a/test/rails/rails.rb
+++ b/test/rails/rails.rb
@@ -60,14 +60,13 @@ class TestRails < Test::Unit::TestCase
}
assert(host, "Could not find host object")
- assert(host.resources, "No objects on host")
+ assert(host.rails_resources, "No objects on host")
- # This changed from a hash to a method call, hope that doesn't break anything!
- assert_equal(facts["hostname"], host.facts("hostname"),
+ assert_equal(facts["hostname"], host.facts["hostname"],
"Did not retrieve facts")
count = 0
- host.resources.each do |resource|
+ host.rails_resources.each do |resource|
count += 1
i = nil
if resource[:title] =~ /file([0-9]+)/
@@ -75,7 +74,9 @@ class TestRails < Test::Unit::TestCase
else
raise "Got weird resource %s" % resource.inspect
end
- assert_equal("user#{i}", resource.parameters[:owner])
+
+ assert_equal("user#{i}",
+ resource.rails_parameters.find_by_name("owner")[:value])
end
assert_equal(20, count, "Did not get enough resources")
diff --git a/test/rails/railsparameter.rb b/test/rails/railsparameter.rb
index f76939ac9..46a12e57c 100755
--- a/test/rails/railsparameter.rb
+++ b/test/rails/railsparameter.rb
@@ -16,33 +16,32 @@ class TestRailsParameter < Test::Unit::TestCase
def test_to_resourceparam
railsinit
# First create our parameter
- #FIXME Need to re-add file/line support
- pname = { :name => "myname" }
- pvalue = { :value => "myval" }
- pn = Puppet::Rails::ParamName.new(:name => pname[:name])
- pv = Puppet::Rails::ParamValue.new(:value => pvalue[:value])
+ rparam = nil
+ hash = { :name => :myparam, :value => "myval",
+ :file => __FILE__, :line => __LINE__}
assert_nothing_raised do
- pn.param_values << pv
+ rparam = Puppet::Rails::RailsParameter.new(hash)
end
- assert(pn, "Did not create rails parameter")
+ assert(rparam, "Did not create rails parameter")
# The id doesn't get assigned until we save
- pn.save
+ rparam.save
# Now create a source
interp = mkinterp
source = interp.newclass "myclass"
# And try to convert our parameter
- #FIXME Why does this assert prevent the block from executing?
- #assert_nothing_raised do
- pp = pn.to_resourceparam(source)
- #end
-
- assert_instance_of(Puppet::Parser::Resource::Param, pp)
- pname.each do |name, value|
- assert_equal(value.to_sym, pp.send(name), "%s was not equal" % name)
+ pparam = nil
+ assert_nothing_raised do
+ pparam = rparam.to_resourceparam(source)
+ end
+
+
+ assert_instance_of(Puppet::Parser::Resource::Param, pparam)
+ hash.each do |name, value|
+ assert_equal(value, pparam.send(name), "%s was not equal" % name)
end
end
end
diff --git a/test/rails/railsresource.rb b/test/rails/railsresource.rb
index e109ba7d1..666d26bcf 100755
--- a/test/rails/railsresource.rb
+++ b/test/rails/railsresource.rb
@@ -22,38 +22,33 @@ class TestRailsResource < Test::Unit::TestCase
host = Puppet::Rails::Host.new(:name => "myhost")
# Now build a resource
- resource = host.resources.create(
- :title => "/tmp/to_resource",
- :exported => true)
-
- # For some reason the child class doesn't exist until after the resource is created.
- # Probably an issue with the dynamic class generation.
- resource.type = "PuppetFile"
- resource.save
-
+ resource = host.rails_resources.build(
+ :title => "/tmp/to_resource", :restype => "file",
+ :exported => true
+ )
+
# Now add some params
{"owner" => "root", "mode" => "644"}.each do |param, value|
- pn = resource.param_names.find_or_create_by_name(param)
- pv = pn.param_values.find_or_create_by_value(value)
- resource.param_names << pn
+ resource.rails_parameters.build(
+ :name => param, :value => value
+ )
end
# Now save the whole thing
host.save
+ # Now, try to convert our resource to a real resource
# We need a scope
interp, scope, source = mkclassframing
- # Find the new resource and include all it's parameters.
- resource = Puppet::Rails::Resource.find_by_id(resource.id, :include => [ :param_names, :param_values ])
-
- # Now, try to convert our resource to a real resource
res = nil
assert_nothing_raised do
res = resource.to_resource(scope)
end
+
assert_instance_of(Puppet::Parser::Resource, res)
+
assert_equal("root", res[:owner])
assert_equal("644", res[:mode])
assert_equal("/tmp/to_resource", res.title)