puppet_litmus 1.0.1 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/exe/matrix_from_metadata_v2 +7 -5
- data/lib/puppet_litmus/inventory_manipulation.rb +4 -30
- data/lib/puppet_litmus/puppet_helpers.rb +217 -302
- data/lib/puppet_litmus/rake_helper.rb +83 -182
- data/lib/puppet_litmus/rake_tasks.rb +3 -8
- data/lib/puppet_litmus/version.rb +1 -1
- data/spec/exe/matrix_from_metadata_v2_spec.rb +3 -3
- data/spec/lib/puppet_litmus/puppet_helpers_spec.rb +0 -20
- data/spec/lib/puppet_litmus/rake_tasks_spec.rb +1 -1
- metadata +2 -30
@@ -19,12 +19,9 @@ module PuppetLitmus::PuppetHelpers
|
|
19
19
|
# :noop [Boolean] run puppet apply with the noop flag.
|
20
20
|
# @return [Boolean] The result of the 2 apply manifests.
|
21
21
|
def idempotent_apply(manifest, opts = {})
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
apply_manifest(nil, **opts, catch_failures: true, manifest_file_location: manifest_file_location)
|
26
|
-
apply_manifest(nil, **opts, catch_changes: true, manifest_file_location: manifest_file_location)
|
27
|
-
end
|
22
|
+
manifest_file_location = create_manifest_file(manifest)
|
23
|
+
apply_manifest(nil, **opts, catch_failures: true, manifest_file_location: manifest_file_location)
|
24
|
+
apply_manifest(nil, **opts, catch_changes: true, manifest_file_location: manifest_file_location)
|
28
25
|
end
|
29
26
|
|
30
27
|
# Applies a manifest. returning the result of that apply. Mimics the apply_manifest from beaker
|
@@ -49,99 +46,85 @@ module PuppetLitmus::PuppetHelpers
|
|
49
46
|
# @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
|
50
47
|
# @return [Object] A result object from the apply.
|
51
48
|
def apply_manifest(manifest, opts = {})
|
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
|
-
acceptable_exit_codes = [2]
|
77
|
-
else
|
78
|
-
use_detailed_exit_codes = false
|
79
|
-
acceptable_exit_codes = [0]
|
80
|
-
end
|
49
|
+
target_node_name = targeting_localhost? ? 'litmus_localhost' : ENV.fetch('TARGET_HOST', nil)
|
50
|
+
raise 'manifest and manifest_file_location in the opts hash are mutually exclusive arguments, pick one' if !manifest.nil? && !opts[:manifest_file_location].nil?
|
51
|
+
raise 'please pass a manifest or the manifest_file_location in the opts hash' if (manifest.nil? || manifest == '') && opts[:manifest_file_location].nil?
|
52
|
+
raise 'please specify only one of `catch_changes`, `expect_changes`, `catch_failures` or `expect_failures`' if
|
53
|
+
[opts[:catch_changes], opts[:expect_changes], opts[:catch_failures], opts[:expect_failures]].compact.length > 1
|
54
|
+
|
55
|
+
opts = { trace: true }.merge(opts)
|
56
|
+
|
57
|
+
if opts[:catch_changes]
|
58
|
+
use_detailed_exit_codes = true
|
59
|
+
acceptable_exit_codes = [0]
|
60
|
+
elsif opts[:catch_failures]
|
61
|
+
use_detailed_exit_codes = true
|
62
|
+
acceptable_exit_codes = [0, 2]
|
63
|
+
elsif opts[:expect_failures]
|
64
|
+
use_detailed_exit_codes = true
|
65
|
+
acceptable_exit_codes = [1, 4, 6]
|
66
|
+
elsif opts[:expect_changes]
|
67
|
+
use_detailed_exit_codes = true
|
68
|
+
acceptable_exit_codes = [2]
|
69
|
+
else
|
70
|
+
use_detailed_exit_codes = false
|
71
|
+
acceptable_exit_codes = [0]
|
72
|
+
end
|
81
73
|
|
82
|
-
|
83
|
-
|
74
|
+
manifest_file_location = opts[:manifest_file_location] || create_manifest_file(manifest)
|
75
|
+
inventory_hash = File.exist?('spec/fixtures/litmus_inventory.yaml') ? inventory_hash_from_inventory_file : localhost_inventory_hash
|
84
76
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
77
|
+
target_option = opts['targets'] || opts[:targets]
|
78
|
+
if target_option.nil?
|
79
|
+
raise "Target '#{target_node_name}' not found in spec/fixtures/litmus_inventory.yaml" unless target_in_inventory?(inventory_hash, target_node_name)
|
80
|
+
else
|
81
|
+
target_node_name = search_for_target(target_option, inventory_hash)
|
82
|
+
end
|
91
83
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
span.add_field('litmus.command_to_run', command_to_run)
|
114
|
-
bolt_result = Tempfile.open(['temp', '.ps1']) do |script|
|
115
|
-
script.write(command_to_run)
|
116
|
-
script.close
|
117
|
-
run_script(script.path, target_node_name, [], options: {}, config: nil, inventory: inventory_hash)
|
118
|
-
end
|
119
|
-
else
|
120
|
-
span.add_field('litmus.command_to_run', command_to_run)
|
121
|
-
bolt_result = run_command(command_to_run, target_node_name, config: nil, inventory: inventory_hash)
|
122
|
-
end
|
123
|
-
span.add_field('litmus.bolt_result', bolt_result)
|
124
|
-
result = OpenStruct.new(exit_code: bolt_result.first['value']['exit_code'],
|
125
|
-
stdout: bolt_result.first['value']['stdout'],
|
126
|
-
stderr: bolt_result.first['value']['stderr'])
|
127
|
-
span.add_field('litmus.result', result.to_h)
|
128
|
-
|
129
|
-
status = result.exit_code
|
130
|
-
if opts[:catch_changes] && !acceptable_exit_codes.include?(status)
|
131
|
-
report_puppet_apply_change(command_to_run, bolt_result)
|
132
|
-
elsif !acceptable_exit_codes.include?(status)
|
133
|
-
report_puppet_apply_error(command_to_run, bolt_result, acceptable_exit_codes)
|
84
|
+
# Forcibly set the locale of the command
|
85
|
+
locale = if os[:family] == 'windows'
|
86
|
+
''
|
87
|
+
else
|
88
|
+
'LC_ALL=en_US.UTF-8 '
|
89
|
+
end
|
90
|
+
command_to_run = "#{locale}#{opts[:prefix_command]} puppet apply #{manifest_file_location}"
|
91
|
+
command_to_run += ' --trace' if !opts[:trace].nil? && (opts[:trace] == true)
|
92
|
+
command_to_run += " --modulepath #{Dir.pwd}/spec/fixtures/modules" if target_node_name == 'litmus_localhost'
|
93
|
+
command_to_run += " --hiera_config='#{opts[:hiera_config]}'" unless opts[:hiera_config].nil?
|
94
|
+
command_to_run += ' --debug' if !opts[:debug].nil? && (opts[:debug] == true)
|
95
|
+
command_to_run += ' --noop' if !opts[:noop].nil? && (opts[:noop] == true)
|
96
|
+
command_to_run += ' --detailed-exitcodes' if use_detailed_exit_codes == true
|
97
|
+
|
98
|
+
if os[:family] == 'windows'
|
99
|
+
# IAC-1365 - Workaround for BOLT-1535 and bolt issue #1650
|
100
|
+
command_to_run = "try { #{command_to_run}; exit $LASTEXITCODE } catch { write-error $_ ; exit 1 }"
|
101
|
+
bolt_result = Tempfile.open(['temp', '.ps1']) do |script|
|
102
|
+
script.write(command_to_run)
|
103
|
+
script.close
|
104
|
+
run_script(script.path, target_node_name, [], options: {}, config: nil, inventory: inventory_hash)
|
134
105
|
end
|
106
|
+
else
|
107
|
+
bolt_result = run_command(command_to_run, target_node_name, config: nil, inventory: inventory_hash)
|
108
|
+
end
|
109
|
+
result = OpenStruct.new(exit_code: bolt_result.first['value']['exit_code'],
|
110
|
+
stdout: bolt_result.first['value']['stdout'],
|
111
|
+
stderr: bolt_result.first['value']['stderr'])
|
112
|
+
|
113
|
+
status = result.exit_code
|
114
|
+
if opts[:catch_changes] && !acceptable_exit_codes.include?(status)
|
115
|
+
report_puppet_apply_change(command_to_run, bolt_result)
|
116
|
+
elsif !acceptable_exit_codes.include?(status)
|
117
|
+
report_puppet_apply_error(command_to_run, bolt_result, acceptable_exit_codes)
|
118
|
+
end
|
135
119
|
|
136
|
-
|
120
|
+
yield result if block_given?
|
137
121
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
end
|
143
|
-
result
|
122
|
+
if ENV['RSPEC_DEBUG']
|
123
|
+
puts "apply manifest succeded\n #{command_to_run}\n======\nwith status #{result.exit_code}"
|
124
|
+
puts result.stderr
|
125
|
+
puts result.stdout
|
144
126
|
end
|
127
|
+
result
|
145
128
|
end
|
146
129
|
|
147
130
|
# Creates a manifest file locally in a temp location, if its a remote target copy it to there.
|
@@ -149,36 +132,27 @@ module PuppetLitmus::PuppetHelpers
|
|
149
132
|
# @param manifest [String] puppet manifest code.
|
150
133
|
# @return [String] The path to the location of the manifest.
|
151
134
|
def create_manifest_file(manifest, opts = {})
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
manifest_file.
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
# transfer to TARGET_HOST
|
167
|
-
inventory_hash = inventory_hash_from_inventory_file
|
168
|
-
target_option = opts['targets'] || opts[:targets]
|
169
|
-
target_node_name = search_for_target(target_option, inventory_hash) unless target_option.nil?
|
170
|
-
add_node_fields_to_span(span, target_node_name, inventory_hash)
|
171
|
-
|
172
|
-
manifest_file_location = File.basename(manifest_file)
|
173
|
-
bolt_result = upload_file(manifest_file.path, manifest_file_location, target_node_name, options: {}, config: nil, inventory: inventory_hash)
|
174
|
-
span.add_field('litmus.bolt_result', bolt_result)
|
175
|
-
raise bolt_result.first['value'].to_s unless bolt_result.first['status'] == 'success'
|
176
|
-
end
|
177
|
-
|
178
|
-
span.add_field('litmus.manifest_file_location', manifest_file_location)
|
135
|
+
require 'tmpdir'
|
136
|
+
target_node_name = ENV.fetch('TARGET_HOST', nil)
|
137
|
+
tmp_filename = File.join(Dir.tmpdir, "manifest_#{Time.now.strftime('%Y%m%d')}_#{Process.pid}_#{rand(0x100000000).to_s(36)}.pp")
|
138
|
+
manifest_file = File.open(tmp_filename, 'w')
|
139
|
+
manifest_file.write(manifest)
|
140
|
+
manifest_file.close
|
141
|
+
if target_node_name.nil? || target_node_name == 'localhost'
|
142
|
+
# no need to transfer
|
143
|
+
manifest_file_location = manifest_file.path
|
144
|
+
else
|
145
|
+
# transfer to TARGET_HOST
|
146
|
+
inventory_hash = inventory_hash_from_inventory_file
|
147
|
+
target_option = opts['targets'] || opts[:targets]
|
148
|
+
target_node_name = search_for_target(target_option, inventory_hash) unless target_option.nil?
|
179
149
|
|
180
|
-
manifest_file_location
|
150
|
+
manifest_file_location = File.basename(manifest_file)
|
151
|
+
bolt_result = upload_file(manifest_file.path, manifest_file_location, target_node_name, options: {}, config: nil, inventory: inventory_hash)
|
152
|
+
raise bolt_result.first['value'].to_s unless bolt_result.first['status'] == 'success'
|
181
153
|
end
|
154
|
+
|
155
|
+
manifest_file_location
|
182
156
|
end
|
183
157
|
|
184
158
|
# Writes a string variable to a file on a target node at a specified path.
|
@@ -187,35 +161,27 @@ module PuppetLitmus::PuppetHelpers
|
|
187
161
|
# @param destination [String] The path on the target node to write the file.
|
188
162
|
# @return [Bool] Success. The file was succesfully writtne on the target.
|
189
163
|
def write_file(content, destination, opts = {})
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
tmp_file.
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
else
|
208
|
-
# transfer to TARGET_HOST
|
209
|
-
add_node_fields_to_span(span, target_node_name, inventory_hash)
|
210
|
-
|
211
|
-
bolt_result = upload_file(tmp_file.path, destination, target_node_name, options: {}, config: nil, inventory: inventory_hash)
|
212
|
-
span.add_field('litmus.bolt_result.file_upload', bolt_result)
|
213
|
-
raise bolt_result.first['value'].to_s unless bolt_result.first['status'] == 'success'
|
214
|
-
end
|
164
|
+
require 'tmpdir'
|
165
|
+
inventory_hash = inventory_hash_from_inventory_file
|
166
|
+
target_node_name = ENV.fetch('TARGET_HOST', nil)
|
167
|
+
target_option = opts['targets'] || opts[:targets]
|
168
|
+
target_node_name = search_for_target(target_option, inventory_hash) unless target_option.nil?
|
169
|
+
|
170
|
+
Tempfile.create('litmus') do |tmp_file|
|
171
|
+
tmp_file.write(content)
|
172
|
+
tmp_file.flush
|
173
|
+
if target_node_name.nil? || target_node_name == 'localhost'
|
174
|
+
require 'fileutils'
|
175
|
+
# no need to transfer
|
176
|
+
FileUtils.cp(tmp_file.path, destination)
|
177
|
+
else
|
178
|
+
# transfer to TARGET_HOST
|
179
|
+
bolt_result = upload_file(tmp_file.path, destination, target_node_name, options: {}, config: nil, inventory: inventory_hash)
|
180
|
+
raise bolt_result.first['value'].to_s unless bolt_result.first['status'] == 'success'
|
215
181
|
end
|
216
|
-
|
217
|
-
true
|
218
182
|
end
|
183
|
+
|
184
|
+
true
|
219
185
|
end
|
220
186
|
|
221
187
|
# Runs a command against the target system
|
@@ -225,36 +191,26 @@ module PuppetLitmus::PuppetHelpers
|
|
225
191
|
# @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
|
226
192
|
# @return [Object] A result object from the command.
|
227
193
|
def run_shell(command_to_run, opts = {})
|
228
|
-
|
229
|
-
ENV['HONEYCOMB_TRACE'] = span.to_trace_header
|
230
|
-
span.add_field('litmus.command_to_run', command_to_run)
|
231
|
-
span.add_field('litmus.opts', opts)
|
232
|
-
|
233
|
-
inventory_hash = File.exist?('spec/fixtures/litmus_inventory.yaml') ? inventory_hash_from_inventory_file : localhost_inventory_hash
|
194
|
+
inventory_hash = File.exist?('spec/fixtures/litmus_inventory.yaml') ? inventory_hash_from_inventory_file : localhost_inventory_hash
|
234
195
|
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
add_node_fields_to_span(span, target_node_name, inventory_hash)
|
196
|
+
target_option = opts['targets'] || opts[:targets]
|
197
|
+
if target_option.nil?
|
198
|
+
target_node_name = targeting_localhost? ? 'litmus_localhost' : ENV.fetch('TARGET_HOST', nil)
|
199
|
+
raise "Target '#{target_node_name}' not found in spec/fixtures/litmus_inventory.yaml" unless target_in_inventory?(inventory_hash, target_node_name)
|
200
|
+
else
|
201
|
+
target_node_name = search_for_target(target_option, inventory_hash)
|
202
|
+
end
|
244
203
|
|
245
|
-
|
246
|
-
span.add_field('litmus.bolt_result', bolt_result)
|
204
|
+
bolt_result = run_command(command_to_run, target_node_name, config: nil, inventory: inventory_hash)
|
247
205
|
|
248
|
-
|
206
|
+
raise "shell failed\n`#{command_to_run}`\n======\n#{bolt_result}" if bolt_result.first['value']['exit_code'] != 0 && opts[:expect_failures] != true
|
249
207
|
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
result
|
257
|
-
end
|
208
|
+
result = OpenStruct.new(exit_code: bolt_result.first['value']['exit_code'],
|
209
|
+
exit_status: bolt_result.first['value']['exit_code'],
|
210
|
+
stdout: bolt_result.first['value']['stdout'],
|
211
|
+
stderr: bolt_result.first['value']['stderr'])
|
212
|
+
yield result if block_given?
|
213
|
+
result
|
258
214
|
end
|
259
215
|
|
260
216
|
# Copies file to the target, using its respective transport
|
@@ -265,51 +221,36 @@ module PuppetLitmus::PuppetHelpers
|
|
265
221
|
# @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
|
266
222
|
# @return [Object] A result object from the command.
|
267
223
|
def bolt_upload_file(source, destination, opts = {}, options = {})
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
target_option = opts['targets'] || opts[:targets]
|
277
|
-
if target_option.nil?
|
278
|
-
target_node_name = targeting_localhost? ? 'litmus_localhost' : ENV.fetch('TARGET_HOST', nil)
|
279
|
-
raise "Target '#{target_node_name}' not found in spec/fixtures/litmus_inventory.yaml" unless target_in_inventory?(inventory_hash, target_node_name)
|
280
|
-
else
|
281
|
-
target_node_name = search_for_target(target_option, inventory_hash)
|
282
|
-
end
|
283
|
-
|
284
|
-
add_node_fields_to_span(span, target_node_name, inventory_hash)
|
285
|
-
|
286
|
-
bolt_result = upload_file(source, destination, target_node_name, options: options, config: nil, inventory: inventory_hash)
|
287
|
-
span.add_field('litmus.bolt_result', bolt_result)
|
224
|
+
inventory_hash = File.exist?('spec/fixtures/litmus_inventory.yaml') ? inventory_hash_from_inventory_file : localhost_inventory_hash
|
225
|
+
target_option = opts['targets'] || opts[:targets]
|
226
|
+
if target_option.nil?
|
227
|
+
target_node_name = targeting_localhost? ? 'litmus_localhost' : ENV.fetch('TARGET_HOST', nil)
|
228
|
+
raise "Target '#{target_node_name}' not found in spec/fixtures/litmus_inventory.yaml" unless target_in_inventory?(inventory_hash, target_node_name)
|
229
|
+
else
|
230
|
+
target_node_name = search_for_target(target_option, inventory_hash)
|
231
|
+
end
|
288
232
|
|
289
|
-
|
290
|
-
exit_code: 0,
|
291
|
-
stdout: bolt_result.first['value']['_output'],
|
292
|
-
stderr: nil,
|
293
|
-
result: bolt_result.first['value']
|
294
|
-
}
|
233
|
+
bolt_result = upload_file(source, destination, target_node_name, options: options, config: nil, inventory: inventory_hash)
|
295
234
|
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
235
|
+
result_obj = {
|
236
|
+
exit_code: 0,
|
237
|
+
stdout: bolt_result.first['value']['_output'],
|
238
|
+
stderr: nil,
|
239
|
+
result: bolt_result.first['value']
|
240
|
+
}
|
301
241
|
|
302
|
-
|
303
|
-
|
304
|
-
end
|
242
|
+
if bolt_result.first['status'] != 'success'
|
243
|
+
raise "upload file failed\n======\n#{bolt_result}" if opts[:expect_failures] != true
|
305
244
|
|
306
|
-
|
307
|
-
|
308
|
-
stderr: result_obj[:stderr])
|
309
|
-
span.add_field('litmus.result', result.to_h)
|
310
|
-
yield result if block_given?
|
311
|
-
result
|
245
|
+
result_obj[:exit_code] = 255
|
246
|
+
result_obj[:stderr] = bolt_result.first['value']['_error']['msg']
|
312
247
|
end
|
248
|
+
|
249
|
+
result = OpenStruct.new(exit_code: result_obj[:exit_code],
|
250
|
+
stdout: result_obj[:stdout],
|
251
|
+
stderr: result_obj[:stderr])
|
252
|
+
yield result if block_given?
|
253
|
+
result
|
313
254
|
end
|
314
255
|
|
315
256
|
# Runs a task against the target system.
|
@@ -321,69 +262,56 @@ module PuppetLitmus::PuppetHelpers
|
|
321
262
|
# :inventory_file [String] path to the inventory file to use with the task.
|
322
263
|
# @return [Object] A result object from the task.The values available are stdout, stderr and result.
|
323
264
|
def run_bolt_task(task_name, params = {}, opts = {})
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
target_option = opts['targets'] || opts[:targets]
|
341
|
-
if target_option.nil?
|
342
|
-
raise "Target '#{target_node_name}' not found in spec/fixtures/litmus_inventory.yaml" unless target_in_inventory?(inventory_hash, target_node_name)
|
343
|
-
else
|
344
|
-
target_node_name = search_for_target(target_option, inventory_hash)
|
345
|
-
end
|
346
|
-
|
347
|
-
add_node_fields_to_span(span, target_node_name, inventory_hash)
|
348
|
-
|
349
|
-
bolt_result = run_task(task_name, target_node_name, params, config: config_data, inventory: inventory_hash)
|
350
|
-
result_obj = {
|
351
|
-
exit_code: 0,
|
352
|
-
stdout: nil,
|
353
|
-
stderr: nil,
|
354
|
-
result: bolt_result.first['value']
|
355
|
-
}
|
356
|
-
|
357
|
-
if bolt_result.first['status'] == 'success'
|
358
|
-
# stdout returns unstructured data if structured data is not available
|
359
|
-
result_obj[:stdout] = if bolt_result.first['value']['_output'].nil?
|
360
|
-
bolt_result.first['value'].to_s
|
361
|
-
else
|
362
|
-
bolt_result.first['value']['_output']
|
363
|
-
end
|
364
|
-
|
365
|
-
else
|
366
|
-
if opts[:expect_failures] != true
|
367
|
-
span.add_field('litmus_runtaskfailure', bolt_result)
|
368
|
-
raise "task failed\n`#{task_name}`\n======\n#{bolt_result}"
|
369
|
-
end
|
370
|
-
|
371
|
-
result_obj[:exit_code] = if bolt_result.first['value']['_error']['details'].nil?
|
372
|
-
255
|
373
|
-
else
|
374
|
-
bolt_result.first['value']['_error']['details'].fetch('exitcode', 255)
|
375
|
-
end
|
376
|
-
result_obj[:stderr] = bolt_result.first['value']['_error']['msg']
|
377
|
-
end
|
265
|
+
config_data = { 'modulepath' => File.join(Dir.pwd, 'spec', 'fixtures', 'modules') }
|
266
|
+
target_node_name = targeting_localhost? ? 'litmus_localhost' : ENV.fetch('TARGET_HOST', nil)
|
267
|
+
inventory_hash = if !opts[:inventory_file].nil? && File.exist?(opts[:inventory_file])
|
268
|
+
inventory_hash_from_inventory_file(opts[:inventory_file])
|
269
|
+
elsif File.exist?('spec/fixtures/litmus_inventory.yaml')
|
270
|
+
inventory_hash_from_inventory_file('spec/fixtures/litmus_inventory.yaml')
|
271
|
+
else
|
272
|
+
localhost_inventory_hash
|
273
|
+
end
|
274
|
+
|
275
|
+
target_option = opts['targets'] || opts[:targets]
|
276
|
+
if target_option.nil?
|
277
|
+
raise "Target '#{target_node_name}' not found in spec/fixtures/litmus_inventory.yaml" unless target_in_inventory?(inventory_hash, target_node_name)
|
278
|
+
else
|
279
|
+
target_node_name = search_for_target(target_option, inventory_hash)
|
280
|
+
end
|
378
281
|
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
282
|
+
bolt_result = run_task(task_name, target_node_name, params, config: config_data, inventory: inventory_hash)
|
283
|
+
result_obj = {
|
284
|
+
exit_code: 0,
|
285
|
+
stdout: nil,
|
286
|
+
stderr: nil,
|
287
|
+
result: bolt_result.first['value']
|
288
|
+
}
|
289
|
+
|
290
|
+
if bolt_result.first['status'] == 'success'
|
291
|
+
# stdout returns unstructured data if structured data is not available
|
292
|
+
result_obj[:stdout] = if bolt_result.first['value']['_output'].nil?
|
293
|
+
bolt_result.first['value'].to_s
|
294
|
+
else
|
295
|
+
bolt_result.first['value']['_output']
|
296
|
+
end
|
297
|
+
|
298
|
+
else
|
299
|
+
raise "task failed\n`#{task_name}`\n======\n#{bolt_result}" if opts[:expect_failures] != true
|
300
|
+
|
301
|
+
result_obj[:exit_code] = if bolt_result.first['value']['_error']['details'].nil?
|
302
|
+
255
|
303
|
+
else
|
304
|
+
bolt_result.first['value']['_error']['details'].fetch('exitcode', 255)
|
305
|
+
end
|
306
|
+
result_obj[:stderr] = bolt_result.first['value']['_error']['msg']
|
386
307
|
end
|
308
|
+
|
309
|
+
result = OpenStruct.new(exit_code: result_obj[:exit_code],
|
310
|
+
stdout: result_obj[:stdout],
|
311
|
+
stderr: result_obj[:stderr],
|
312
|
+
result: result_obj[:result])
|
313
|
+
yield result if block_given?
|
314
|
+
result
|
387
315
|
end
|
388
316
|
|
389
317
|
# Runs a script against the target system.
|
@@ -394,37 +322,24 @@ module PuppetLitmus::PuppetHelpers
|
|
394
322
|
# @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
|
395
323
|
# @return [Object] A result object from the script run.
|
396
324
|
def bolt_run_script(script, opts = {}, arguments: [])
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
target_node_name =
|
404
|
-
|
405
|
-
target_option = opts['targets'] || opts[:targets]
|
406
|
-
if target_option.nil?
|
407
|
-
raise "Target '#{target_node_name}' not found in spec/fixtures/litmus_inventory.yaml" unless target_in_inventory?(inventory_hash, target_node_name)
|
408
|
-
else
|
409
|
-
target_node_name = search_for_target(target_option, inventory_hash)
|
410
|
-
end
|
325
|
+
target_node_name = targeting_localhost? ? 'litmus_localhost' : ENV.fetch('TARGET_HOST', nil)
|
326
|
+
inventory_hash = File.exist?('spec/fixtures/litmus_inventory.yaml') ? inventory_hash_from_inventory_file : localhost_inventory_hash
|
327
|
+
target_option = opts['targets'] || opts[:targets]
|
328
|
+
if target_option.nil?
|
329
|
+
raise "Target '#{target_node_name}' not found in spec/fixtures/litmus_inventory.yaml" unless target_in_inventory?(inventory_hash, target_node_name)
|
330
|
+
else
|
331
|
+
target_node_name = search_for_target(target_option, inventory_hash)
|
332
|
+
end
|
411
333
|
|
412
|
-
|
334
|
+
bolt_result = run_script(script, target_node_name, arguments, options: opts, config: nil, inventory: inventory_hash)
|
413
335
|
|
414
|
-
|
336
|
+
raise "script run failed\n`#{script}`\n======\n#{bolt_result}" if bolt_result.first['value']['exit_code'] != 0 && opts[:expect_failures] != true
|
415
337
|
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
result = OpenStruct.new(exit_code: bolt_result.first['value']['exit_code'],
|
422
|
-
stdout: bolt_result.first['value']['stdout'],
|
423
|
-
stderr: bolt_result.first['value']['stderr'])
|
424
|
-
yield result if block_given?
|
425
|
-
span.add_field('litmus.result', result.to_h)
|
426
|
-
result
|
427
|
-
end
|
338
|
+
result = OpenStruct.new(exit_code: bolt_result.first['value']['exit_code'],
|
339
|
+
stdout: bolt_result.first['value']['stdout'],
|
340
|
+
stderr: bolt_result.first['value']['stderr'])
|
341
|
+
yield result if block_given?
|
342
|
+
result
|
428
343
|
end
|
429
344
|
|
430
345
|
# Determines if the current execution is targeting localhost or not
|