summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorshadoi <shadoi@980ebf18-57e1-0310-9a29-db15c13687c0>2006-11-09 18:57:01 +0000
committershadoi <shadoi@980ebf18-57e1-0310-9a29-db15c13687c0>2006-11-09 18:57:01 +0000
commitcf166c25911f521cdf12178ebbe0b39f81473b35 (patch)
tree1a96094de1203cb3d48b6c07255414bea351092a
parent28c283c73388c3f76e1d715c41ebd82ac35ca9a4 (diff)
downloadpuppet-cf166c25911f521cdf12178ebbe0b39f81473b35.tar.gz
puppet-cf166c25911f521cdf12178ebbe0b39f81473b35.tar.xz
puppet-cf166c25911f521cdf12178ebbe0b39f81473b35.zip
Rails stuff part 1
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1837 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r--lib/puppet.rb3
-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/host.rb59
-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
9 files changed, 117 insertions, 83 deletions
diff --git a/lib/puppet.rb b/lib/puppet.rb
index fda428360..e10cc6489 100644
--- a/lib/puppet.rb
+++ b/lib/puppet.rb
@@ -391,5 +391,8 @@ end
require 'puppet/server'
require 'puppet/type'
require 'puppet/storage'
+if Puppet[:storeconfigs]
+ require 'puppet/rails'
+end
# $Id$
diff --git a/lib/puppet/parser/resource.rb b/lib/puppet/parser/resource.rb
index af5d10501..d27c8b27e 100644
--- a/lib/puppet/parser/resource.rb
+++ b/lib/puppet/parser/resource.rb
@@ -252,27 +252,25 @@ class Puppet::Parser::Resource
# with.
def store(host)
args = {}
- %w{type title tags file line exported}.each do |param|
+ #FIXME: support files/lines, etc.
+ #%w{type title tags file line exported}.each do |param|
+ %w{type title tags 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.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)
+ if obj = host.resources.find_by_type_and_title(self.type, self.title)
# We exist
args.each do |param, value|
obj[param] = value
end
else
# Else create it anew
- obj = host.rails_resources.build(args)
+ obj = host.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 600e44781..bcd59573a 100644
--- a/lib/puppet/parser/resource/param.rb
+++ b/lib/puppet/parser/resource/param.rb
@@ -18,20 +18,22 @@ class Puppet::Parser::Resource::Param
# Store this parameter in a Rails db.
def store(resource)
args = {}
- [:name, :value, :line, :file].each do |var|
+ #FIXME: re-add line/file support
+ #[:name, :value, :line, :file].each do |var|
+ [:name, :value ].each do |var|
if val = self.send(var)
args[var] = val
end
end
args[:name] = args[:name].to_s
- if obj = resource.rails_parameters.find_by_name(self.name)
+ if pname = resource.param_names.find_by_name(self.name)
# We exist
args.each do |p, v|
- obj[p] = v
+ pname.param_values.build(v)
end
else
# Else create it anew
- obj = resource.rails_parameters.build(args)
+ obj = resource.param_names.build(:name => self.class)
end
return obj
diff --git a/lib/puppet/rails.rb b/lib/puppet/rails.rb
index 01f3312be..e041f8342 100644
--- a/lib/puppet/rails.rb
+++ b/lib/puppet/rails.rb
@@ -34,8 +34,11 @@ unless defined? ActiveRecord
end
module Puppet::Rails
+require 'puppet/rails/database/schema_init'
+
Puppet.config.setdefaults(:puppetmaster,
- :dblocation => { :default => "$statedir/clientconfigs.sqlite3",
+ #this should be changed to use $statedir, but for now it only works this way.
+ :dblocation => { :default => "#{Puppet[:statedir]}/clientconfigs.sqlite3",
:mode => 0600,
:owner => "$user",
:group => "$group",
@@ -44,19 +47,21 @@ module Puppet::Rails
},
:dbadapter => [ "sqlite3", "The type of database to use." ],
:dbname => [ "puppet", "The name of the database to use." ],
- :dbserver => [ "puppet", "The database server for Client caching. Only
+ :dbserver => [ "localhost", "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."],
- :railslog => {:default => "$logdir/puppetrails.log",
+ #this should be changed to use $logdir, but for now it only works this way.
+ :railslog => {:default => "#{Puppet[:logdir]}/puppetrails.log",
:mode => 0600,
:owner => "$user",
:group => "$group",
:desc => "Where Rails-specific logs are sent"
}
)
+ ActiveRecord::Base.logger = Logger.new(Puppet[:railslog])
def self.clear
@inited = false
@@ -73,14 +78,14 @@ module Puppet::Rails
# the state dir on every test.
#unless (defined? @inited and @inited) or defined? Test::Unit::TestCase
unless (defined? @inited and @inited)
- Puppet.config.use(:puppet)
+ Puppet.config.use(:puppetmaster)
- ActiveRecord::Base.logger = Logger.new(Puppet[:railslog])
args = {:adapter => Puppet[:dbadapter]}
case Puppet[:dbadapter]
when "sqlite3":
args[:database] = Puppet[:dblocation]
+
when "mysql":
args[:host] = Puppet[:dbserver]
args[:username] = Puppet[:dbuser]
@@ -88,18 +93,24 @@ module Puppet::Rails
args[:database] = Puppet[:dbname]
end
- ActiveRecord::Base.establish_connection(args)
-
- @inited = true
+ begin
+ ActiveRecord::Base.establish_connection(args)
+ rescue => detail
+ if Puppet[:trace]
+ puts detail.backtrace
+ end
+ raise Puppet::Error, "Could not connect to database: %s" % detail
+ end
+ @inited = true if ActiveRecord::Base.connection.tables.include? "resources"
+ #puts "Database initialized: #{@inited.inspect} "
end
- 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
}
@@ -115,8 +126,10 @@ module Puppet::Rails
end
raise Puppet::Error, "Could not initialize database: %s" % detail
end
+ else
+ Puppet::Rails::Schema.init
end
- Puppet.config.use(:puppetmaster)
+ Puppet.config.use(:puppet)
end
end
diff --git a/lib/puppet/rails/host.rb b/lib/puppet/rails/host.rb
index ccda1af64..72ec64815 100644
--- a/lib/puppet/rails/host.rb
+++ b/lib/puppet/rails/host.rb
@@ -1,12 +1,18 @@
-require 'puppet/rails/rails_resource'
+require 'puppet/rails/resource'
-#RailsObject = Puppet::Rails::RailsObject
class Puppet::Rails::Host < ActiveRecord::Base
- serialize :facts, Hash
- serialize :classes, Array
+ 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 ]
- has_many :rails_resources, :dependent => :delete_all,
- :include => :rails_parameters
+ acts_as_taggable
+
+ def facts(name)
+ fv = self.fact_values.find(:first, :conditions => "fact_names.name = '#{name}'")
+ return fv.value
+ end
# If the host already exists, get rid of its objects
def self.clean(host)
@@ -25,33 +31,42 @@ 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
- 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)
+ typenames = []
+ Puppet::Type.eachtype do |type|
+ typenames << type.name.to_s
end
- hash[:resources].each do |res|
- res.store(host)
+ 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}"
+ 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
end
Puppet::Util.benchmark(:info, "Saved host to database") do
diff --git a/test/lib/puppettest/railstesting.rb b/test/lib/puppettest/railstesting.rb
index 3a32d0c9e..e3b472b4f 100644
--- a/test/lib/puppettest/railstesting.rb
+++ b/test/lib/puppettest/railstesting.rb
@@ -14,16 +14,14 @@ module PuppetTest::RailsTesting
host = Puppet::Rails::Host.new(:name => Facter.value("hostname"))
# Now build a resource
- resource = host.rails_resources.build(
- :title => title, :restype => type,
- :exported => true
+ resource = host.resources.build(
+ :title => title, :exported => true
)
# Now add some params
params.each do |param, value|
- resource.rails_parameters.build(
- :name => param, :value => value
- )
+ pvalue = ParamValue.new(:value => value)
+ resource.param_name.find_or_create_by_name(param).param_values << pvalue
end
# Now save the whole thing
diff --git a/test/rails/rails.rb b/test/rails/rails.rb
index 8be1bc66d..699d132a2 100755
--- a/test/rails/rails.rb
+++ b/test/rails/rails.rb
@@ -60,13 +60,14 @@ class TestRails < Test::Unit::TestCase
}
assert(host, "Could not find host object")
- assert(host.rails_resources, "No objects on host")
+ assert(host.resources, "No objects on host")
- assert_equal(facts["hostname"], host.facts["hostname"],
+ # This changed from a hash to a method call, hope that doesn't break anything!
+ assert_equal(facts["hostname"], host.facts("hostname"),
"Did not retrieve facts")
count = 0
- host.rails_resources.each do |resource|
+ host.resources.each do |resource|
count += 1
i = nil
if resource[:title] =~ /file([0-9]+)/
@@ -74,9 +75,7 @@ class TestRails < Test::Unit::TestCase
else
raise "Got weird resource %s" % resource.inspect
end
-
- assert_equal("user#{i}",
- resource.rails_parameters.find_by_name("owner")[:value])
+ assert_equal("user#{i}", resource.parameters[:owner])
end
assert_equal(20, count, "Did not get enough resources")
diff --git a/test/rails/railsparameter.rb b/test/rails/railsparameter.rb
index 46a12e57c..f76939ac9 100755
--- a/test/rails/railsparameter.rb
+++ b/test/rails/railsparameter.rb
@@ -16,32 +16,33 @@ class TestRailsParameter < Test::Unit::TestCase
def test_to_resourceparam
railsinit
# First create our parameter
- rparam = nil
- hash = { :name => :myparam, :value => "myval",
- :file => __FILE__, :line => __LINE__}
+ #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])
assert_nothing_raised do
- rparam = Puppet::Rails::RailsParameter.new(hash)
+ pn.param_values << pv
end
- assert(rparam, "Did not create rails parameter")
+ assert(pn, "Did not create rails parameter")
# The id doesn't get assigned until we save
- rparam.save
+ pn.save
# Now create a source
interp = mkinterp
source = interp.newclass "myclass"
# And try to convert our parameter
- 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)
+ #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)
end
end
end
diff --git a/test/rails/railsresource.rb b/test/rails/railsresource.rb
index 666d26bcf..e109ba7d1 100755
--- a/test/rails/railsresource.rb
+++ b/test/rails/railsresource.rb
@@ -22,33 +22,38 @@ class TestRailsResource < Test::Unit::TestCase
host = Puppet::Rails::Host.new(:name => "myhost")
# Now build a resource
- resource = host.rails_resources.build(
- :title => "/tmp/to_resource", :restype => "file",
- :exported => true
- )
-
+ 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
+
# Now add some params
{"owner" => "root", "mode" => "644"}.each do |param, value|
- resource.rails_parameters.build(
- :name => param, :value => 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
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)