Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 242 lines (183 sloc) 6.154 kb
0df0f35 first commit
joewilliams authored
1 #
2 ## Author:: Joe Williams (j@boundary.com)
3 ##
4 ## Licensed under the Apache License, Version 2.0 (the "License");
5 ## you may not use this file except in compliance with the License.
6 ## You may obtain a copy of the License at
7 ##
8 ## http://www.apache.org/licenses/LICENSE-2.0
9 ##
10 ## Unless required by applicable law or agreed to in writing, software
11 ## distributed under the License is distributed on an "AS IS" BASIS,
12 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ## See the License for the specific language governing permissions and
14 ## limitations under the License.
15 ##
16 #
17
1701ba8 add find plugin and create a common lib dir
joewilliams authored
18 require File.expand_path('../lib/boundary_knife_plugins.rb', __FILE__)
19
0df0f35 first commit
joewilliams authored
20 require 'chef/knife'
21 require 'digest/md5'
1701ba8 add find plugin and create a common lib dir
joewilliams authored
22
23 module BoundaryKnifePlugins
0df0f35 first commit
joewilliams authored
24 class DiffCookbooks < Chef::Knife
1701ba8 add find plugin and create a common lib dir
joewilliams authored
25 include Common
0df0f35 first commit
joewilliams authored
26
27 deps do
28 require 'chef/cookbook_loader'
29 end
30
31 banner "knife diff cookbooks"
32
33 def run
34 self.config = Chef::Config.merge!(config)
35
1701ba8 add find plugin and create a common lib dir
joewilliams authored
36 remote_cookbooks = build_sorted_remote_cookbook_list(rest)
0df0f35 first commit
joewilliams authored
37
1701ba8 add find plugin and create a common lib dir
joewilliams authored
38 local_cookbooks = build_sorted_local_cookbook_list
0df0f35 first commit
joewilliams authored
39
1701ba8 add find plugin and create a common lib dir
joewilliams authored
40 local_only = diff_lists(local_cookbooks, remote_cookbooks)
0df0f35 first commit
joewilliams authored
41 ui.info("Local orphan cookbooks:")
42 ui.info(local_only)
43
1701ba8 add find plugin and create a common lib dir
joewilliams authored
44 remote_only = diff_lists(remote_cookbooks, local_cookbooks)
0df0f35 first commit
joewilliams authored
45 ui.info("\nRemote orphan cookbooks:")
46 ui.info(remote_only)
47
48 end
49 end
50
51 class DiffCookbook < Chef::Knife
1701ba8 add find plugin and create a common lib dir
joewilliams authored
52 include Common
53
0df0f35 first commit
joewilliams authored
54 deps do
55 require 'chef/cookbook_loader'
56 end
57
58 banner "knife diff cookbook COOKBOOK [options]"
59
60 option :all,
61 :short => "-a",
62 :long => "--all",
63 :description => "Compare all cookbooks (list provided by local repo), rather than just a single cookbook"
64
65 def run
66 self.config = Chef::Config.merge!(config)
67
68 if config[:all]
1701ba8 add find plugin and create a common lib dir
joewilliams authored
69 remote_cookbooks = build_sorted_local_cookbook_list
0df0f35 first commit
joewilliams authored
70
71 remote_cookbooks.each do |cookbook|
1701ba8 add find plugin and create a common lib dir
joewilliams authored
72 cookbook_compare(ui, rest, cookbook)
0df0f35 first commit
joewilliams authored
73 end
74
75 else
76 unless name_args.size == 1
77 ui.error("Please specify a cookbook!")
78 show_usage
79 exit 1
80 end
81
1701ba8 add find plugin and create a common lib dir
joewilliams authored
82 cookbook_compare(ui, rest, name_args.first)
0df0f35 first commit
joewilliams authored
83 end
84 end
85 end
86
992a7be initial databag support
joewilliams authored
87 class DiffDatabags < Chef::Knife
1701ba8 add find plugin and create a common lib dir
joewilliams authored
88 include Common
89
992a7be initial databag support
joewilliams authored
90 deps do
91 require 'chef/data_bag'
92 end
93
94 banner "knife diff databags"
95
96 def run
97 path = "#{Chef::Config[:cookbook_path]}/../data_bags"
98
1701ba8 add find plugin and create a common lib dir
joewilliams authored
99 local_dbags = get_sorted_local_databags(path)
992a7be initial databag support
joewilliams authored
100
101 remote_dbags = Chef::DataBag.list.keys.sort
102
103 ui.info("Local orphan databags:")
104 ui.info(local_dbags.sort - remote_dbags)
105
106 ui.info("\nRemote orphan databags:")
107 ui.info(remote_dbags - local_dbags.sort)
108 end
109
110 end
111
112 class DiffDatabagItems < Chef::Knife
1701ba8 add find plugin and create a common lib dir
joewilliams authored
113 include Common
114
992a7be initial databag support
joewilliams authored
115 deps do
116 require 'chef/data_bag'
117 end
118
119 banner "knife diff databag items DATABAG [options]"
120
121 option :all,
122 :short => "-a",
123 :long => "--all",
124 :description => "Compare all data bags (list provided by local repo), rather than just a single cookbook"
125
126 def run
127 path = "#{Chef::Config[:cookbook_path]}/../data_bags"
128
129 if config[:all]
1701ba8 add find plugin and create a common lib dir
joewilliams authored
130 local_dbags = get_sorted_local_databags(path)
992a7be initial databag support
joewilliams authored
131
132 local_dbags.each do |databag|
1701ba8 add find plugin and create a common lib dir
joewilliams authored
133 compare_databag_item_lists(ui, path, databag)
992a7be initial databag support
joewilliams authored
134 end
135 else
1701ba8 add find plugin and create a common lib dir
joewilliams authored
136 compare_databag_item_lists(ui, path, name_args.first)
992a7be initial databag support
joewilliams authored
137 end
138 end
139
140 end
141
142 class DiffDatabag < Chef::Knife
1701ba8 add find plugin and create a common lib dir
joewilliams authored
143 include Common
144
992a7be initial databag support
joewilliams authored
145 deps do
146 require 'chef/data_bag'
147 end
148
149 banner "knife diff databag DATABAG [options]"
150
151 option :all,
152 :short => "-a",
153 :long => "--all",
154 :description => "Compare all data bags (list provided by local repo), rather than just a single cookbook"
155
156 def run
157 path = "#{Chef::Config[:cookbook_path]}/../data_bags"
158
159 if config[:all]
160 remote_dbags = Chef::DataBag.list.keys
161
162 remote_dbags.each do |dbag|
1701ba8 add find plugin and create a common lib dir
joewilliams authored
163 compare_databag_items(ui, path, dbag)
992a7be initial databag support
joewilliams authored
164 end
165
166 else
1701ba8 add find plugin and create a common lib dir
joewilliams authored
167 compare_databag_items(ui, path, name_args.first)
992a7be initial databag support
joewilliams authored
168 end
169 end
170
171 end
0df0f35 first commit
joewilliams authored
172
adca6c7 add diff role, thanks spheromak
joewilliams authored
173 class DiffRole < Chef::Knife
174
175 deps do
176 require 'chef/role'
177 require 'chef/knife/core/object_loader'
178 require 'chef/json_compat'
179 end
180
181 option :all,
182 :short => "-a",
183 :long => "--all",
184 :description => "Compare all roles, Uses remote list to determine set. Only supports local .json roles for now"
185
186 banner "knife diff role ROLE"
187
188 def loader
189 @loader ||= Chef::Knife::Core::ObjectLoader.new(Chef::Role, ui)
190 end
191
192 def run
193 @path = "#{Chef::Config[:cookbook_path].first}/../roles"
194 @local_roles = local_roles
195 @remote_roles = format_list_for_display(Chef::Role.list)
196
197 if config[:all]
198 @remote_roles.each do |role|
199 compare_role(role)
200 end
201 else
202 compare_role(name_args.first)
203 end
204 end
205
206
207 # md5 and cmp remote to local
208 def compare_role(role)
209 if @local_roles.has_key?(role)
210 remote_role = Chef::Role.load(role)
211 remote_sum = digest(Chef::Role.load(role).to_json)
212 local_sum = digest(loader.load_from("roles", @local_roles[role]).to_json)
213 ui.info "#{role} is out of sync" if local_sum != remote_sum
214 else
215 ui.info "#{role} is on server, but not found in local list"
216 end
217 end
218
219 # return an array of rolefile names
220 # supports 2 levels of subdirs
221 def local_files
222 files = []
223 %w/json rb/.each do |ext|
224 files << Dir["#{@path}/*.#{ext}", "#{@path}/*/*.#{ext}", "#{@path}/*/*/*.#{ext}"]
225 end
226 files.flatten.sort.uniq
227 end
228
229 # hash of local role names as keys with path as values
230 def local_roles
231 file_map = {}
232 local_files.map {|file| file_map[ File.basename(file, File.extname(file)) ] = file }
233 file_map
234 end
235
236 # generate an md5sum
237 def digest(data)
238 Digest::MD5.hexdigest(data)
239 end
240 end
d71cbcd a few clean ups
joewilliams authored
241
0df0f35 first commit
joewilliams authored
242 end
Something went wrong with that request. Please try again.