Module: Puppet::CatalogDiff::Comparer

Included in:
Differ
Defined in:
lib/puppet/catalog-diff/comparer.rb

Overview

Comparer providers methods to compare resources

Instance Method Summary collapse

Instance Method Details

#compare_resources(old, new, options) ⇒ Object

Compares two sets of resources and prints the differences if the two sets do not include the same resource counts this will only print the resources available in both



25
26
27
28
29
30
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
# File 'lib/puppet/catalog-diff/comparer.rb', line 25

def compare_resources(old, new, options)
  resource_differences = {}
  differences_in_old = {}
  differences_in_new = {}
  string_differences = {}
  content_differences = {}
  parameters_in_old = {}
  parameters_in_new = {}
  old.each do |resource|
    new_resource = new.find { |res| res[:resource_id] == resource[:resource_id] }
    next if new_resource.nil?

    if options[:ignore_parameters]
      blacklist = options[:ignore_parameters].split(',')
      filter_parameters!(new_resource[:parameters], blacklist)
      filter_parameters!(resource[:parameters], blacklist)
    end

    sort_dependencies!(new_resource[:parameters])
    sort_dependencies!(resource[:parameters])

    next if new_resource[:parameters] == resource[:parameters]

    parameters_in_old[resource[:resource_id]] = \
      (resource[:parameters].to_a - new_resource[:parameters].to_a).to_h

    parameters_in_new[resource[:resource_id]] = \
      (new_resource[:parameters].to_a - resource[:parameters].to_a).to_h

    if options[:show_resource_diff]
      Puppet.debug("Resource diff: #{resource[:resource_id]}")

      diff_array = str_diff(
        Puppet::CatalogDiff::Formater.new.resource_to_string(resource),
        Puppet::CatalogDiff::Formater.new.resource_to_string(new_resource)
      ).split("\n")
      if diff_array.size >= 3
        string_differences[resource[:resource_id]] = diff_array[3..-1]
      else
        Puppet.debug('Could not automatically detect diff')
        string_differences[resource[:resource_id]] = resource[:parameters].inspect + new_resource[:parameters].inspect
      end

    else
      differences_in_old[resource[:resource_id]] = resource

      differences_in_new[resource[:resource_id]] = new_resource
    end

    cont_diff = str_diff(resource[:parameters][:content], new_resource[:parameters][:content])
    content_differences[resource[:resource_id]] = cont_diff if cont_diff
  end
  resource_differences[:old] = differences_in_old
  resource_differences[:new] = differences_in_new
  resource_differences[:string_diffs] = string_differences
  resource_differences[:content_differences] = content_differences
  resource_differences[:old_params]  = parameters_in_old
  resource_differences[:new_params]  = parameters_in_new
  resource_differences
end

#do_str_diff(str1, str2) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/puppet/catalog-diff/comparer.rb', line 108

def do_str_diff(str1, str2)
  paths = [str1, str2].map do |s|
    tempfile = Tempfile.new('puppet-diffing')
    tempfile.open
    tempfile.print s
    tempfile.close
    tempfile
  end
  diff = Puppet::Util::Diff.diff(paths[0].path, paths[1].path)
  paths.each(&:delete)
  diff
end

#extract_titles(resources) ⇒ Object

Creates an array of just the resource titles it would be records like file



12
13
14
15
16
17
18
19
20
# File 'lib/puppet/catalog-diff/comparer.rb', line 12

def extract_titles(resources)
  titles = []

  resources.each do |resource|
    titles << resource[:resource_id]
  end

  titles
end

#filter_parameters!(params, blacklist) ⇒ Object

filter parameters



87
88
89
# File 'lib/puppet/catalog-diff/comparer.rb', line 87

def filter_parameters!(params, blacklist)
  params.reject! { |p, _k| blacklist.include?(p.to_s) }
end

#return_resource_diffs(old, new) ⇒ Object

Takes arrays of resource titles and shows the differences



101
102
103
104
105
106
# File 'lib/puppet/catalog-diff/comparer.rb', line 101

def return_resource_diffs(old, new)
  {
    titles_only_in_old: (old - new).map(&:to_s),
    titles_only_in_new: (new - old).map(&:to_s),
  }
end

#sort_dependencies!(params) ⇒ Object

sort require/before/notify/subscribe before comparison



92
93
94
95
96
97
98
# File 'lib/puppet/catalog-diff/comparer.rb', line 92

def sort_dependencies!(params)
  params.each do |x|
    next unless %i[require before notify subscribe].include?(x[0])

    x[1].sort! if x[1].instance_of?(Array)
  end
end

#str_diff(cont1, cont2) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/puppet/catalog-diff/comparer.rb', line 129

def str_diff(cont1, cont2)
  return nil unless cont1 && cont2

  if cont1.is_a?(Hash)
    str1 = cont1[:content]
    sum1 = cont1[:checksum]
  else
    str1 = cont1
    sum1 = Digest::MD5.hexdigest(str1)
  end

  if cont2.is_a?(Hash)
    str2 = cont2[:content]
    sum2 = cont2[:checksum]
  else
    str2 = cont2
    sum2 = Digest::MD5.hexdigest(str2)
  end

  return nil if sum1 == sum2
  return nil unless str1 && str2

  str1 = validate_encoding(str1)
  str2 = validate_encoding(str2)

  @@cached_str_diffs ||= {}
  @@cached_str_diffs["#{sum1}/#{sum2}"] ||= do_str_diff(str1, str2)
end

#validate_encoding(str) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/puppet/catalog-diff/comparer.rb', line 121

def validate_encoding(str)
  unless str.valid_encoding?
    Puppet.debug("Detected that string used in diff had invalid #{str.encoding} encoding. Replacing invalid characters in diff output.")
    str.encode!('UTF-8', 'UTF-8', invalid: :replace)
  end
  str
end