forked from ripienaar/puppet-catalog-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diffcatalogs.rb
executable file
·141 lines (116 loc) · 3.32 KB
/
diffcatalogs.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env ruby
# A tool to compare catalogs that have been converted to our intermediate
# format using dumplocalconfig.rb
#
# Contact:
# R.I.Pienaar <[email protected]> - www.devco.net - @ripienaar
require 'yaml'
require 'pp'
require 'rubygems'
require 'diffy'
require 'getopt/std'
def print_help()
puts
puts "USAGE: diffcatalogs.rb -o <ref> -n <test> [-u]"
puts "\t -o <ref> the old catalog"
puts "\t -n <test> the new catalog"
puts "\t -u print individual resource diffs in unified diff format"
puts
exit 1
end
opt = Getopt::Std.getopts("o:n:u")
if not opt["n"] or not opt["o"]
print_help()
end
if opt["n"]
TO=opt["n"]
end
if opt["o"]
FROM=opt["o"]
end
if opt["u"]
UNIFIED=true
else
UNIFIED=false
end
[FROM, TO].each do |r|
unless File.exist?(r)
puts "Cannot find resources in #{r}"
exit 1
end
end
# Creates an array of just the resource titles
# it would be records like file["/foo"]
def extract_titles(resources)
titles = []
resources.each do |resource|
titles << resource[:resource_id]
end
titles
end
# Make a resource into a string that looks like puppet code
def string_resource(resource)
mystring="\t" + resource[:type].downcase + '{"' + resource[:title] + '":'+"\n"
resource[:parameters].each_pair do |k,v|
if v.is_a?(Array)
indent = " " * k.to_s.size
mystring+="\t #{k} => [\n"
v.each do |val|
mystring+="\t #{indent} #{val},\n"
end
mystring+="\t #{indent} ]\n"
else
mystring+="\t #{k} => #{v}\n"
end
end
mystring+="\t}\n"
end
# 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
def compare_resources(old, new, unified)
puts "Individual Resource differences:"
old.each do |resource|
new_resource = new.find{|res| res[:resource_id] == resource[:resource_id]}
next if new_resource.nil?
unless new_resource[:parameters] == resource[:parameters]
if unified
#Only print the diff of resources
puts Diffy::Diff.new( string_resource(resource), string_resource(new_resource), :diff => "-U 1000")
else
puts "Old Resource:"
puts string_resource(resource)
puts
puts "New Resource:"
puts string_resource(new_resource)
end
end
end
end
# Takes arrays of resource titles and shows the differences
def print_resource_diffs(r1, r2)
diffresources = r1 - r2
diffresources.each {|resource| puts "\t#{resource}"}
end
from = YAML.load(File.read(FROM))
to = YAML.load(File.read(TO))
unified = UNIFIED
titles = {}
titles[:to] = extract_titles(to)
titles[:from] = extract_titles(from)
puts "Resource counts:"
puts "\tOld: #{titles[:from].size}"
puts "\tNew: #{titles[:to].size}"
puts
if titles[:from].size > titles[:to].size
puts "Resources not in new catalog"
print_resource_diffs(titles[:from], titles[:to])
elsif titles[:to].size > titles[:from].size
puts "Resources not in old catalog"
print_resource_diffs(titles[:to], titles[:from])
else
puts "Catalogs contain the same resources by resource title"
end
puts
puts
compare_resources(from, to, unified)