diff options
Diffstat (limited to 'lib/puppet/rails')
-rw-r--r-- | lib/puppet/rails/benchmark.rb | 86 | ||||
-rw-r--r-- | lib/puppet/rails/database/001_add_created_at_to_all_tables.rb | 16 | ||||
-rw-r--r-- | lib/puppet/rails/database/002_remove_duplicated_index_on_all_tables.rb | 16 | ||||
-rw-r--r-- | lib/puppet/rails/database/003_add_environment_to_host.rb | 12 | ||||
-rw-r--r-- | lib/puppet/rails/database/schema.rb | 198 | ||||
-rw-r--r-- | lib/puppet/rails/fact_name.rb | 2 | ||||
-rw-r--r-- | lib/puppet/rails/fact_value.rb | 10 | ||||
-rw-r--r-- | lib/puppet/rails/host.rb | 504 | ||||
-rw-r--r-- | lib/puppet/rails/param_name.rb | 30 | ||||
-rw-r--r-- | lib/puppet/rails/param_value.rb | 110 | ||||
-rw-r--r-- | lib/puppet/rails/puppet_tag.rb | 8 | ||||
-rw-r--r-- | lib/puppet/rails/resource.rb | 376 | ||||
-rw-r--r-- | lib/puppet/rails/resource_tag.rb | 38 | ||||
-rw-r--r-- | lib/puppet/rails/source_file.rb | 10 |
14 files changed, 708 insertions, 708 deletions
diff --git a/lib/puppet/rails/benchmark.rb b/lib/puppet/rails/benchmark.rb index 35e6fdcef..375674e19 100644 --- a/lib/puppet/rails/benchmark.rb +++ b/lib/puppet/rails/benchmark.rb @@ -1,63 +1,63 @@ require 'benchmark' module Puppet::Rails::Benchmark - $benchmarks = {:accumulated => {}} + $benchmarks = {:accumulated => {}} - def time_debug? - Puppet::Rails::TIME_DEBUG - end + def time_debug? + Puppet::Rails::TIME_DEBUG + end - def railsmark(message) - result = nil - seconds = Benchmark.realtime { result = yield } - Puppet.debug(message + " in %0.2f seconds" % seconds) + def railsmark(message) + result = nil + seconds = Benchmark.realtime { result = yield } + Puppet.debug(message + " in %0.2f seconds" % seconds) - $benchmarks[message] = seconds if time_debug? - result - end + $benchmarks[message] = seconds if time_debug? + result + end - def debug_benchmark(message) - return yield unless Puppet::Rails::TIME_DEBUG + def debug_benchmark(message) + return yield unless Puppet::Rails::TIME_DEBUG - railsmark(message) { yield } - end + railsmark(message) { yield } + end - # Collect partial benchmarks to be logged when they're - # all done. - # These are always low-level debugging so we only - # print them if time_debug is enabled. - def accumulate_benchmark(message, label) - return yield unless time_debug? + # Collect partial benchmarks to be logged when they're + # all done. + # These are always low-level debugging so we only + # print them if time_debug is enabled. + def accumulate_benchmark(message, label) + return yield unless time_debug? - $benchmarks[:accumulated][message] ||= Hash.new(0) - $benchmarks[:accumulated][message][label] += Benchmark.realtime { yield } - end + $benchmarks[:accumulated][message] ||= Hash.new(0) + $benchmarks[:accumulated][message][label] += Benchmark.realtime { yield } + end - # Log the accumulated marks. - def log_accumulated_marks(message) - return unless time_debug? + # Log the accumulated marks. + def log_accumulated_marks(message) + return unless time_debug? - return if $benchmarks[:accumulated].empty? or $benchmarks[:accumulated][message].nil? or $benchmarks[:accumulated][message].empty? + return if $benchmarks[:accumulated].empty? or $benchmarks[:accumulated][message].nil? or $benchmarks[:accumulated][message].empty? - $benchmarks[:accumulated][message].each do |label, value| - Puppet.debug(message + ("(#{label})") + (" in %0.2f seconds" % value)) - end + $benchmarks[:accumulated][message].each do |label, value| + Puppet.debug(message + ("(#{label})") + (" in %0.2f seconds" % value)) end + end - def write_benchmarks - return unless time_debug? + def write_benchmarks + return unless time_debug? - branch = %x{git branch}.split("\n").find { |l| l =~ /^\*/ }.sub("* ", '') + branch = %x{git branch}.split("\n").find { |l| l =~ /^\*/ }.sub("* ", '') - file = "/tmp/time_debugging.yaml" + file = "/tmp/time_debugging.yaml" - require 'yaml' + require 'yaml' - if FileTest.exist?(file) - data = YAML.load_file(file) - else - data = {} - end - data[branch] = $benchmarks - Puppet::Util.secure_open(file, "w") { |f| f.print YAML.dump(data) } + if FileTest.exist?(file) + data = YAML.load_file(file) + else + data = {} end + data[branch] = $benchmarks + Puppet::Util.secure_open(file, "w") { |f| f.print YAML.dump(data) } + end end diff --git a/lib/puppet/rails/database/001_add_created_at_to_all_tables.rb b/lib/puppet/rails/database/001_add_created_at_to_all_tables.rb index 65432a6b7..22dffb854 100644 --- a/lib/puppet/rails/database/001_add_created_at_to_all_tables.rb +++ b/lib/puppet/rails/database/001_add_created_at_to_all_tables.rb @@ -1,13 +1,13 @@ class AddCreatedAtToAllTables < ActiveRecord::Migration - def self.up - ActiveRecord::Base.connection.tables.each do |t| - add_column t.to_s, :created_at, :datetime unless ActiveRecord::Base.connection.columns(t).collect {|c| c.name}.include?("created_at") - end + def self.up + ActiveRecord::Base.connection.tables.each do |t| + add_column t.to_s, :created_at, :datetime unless ActiveRecord::Base.connection.columns(t).collect {|c| c.name}.include?("created_at") end + end - def self.down - ActiveRecord::Base.connection.tables.each do |t| - remove_column t.to_s, :created_at unless ActiveRecord::Base.connection.columns(t).collect {|c| c.name}.include?("created_at") - end + def self.down + ActiveRecord::Base.connection.tables.each do |t| + remove_column t.to_s, :created_at unless ActiveRecord::Base.connection.columns(t).collect {|c| c.name}.include?("created_at") end + end end diff --git a/lib/puppet/rails/database/002_remove_duplicated_index_on_all_tables.rb b/lib/puppet/rails/database/002_remove_duplicated_index_on_all_tables.rb index c1e60db14..021889df7 100644 --- a/lib/puppet/rails/database/002_remove_duplicated_index_on_all_tables.rb +++ b/lib/puppet/rails/database/002_remove_duplicated_index_on_all_tables.rb @@ -1,13 +1,13 @@ class RemoveDuplicatedIndexOnAllTables < ActiveRecord::Migration - def self.up - ActiveRecord::Base.connection.tables.each do |t| - remove_index t.to_s, :id if ActiveRecord::Base.connection.indexes(t).collect {|c| c.columns}.include?("id") - end + def self.up + ActiveRecord::Base.connection.tables.each do |t| + remove_index t.to_s, :id if ActiveRecord::Base.connection.indexes(t).collect {|c| c.columns}.include?("id") end + end - def self.down - ActiveRecord::Base.connection.tables.each do |t| - add_index t.to_s, :id, :integer => true unless ActiveRecord::Base.connection.indexes(t).collect {|c| c.columns}.include?("id") - end + def self.down + ActiveRecord::Base.connection.tables.each do |t| + add_index t.to_s, :id, :integer => true unless ActiveRecord::Base.connection.indexes(t).collect {|c| c.columns}.include?("id") end + end end diff --git a/lib/puppet/rails/database/003_add_environment_to_host.rb b/lib/puppet/rails/database/003_add_environment_to_host.rb index 95f036d11..5c06bb8c0 100644 --- a/lib/puppet/rails/database/003_add_environment_to_host.rb +++ b/lib/puppet/rails/database/003_add_environment_to_host.rb @@ -1,9 +1,9 @@ class AddEnvironmentToHost < ActiveRecord::Migration - def self.up - add_column :hosts, :environment, :string unless ActiveRecord::Base.connection.columns(:hosts).collect {|c| c.name}.include?("environment") - end + def self.up + add_column :hosts, :environment, :string unless ActiveRecord::Base.connection.columns(:hosts).collect {|c| c.name}.include?("environment") + end - def self.down - remove_column :hosts, :environment if ActiveRecord::Base.connection.columns(:hosts).collect {|c| c.name}.include?("environment") - end + def self.down + remove_column :hosts, :environment if ActiveRecord::Base.connection.columns(:hosts).collect {|c| c.name}.include?("environment") + end end diff --git a/lib/puppet/rails/database/schema.rb b/lib/puppet/rails/database/schema.rb index 8cb7350eb..8b389d773 100644 --- a/lib/puppet/rails/database/schema.rb +++ b/lib/puppet/rails/database/schema.rb @@ -1,114 +1,114 @@ class Puppet::Rails::Schema - def self.init - oldout = nil - 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, :text, :null => false - 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 - t.column :updated_at, :datetime - t.column :created_at, :datetime - end - add_index :resources, :host_id, :integer => true - add_index :resources, :source_file_id, :integer => true + def self.init + oldout = nil + 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, :text, :null => false + 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 + t.column :updated_at, :datetime + t.column :created_at, :datetime + end + add_index :resources, :host_id, :integer => true + add_index :resources, :source_file_id, :integer => true - # Thanks, mysql! MySQL requires a length on indexes in text fields. - # So, we provide them for mysql and handle everything else specially. - # Oracle doesn't index on CLOB fields, so we skip it - if Puppet[:dbadapter] == "mysql" - execute "CREATE INDEX typentitle ON resources (restype,title(50));" - elsif Puppet[:dbadapter] != "oracle_enhanced" - add_index :resources, [:title, :restype] - end + # Thanks, mysql! MySQL requires a length on indexes in text fields. + # So, we provide them for mysql and handle everything else specially. + # Oracle doesn't index on CLOB fields, so we skip it + if Puppet[:dbadapter] == "mysql" + execute "CREATE INDEX typentitle ON resources (restype,title(50));" + elsif Puppet[:dbadapter] != "oracle_enhanced" + add_index :resources, [:title, :restype] + end - create_table :source_files do |t| - t.column :filename, :string - t.column :path, :string - t.column :updated_at, :datetime - t.column :created_at, :datetime - end - add_index :source_files, :filename + create_table :source_files do |t| + t.column :filename, :string + t.column :path, :string + t.column :updated_at, :datetime + t.column :created_at, :datetime + end + add_index :source_files, :filename - create_table :resource_tags do |t| - t.column :resource_id, :integer - t.column :puppet_tag_id, :integer - t.column :updated_at, :datetime - t.column :created_at, :datetime - end - add_index :resource_tags, :resource_id, :integer => true - add_index :resource_tags, :puppet_tag_id, :integer => true + create_table :resource_tags do |t| + t.column :resource_id, :integer + t.column :puppet_tag_id, :integer + t.column :updated_at, :datetime + t.column :created_at, :datetime + end + add_index :resource_tags, :resource_id, :integer => true + add_index :resource_tags, :puppet_tag_id, :integer => true - create_table :puppet_tags do |t| - t.column :name, :string - t.column :updated_at, :datetime - t.column :created_at, :datetime - end + create_table :puppet_tags do |t| + t.column :name, :string + t.column :updated_at, :datetime + t.column :created_at, :datetime + end - # Oracle automatically creates a primary key index - add_index :puppet_tags, :id, :integer => true if Puppet[:dbadapter] != "oracle_enhanced" + # Oracle automatically creates a primary key index + add_index :puppet_tags, :id, :integer => true if Puppet[:dbadapter] != "oracle_enhanced" - create_table :hosts do |t| - t.column :name, :string, :null => false - t.column :ip, :string - t.column :environment, :text - t.column :last_compile, :datetime - t.column :last_freshcheck, :datetime - t.column :last_report, :datetime - #Use updated_at to automatically add timestamp on save. - t.column :updated_at, :datetime - t.column :source_file_id, :integer - t.column :created_at, :datetime - end - add_index :hosts, :source_file_id, :integer => true - add_index :hosts, :name + create_table :hosts do |t| + t.column :name, :string, :null => false + t.column :ip, :string + t.column :environment, :text + t.column :last_compile, :datetime + t.column :last_freshcheck, :datetime + t.column :last_report, :datetime + #Use updated_at to automatically add timestamp on save. + t.column :updated_at, :datetime + t.column :source_file_id, :integer + t.column :created_at, :datetime + end + add_index :hosts, :source_file_id, :integer => true + add_index :hosts, :name - create_table :fact_names do |t| - t.column :name, :string, :null => false - t.column :updated_at, :datetime - t.column :created_at, :datetime - end - add_index :fact_names, :name + create_table :fact_names do |t| + t.column :name, :string, :null => false + t.column :updated_at, :datetime + t.column :created_at, :datetime + end + add_index :fact_names, :name - create_table :fact_values do |t| - t.column :value, :text, :null => false - t.column :fact_name_id, :integer, :null => false - t.column :host_id, :integer, :null => false - t.column :updated_at, :datetime - t.column :created_at, :datetime - end - add_index :fact_values, :fact_name_id, :integer => true - add_index :fact_values, :host_id, :integer => true + create_table :fact_values do |t| + t.column :value, :text, :null => false + t.column :fact_name_id, :integer, :null => false + t.column :host_id, :integer, :null => false + t.column :updated_at, :datetime + t.column :created_at, :datetime + end + add_index :fact_values, :fact_name_id, :integer => true + add_index :fact_values, :host_id, :integer => true - create_table :param_values do |t| - t.column :value, :text, :null => false - t.column :param_name_id, :integer, :null => false - t.column :line, :integer - t.column :resource_id, :integer - t.column :updated_at, :datetime - t.column :created_at, :datetime - end - add_index :param_values, :param_name_id, :integer => true - add_index :param_values, :resource_id, :integer => true + create_table :param_values do |t| + t.column :value, :text, :null => false + t.column :param_name_id, :integer, :null => false + t.column :line, :integer + t.column :resource_id, :integer + t.column :updated_at, :datetime + t.column :created_at, :datetime + end + add_index :param_values, :param_name_id, :integer => true + add_index :param_values, :resource_id, :integer => true - create_table :param_names do |t| - t.column :name, :string, :null => false - t.column :updated_at, :datetime - t.column :created_at, :datetime - end - add_index :param_names, :name - end + create_table :param_names do |t| + t.column :name, :string, :null => false + t.column :updated_at, :datetime + t.column :created_at, :datetime end - ensure - $stdout.close - $stdout = oldout if oldout - oldout = nil + add_index :param_names, :name + end end + ensure + $stdout.close + $stdout = oldout if oldout + oldout = nil + end end diff --git a/lib/puppet/rails/fact_name.rb b/lib/puppet/rails/fact_name.rb index e6e7c5d03..fb40ec48f 100644 --- a/lib/puppet/rails/fact_name.rb +++ b/lib/puppet/rails/fact_name.rb @@ -1,7 +1,7 @@ require 'puppet/rails/fact_value' class Puppet::Rails::FactName < ActiveRecord::Base - has_many :fact_values, :dependent => :destroy + has_many :fact_values, :dependent => :destroy end # $Id: fact_name.rb 1952 2006-12-19 05:47:57Z luke $ diff --git a/lib/puppet/rails/fact_value.rb b/lib/puppet/rails/fact_value.rb index a451cbb88..45a88b2dc 100644 --- a/lib/puppet/rails/fact_value.rb +++ b/lib/puppet/rails/fact_value.rb @@ -1,10 +1,10 @@ class Puppet::Rails::FactValue < ActiveRecord::Base - belongs_to :fact_name - belongs_to :host + belongs_to :fact_name + belongs_to :host - def to_label - "#{self.fact_name.name}" - end + def to_label + "#{self.fact_name.name}" + end end # $Id: fact_value.rb 1952 2006-12-19 05:47:57Z luke $ diff --git a/lib/puppet/rails/host.rb b/lib/puppet/rails/host.rb index 510790235..6c008bccf 100644 --- a/lib/puppet/rails/host.rb +++ b/lib/puppet/rails/host.rb @@ -5,318 +5,318 @@ require 'puppet/rails/benchmark' require 'puppet/util/rails/collection_merger' class Puppet::Rails::Host < ActiveRecord::Base - include Puppet::Rails::Benchmark - extend Puppet::Rails::Benchmark - include Puppet::Util - include Puppet::Util::CollectionMerger - - has_many :fact_values, :dependent => :destroy, :class_name => "Puppet::Rails::FactValue" - has_many :fact_names, :through => :fact_values, :class_name => "Puppet::Rails::FactName" - belongs_to :source_file - has_many :resources, :dependent => :destroy, :class_name => "Puppet::Rails::Resource" - - # If the host already exists, get rid of its objects - def self.clean(host) - if obj = self.find_by_name(host) - obj.rails_objects.clear - return obj - else - return nil - end + include Puppet::Rails::Benchmark + extend Puppet::Rails::Benchmark + include Puppet::Util + include Puppet::Util::CollectionMerger + + has_many :fact_values, :dependent => :destroy, :class_name => "Puppet::Rails::FactValue" + has_many :fact_names, :through => :fact_values, :class_name => "Puppet::Rails::FactName" + belongs_to :source_file + has_many :resources, :dependent => :destroy, :class_name => "Puppet::Rails::Resource" + + # If the host already exists, get rid of its objects + def self.clean(host) + if obj = self.find_by_name(host) + obj.rails_objects.clear + return obj + else + return nil end + end - def self.from_puppet(node) - host = find_by_name(node.name) || new(:name => node.name) - - {"ipaddress" => "ip", "environment" => "environment"}.each do |myparam, itsparam| - if value = node.send(myparam) - host.send(itsparam + "=", value) - end - end + def self.from_puppet(node) + host = find_by_name(node.name) || new(:name => node.name) - host + {"ipaddress" => "ip", "environment" => "environment"}.each do |myparam, itsparam| + if value = node.send(myparam) + host.send(itsparam + "=", value) + end end - # Store our host in the database. - def self.store(node, resources) - args = {} - - host = nil - railsmark "Stored node" do - transaction do - #unless host = find_by_name(name) - - debug_benchmark("Searched for host")do - unless host = find_by_name(node.name) - host = new(:name => node.name) - end - end - if ip = node.parameters["ipaddress"] - host.ip = ip - end - - if env = node.environment - host.environment = env - end - - # Store the facts into the database. - host.merge_facts(node.parameters) + host + end - debug_benchmark("Handled resources") { - host.merge_resources(resources) - } + # Store our host in the database. + def self.store(node, resources) + args = {} - host.last_compile = Time.now - - debug_benchmark("Saved host") { - host.save - } - end + host = nil + railsmark "Stored node" do + transaction do + #unless host = find_by_name(name) + debug_benchmark("Searched for host")do + unless host = find_by_name(node.name) + host = new(:name => node.name) + end + end + if ip = node.parameters["ipaddress"] + host.ip = ip end - # This only runs if time debugging is enabled. - write_benchmarks + if env = node.environment + host.environment = env + end - host - end + # Store the facts into the database. + host.merge_facts(node.parameters) - # Return the value of a fact. - def fact(name) + debug_benchmark("Handled resources") { + host.merge_resources(resources) + } - if fv = self.fact_values.find( - :all, :include => :fact_name, + host.last_compile = Time.now - :conditions => "fact_names.name = '#{name}'") - return fv - else - return nil - end - end + debug_benchmark("Saved host") { + host.save + } + end - # returns a hash of fact_names.name => [ fact_values ] for this host. - # Note that 'fact_values' is actually a list of the value instances, not - # just actual values. - def get_facts_hash - fact_values = self.fact_values.find(:all, :include => :fact_name) - return fact_values.inject({}) do | hash, value | - hash[value.fact_name.name] ||= [] - hash[value.fact_name.name] << value - hash - end end + # This only runs if time debugging is enabled. + write_benchmarks - # This is *very* similar to the merge_parameters method - # of Puppet::Rails::Resource. - def merge_facts(facts) - db_facts = {} + host + end - deletions = [] - self.fact_values.find(:all, :include => :fact_name).each do |value| - deletions << value['id'] and next unless facts.include?(value['name']) - # Now store them for later testing. - db_facts[value['name']] ||= [] - db_facts[value['name']] << value - end + # Return the value of a fact. + def fact(name) - # Now get rid of any parameters whose value list is different. - # This might be extra work in cases where an array has added or lost - # a single value, but in the most common case (a single value has changed) - # this makes sense. - db_facts.each do |name, value_hashes| - values = value_hashes.collect { |v| v['value'] } + if fv = self.fact_values.find( + :all, :include => :fact_name, - unless values == facts[name] - value_hashes.each { |v| deletions << v['id'] } - end - end + :conditions => "fact_names.name = '#{name}'") + return fv + else + return nil + end + end + + # returns a hash of fact_names.name => [ fact_values ] for this host. + # Note that 'fact_values' is actually a list of the value instances, not + # just actual values. + def get_facts_hash + fact_values = self.fact_values.find(:all, :include => :fact_name) + return fact_values.inject({}) do | hash, value | + hash[value.fact_name.name] ||= [] + hash[value.fact_name.name] << value + hash + end + end - # Perform our deletions. - Puppet::Rails::FactValue.delete(deletions) unless deletions.empty? - # Lastly, add any new parameters. - facts.each do |name, value| - next if db_facts.include?(name) - values = value.is_a?(Array) ? value : [value] + # This is *very* similar to the merge_parameters method + # of Puppet::Rails::Resource. + def merge_facts(facts) + db_facts = {} - values.each do |v| - fact_values.build(:value => v, :fact_name => Puppet::Rails::FactName.find_or_create_by_name(name)) - end - end + deletions = [] + self.fact_values.find(:all, :include => :fact_name).each do |value| + deletions << value['id'] and next unless facts.include?(value['name']) + # Now store them for later testing. + db_facts[value['name']] ||= [] + db_facts[value['name']] << value end - # Set our resources. - def merge_resources(list) - # keep only exported resources in thin_storeconfig mode - list = list.select { |r| r.exported? } if Puppet.settings[:thin_storeconfigs] - - resources_by_id = nil - debug_benchmark("Searched for resources") { - resources_by_id = find_resources - } - - debug_benchmark("Searched for resource params and tags") { - find_resources_parameters_tags(resources_by_id) - } if id + # Now get rid of any parameters whose value list is different. + # This might be extra work in cases where an array has added or lost + # a single value, but in the most common case (a single value has changed) + # this makes sense. + db_facts.each do |name, value_hashes| + values = value_hashes.collect { |v| v['value'] } - debug_benchmark("Performed resource comparison") { - compare_to_catalog(resources_by_id, list) - } + unless values == facts[name] + value_hashes.each { |v| deletions << v['id'] } + end end - def find_resources - condition = { :exported => true } if Puppet.settings[:thin_storeconfigs] + # Perform our deletions. + Puppet::Rails::FactValue.delete(deletions) unless deletions.empty? - resources.find(:all, :include => :source_file, :conditions => condition || {}).inject({}) do | hash, resource | - hash[resource.id] = resource - hash - end - end + # Lastly, add any new parameters. + facts.each do |name, value| + next if db_facts.include?(name) + values = value.is_a?(Array) ? value : [value] - def find_resources_parameters_tags(resources) - find_resources_parameters(resources) - find_resources_tags(resources) + values.each do |v| + fact_values.build(:value => v, :fact_name => Puppet::Rails::FactName.find_or_create_by_name(name)) + end end + end - def compare_to_catalog(existing, list) - compiled = list.inject({}) do |hash, resource| - hash[resource.ref] = resource - hash - end + # Set our resources. + def merge_resources(list) + # keep only exported resources in thin_storeconfig mode + list = list.select { |r| r.exported? } if Puppet.settings[:thin_storeconfigs] - resources = nil - debug_benchmark("Resource removal") { - resources = remove_unneeded_resources(compiled, existing) - } + resources_by_id = nil + debug_benchmark("Searched for resources") { + resources_by_id = find_resources + } - # Now for all resources in the catalog but not in the db, we're pretty easy. - additions = nil - debug_benchmark("Resource merger") { - additions = perform_resource_merger(compiled, resources) - } + debug_benchmark("Searched for resource params and tags") { + find_resources_parameters_tags(resources_by_id) + } if id - debug_benchmark("Resource addition") { - additions.each do |resource| - build_rails_resource_from_parser_resource(resource) - end + debug_benchmark("Performed resource comparison") { + compare_to_catalog(resources_by_id, list) + } + end - log_accumulated_marks "Added resources" - } - end + def find_resources + condition = { :exported => true } if Puppet.settings[:thin_storeconfigs] - def add_new_resources(additions) - additions.each do |resource| - Puppet::Rails::Resource.from_parser_resource(self, resource) - end + resources.find(:all, :include => :source_file, :conditions => condition || {}).inject({}) do | hash, resource | + hash[resource.id] = resource + hash end + end - # Turn a parser resource into a Rails resource. - def build_rails_resource_from_parser_resource(resource) - db_resource = nil - accumulate_benchmark("Added resources", :initialization) { - args = Puppet::Rails::Resource.rails_resource_initial_args(resource) + def find_resources_parameters_tags(resources) + find_resources_parameters(resources) + find_resources_tags(resources) + end - db_resource = self.resources.build(args) + def compare_to_catalog(existing, list) + compiled = list.inject({}) do |hash, resource| + hash[resource.ref] = resource + hash + end - # Our file= method does the name to id conversion. - db_resource.file = resource.file - } + resources = nil + debug_benchmark("Resource removal") { + resources = remove_unneeded_resources(compiled, existing) + } + + # Now for all resources in the catalog but not in the db, we're pretty easy. + additions = nil + debug_benchmark("Resource merger") { + additions = perform_resource_merger(compiled, resources) + } + + debug_benchmark("Resource addition") { + additions.each do |resource| + build_rails_resource_from_parser_resource(resource) + end + + log_accumulated_marks "Added resources" + } + end + + def add_new_resources(additions) + additions.each do |resource| + Puppet::Rails::Resource.from_parser_resource(self, resource) + end + end + # Turn a parser resource into a Rails resource. + def build_rails_resource_from_parser_resource(resource) + db_resource = nil + accumulate_benchmark("Added resources", :initialization) { + args = Puppet::Rails::Resource.rails_resource_initial_args(resource) - accumulate_benchmark("Added resources", :parameters) { - resource.each do |param, value| - Puppet::Rails::ParamValue.from_parser_param(param, value).each do |value_hash| - db_resource.param_values.build(value_hash) - end - end - } + db_resource = self.resources.build(args) - accumulate_benchmark("Added resources", :tags) { - resource.tags.each { |tag| db_resource.add_resource_tag(tag) } - } + # Our file= method does the name to id conversion. + db_resource.file = resource.file + } - db_resource.save - db_resource - end + accumulate_benchmark("Added resources", :parameters) { + resource.each do |param, value| + Puppet::Rails::ParamValue.from_parser_param(param, value).each do |value_hash| + db_resource.param_values.build(value_hash) + end + end + } + accumulate_benchmark("Added resources", :tags) { + resource.tags.each { |tag| db_resource.add_resource_tag(tag) } + } - def perform_resource_merger(compiled, resources) - return compiled.values if resources.empty? + db_resource.save - # Now for all resources in the catalog but not in the db, we're pretty easy. - additions = [] - compiled.each do |ref, resource| - if db_resource = resources[ref] - db_resource.merge_parser_resource(resource) - else - additions << resource - end - end - log_accumulated_marks "Resource merger" + db_resource + end - additions - end - def remove_unneeded_resources(compiled, existing) - deletions = [] - resources = {} - existing.each do |id, resource| - # it seems that it can happen (see bug #2010) some resources are duplicated in the - # database (ie logically corrupted database), in which case we remove the extraneous - # entries. - if resources.include?(resource.ref) - deletions << id - next - end - - # If the resource is in the db but not in the catalog, mark it - # for removal. - unless compiled.include?(resource.ref) - deletions << id - next - end - - resources[resource.ref] = resource - end - # We need to use 'destroy' here, not 'delete', so that all - # dependent objects get removed, too. - Puppet::Rails::Resource.destroy(deletions) unless deletions.empty? + def perform_resource_merger(compiled, resources) + return compiled.values if resources.empty? - resources + # Now for all resources in the catalog but not in the db, we're pretty easy. + additions = [] + compiled.each do |ref, resource| + if db_resource = resources[ref] + db_resource.merge_parser_resource(resource) + else + additions << resource + end end - - def find_resources_parameters(resources) - params = Puppet::Rails::ParamValue.find_all_params_from_host(self) - - # assign each loaded parameters/tags to the resource it belongs to - params.each do |param| - resources[param['resource_id']].add_param_to_list(param) if resources.include?(param['resource_id']) - end + log_accumulated_marks "Resource merger" + + additions + end + + def remove_unneeded_resources(compiled, existing) + deletions = [] + resources = {} + existing.each do |id, resource| + # it seems that it can happen (see bug #2010) some resources are duplicated in the + # database (ie logically corrupted database), in which case we remove the extraneous + # entries. + if resources.include?(resource.ref) + deletions << id + next + end + + # If the resource is in the db but not in the catalog, mark it + # for removal. + unless compiled.include?(resource.ref) + deletions << id + next + end + + resources[resource.ref] = resource end + # We need to use 'destroy' here, not 'delete', so that all + # dependent objects get removed, too. + Puppet::Rails::Resource.destroy(deletions) unless deletions.empty? - def find_resources_tags(resources) - tags = Puppet::Rails::ResourceTag.find_all_tags_from_host(self) + resources + end - tags.each do |tag| - resources[tag['resource_id']].add_tag_to_list(tag) if resources.include?(tag['resource_id']) - end - end + def find_resources_parameters(resources) + params = Puppet::Rails::ParamValue.find_all_params_from_host(self) - def update_connect_time - self.last_connect = Time.now - save + # assign each loaded parameters/tags to the resource it belongs to + params.each do |param| + resources[param['resource_id']].add_param_to_list(param) if resources.include?(param['resource_id']) end + end - def to_puppet - node = Puppet::Node.new(self.name) - {"ip" => "ipaddress", "environment" => "environment"}.each do |myparam, itsparam| - if value = send(myparam) - node.send(itsparam + "=", value) - end - end + def find_resources_tags(resources) + tags = Puppet::Rails::ResourceTag.find_all_tags_from_host(self) - node + tags.each do |tag| + resources[tag['resource_id']].add_tag_to_list(tag) if resources.include?(tag['resource_id']) + end + end + + def update_connect_time + self.last_connect = Time.now + save + end + + def to_puppet + node = Puppet::Node.new(self.name) + {"ip" => "ipaddress", "environment" => "environment"}.each do |myparam, itsparam| + if value = send(myparam) + node.send(itsparam + "=", value) + end end + + node + end end diff --git a/lib/puppet/rails/param_name.rb b/lib/puppet/rails/param_name.rb index 69e2fb6e2..91cf45628 100644 --- a/lib/puppet/rails/param_name.rb +++ b/lib/puppet/rails/param_name.rb @@ -3,23 +3,23 @@ require 'puppet/rails/param_value' require 'puppet/util/rails/cache_accumulator' class Puppet::Rails::ParamName < ActiveRecord::Base - include Puppet::Util::CollectionMerger - has_many :param_values, :dependent => :destroy + include Puppet::Util::CollectionMerger + has_many :param_values, :dependent => :destroy - include Puppet::Util::CacheAccumulator - accumulates :name + include Puppet::Util::CacheAccumulator + accumulates :name - def to_resourceparam(resource, source) - hash = {} - hash[:name] = self.name.to_sym - hash[:source] = source - hash[:value] = resource.param_values.find(:all, :conditions => [ "param_name_id = ?", self.id]).collect { |v| v.value } - if hash[:value].length == 1 - hash[:value] = hash[:value].shift - elsif hash[:value].empty? - hash[:value] = nil - end - Puppet::Parser::Resource::Param.new hash + def to_resourceparam(resource, source) + hash = {} + hash[:name] = self.name.to_sym + hash[:source] = source + hash[:value] = resource.param_values.find(:all, :conditions => [ "param_name_id = ?", self.id]).collect { |v| v.value } + if hash[:value].length == 1 + hash[:value] = hash[:value].shift + elsif hash[:value].empty? + hash[:value] = nil end + Puppet::Parser::Resource::Param.new hash + end end diff --git a/lib/puppet/rails/param_value.rb b/lib/puppet/rails/param_value.rb index e4b5a668f..d7c88f844 100644 --- a/lib/puppet/rails/param_value.rb +++ b/lib/puppet/rails/param_value.rb @@ -1,74 +1,74 @@ require 'puppet/util/rails/reference_serializer' class Puppet::Rails::ParamValue < ActiveRecord::Base - include Puppet::Util::ReferenceSerializer - extend Puppet::Util::ReferenceSerializer + include Puppet::Util::ReferenceSerializer + extend Puppet::Util::ReferenceSerializer - belongs_to :param_name - belongs_to :resource + belongs_to :param_name + belongs_to :resource - # Store a new parameter in a Rails db. - def self.from_parser_param(param, values) - values = munge_parser_values(values) + # Store a new parameter in a Rails db. + def self.from_parser_param(param, values) + values = munge_parser_values(values) - param_name = Puppet::Rails::ParamName.find_or_create_by_name(param.to_s) - return values.collect do |v| - {:value => v, :param_name => param_name} - end + param_name = Puppet::Rails::ParamName.find_or_create_by_name(param.to_s) + return values.collect do |v| + {:value => v, :param_name => param_name} end + end - # Make sure an array (or possibly not an array) of values is correctly - # set up for Rails. The main thing is that Resource::Reference objects - # should stay objects, so they just get serialized. - def self.munge_parser_values(value) - values = value.is_a?(Array) ? value : [value] - values.map do |v| - if v.is_a?(Puppet::Resource) - v - else - v.to_s - end - end + # Make sure an array (or possibly not an array) of values is correctly + # set up for Rails. The main thing is that Resource::Reference objects + # should stay objects, so they just get serialized. + def self.munge_parser_values(value) + values = value.is_a?(Array) ? value : [value] + values.map do |v| + if v.is_a?(Puppet::Resource) + v + else + v.to_s + end end + end - def value - unserialize_value(self[:value]) - end + def value + unserialize_value(self[:value]) + end - # I could not find a cleaner way to handle making sure that resource references - # were consistently serialized and deserialized. - def value=(val) - self[:value] = serialize_value(val) - end + # I could not find a cleaner way to handle making sure that resource references + # were consistently serialized and deserialized. + def value=(val) + self[:value] = serialize_value(val) + end - def to_label - "#{self.param_name.name}" - end + def to_label + "#{self.param_name.name}" + end - # returns an array of hash containing all the parameters of a given resource - def self.find_all_params_from_resource(db_resource) - params = db_resource.connection.select_all("SELECT v.id, v.value, v.line, v.resource_id, v.param_name_id, n.name FROM param_values v INNER JOIN param_names n ON v.param_name_id=n.id WHERE v.resource_id=#{db_resource.id}") - params.each do |val| - val['value'] = unserialize_value(val['value']) - val['line'] = val['line'] ? Integer(val['line']) : nil - val['resource_id'] = Integer(val['resource_id']) - end - params + # returns an array of hash containing all the parameters of a given resource + def self.find_all_params_from_resource(db_resource) + params = db_resource.connection.select_all("SELECT v.id, v.value, v.line, v.resource_id, v.param_name_id, n.name FROM param_values v INNER JOIN param_names n ON v.param_name_id=n.id WHERE v.resource_id=#{db_resource.id}") + params.each do |val| + val['value'] = unserialize_value(val['value']) + val['line'] = val['line'] ? Integer(val['line']) : nil + val['resource_id'] = Integer(val['resource_id']) end + params + end - # returns an array of hash containing all the parameters of a given host - def self.find_all_params_from_host(db_host) - params = db_host.connection.select_all("SELECT v.id, v.value, v.line, v.resource_id, v.param_name_id, n.name FROM param_values v INNER JOIN resources r ON v.resource_id=r.id INNER JOIN param_names n ON v.param_name_id=n.id WHERE r.host_id=#{db_host.id}") - params.each do |val| - val['value'] = unserialize_value(val['value']) - val['line'] = val['line'] ? Integer(val['line']) : nil - val['resource_id'] = Integer(val['resource_id']) - end - params + # returns an array of hash containing all the parameters of a given host + def self.find_all_params_from_host(db_host) + params = db_host.connection.select_all("SELECT v.id, v.value, v.line, v.resource_id, v.param_name_id, n.name FROM param_values v INNER JOIN resources r ON v.resource_id=r.id INNER JOIN param_names n ON v.param_name_id=n.id WHERE r.host_id=#{db_host.id}") + params.each do |val| + val['value'] = unserialize_value(val['value']) + val['line'] = val['line'] ? Integer(val['line']) : nil + val['resource_id'] = Integer(val['resource_id']) end + params + end - def to_s - "#{self.name} => #{self.value}" - end + def to_s + "#{self.name} => #{self.value}" + end end diff --git a/lib/puppet/rails/puppet_tag.rb b/lib/puppet/rails/puppet_tag.rb index 2cc2c135f..fadbfa6f6 100644 --- a/lib/puppet/rails/puppet_tag.rb +++ b/lib/puppet/rails/puppet_tag.rb @@ -2,9 +2,9 @@ require 'puppet/rails/resource_tag' require 'puppet/util/rails/cache_accumulator' class Puppet::Rails::PuppetTag < ActiveRecord::Base - has_many :resource_tags, :dependent => :destroy - has_many :resources, :through => :resource_tags + has_many :resource_tags, :dependent => :destroy + has_many :resources, :through => :resource_tags - include Puppet::Util::CacheAccumulator - accumulates :name + include Puppet::Util::CacheAccumulator + accumulates :name end diff --git a/lib/puppet/rails/resource.rb b/lib/puppet/rails/resource.rb index f485909a3..a5cdd0c13 100644 --- a/lib/puppet/rails/resource.rb +++ b/lib/puppet/rails/resource.rb @@ -6,226 +6,226 @@ require 'puppet/rails/benchmark' require 'puppet/util/rails/collection_merger' class Puppet::Rails::Resource < ActiveRecord::Base - include Puppet::Util::CollectionMerger - include Puppet::Util::ReferenceSerializer - include Puppet::Rails::Benchmark + include Puppet::Util::CollectionMerger + include Puppet::Util::ReferenceSerializer + include Puppet::Rails::Benchmark + + has_many :param_values, :dependent => :destroy, :class_name => "Puppet::Rails::ParamValue" + has_many :param_names, :through => :param_values, :class_name => "Puppet::Rails::ParamName" - has_many :param_values, :dependent => :destroy, :class_name => "Puppet::Rails::ParamValue" - has_many :param_names, :through => :param_values, :class_name => "Puppet::Rails::ParamName" + has_many :resource_tags, :dependent => :destroy, :class_name => "Puppet::Rails::ResourceTag" + has_many :puppet_tags, :through => :resource_tags, :class_name => "Puppet::Rails::PuppetTag" - has_many :resource_tags, :dependent => :destroy, :class_name => "Puppet::Rails::ResourceTag" - has_many :puppet_tags, :through => :resource_tags, :class_name => "Puppet::Rails::PuppetTag" + belongs_to :source_file + belongs_to :host - belongs_to :source_file - belongs_to :host + @tags = {} + def self.tags + @tags + end - @tags = {} - def self.tags - @tags + # Determine the basic details on the resource. + def self.rails_resource_initial_args(resource) + result = [:type, :title, :line].inject({}) do |hash, param| + # 'type' isn't a valid column name, so we have to use another name. + to = (param == :type) ? :restype : param + if value = resource.send(param) + hash[to] = value + end + hash end - # Determine the basic details on the resource. - def self.rails_resource_initial_args(resource) - result = [:type, :title, :line].inject({}) do |hash, param| - # 'type' isn't a valid column name, so we have to use another name. - to = (param == :type) ? :restype : param - if value = resource.send(param) - hash[to] = value - end - hash - end - - # We always want a value here, regardless of what the resource has, - # so we break it out separately. - result[:exported] = resource.exported || false - - result - end + # We always want a value here, regardless of what the resource has, + # so we break it out separately. + result[:exported] = resource.exported || false - def add_resource_tag(tag) - pt = Puppet::Rails::PuppetTag.accumulate_by_name(tag) - resource_tags.build(:puppet_tag => pt) - end + result + end - def file - (f = self.source_file) ? f.filename : nil - end + def add_resource_tag(tag) + pt = Puppet::Rails::PuppetTag.accumulate_by_name(tag) + resource_tags.build(:puppet_tag => pt) + end - def file=(file) - self.source_file = Puppet::Rails::SourceFile.find_or_create_by_filename(file) - end + def file + (f = self.source_file) ? f.filename : nil + end - def title - unserialize_value(self[:title]) - end + def file=(file) + self.source_file = Puppet::Rails::SourceFile.find_or_create_by_filename(file) + end - def params_list - @params_list ||= [] - end + def title + unserialize_value(self[:title]) + end - def params_list=(params) - @params_list = params - end + def params_list + @params_list ||= [] + end - def add_param_to_list(param) - params_list << param - end + def params_list=(params) + @params_list = params + end - def tags_list - @tags_list ||= [] - end + def add_param_to_list(param) + params_list << param + end - def tags_list=(tags) - @tags_list = tags - end + def tags_list + @tags_list ||= [] + end - def add_tag_to_list(tag) - tags_list << tag - end + def tags_list=(tags) + @tags_list = tags + end - def [](param) - super || parameter(param) - end + def add_tag_to_list(tag) + tags_list << tag + end - # Make sure this resource is equivalent to the provided Parser resource. - def merge_parser_resource(resource) - accumulate_benchmark("Individual resource merger", :attributes) { merge_attributes(resource) } - accumulate_benchmark("Individual resource merger", :parameters) { merge_parameters(resource) } - accumulate_benchmark("Individual resource merger", :tags) { merge_tags(resource) } - save - end + def [](param) + super || parameter(param) + end - def merge_attributes(resource) - args = self.class.rails_resource_initial_args(resource) - args.each do |param, value| - self[param] = value unless resource[param] == value - end + # Make sure this resource is equivalent to the provided Parser resource. + def merge_parser_resource(resource) + accumulate_benchmark("Individual resource merger", :attributes) { merge_attributes(resource) } + accumulate_benchmark("Individual resource merger", :parameters) { merge_parameters(resource) } + accumulate_benchmark("Individual resource merger", :tags) { merge_tags(resource) } + save + end - # Handle file specially - self.file = resource.file if (resource.file and (!resource.file or self.file != resource.file)) + def merge_attributes(resource) + args = self.class.rails_resource_initial_args(resource) + args.each do |param, value| + self[param] = value unless resource[param] == value end - def merge_parameters(resource) - catalog_params = {} - resource.each do |param, value| - catalog_params[param.to_s] = value - end - - db_params = {} - - deletions = [] - params_list.each do |value| - # First remove any parameters our catalog resource doesn't have at all. - deletions << value['id'] and next unless catalog_params.include?(value['name']) - - # Now store them for later testing. - db_params[value['name']] ||= [] - db_params[value['name']] << value - end - - # Now get rid of any parameters whose value list is different. - # This might be extra work in cases where an array has added or lost - # a single value, but in the most common case (a single value has changed) - # this makes sense. - db_params.each do |name, value_hashes| - values = value_hashes.collect { |v| v['value'] } - - value_hashes.each { |v| deletions << v['id'] } unless value_compare(catalog_params[name], values) - end - - # Perform our deletions. - Puppet::Rails::ParamValue.delete(deletions) unless deletions.empty? - - # Lastly, add any new parameters. - catalog_params.each do |name, value| - next if db_params.include?(name) && ! db_params[name].find{ |val| deletions.include?( val["id"] ) } - values = value.is_a?(Array) ? value : [value] - - values.each do |v| - param_values.build(:value => serialize_value(v), :line => resource.line, :param_name => Puppet::Rails::ParamName.accumulate_by_name(name)) - end - end - end + # Handle file specially + self.file = resource.file if (resource.file and (!resource.file or self.file != resource.file)) + end - # Make sure the tag list is correct. - def merge_tags(resource) - in_db = [] - deletions = [] - resource_tags = resource.tags - tags_list.each do |tag| - deletions << tag['id'] and next unless resource_tags.include?(tag['name']) - in_db << tag['name'] - end - Puppet::Rails::ResourceTag.delete(deletions) unless deletions.empty? - - (resource_tags - in_db).each do |tag| - add_resource_tag(tag) - end + def merge_parameters(resource) + catalog_params = {} + resource.each do |param, value| + catalog_params[param.to_s] = value end - def value_compare(v,db_value) - v = [v] unless v.is_a?(Array) + db_params = {} - v == db_value - end + deletions = [] + params_list.each do |value| + # First remove any parameters our catalog resource doesn't have at all. + deletions << value['id'] and next unless catalog_params.include?(value['name']) + + # Now store them for later testing. + db_params[value['name']] ||= [] + db_params[value['name']] << value + end + + # Now get rid of any parameters whose value list is different. + # This might be extra work in cases where an array has added or lost + # a single value, but in the most common case (a single value has changed) + # this makes sense. + db_params.each do |name, value_hashes| + values = value_hashes.collect { |v| v['value'] } - def name - ref + value_hashes.each { |v| deletions << v['id'] } unless value_compare(catalog_params[name], values) end - def parameter(param) - if pn = param_names.find_by_name(param) - return (pv = param_values.find(:first, :conditions => [ 'param_name_id = ?', pn])) ? pv.value : nil - end - end + # Perform our deletions. + Puppet::Rails::ParamValue.delete(deletions) unless deletions.empty? + + # Lastly, add any new parameters. + catalog_params.each do |name, value| + next if db_params.include?(name) && ! db_params[name].find{ |val| deletions.include?( val["id"] ) } + values = value.is_a?(Array) ? value : [value] + + values.each do |v| + param_values.build(:value => serialize_value(v), :line => resource.line, :param_name => Puppet::Rails::ParamName.accumulate_by_name(name)) + end + end + end - def ref(dummy_argument=:work_arround_for_ruby_GC_bug) - "#{self[:restype].split("::").collect { |s| s.capitalize }.join("::")}[#{self.title}]" - end + # Make sure the tag list is correct. + def merge_tags(resource) + in_db = [] + deletions = [] + resource_tags = resource.tags + tags_list.each do |tag| + deletions << tag['id'] and next unless resource_tags.include?(tag['name']) + in_db << tag['name'] + end + Puppet::Rails::ResourceTag.delete(deletions) unless deletions.empty? - # Returns a hash of parameter names and values, no ActiveRecord instances. - def to_hash - Puppet::Rails::ParamValue.find_all_params_from_resource(self).inject({}) do |hash, value| - hash[value['name']] ||= [] - hash[value['name']] << value.value - hash - end + (resource_tags - in_db).each do |tag| + add_resource_tag(tag) + end + end + + def value_compare(v,db_value) + v = [v] unless v.is_a?(Array) + + v == db_value + end + + def name + ref + end + + def parameter(param) + if pn = param_names.find_by_name(param) + return (pv = param_values.find(:first, :conditions => [ 'param_name_id = ?', pn])) ? pv.value : nil + end + end + + def ref(dummy_argument=:work_arround_for_ruby_GC_bug) + "#{self[:restype].split("::").collect { |s| s.capitalize }.join("::")}[#{self.title}]" + end + + # Returns a hash of parameter names and values, no ActiveRecord instances. + def to_hash + Puppet::Rails::ParamValue.find_all_params_from_resource(self).inject({}) do |hash, value| + hash[value['name']] ||= [] + hash[value['name']] << value.value + hash end + end + + # Convert our object to a resource. Do not retain whether the object + # 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") - # Convert our object to a resource. Do not retain whether the object - # 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("updated_at") - hash.delete("source_file_id") - hash.delete("created_at") - hash.delete("id") - hash.each do |p, v| - hash.delete(p) if v.nil? - end - hash[:scope] = scope - hash[:source] = scope.source - hash[:params] = [] - names = [] - self.param_names.each do |pname| - # We can get the same name multiple times because of how the - # db layout works. - next if names.include?(pname.name) - names << pname.name - hash[:params] << pname.to_resourceparam(self, scope.source) - end - obj = Puppet::Parser::Resource.new(hash) - - # Store the ID, so we can check if we're re-collecting the same resource. - obj.rails_id = self.id - - obj + # FIXME At some point, we're going to want to retain this information + # for logging and auditing. + hash.delete("host_id") + hash.delete("updated_at") + hash.delete("source_file_id") + hash.delete("created_at") + hash.delete("id") + hash.each do |p, v| + hash.delete(p) if v.nil? end + hash[:scope] = scope + hash[:source] = scope.source + hash[:params] = [] + names = [] + self.param_names.each do |pname| + # We can get the same name multiple times because of how the + # db layout works. + next if names.include?(pname.name) + names << pname.name + hash[:params] << pname.to_resourceparam(self, scope.source) + end + obj = Puppet::Parser::Resource.new(hash) + + # Store the ID, so we can check if we're re-collecting the same resource. + obj.rails_id = self.id + + obj + end end diff --git a/lib/puppet/rails/resource_tag.rb b/lib/puppet/rails/resource_tag.rb index 0bbac11d3..1c1aa4595 100644 --- a/lib/puppet/rails/resource_tag.rb +++ b/lib/puppet/rails/resource_tag.rb @@ -1,26 +1,26 @@ class Puppet::Rails::ResourceTag < ActiveRecord::Base - belongs_to :puppet_tag - belongs_to :resource + belongs_to :puppet_tag + belongs_to :resource - def to_label - "#{self.puppet_tag.name}" - end + def to_label + "#{self.puppet_tag.name}" + end - # returns an array of hash containing tags of resource - def self.find_all_tags_from_resource(db_resource) - tags = db_resource.connection.select_all("SELECT t.id, t.resource_id, p.name FROM resource_tags t INNER JOIN puppet_tags p ON t.puppet_tag_id=p.id WHERE t.resource_id=#{db_resource.id}") - tags.each do |val| - val['resource_id'] = Integer(val['resource_id']) - end - tags + # returns an array of hash containing tags of resource + def self.find_all_tags_from_resource(db_resource) + tags = db_resource.connection.select_all("SELECT t.id, t.resource_id, p.name FROM resource_tags t INNER JOIN puppet_tags p ON t.puppet_tag_id=p.id WHERE t.resource_id=#{db_resource.id}") + tags.each do |val| + val['resource_id'] = Integer(val['resource_id']) end + tags + end - # returns an array of hash containing tags of a host - def self.find_all_tags_from_host(db_host) - tags = db_host.connection.select_all("SELECT t.id, t.resource_id, p.name FROM resource_tags t INNER JOIN resources r ON t.resource_id=r.id INNER JOIN puppet_tags p ON t.puppet_tag_id=p.id WHERE r.host_id=#{db_host.id}") - tags.each do |val| - val['resource_id'] = Integer(val['resource_id']) - end - tags + # returns an array of hash containing tags of a host + def self.find_all_tags_from_host(db_host) + tags = db_host.connection.select_all("SELECT t.id, t.resource_id, p.name FROM resource_tags t INNER JOIN resources r ON t.resource_id=r.id INNER JOIN puppet_tags p ON t.puppet_tag_id=p.id WHERE r.host_id=#{db_host.id}") + tags.each do |val| + val['resource_id'] = Integer(val['resource_id']) end + tags + end end diff --git a/lib/puppet/rails/source_file.rb b/lib/puppet/rails/source_file.rb index 6a691893a..02557ffca 100644 --- a/lib/puppet/rails/source_file.rb +++ b/lib/puppet/rails/source_file.rb @@ -1,8 +1,8 @@ class Puppet::Rails::SourceFile < ActiveRecord::Base - has_one :host - has_one :resource + has_one :host + has_one :resource - def to_label - "#{self.filename}" - end + def to_label + "#{self.filename}" + end end |