puppet_litmus 0.7.1 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,203 +1,210 @@
1
- # frozen_string_literal: true
2
-
3
- # helper functions for running puppet commands. They execute a target system specified by ENV['TARGET_HOST']
4
- # heavily uses functions from here https://github.com/puppetlabs/bolt/blob/master/developer-docs/bolt_spec-run.md
5
- module PuppetLitmus::Serverspec
6
- # Applies a manifest twice. First checking for errors. Secondly to make sure no changes occur.
7
- #
8
- # @param manifest [String] puppet manifest code to be applied.
9
- # @return [Boolean] The result of the 2 apply manifests.
10
- def idempotent_apply(manifest)
11
- manifest_file_location = create_manifest_file(manifest)
12
- apply_manifest(nil, catch_failures: true, manifest_file_location: manifest_file_location)
13
- apply_manifest(nil, catch_changes: true, manifest_file_location: manifest_file_location)
14
- end
15
-
16
- # rubocop:disable Layout/TrailingWhitespace
17
-
18
- # Applies a manifest. returning the result of that apply. Mimics the apply_manifest from beaker
19
- #
20
- # @param manifest [String] puppet manifest code to be applied.
21
- # @param opts [Hash] Alters the behaviour of the command. Valid options are:
22
- # :catch_changes [Boolean] exit status of 1 if there were changes.
23
- # :expect_failures [Boolean] doesnt return an exit code of non-zero if the apply failed.
24
- # :manifest_file_location [Path] The place on the target system.
25
- # :prefix_command [String] prefixes the puppet apply command; eg "export LANGUAGE='ja'".
26
- # :debug [Boolean] run puppet apply with the debug flag.
27
- # :noop [Boolean] run puppet apply with the noop flag.
28
- # @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
29
- # @return [Object] A result object from the apply.
30
- def apply_manifest(manifest, opts = {})
31
- # rubocop:enable Layout/TrailingWhitespace
32
- target_node_name = ENV['TARGET_HOST']
33
- raise 'manifest and manifest_file_location in the opts hash are mutually exclusive arguments, pick one' if !manifest.nil? && !opts[:manifest_file_location].nil?
34
- raise 'please pass a manifest or the manifest_file_location in the opts hash' if (manifest.nil? || manifest == '') && opts[:manifest_file_location].nil?
35
-
36
- manifest_file_location = opts[:manifest_file_location] || create_manifest_file(manifest)
37
- inventory_hash = if target_node_name.nil? || target_node_name == 'localhost'
38
- nil
39
- else
40
- inventory_hash_from_inventory_file
41
- end
42
- command_to_run = "#{opts[:prefix_command]} puppet apply #{manifest_file_location}"
43
- command_to_run += " --modulepath #{Dir.pwd}/spec/fixtures/modules" if target_node_name.nil? || target_node_name == 'localhost'
44
- command_to_run += ' --detailed-exitcodes' if !opts[:catch_changes].nil? && (opts[:catch_changes] == true)
45
- command_to_run += ' --debug' if !opts[:debug].nil? && (opts[:debug] == true)
46
- command_to_run += ' --noop' if !opts[:noop].nil? && (opts[:noop] == true)
47
- result = run_command(command_to_run, target_node_name, config: nil, inventory: inventory_hash)
48
-
49
- raise "apply manifest failed\n`#{command_to_run}`\n======\n#{result}" if result.first['result']['exit_code'] != 0 && opts[:expect_failures] != true
50
-
51
- result = OpenStruct.new(exit_code: result.first['result']['exit_code'],
52
- stdout: result.first['result']['stdout'],
53
- stderr: result.first['result']['stderr'])
54
- yield result if block_given?
55
- result
56
- end
57
-
58
- # Creates a manifest file locally in a temp location, if its a remote target copy it to there.
59
- #
60
- # @param manifest [String] puppet manifest code.
61
- # @return [String] The path to the location of the manifest.
62
- def create_manifest_file(manifest)
63
- require 'tmpdir'
64
- target_node_name = ENV['TARGET_HOST']
65
- tmp_filename = File.join(Dir.tmpdir, "manifest_#{Time.now.strftime('%Y%m%d')}_#{Process.pid}_#{rand(0x100000000).to_s(36)}.pp")
66
- manifest_file = File.open(tmp_filename, 'w')
67
- manifest_file.write(manifest)
68
- manifest_file.close
69
- if target_node_name.nil? || target_node_name == 'localhost'
70
- # no need to transfer
71
- manifest_file_location = manifest_file.path
72
- else
73
- # transfer to TARGET_HOST
74
- inventory_hash = inventory_hash_from_inventory_file
75
- manifest_file_location = "/tmp/#{File.basename(manifest_file)}"
76
- result = upload_file(manifest_file.path, manifest_file_location, target_node_name, options: {}, config: nil, inventory: inventory_hash)
77
- raise result.first['result'].to_s unless result.first['status'] == 'success'
78
- end
79
- manifest_file_location
80
- end
81
-
82
- # Runs a command against the target system
83
- #
84
- # @param command_to_run [String] The command to execute.
85
- # @param opts [Hash] Alters the behaviour of the command. Valid options are :expect_failures [Boolean] doesnt return an exit code of non-zero if the command failed.
86
- # @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
87
- # @return [Object] A result object from the command.
88
- def run_shell(command_to_run, opts = {})
89
- target_node_name = ENV['TARGET_HOST']
90
- inventory_hash = if target_node_name.nil? || target_node_name == 'localhost'
91
- nil
92
- else
93
- inventory_hash_from_inventory_file
94
- end
95
- result = run_command(command_to_run, target_node_name, config: nil, inventory: inventory_hash)
96
- raise "shell failed\n`#{command_to_run}`\n======\n#{result}" if result.first['result']['exit_code'] != 0 && opts[:expect_failures] != true
97
-
98
- result = OpenStruct.new(exit_code: result.first['result']['exit_code'],
99
- stdout: result.first['result']['stdout'],
100
- stderr: result.first['result']['stderr'])
101
- yield result if block_given?
102
- result
103
- end
104
-
105
- # Copies file to the target, using its respective transport
106
- #
107
- # @param source [String] place locally, to copy from.
108
- # @param destination [String] place on the target, to copy to.
109
- # @param opts [Hash] Alters the behaviour of the command. Valid options are :expect_failures [Boolean] doesnt return an exit code of non-zero if the command failed.
110
- # @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
111
- # @return [Object] A result object from the command.
112
- def bolt_upload_file(source, destination, opts = {}, options = {})
113
- target_node_name = ENV['TARGET_HOST'] if target_node_name.nil?
114
- inventory_hash = if target_node_name.nil? || target_node_name == 'localhost'
115
- nil
116
- else
117
- inventory_hash_from_inventory_file
118
- end
119
-
120
- result = upload_file(source, destination, target_node_name, options: options, config: nil, inventory: inventory_hash)
121
-
122
- result_obj = {
123
- exit_code: 0,
124
- stdout: result.first['result']['_output'],
125
- stderr: nil,
126
- result: result.first['result'],
127
- }
128
-
129
- if result.first['status'] != 'success'
130
- raise "upload file failed\n======\n#{result}" if opts[:expect_failures] != true
131
-
132
- result_obj[:exit_code] = 255
133
- result_obj[:stderr] = result.first['result']['_error']['msg']
134
- end
135
-
136
- result = OpenStruct.new(exit_code: result_obj[:exit_code],
137
- stdout: result_obj[:stdout],
138
- stderr: result_obj[:stderr])
139
- yield result if block_given?
140
- result
141
- end
142
-
143
- # Runs a task against the target system.
144
- #
145
- # @param task_name [String] The name of the task to run.
146
- # @param params [Hash] key : value pairs to be passed to the task.
147
- # @param opts [Hash] Alters the behaviour of the command. Valid options are :expect_failures [Boolean] doesnt return an exit code of non-zero if the command failed.
148
- # @return [Object] A result object from the task.The values available are stdout, stderr and result.
149
- def run_bolt_task(task_name, params = {}, opts = {})
150
- config_data = { 'modulepath' => File.join(Dir.pwd, 'spec', 'fixtures', 'modules') }
151
- inventory_hash = inventory_hash_from_inventory_file
152
- target_node_name = ENV['TARGET_HOST'] if target_node_name.nil?
153
-
154
- result = run_task(task_name, target_node_name, params, config: config_data, inventory: inventory_hash)
155
-
156
- result_obj = {
157
- exit_code: 0,
158
- stdout: result.first['result']['_output'],
159
- stderr: nil,
160
- result: result.first['result'],
161
- }
162
-
163
- if result.first['status'] != 'success'
164
- raise "task failed\n`#{task_name}`\n======\n#{result}" if opts[:expect_failures] != true
165
-
166
- result_obj[:exit_code] = result.first['result']['_error']['details'].fetch('exitcode', 255)
167
- result_obj[:stderr] = result.first['result']['_error']['msg']
168
- end
169
-
170
- result = OpenStruct.new(exit_code: result_obj[:exit_code],
171
- stdout: result_obj[:stdout],
172
- stderr: result_obj[:stderr],
173
- result: result_obj[:result])
174
- yield result if block_given?
175
- result
176
- end
177
-
178
- # Runs a script against the target system.
179
- #
180
- # @param script [String] The path to the script on the source machine
181
- # @param opts [Hash] Alters the behaviour of the command. Valid options are :expect_failures [Boolean] doesnt return an exit code of non-zero if the command failed.
182
- # @param arguments [Array] Array of arguments to pass to script on runtime
183
- # @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
184
- # @return [Object] A result object from the script run.
185
- def bolt_run_script(script, opts = {}, arguments: [])
186
- target_node_name = ENV['TARGET_HOST'] if target_node_name.nil?
187
- inventory_hash = if target_node_name.nil? || target_node_name == 'localhost'
188
- nil
189
- else
190
- inventory_hash_from_inventory_file
191
- end
192
-
193
- result = run_script(script, target_node_name, arguments, options: opts, config: nil, inventory: inventory_hash)
194
-
195
- raise "script run failed\n`#{script}`\n======\n#{result}" if result.first['result']['exit_code'] != 0 && opts[:expect_failures] != true
196
-
197
- result = OpenStruct.new(exit_code: result.first['result']['exit_code'],
198
- stdout: result.first['result']['stdout'],
199
- stderr: result.first['result']['stderr'])
200
- yield result if block_given?
201
- result
202
- end
203
- end
1
+ # frozen_string_literal: true
2
+
3
+ # helper functions for running puppet commands. They execute a target system specified by ENV['TARGET_HOST']
4
+ # heavily uses functions from here https://github.com/puppetlabs/bolt/blob/master/developer-docs/bolt_spec-run.md
5
+ module PuppetLitmus::Serverspec
6
+ # Applies a manifest twice. First checking for errors. Secondly to make sure no changes occur.
7
+ #
8
+ # @param manifest [String] puppet manifest code to be applied.
9
+ # @return [Boolean] The result of the 2 apply manifests.
10
+ def idempotent_apply(manifest)
11
+ manifest_file_location = create_manifest_file(manifest)
12
+ apply_manifest(nil, catch_failures: true, manifest_file_location: manifest_file_location)
13
+ apply_manifest(nil, catch_changes: true, manifest_file_location: manifest_file_location)
14
+ end
15
+
16
+ # rubocop:disable Layout/TrailingWhitespace
17
+
18
+ # Applies a manifest. returning the result of that apply. Mimics the apply_manifest from beaker
19
+ #
20
+ # @param manifest [String] puppet manifest code to be applied.
21
+ # @param opts [Hash] Alters the behaviour of the command. Valid options are:
22
+ # :catch_changes [Boolean] exit status of 1 if there were changes.
23
+ # :expect_failures [Boolean] doesnt return an exit code of non-zero if the apply failed.
24
+ # :manifest_file_location [Path] The place on the target system.
25
+ # :prefix_command [String] prefixes the puppet apply command; eg "export LANGUAGE='ja'".
26
+ # :debug [Boolean] run puppet apply with the debug flag.
27
+ # :noop [Boolean] run puppet apply with the noop flag.
28
+ # @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
29
+ # @return [Object] A result object from the apply.
30
+ def apply_manifest(manifest, opts = {})
31
+ # rubocop:enable Layout/TrailingWhitespace
32
+ target_node_name = ENV['TARGET_HOST']
33
+ raise 'manifest and manifest_file_location in the opts hash are mutually exclusive arguments, pick one' if !manifest.nil? && !opts[:manifest_file_location].nil?
34
+ raise 'please pass a manifest or the manifest_file_location in the opts hash' if (manifest.nil? || manifest == '') && opts[:manifest_file_location].nil?
35
+
36
+ manifest_file_location = opts[:manifest_file_location] || create_manifest_file(manifest)
37
+ inventory_hash = if target_node_name.nil? || target_node_name == 'localhost'
38
+ nil
39
+ else
40
+ inventory_hash_from_inventory_file
41
+ end
42
+ command_to_run = "#{opts[:prefix_command]} puppet apply #{manifest_file_location}"
43
+ command_to_run += " --modulepath #{Dir.pwd}/spec/fixtures/modules" if target_node_name.nil? || target_node_name == 'localhost'
44
+ command_to_run += ' --detailed-exitcodes' if !opts[:catch_changes].nil? && (opts[:catch_changes] == true)
45
+ command_to_run += ' --debug' if !opts[:debug].nil? && (opts[:debug] == true)
46
+ command_to_run += ' --noop' if !opts[:noop].nil? && (opts[:noop] == true)
47
+ result = run_command(command_to_run, target_node_name, config: nil, inventory: inventory_hash)
48
+
49
+ raise "apply manifest failed\n`#{command_to_run}`\n======\n#{result}" if result.first['result']['exit_code'] != 0 && opts[:expect_failures] != true
50
+
51
+ result = OpenStruct.new(exit_code: result.first['result']['exit_code'],
52
+ stdout: result.first['result']['stdout'],
53
+ stderr: result.first['result']['stderr'])
54
+ yield result if block_given?
55
+ result
56
+ end
57
+
58
+ # Creates a manifest file locally in a temp location, if its a remote target copy it to there.
59
+ #
60
+ # @param manifest [String] puppet manifest code.
61
+ # @return [String] The path to the location of the manifest.
62
+ def create_manifest_file(manifest)
63
+ require 'tmpdir'
64
+ target_node_name = ENV['TARGET_HOST']
65
+ tmp_filename = File.join(Dir.tmpdir, "manifest_#{Time.now.strftime('%Y%m%d')}_#{Process.pid}_#{rand(0x100000000).to_s(36)}.pp")
66
+ manifest_file = File.open(tmp_filename, 'w')
67
+ manifest_file.write(manifest)
68
+ manifest_file.close
69
+ if target_node_name.nil? || target_node_name == 'localhost'
70
+ # no need to transfer
71
+ manifest_file_location = manifest_file.path
72
+ else
73
+ # transfer to TARGET_HOST
74
+ inventory_hash = inventory_hash_from_inventory_file
75
+ manifest_file_location = "/tmp/#{File.basename(manifest_file)}"
76
+ result = upload_file(manifest_file.path, manifest_file_location, target_node_name, options: {}, config: nil, inventory: inventory_hash)
77
+ raise result.first['result'].to_s unless result.first['status'] == 'success'
78
+ end
79
+ manifest_file_location
80
+ end
81
+
82
+ # Runs a command against the target system
83
+ #
84
+ # @param command_to_run [String] The command to execute.
85
+ # @param opts [Hash] Alters the behaviour of the command. Valid options are :expect_failures [Boolean] doesnt return an exit code of non-zero if the command failed.
86
+ # @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
87
+ # @return [Object] A result object from the command.
88
+ def run_shell(command_to_run, opts = {})
89
+ target_node_name = ENV['TARGET_HOST']
90
+ inventory_hash = if target_node_name.nil? || target_node_name == 'localhost'
91
+ nil
92
+ else
93
+ inventory_hash_from_inventory_file
94
+ end
95
+ result = run_command(command_to_run, target_node_name, config: nil, inventory: inventory_hash)
96
+ raise "shell failed\n`#{command_to_run}`\n======\n#{result}" if result.first['result']['exit_code'] != 0 && opts[:expect_failures] != true
97
+
98
+ result = OpenStruct.new(exit_code: result.first['result']['exit_code'],
99
+ stdout: result.first['result']['stdout'],
100
+ stderr: result.first['result']['stderr'])
101
+ yield result if block_given?
102
+ result
103
+ end
104
+
105
+ # Copies file to the target, using its respective transport
106
+ #
107
+ # @param source [String] place locally, to copy from.
108
+ # @param destination [String] place on the target, to copy to.
109
+ # @param opts [Hash] Alters the behaviour of the command. Valid options are :expect_failures [Boolean] doesnt return an exit code of non-zero if the command failed.
110
+ # @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
111
+ # @return [Object] A result object from the command.
112
+ def bolt_upload_file(source, destination, opts = {}, options = {})
113
+ target_node_name = ENV['TARGET_HOST'] if target_node_name.nil?
114
+ inventory_hash = if target_node_name.nil? || target_node_name == 'localhost'
115
+ nil
116
+ else
117
+ inventory_hash_from_inventory_file
118
+ end
119
+
120
+ result = upload_file(source, destination, target_node_name, options: options, config: nil, inventory: inventory_hash)
121
+
122
+ result_obj = {
123
+ exit_code: 0,
124
+ stdout: result.first['result']['_output'],
125
+ stderr: nil,
126
+ result: result.first['result'],
127
+ }
128
+
129
+ if result.first['status'] != 'success'
130
+ raise "upload file failed\n======\n#{result}" if opts[:expect_failures] != true
131
+
132
+ result_obj[:exit_code] = 255
133
+ result_obj[:stderr] = result.first['result']['_error']['msg']
134
+ end
135
+
136
+ result = OpenStruct.new(exit_code: result_obj[:exit_code],
137
+ stdout: result_obj[:stdout],
138
+ stderr: result_obj[:stderr])
139
+ yield result if block_given?
140
+ result
141
+ end
142
+
143
+ # Runs a task against the target system.
144
+ #
145
+ # @param task_name [String] The name of the task to run.
146
+ # @param params [Hash] key : value pairs to be passed to the task.
147
+ # @param opts [Hash] Alters the behaviour of the command. Valid options are :expect_failures [Boolean] doesnt return an exit code of non-zero if the command failed.
148
+ # @return [Object] A result object from the task.The values available are stdout, stderr and result.
149
+ def run_bolt_task(task_name, params = {}, opts = {})
150
+ config_data = { 'modulepath' => File.join(Dir.pwd, 'spec', 'fixtures', 'modules') }
151
+ inventory_hash = inventory_hash_from_inventory_file
152
+ target_node_name = ENV['TARGET_HOST'] if target_node_name.nil?
153
+
154
+ result = run_task(task_name, target_node_name, params, config: config_data, inventory: inventory_hash)
155
+ result_obj = {
156
+ exit_code: 0,
157
+ stdout: nil,
158
+ stderr: nil,
159
+ result: result.first['result'],
160
+ }
161
+
162
+ if result.first['status'] == 'success'
163
+ # stdout returns unstructured data if structured data is not available
164
+ result_obj[:stdout] = if result.first['result']['_output'].nil?
165
+ result.first['result'].to_s
166
+ else
167
+ result.first['result']['_output']
168
+ end
169
+
170
+ else
171
+ raise "task failed\n`#{task_name}`\n======\n#{result}" if opts[:expect_failures] != true
172
+
173
+ result_obj[:exit_code] = result.first['result']['_error']['details'].fetch('exitcode', 255)
174
+ result_obj[:stderr] = result.first['result']['_error']['msg']
175
+ end
176
+
177
+ result = OpenStruct.new(exit_code: result_obj[:exit_code],
178
+ stdout: result_obj[:stdout],
179
+ stderr: result_obj[:stderr],
180
+ result: result_obj[:result])
181
+ yield result if block_given?
182
+ result
183
+ end
184
+
185
+ # Runs a script against the target system.
186
+ #
187
+ # @param script [String] The path to the script on the source machine
188
+ # @param opts [Hash] Alters the behaviour of the command. Valid options are :expect_failures [Boolean] doesnt return an exit code of non-zero if the command failed.
189
+ # @param arguments [Array] Array of arguments to pass to script on runtime
190
+ # @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
191
+ # @return [Object] A result object from the script run.
192
+ def bolt_run_script(script, opts = {}, arguments: [])
193
+ target_node_name = ENV['TARGET_HOST'] if target_node_name.nil?
194
+ inventory_hash = if target_node_name.nil? || target_node_name == 'localhost'
195
+ nil
196
+ else
197
+ inventory_hash_from_inventory_file
198
+ end
199
+
200
+ result = run_script(script, target_node_name, arguments, options: opts, config: nil, inventory: inventory_hash)
201
+
202
+ raise "script run failed\n`#{script}`\n======\n#{result}" if result.first['result']['exit_code'] != 0 && opts[:expect_failures] != true
203
+
204
+ result = OpenStruct.new(exit_code: result.first['result']['exit_code'],
205
+ stdout: result.first['result']['stdout'],
206
+ stderr: result.first['result']['stderr'])
207
+ yield result if block_given?
208
+ result
209
+ end
210
+ end
@@ -1,6 +1,6 @@
1
- # frozen_string_literal: true
2
-
3
- # version of this gem
4
- module PuppetLitmus
5
- VERSION ||= '0.7.1'
6
- end
1
+ # frozen_string_literal: true
2
+
3
+ # version of this gem
4
+ module PuppetLitmus
5
+ VERSION ||= '0.7.2'
6
+ end
@@ -1,16 +1,16 @@
1
- ---
2
- groups:
3
- - name: ssh_nodes
4
- nodes:
5
- - name: test.delivery.puppetlabs.net
6
- config:
7
- transport: ssh
8
- ssh:
9
- user: root
10
- password: Qu@lity!
11
- host-key-check: false
12
- facts:
13
- provisioner: vmpooler
14
- platform: centos-5-x86_64
15
- - name: winrm_nodes
16
- nodes: []
1
+ ---
2
+ groups:
3
+ - name: ssh_nodes
4
+ nodes:
5
+ - name: test.delivery.puppetlabs.net
6
+ config:
7
+ transport: ssh
8
+ ssh:
9
+ user: root
10
+ password: Qu@lity!
11
+ host-key-check: false
12
+ facts:
13
+ provisioner: vmpooler
14
+ platform: centos-5-x86_64
15
+ - name: winrm_nodes
16
+ nodes: []