diff options
Diffstat (limited to 'lib/puppet')
-rw-r--r-- | lib/puppet/configuration.rb | 19 | ||||
-rw-r--r-- | lib/puppet/parser/collector.rb | 83 | ||||
-rw-r--r-- | lib/puppet/parser/resource.rb | 74 | ||||
-rw-r--r-- | lib/puppet/parser/resource/param.rb | 43 | ||||
-rw-r--r-- | lib/puppet/rails.rb | 143 | ||||
-rw-r--r-- | lib/puppet/rails/database/schema.rb | 11 | ||||
-rw-r--r-- | lib/puppet/rails/fact_name.rb | 2 | ||||
-rw-r--r-- | lib/puppet/rails/fact_value.rb | 2 | ||||
-rw-r--r-- | lib/puppet/rails/host.rb | 65 | ||||
-rw-r--r-- | lib/puppet/rails/param_name.rb | 10 | ||||
-rw-r--r-- | lib/puppet/rails/param_value.rb | 4 | ||||
-rw-r--r-- | lib/puppet/rails/resource.rb | 35 |
12 files changed, 251 insertions, 240 deletions
diff --git a/lib/puppet/configuration.rb b/lib/puppet/configuration.rb index 074e656e0..f67be5999 100644 --- a/lib/puppet/configuration.rb +++ b/lib/puppet/configuration.rb @@ -19,19 +19,16 @@ module Puppet ) if self.name == "puppetmasterd" - self.setdefaults(:puppetmasterd, - :logdir => {:default => "$vardir/log", - :mode => 0750, - :owner => "$user", - :group => "$group", - :desc => "The Puppet log directory." - } - ) + logopts = {:default => "$vardir/log", + :mode => 0750, + :owner => "$user", + :group => "$group", + :desc => "The Puppet log directory." + } else - self.setdefaults(:puppet, - :logdir => ["$vardir/log", "The Puppet log directory."] - ) + logopts = ["$vardir/log", "The Puppet log directory."] end + setdefaults(:puppet, :logdir => logopts) self.setdefaults(:puppet, :trace => [false, "Whether to print stack traces on some errors"], diff --git a/lib/puppet/parser/collector.rb b/lib/puppet/parser/collector.rb index ea54c2dcf..841d8585f 100644 --- a/lib/puppet/parser/collector.rb +++ b/lib/puppet/parser/collector.rb @@ -6,13 +6,16 @@ class Puppet::Parser::Collector # Collect exported objects. def collect_exported require 'puppet/rails' - Puppet.info "collecting %s" % @type # First get everything from the export table. Just reuse our # collect_virtual method but tell it to use 'exported? for the test. resources = collect_virtual(true) count = 0 + unless @scope.host + raise Puppet::DevError, "Cannot collect resources for a nil host" + end + # We're going to collect objects from rails, but we don't want any # objects from this host. host = Puppet::Rails::Host.find_by_name(@scope.host) @@ -23,53 +26,23 @@ class Puppet::Parser::Collector else #Puppet.info "Host %s is uninitialized" % @scope.host end - Puppet.info "collecting %s" % @type # Now look them up in the rails db. When we support attribute comparison # and such, we'll need to vary the conditions, but this works with no # attributes, anyway. time = Puppet::Util.thinmark do - Puppet::Rails::Resource.find_all_by_type_and_exported(@type, true, + Puppet::Rails::Resource.find_all_by_restype_and_exported(@type, true, args ).each do |obj| - if existing = @scope.findresource(obj.type, obj.title) - # See if we exported it; if so, just move on - if @scope.host == obj.host.name - next - else - # Next see if we've already collected this resource - if existing.rails_id == obj.id - # This is the one we've already collected - next - else - raise Puppet::ParseError, - "Exported resource %s[%s] cannot override local resource" % - [obj.type, obj.title] - end - end + if resource = export_resource(obj) + count += 1 + resources << resource end - count += 1 - begin - resource = obj.to_resource(self.scope) - - # XXX Because the scopes don't expect objects to return values, - # we have to manually add our objects to the scope. This is - # uber-lame. - scope.setresource(resource) - rescue => detail - if Puppet[:trace] - puts detail.backtrace - end - raise - end - resource.exported = false - - resources << resource end end - scope.debug("Collected %s %s resources in %.2f seconds" % - [count, @type, time]) + scope.debug("Collected %s %s resource%s in %.2f seconds" % + [count, @type, count == 1 ? "" : "s", time]) return resources end @@ -148,6 +121,42 @@ class Puppet::Parser::Collector return true end end + + def export_resource(obj) + if existing = @scope.findresource(obj.restype, obj.title) + # See if we exported it; if so, just move on + if @scope.host == obj.host.name + return nil + else + # Next see if we've already collected this resource + if existing.rails_id == obj.id + # This is the one we've already collected + return nil + else + raise Puppet::ParseError, + "Exported resource %s cannot override local resource" % + [obj.ref] + end + end + end + + begin + resource = obj.to_resource(self.scope) + + # XXX Because the scopes don't expect objects to return values, + # we have to manually add our objects to the scope. This is + # ber-lame. + scope.setresource(resource) + rescue => detail + if Puppet[:trace] + puts detail.backtrace + end + raise + end + resource.exported = false + + return resource + end end # $Id$ diff --git a/lib/puppet/parser/resource.rb b/lib/puppet/parser/resource.rb index 7ea85dded..857b5fa39 100644 --- a/lib/puppet/parser/resource.rb +++ b/lib/puppet/parser/resource.rb @@ -101,27 +101,6 @@ class Puppet::Parser::Resource else self.fail "Cannot find definition %s" % self.type end - - -# if builtin? -# devfail "Cannot evaluate a builtin type" -# end -# -# unless klass = scope.finddefine(self.type) -# self.fail "Cannot find definition %s" % self.type -# end -# -# finish() -# -# scope.deleteresource(self) -# -# return klass.evaluate(:scope => scope, -# :type => self.type, -# :name => self.title, -# :arguments => self.to_hash, -# :scope => self.scope, -# :exported => self.exported -# ) ensure @evaluated = true end @@ -252,9 +231,24 @@ class Puppet::Parser::Resource end end - # Store our object as a Rails object. We need the host object we're storing it - # with. - def store(host) + #def tags + # unless defined? @tags + # @tags = scope.tags + # @tags << self.type + # end + # @tags + #end + + def to_hash + @params.inject({}) do |hash, ary| + param = ary[1] + hash[param.name] = param.value + hash + end + end + + # Turn our parser resource into a Rails resource. + def to_rails(host) args = {} #FIXME: support files/lines, etc. #%w{type title tags file line exported}.each do |param| @@ -280,28 +274,26 @@ class Puppet::Parser::Resource obj = host.resources.build(args) end + if l = self.line + obj.line = l + end + # Either way, now add our parameters + exists = {} + obj.param_names.each do |pn| exists[pn.name] = pn end @params.each do |name, param| - param.store(obj) + param.to_rails(obj) + exists.delete(name.to_s) if exists.include?(name.to_s) end - return obj - end - - #def tags - # unless defined? @tags - # @tags = scope.tags - # @tags << self.type - # end - # @tags - #end - - def to_hash - @params.inject({}) do |hash, ary| - param = ary[1] - hash[param.name] = param.value - hash + unless exists.empty? + obj.save + exists.each do |name, param| + obj.param_names.delete(param) + end end + + return obj end def to_s diff --git a/lib/puppet/parser/resource/param.rb b/lib/puppet/parser/resource/param.rb index 2e5d78034..6f24f1486 100644 --- a/lib/puppet/parser/resource/param.rb +++ b/lib/puppet/parser/resource/param.rb @@ -16,22 +16,41 @@ class Puppet::Parser::Resource::Param end # Store this parameter in a Rails db. - def store(resource) - args = {} - #[:name, :value, :line, :file].each do |var| - [:name, :value].each do |var| - args[var] = self.send(var) + def to_rails(res) + values = value.is_a?(Array) ? value : [value] + + unless pn = res.param_names.find_by_name(self.name.to_s) + # We're creating it anew. + pn = res.param_names.build(:name => self.name.to_s) + end + + if l = self.line + pn.line = Integer(l) + end + + exists = {} + pn.param_values.each { |pv| exists[pv.value] = pv } + values.each do |value| + unless pn.param_values.find_by_value(value) + pn.param_values.build(:value => value) + end + # Mark that this is still valid. + if exists.include?(value) + exists.delete(value) + end end - args[:name] = args[:name].to_s - args[:name].each do |name| - pn = resource.param_names.find_or_create_by_name(name) - args[:value].each do |value| - pv = pn.param_values.find_or_create_by_value(value) + + # And remove any existing values that are not in the current value list. + unless exists.empty? + # We have to save the current state else the deletion somehow deletes + # our new values. + pn.save + exists.each do |value, obj| + pn.param_values.delete(obj) end end - obj = resource.param_names.find_by_name(args[:name], :include => :param_values) - return obj + return pn end def to_s diff --git a/lib/puppet/rails.rb b/lib/puppet/rails.rb index b3795f4e0..925593434 100644 --- a/lib/puppet/rails.rb +++ b/lib/puppet/rails.rb @@ -21,7 +21,7 @@ module Puppet::Rails used when networked databases are used."], :dbpassword => [ "puppet", "The database password for Client caching. Only used when networked databases are used."], - :railslog => {:default => "/tmp/puppetpuppetrails.log", + :railslog => {:default => "$logdir/rails.log", :mode => 0600, :owner => "$user", :group => "$group", @@ -32,38 +32,20 @@ module Puppet::Rails def self.clear end - def self.teardown - unless defined? ActiveRecord::Base - raise Puppet::DevError, "No activerecord, cannot init Puppet::Rails" - end - Puppet.config.use(:puppetmaster) + # The arguments for initializing the database connection. + def self.database_arguments + args = {:adapter => Puppet[:dbadapter]} - args = {:adapter => Puppet[:dbadapter]} case Puppet[:dbadapter] - when "sqlite3": - args[:dbfile] = Puppet[:dblocation] - - when "mysql": - args[:host] = Puppet[:dbserver] - args[:username] = Puppet[:dbuser] - args[:password] = Puppet[:dbpassword] - args[:database] = Puppet[:dbname] - end - - 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 - - ActiveRecord::Base.connection.tables.each do |t| - ActiveRecord::Base.connection.drop_table t + when "sqlite3": + args[:dbfile] = Puppet[:dblocation] + when "mysql": + args[:host] = Puppet[:dbserver] + args[:username] = Puppet[:dbuser] + args[:password] = Puppet[:dbpassword] + args[:database] = Puppet[:dbname] end - - @inited = false + args end # Set up our database connection. It'd be nice to have a "use" system @@ -76,70 +58,83 @@ module Puppet::Rails # 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) + unless ActiveRecord::Base.connected? 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 - - when "mysql": - args[:host] = Puppet[:dbserver] - args[:username] = Puppet[:dbuser] - args[:password] = Puppet[:dbpassword] - args[:database] = Puppet[:dbname] - end begin - ActiveRecord::Base.establish_connection(args) + ActiveRecord::Base.establish_connection(database_arguments()) rescue => detail if Puppet[:trace] puts detail.backtrace end raise Puppet::Error, "Could not connect to database: %s" % detail end - unless ActiveRecord::Base.connection.tables.include?("resources") - require 'puppet/rails/database/schema' - Puppet::Rails::Schema.init - end - @inited = true end - ActiveRecord::Base.logger = Logger.new(Puppet[:railslog]) + + unless ActiveRecord::Base.connection.tables.include?("resources") + require 'puppet/rails/database/schema' + Puppet::Rails::Schema.init + end if Puppet[:dbmigrate] - dbdir = nil - $:.each { |d| - tmp = File.join(d, "puppet/rails/database") - if FileTest.directory?(tmp) - dbdir = tmp - break - end - } + migrate() + end + + # For now, we have to use :puppet, too, since non-puppetmasterd processes + # (including testing) put the logdir in :puppet, not in :puppetmasterd. + Puppet.config.use(:puppetmaster, :puppet) - unless dbdir - raise Puppet::Error, "Could not find Puppet::Rails database dir" + # This has to come after we create the logdir with the :use above. + ActiveRecord::Base.logger = Logger.new(Puppet[:railslog]) + end + + # Migrate to the latest db schema. + def self.migrate + dbdir = nil + $:.each { |d| + tmp = File.join(d, "puppet/rails/database") + if FileTest.directory?(tmp) + dbdir = tmp + break end + } - begin - ActiveRecord::Migrator.migrate(dbdir) - rescue => detail - if Puppet[:trace] - puts detail.backtrace - end - raise Puppet::Error, "Could not initialize database: %s" % detail + unless dbdir + raise Puppet::Error, "Could not find Puppet::Rails database dir" + end + + begin + ActiveRecord::Migrator.migrate(dbdir) + rescue => detail + if Puppet[:trace] + puts detail.backtrace end + raise Puppet::Error, "Could not migrate database: %s" % detail + end + end + + # Tear down the database. Mostly only used during testing. + def self.teardown + unless Puppet.features.rails? + raise Puppet::DevError, "No activerecord, cannot init Puppet::Rails" end + Puppet.config.use(:puppetmaster) - ActiveRecord::Base.logger = Logger.new(Puppet[:railslog]) + + begin + ActiveRecord::Base.establish_connection(database_arguments()) + rescue => detail + if Puppet[:trace] + puts detail.backtrace + end + raise Puppet::Error, "Could not connect to database: %s" % detail + end + + ActiveRecord::Base.connection.tables.each do |t| + ActiveRecord::Base.connection.drop_table t + end end end 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 |