Class: Puppet::CatalogDiff::Differ
- Inherits:
-
Object
- Object
- Puppet::CatalogDiff::Differ
- Includes:
- Comparer, Preprocessor
- Defined in:
- lib/puppet/catalog-diff/differ.rb
Overview
Differ allows to diff two catalogs
Instance Attribute Summary collapse
-
#from_file ⇒ Object
Returns the value of attribute from_file.
-
#to_file ⇒ Object
Returns the value of attribute to_file.
Instance Method Summary collapse
- #diff(options = {}) ⇒ Object
-
#initialize(from, to) ⇒ Differ
constructor
A new instance of Differ.
- #str_to_catalog(str) ⇒ Object
Constructor Details
#initialize(from, to) ⇒ Differ
Returns a new instance of Differ.
22 23 24 25 |
# File 'lib/puppet/catalog-diff/differ.rb', line 22 def initialize(from, to) @from_file = from @to_file = to end |
Instance Attribute Details
#from_file ⇒ Object
Returns the value of attribute from_file.
20 21 22 |
# File 'lib/puppet/catalog-diff/differ.rb', line 20 def from_file @from_file end |
#to_file ⇒ Object
Returns the value of attribute to_file.
20 21 22 |
# File 'lib/puppet/catalog-diff/differ.rb', line 20 def to_file @to_file end |
Instance Method Details
#diff(options = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/puppet/catalog-diff/differ.rb', line 31 def diff( = {}) from = [] = {} to = [] = {} { from_file => [from, ], to_file => [to, ] }.each do |r, a| v, m = a raise "Cannot find resources in #{r}" unless File.exist?(r) case File.extname(r) when '.yaml' tmp = YAML.safe_load(File.read(r)) when '.marshal' tmp = Marshal.load(File.read(r)) when '.pson' tmp = PSON.parse(File.read(r)) tmp = str_to_catalog(tmp) unless tmp.respond_to?(:version) when '.json' tmp = str_to_catalog(JSON.parse(File.read(r))) else raise 'Provide catalog with the appropriate file extension, valid extensions are json, pson, yaml and marshal' end m[:version] = tmp.version m[:environment] = tmp.environment convert25(tmp, v) end if [:exclude_classes] [to, from].each do |c| c.reject! { |x| x[:type] == 'Class' } end end if [:exclude_defined_resources] [to, from].each do |c| c.reject! { |x| x[:type].include?('::') } end end if [:exclude_resource_types] types = [:exclude_resource_types].split(',') [to, from].each do |c| c.reject! { |x| types.include?(x[:type]) } end end Puppet.debug("Processing: #{from_file}") titles = {} titles[:to] = extract_titles(to) titles[:from] = extract_titles(from) output = {} output[:old_version] = [:version].to_s output[:new_version] = [:version].to_s output[:old_environment] = [:environment] output[:new_environment] = [:environment] output[:total_resources_in_old] = titles[:from].size output[:total_resources_in_new] = titles[:to].size resource_diffs_titles = return_resource_diffs(titles[:from], titles[:to]) output[:only_in_old] = resource_diffs_titles[:titles_only_in_old] output[:only_in_new] = resource_diffs_titles[:titles_only_in_new] resource_diffs = compare_resources(from, to, ) output[:differences_in_old] = resource_diffs[:old] output[:differences_in_new] = resource_diffs[:new] output[:differences_as_diff] = resource_diffs[:string_diffs] output[:params_in_old] = resource_diffs[:old_params] output[:params_in_new] = resource_diffs[:new_params] output[:content_differences] = resource_diffs[:content_differences] additions = resource_diffs_titles[:titles_only_in_new].size subtractions = resource_diffs_titles[:titles_only_in_old].size changes = resource_diffs[:new_params].keys.size changes_percentage = ((titles[:from].size.zero? && 0) || (100 * (resource_diffs[:new_params].keys.size.to_f / titles[:from].size))) additions_percentage = ((titles[:to].size.zero? && 0) || (100 * (additions.to_f / titles[:to].size))) subtractions_percentage = ((titles[:from].size.zero? && 0) || (100 * (subtractions.to_f / titles[:from].size))) output[:catalag_percentage_added] = '%.2f' % additions_percentage output[:catalog_percentage_removed] = '%.2f' % subtractions_percentage output[:catalog_percentage_changed] = '%.2f' % changes_percentage output[:added_and_removed_resources] = "#{(!additions.zero? && "+#{additions}") || 0} / #{(!subtractions.zero? && "-#{subtractions}") || 0}" divide_by = (changes_percentage.zero? ? 0 : 1) + (additions_percentage.zero? ? 0 : 1) + (subtractions_percentage.zero? ? 0 : 1) output[:node_percentage] = ((divide_by.zero? && 0) || (additions_percentage == 100 && 100) || ((changes_percentage + additions_percentage + subtractions_percentage) / divide_by)).to_f output[:node_differences] = (additions.abs.to_i + subtractions.abs.to_i + changes.abs.to_i) output end |
#str_to_catalog(str) ⇒ Object
27 28 29 |
# File 'lib/puppet/catalog-diff/differ.rb', line 27 def str_to_catalog(str) Puppet::Resource::Catalog.from_data_hash str end |