diff options
author | shadoi <shadoi@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-11-09 18:57:01 +0000 |
---|---|---|
committer | shadoi <shadoi@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-11-09 18:57:01 +0000 |
commit | cf166c25911f521cdf12178ebbe0b39f81473b35 (patch) | |
tree | 1a96094de1203cb3d48b6c07255414bea351092a /lib/puppet | |
parent | 28c283c73388c3f76e1d715c41ebd82ac35ca9a4 (diff) | |
download | puppet-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
Diffstat (limited to 'lib/puppet')
-rw-r--r-- | lib/puppet/parser/resource.rb | 12 | ||||
-rw-r--r-- | lib/puppet/parser/resource/param.rb | 10 | ||||
-rw-r--r-- | lib/puppet/rails.rb | 37 | ||||
-rw-r--r-- | lib/puppet/rails/host.rb | 59 |
4 files changed, 73 insertions, 45 deletions
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 |