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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
# File 'lib/chef/knife/depsolver.rb', line 50
def run
begin
DepSelector::Debug.log.level = Logger::INFO
use_local_depsolver = false
if config[:env_constraints_filter_universe]
if config[:node] || config[:environment] || config[:timeout] || config[:capture] || config[:expanded_run_list] || config[:csv_universe_to_json]
msg("ERROR: The --env-constraints-filter-universe option is only compatible with the --env-constraints and --universe options")
exit!
elsif !(config[:env_constraints] && config[:universe])
msg("ERROR: The --env-constraints-filter-universe option requires the --env-constraints and --universe options to be set")
exit!
end
else
if config[:env_constraints] || config[:universe] || config[:expanded_run_list]
if config[:env_constraints] && config[:universe] && config[:expanded_run_list]
use_local_depsolver = true
unless name_args.empty?
puts "ERROR: Setting a run list on the command line is not compatible with the --env-constraints, --universe or --expanded-run-list options"
exit!
end
if config[:node]
puts "ERROR: The --node option is not compatible with the --env-constraints, --universe or --expanded-run-list options"
exit!
end
if config[:environment]
puts "ERROR: The --environment option is not compatible with the --env-constraints, --universe or --expanded-run-list options"
exit!
end
if config[:capture]
puts "ERROR: The --capture option is not compatible with the --env-constraints, --universe or --expanded-run-list options"
exit!
end
else
puts "ERROR: The --env-constraints, --universe and --expanded-run-list options must be used together to use the local depsolver"
exit!
end
end
end
if config[:timeout] && !use_local_depsolver
msg("ERROR: The --timeout option requires the --env-constraints, --universe and --expanded-run-list options to be set")
exit!
end
if config[:print_constrained_cookbook_set] && !use_local_depsolver
msg("ERROR: The --print-constrained-cookbook-set option requires the --env-constraints, --universe and --expanded-run-list options to be set")
exit!
end
timeout = (config[:timeout].to_f * 1000).to_i if config[:timeout]
timeout ||= 5 * 1000
if config[:csv_universe_to_json]
unless File.file?(config[:csv_universe_to_json])
msg("ERROR: #{config[:csv_universe_to_json]} does not exist or is not a file.")
exit!
end
universe = Hash.new { |hash, key| hash[key] = Hash.new }
IO.foreach(config[:csv_universe_to_json]) do |line|
name, version, updated_at, dependencies = line.split(",", 4)
universe[name][version] = {updated_at: updated_at, dependencies: JSON.parse(dependencies)}
end
universe_json = JSON.pretty_generate(universe)
universe_filename = "universe-#{Time.now.strftime("%Y-%m-%d-%H.%M.%S")}-#{Digest::SHA1.hexdigest(universe_json)}.txt"
IO.write(universe_filename, universe_json)
puts "Cookbook universe saved to #{universe_filename}"
exit!
end
if config[:node] && !(config[:env_constraints_filter_universe] && config[:env_constraints])
node = Chef::Node.load(config[:node])
else
node = Chef::Node.new
node.name('depsolver-tmp-node')
if config[:expanded_run_list]
unless File.file?(config[:expanded_run_list])
msg("ERROR: #{config[:expanded_run_list]} does not exist or is not a file.")
exit!
end
contents = JSON.parse(IO.read(config[:expanded_run_list]))
if !(contents.key?('expanded_run_list') && contents['expanded_run_list'].is_a?(Array))
msg("ERROR: #{config[:expanded_run_list]} does not contain an expanded run list array.")
exit!
else
expanded_run_list = contents['expanded_run_list']
expanded_run_list.each do |arg|
node.run_list.add(arg)
end
end
else
run_list = name_args.map {|item| item.to_s.split(/,/) }.flatten.each{|item| item.strip! }
run_list.delete_if {|item| item.empty? }
run_list.each do |arg|
node.run_list.add(arg)
end
end
end
node.chef_environment = config[:environment] if config[:environment]
if config[:capture]
if node.chef_environment == '_default'
environment_cookbook_versions = Hash.new
else
environment_cookbook_versions = Chef::Environment.load(node.chef_environment).cookbook_versions
end
env = { name: node.chef_environment, cookbook_versions: environment_cookbook_versions }
environment_constraints_json = JSON.pretty_generate(env)
environment_constraints_filename = "#{node.chef_environment}-environment-#{Time.now.strftime("%Y-%m-%d-%H.%M.%S")}-#{Digest::SHA1.hexdigest(environment_constraints_json)}.txt"
IO.write(environment_constraints_filename, environment_constraints_json)
puts "Environment constraints saved to #{environment_constraints_filename}"
begin
universe = rest.get_rest("universe")
universe_json = JSON.pretty_generate(universe)
universe_filename = ""
rest.url.to_s.match(".*/organizations/(.*)/?") { universe_filename = "#{$1}-" }
universe_filename += "universe-#{Time.now.strftime("%Y-%m-%d-%H.%M.%S")}-#{Digest::SHA1.hexdigest(universe_json)}.txt"
IO.write(universe_filename, universe_json)
puts "Cookbook universe saved to #{universe_filename}"
rescue Net::HTTPServerException
puts "WARNING: The cookbook universe API endpoint is not available."
puts "WARNING: Try capturing the cookbook universe using the SQL query found in the knife-depsolver README."
puts "WARNING: Then convert the results using knife-depsolver's --csv-universe-to-json option"
end
end
if config[:env_constraints]
unless File.file?(config[:env_constraints])
msg("ERROR: #{config[:env_constraints]} does not exist or is not a file.")
exit!
end
env = JSON.parse(IO.read(config[:env_constraints]))
if env['name'].to_s.empty?
msg("ERROR: #{config[:env_constraints]} does not contain an environment name.")
exit!
else
node.chef_environment = env['name']
end
if !env['cookbook_versions'].is_a?(Hash)
msg("ERROR: #{config[:env_constraints]} does not contain a Hash of cookbook version constraints.")
exit!
else
environment_cookbook_versions = env['cookbook_versions']
end
end
if config[:universe]
unless File.file?(config[:universe])
msg("ERROR: #{config[:universe]} does not exist or is not a file.")
exit!
end
universe = JSON.parse(IO.read(config[:universe]))
if !universe.is_a?(Hash)
msg("ERROR: #{config[:universe]} does not contain a cookbook universe Hash.")
exit!
end
end
if config[:env_constraints_filter_universe]
env_constraints = environment_cookbook_versions.each_with_object({}) do |env_constraint, memo|
name, constraint = env_constraint
constraint, version = constraint.split
memo[name] = DepSelector::VersionConstraint.new(constraint_to_str(constraint, version))
end
universe.each do |name, versions|
versions.delete_if {|version, v| !env_constraints[name].include?(version)} if env_constraints[name]
end
filtered_universe_json = JSON.pretty_generate(universe)
filtered_universe_filename = "filtered-universe-#{Time.now.strftime("%Y-%m-%d-%H.%M.%S")}-#{Digest::SHA1.hexdigest(filtered_universe_json)}.txt"
IO.write(filtered_universe_filename, filtered_universe_json)
puts "Filtered cookbook universe saved to #{filtered_universe_filename}"
exit!
end
run_list_expansion = node.run_list.expand(node.chef_environment, 'server')
expanded_run_list_with_versions = run_list_expansion.recipes.with_version_constraints_strings
exit if config[:capture]
depsolver_results = Hash.new
if use_local_depsolver
env_ckbk_constraints = environment_cookbook_versions.map do |ckbk_name, ckbk_constraint|
[ckbk_name, ckbk_constraint.split.reverse].flatten
end
all_versions = universe.map do |ckbk_name, ckbk_metadata|
ckbk_versions = ckbk_metadata.map do |version, version_metadata|
[version, version_metadata['dependencies'].map { |dep_ckbk_name, dep_ckbk_constraint| [dep_ckbk_name, dep_ckbk_constraint.split.reverse].flatten }]
end
[ckbk_name, ckbk_versions]
end
expanded_run_list_with_split_versions = expanded_run_list_with_versions.map do |run_list_item|
name, version = run_list_item.split('@')
name.sub!(/::.*/, '')
version ? [name, version] : name
end
data = {environment_constraints: env_ckbk_constraints, all_versions: all_versions, run_list: expanded_run_list_with_split_versions, timeout_ms: timeout}
if config[:print_constrained_cookbook_set]
print_constrained_cookbook_set(data)
exit!
end
depsolver_start_time = Time.now
solution = solve(data)
depsolver_finish_time = Time.now
if solution.first == :ok
solution.last.map { |ckbk| ckbk_name, ckbk_version = ckbk; depsolver_results[ckbk_name] = ckbk_version.join('.') }
else
status, error_type, error_detail = solution
depsolver_error = { error_type => error_detail }
end
else
begin
chef_server_version = rest.get(server_url.sub(/organizations.*/, 'version')).split("\n")[0]
rescue Net::HTTPServerException
chef_server_version = "unknown"
end
depsolver_start_time = Time.now
ckbks = rest.post_rest("environments/" + node.chef_environment + "/cookbook_versions", { "run_list" => expanded_run_list_with_versions })
depsolver_finish_time = Time.now
ckbks.each do |name, ckbk|
version = ckbk.is_a?(Hash) ? ckbk['version'] : ckbk.version
depsolver_results[name] = version
end
end
rescue Net::HTTPServerException => e
api_error = {}
api_error[:error_code] = e.response.code
api_error[:error_message] = e.response.message
begin
api_error[:error_body] = JSON.parse(e.response.body)
rescue JSON::ParserError
end
rescue => e
msg("ERROR: #{e.message}")
exit!
ensure
local_software = Hash.new
%w(chef-dk chef dep_selector).each do |gem_name|
begin
local_software[gem_name] = Gem::Specification.find_by_name(gem_name).version
rescue Gem::MissingSpecError
end
end
results = {}
results[:local_software] = local_software unless local_software.empty?
if use_local_depsolver
results[:depsolver] = "used local depsolver"
else
results[:depsolver] = {"used chef server" => chef_server_version} unless chef_server_version.nil?
end
results[:node] = node.name unless node.nil? || node.name.nil?
results[:environment] = node.chef_environment unless node.chef_environment.nil?
results[:run_list] = node.run_list unless node.nil? || node.run_list.nil?
results[:expanded_run_list] = expanded_run_list_with_versions unless expanded_run_list_with_versions.nil?
results[:depsolver_results] = depsolver_results unless depsolver_results.nil? || depsolver_results.empty?
results[:depsolver_cookbook_count] = depsolver_results.count unless depsolver_results.nil? || depsolver_results.empty?
results[:depsolver_elapsed_ms] = ((depsolver_finish_time - depsolver_start_time) * 1000).to_i unless depsolver_finish_time.nil?
results[:depsolver_error] = depsolver_error unless depsolver_error.nil?
results[:api_error] = api_error unless api_error.nil?
if config[:capture]
results_json = JSON.pretty_generate(results)
expanded_run_list_filename = "expanded-run-list-#{Time.now.strftime("%Y-%m-%d-%H.%M.%S")}-#{Digest::SHA1.hexdigest(results_json)}.txt"
IO.write(expanded_run_list_filename, results_json)
puts "Expanded run list saved to #{expanded_run_list_filename}"
else
msg(JSON.pretty_generate(results))
end
end
end
|