kitchen-puppet 0.0.14 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,13 +1,19 @@
1
1
  # kitchen-puppet
2
2
  A Test Kitchen Provisioner for Puppet
3
3
 
4
- The provider works by passing the puppet repository based on attributes in .kitchen.yml & calling puppet apply.
4
+ The providers supports both puppet apply and puppet agent clients
5
+
6
+ The PuppetApply provider works by passing the puppet repository based on attributes in .kitchen.yml & calling puppet apply.
7
+
8
+ The PuppetAgent provider works by passing the puppetmaster and other attributes in .kitchen.yml & calling puppet agent.
9
+
5
10
 
6
11
  This provider has been tested against the Ubuntu 1204 and Centos 6.5 boxes running in vagrant/virtualbox.
7
12
 
8
13
  ## Requirements
9
14
  You'll need a driver box without a chef installation so puppet can be installed. Puppet have one at http://puppet-vagrant-boxes.puppetlabs.com/ubuntu-server-12042-x64-vbox4210-nocm.box or http://puppet-vagrant-boxes.puppetlabs.com/centos-65-x64-virtualbox-nocm.box.
10
15
 
16
+ For PuppetAgent a server with a puppet master is required that can resolve the hostname ip address of the server. The server must also be able to resolve the hostname ip address of the puppet master.
11
17
 
12
18
  ## Installation & Setup
13
19
  You'll need the test-kitchen & kitchen-puppet gem's installed in your system, along with kitchen-vagrant or some ther suitable driver for test-kitchen.
@@ -1,5 +1,5 @@
1
1
  module Kitchen
2
2
  module Puppet
3
- VERSION = "0.0.14"
3
+ VERSION = "0.0.15"
4
4
  end
5
5
  end
@@ -0,0 +1,359 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # Author:: Chris Lundquist (<chris.lundquist@github.com>) Neill Turner (<neillwturner@gmail.com>)
4
+ #
5
+ # Copyright (C) 2013,2014 Chris Lundquist, Neill Turner
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+ # See https://github.com/neillturner/kitchen-puppet/blob/master/provisioner_options.md
20
+ # for documentation configuration parameters with puppet_agent provisioner.
21
+ #
22
+
23
+ require 'json'
24
+ require 'kitchen/provisioner/base'
25
+ require 'kitchen/provisioner/puppet/librarian'
26
+
27
+ module Kitchen
28
+
29
+ class Busser
30
+
31
+ def non_suite_dirs
32
+ %w{data data_bags environments nodes roles puppet}
33
+ end
34
+ end
35
+
36
+ module Provisioner
37
+ #
38
+ # Puppet Agent provisioner.
39
+ #
40
+ class PuppetAgent < Base
41
+ attr_accessor :tmp_dir
42
+
43
+ default_config :require_puppet_omnibus, false
44
+ # TODO use something like https://github.com/fnichol/omnibus-puppet
45
+ default_config :puppet_omnibus_url, nil
46
+ default_config :puppet_omnibus_remote_path, '/opt/puppet'
47
+ default_config :puppet_version, nil
48
+ default_config :require_puppet_repo, true
49
+ default_config :require_chef_for_busser, true
50
+
51
+ default_config :puppet_apt_repo, "http://apt.puppetlabs.com/puppetlabs-release-precise.deb"
52
+ default_config :puppet_yum_repo, "https://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm"
53
+ default_config :chef_bootstrap_url, "https://www.getchef.com/chef/install.sh"
54
+
55
+ default_config :puppet_agent_command, nil
56
+
57
+ default_config :puppet_config_path do |provisioner|
58
+ provisioner.calculate_path('puppet.conf', :file)
59
+ end
60
+
61
+ default_config :puppet_debug, false
62
+ default_config :puppet_verbose, false
63
+ default_config :puppet_noop, false
64
+ default_config :puppet_platform, ''
65
+ default_config :update_package_repos, true
66
+
67
+ default_config :custom_facts, {}
68
+
69
+ default_config :puppet_detailed_exitcodes, nil
70
+ default_config :puppet_logdest, nil
71
+ default_config :puppet_masterport, nil
72
+ default_config :puppet_test, false
73
+ default_config :puppet_onetime, true
74
+ default_config :puppet_no_daemonize, true
75
+ default_config :puppet_server, nil # will default to 'puppet'
76
+ default_config :puppet_waitforcert, '0'
77
+ default_config :puppet_certname, nil
78
+ default_config :puppet_digest, nil
79
+
80
+
81
+ def calculate_path(path, type = :directory)
82
+ base = config[:test_base_path]
83
+ candidates = []
84
+ candidates << File.join(base, instance.suite.name, 'puppet', path)
85
+ candidates << File.join(base, instance.suite.name, path)
86
+ candidates << File.join(base, path)
87
+ candidates << File.join(Dir.pwd, path)
88
+
89
+ candidates.find do |c|
90
+ type == :directory ? File.directory?(c) : File.file?(c)
91
+ end
92
+ end
93
+
94
+ def install_command
95
+ return unless config[:require_puppet_omnibus] or config[:require_puppet_repo]
96
+ if config[:require_puppet_omnibus]
97
+ info("Installing puppet using puppet omnibus")
98
+ version = if !config[:puppet_version].nil?
99
+ "-v #{config[:puppet_version]}"
100
+ else
101
+ ""
102
+ end
103
+ <<-INSTALL
104
+ #{Util.shell_helpers}
105
+
106
+ if [ ! -d "#{config[:puppet_omnibus_remote_path]}" ]; then
107
+ echo "-----> Installing Puppet Omnibus"
108
+ do_download #{config[:puppet_omnibus_url]} /tmp/puppet_install.sh
109
+ #{sudo('sh')} /tmp/puppet_install.sh #{version}
110
+ fi
111
+ #{install_busser}
112
+ INSTALL
113
+ else
114
+ case puppet_platform
115
+ when "debian", "ubuntu"
116
+ info("Installing puppet on #{puppet_platform}")
117
+ <<-INSTALL
118
+ if [ ! $(which puppet) ]; then
119
+ #{sudo('wget')} #{puppet_apt_repo}
120
+ #{sudo('dpkg')} -i #{puppet_apt_repo_file}
121
+ #{update_packages_debian_cmd}
122
+ #{sudo('apt-get')} -y install puppet-common#{puppet_debian_version}
123
+ #{sudo('apt-get')} -y install puppet#{puppet_debian_version}
124
+ fi
125
+ #{install_busser}
126
+ INSTALL
127
+ when "redhat", "centos", "fedora"
128
+ info("Installing puppet on #{puppet_platform}")
129
+ <<-INSTALL
130
+ if [ ! $(which puppet) ]; then
131
+ #{sudo('rpm')} -ivh #{puppet_yum_repo}
132
+ #{update_packages_redhat_cmd}
133
+ #{sudo('yum')} -y install puppet#{puppet_redhat_version}
134
+ fi
135
+ #{install_busser}
136
+ INSTALL
137
+ else
138
+ info("Installing puppet, will try to determine platform os")
139
+ <<-INSTALL
140
+ if [ ! $(which puppet) ]; then
141
+ if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ]; then
142
+ #{sudo('rpm')} -ivh #{puppet_yum_repo}
143
+ #{update_packages_redhat_cmd}
144
+ #{sudo('yum')} -y install puppet#{puppet_redhat_version}
145
+ else
146
+ #{sudo('wget')} #{puppet_apt_repo}
147
+ #{sudo('dpkg')} -i #{puppet_apt_repo_file}
148
+ #{update_packages_debian_cmd}
149
+ #{sudo('apt-get')} -y install puppet-common#{puppet_debian_version}
150
+ #{sudo('apt-get')} -y install puppet#{puppet_debian_version}
151
+ fi
152
+ fi
153
+ #{install_busser}
154
+ INSTALL
155
+ end
156
+ end
157
+ end
158
+
159
+ def install_busser
160
+ if config[:require_chef_for_busser]
161
+ <<-INSTALL
162
+ #{Util.shell_helpers}
163
+ # install chef omnibus so that busser works as this is needed to run tests :(
164
+ # TODO: work out how to install enough ruby
165
+ # and set busser: { :ruby_bindir => '/usr/bin/ruby' } so that we dont need the
166
+ # whole chef client
167
+ if [ ! -d "/opt/chef" ]
168
+ then
169
+ echo "-----> Installing Chef Omnibus to install busser to run tests"
170
+ do_download #{chef_url} /tmp/install.sh
171
+ #{sudo('sh')} /tmp/install.sh
172
+ fi
173
+ INSTALL
174
+ end
175
+ end
176
+
177
+
178
+
179
+ def init_command
180
+ end
181
+
182
+ def create_sandbox
183
+ super
184
+ debug("Creating local sandbox in #{sandbox_path}")
185
+
186
+ yield if block_given?
187
+
188
+ prepare_puppet_config
189
+ info('Finished Preparing files for transfer')
190
+
191
+ end
192
+
193
+ def cleanup_sandbox
194
+ return if sandbox_path.nil?
195
+ debug("Cleaning up local sandbox in #{sandbox_path}")
196
+ FileUtils.rmtree(sandbox_path)
197
+ end
198
+
199
+ def prepare_command
200
+ commands = []
201
+
202
+ if puppet_config
203
+ commands << [
204
+ sudo('cp'),
205
+ File.join(config[:root_path], 'puppet.conf'),
206
+ '/etc/puppet',
207
+ ].join(' ')
208
+ end
209
+
210
+ command = commands.join(' && ')
211
+ debug(command)
212
+ command
213
+ end
214
+
215
+ def run_command
216
+ if !config[:puppet_agent_command].nil?
217
+ return config[:puppet_agent_command]
218
+ else
219
+ [
220
+ custom_facts,
221
+ sudo('puppet'),
222
+ 'agent',
223
+ puppet_server_flag,
224
+ "--waitforcert=#{config[:puppet_waitforcert]}",
225
+ puppet_masterport_flag,
226
+ puppet_certname_flag,
227
+ puppet_digest_flag,
228
+ puppet_detailed_exitcodes_flag,
229
+ puppet_logdest_flag,
230
+ puppet_test_flag,
231
+ puppet_onetime_flag,
232
+ puppet_no_daemonize_flag,
233
+ puppet_noop_flag,
234
+ puppet_verbose_flag,
235
+ puppet_debug_flag
236
+ ].join(" ")
237
+ end
238
+ end
239
+
240
+ protected
241
+
242
+ def load_needed_dependencies!
243
+ end
244
+
245
+ def puppet_config
246
+ config[:puppet_config_path]
247
+ end
248
+
249
+ def puppet_debian_version
250
+ config[:puppet_version] ? "=#{config[:puppet_version]}" : nil
251
+ end
252
+
253
+ def puppet_redhat_version
254
+ config[:puppet_version] ? "-#{config[:puppet_version]}" : nil
255
+ end
256
+
257
+ def puppet_noop_flag
258
+ config[:puppet_noop] ? '--noop' : nil
259
+ end
260
+
261
+ def puppet_debug_flag
262
+ config[:puppet_debug] ? '-d' : nil
263
+ end
264
+
265
+ def puppet_verbose_flag
266
+ config[:puppet_verbose] ? '-v' : nil
267
+ end
268
+
269
+ def puppet_platform
270
+ config[:puppet_platform].to_s.downcase
271
+ end
272
+
273
+ def update_packages_debian_cmd
274
+ config[:update_package_repos] ? "#{sudo('apt-get')} update" : nil
275
+ end
276
+
277
+ def update_packages_redhat_cmd
278
+ config[:update_package_repos] ? "#{sudo('yum')} makecache" : nil
279
+ end
280
+
281
+ def custom_facts
282
+ return nil if config[:custom_facts].none?
283
+ bash_vars = config[:custom_facts].map { |k,v| "FACTER_#{k}=#{v}" }.join(" ")
284
+ bash_vars = "export #{bash_vars};"
285
+ debug(bash_vars)
286
+ bash_vars
287
+ end
288
+
289
+ def puppet_server_flag
290
+ config[:puppet_server] ? "--server=#{config[:puppet_server]}" : nil
291
+ end
292
+
293
+ def puppet_masterport_flag
294
+ config[:puppet_masterport] ? '--masterport=#{config[:puppet_masterport]}' : nil
295
+ end
296
+
297
+ def puppet_detailed_exitcodes_flag
298
+ config[:puppet_detailed_exitcodes] ? '--detailed-exitcodes' : nil
299
+ end
300
+
301
+ def puppet_logdest_flag
302
+ config[:puppet_logdest] ? "--logdest=#{config[:puppet_logdest]}" : nil
303
+ end
304
+
305
+ def puppet_test_flag
306
+ config[:puppet_test] ? '--test' : nil
307
+ end
308
+
309
+ def puppet_onetime_flag
310
+ config[:puppet_onetime] ? '--onetime' : nil
311
+ end
312
+
313
+ def puppet_no_daemonize_flag
314
+ config[:puppet_no_daemonize] ? '--no-daemonize' : nil
315
+ end
316
+
317
+ def puppet_no_daemonize
318
+ config[:puppet_no_daemonize]
319
+ end
320
+
321
+ def puppet_server
322
+ config[:puppet_server]
323
+ end
324
+
325
+ def puppet_certname_flag
326
+ config[:puppet_certname] ? "--certname=#{config[:puppet_certname]}" : nil
327
+ end
328
+
329
+ def puppet_digest_flag
330
+ config[:puppet_digest] ? "--digest=#{config[:puppet_digest]}" : nil
331
+ end
332
+
333
+ def puppet_apt_repo
334
+ config[:puppet_apt_repo]
335
+ end
336
+
337
+ def puppet_apt_repo_file
338
+ config[:puppet_apt_repo].split('/').last
339
+ end
340
+
341
+ def puppet_yum_repo
342
+ config[:puppet_yum_repo]
343
+ end
344
+
345
+ def chef_url
346
+ config[:chef_bootstrap_url]
347
+ end
348
+
349
+ def prepare_puppet_config
350
+ return unless puppet_config
351
+
352
+ info('Preparing puppet.conf')
353
+ debug("Using puppet config from #{puppet_config}")
354
+
355
+ FileUtils.cp_r(puppet_config, File.join(sandbox_path, 'puppet.conf'))
356
+ end
357
+ end
358
+ end
359
+ end
@@ -1,552 +1,566 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
- # Author:: Chris Lundquist (<chris.lundquist@github.com>) Neill Turner (<neillwturner@gmail.com>)
4
- #
5
- # Copyright (C) 2013,2014 Chris Lundquist, Neill Turner
6
- #
7
- # Licensed under the Apache License, Version 2.0 (the "License");
8
- # you may not use this file except in compliance with the License.
9
- # You may obtain a copy of the License at
10
- #
11
- # http://www.apache.org/licenses/LICENSE-2.0
12
- #
13
- # Unless required by applicable law or agreed to in writing, software
14
- # distributed under the License is distributed on an "AS IS" BASIS,
15
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- # See the License for the specific language governing permissions and
17
- # limitations under the License.
18
- #
19
- # See https://github.com/neillturner/kitchen-puppet/blob/master/provisioner_options.md
20
- # for documentation configuration parameters with puppet_apply provisioner.
21
- #
22
-
23
- require 'json'
24
- require 'kitchen/provisioner/base'
25
- require 'kitchen/provisioner/puppet/librarian'
26
-
27
- module Kitchen
28
-
29
- class Busser
30
-
31
- def non_suite_dirs
32
- %w{data data_bags environments nodes roles puppet}
33
- end
34
- end
35
-
36
- module Provisioner
37
- #
38
- # Puppet Apply provisioner.
39
- #
40
- class PuppetApply < Base
41
- attr_accessor :tmp_dir
42
-
43
- default_config :require_puppet_omnibus, false
44
- # TODO use something like https://github.com/fnichol/omnibus-puppet
45
- default_config :puppet_omnibus_url, nil
46
- default_config :puppet_omnibus_remote_path, '/opt/puppet'
47
- default_config :puppet_version, nil
48
- default_config :require_puppet_repo, true
49
- default_config :require_chef_for_busser, true
50
- default_config :resolve_with_librarian_puppet, true
51
- default_config :puppet_environment, nil
52
- default_config :puppet_apt_repo, "http://apt.puppetlabs.com/puppetlabs-release-precise.deb"
53
- default_config :puppet_yum_repo, "https://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm"
54
- default_config :chef_bootstrap_url, "https://www.getchef.com/chef/install.sh"
55
-
56
- default_config :puppet_apply_command, nil
57
-
58
- default_config :hiera_data_remote_path, '/var/lib/hiera'
59
- default_config :manifest, 'site.pp'
60
-
61
- default_config :manifests_path do |provisioner|
62
- provisioner.calculate_path('manifests') or
63
- raise 'No manifests_path detected. Please specify one in .kitchen.yml'
64
- end
65
-
66
- default_config :modules_path do |provisioner|
67
- modules_path = provisioner.calculate_path('modules')
68
- if modules_path.nil? && provisioner.calculate_path('Puppetfile', :file).nil?
69
- raise 'No modules_path detected. Please specify one in .kitchen.yml'
70
- end
71
- modules_path
72
- end
73
-
74
- default_config :files_path do |provisioner|
75
- provisioner.calculate_path('files') || 'files'
76
- end
77
-
78
- default_config :hiera_data_path do |provisioner|
79
- provisioner.calculate_path('hiera')
80
- end
81
-
82
- default_config :puppet_config_path do |provisioner|
83
- provisioner.calculate_path('puppet.conf', :file)
84
- end
85
-
86
- default_config :hiera_config_path do |provisioner|
87
- provisioner.calculate_path('hiera.yaml', :file)
88
- end
89
-
90
- default_config :fileserver_config_path do |provisioner|
91
- provisioner.calculate_path('fileserver.conf', :file)
92
- end
93
-
94
- default_config :puppetfile_path do |provisioner|
95
- provisioner.calculate_path('Puppetfile', :file)
96
- end
97
-
98
- default_config :modulefile_path do |provisioner|
99
- provisioner.calculate_path('Modulefile', :file)
100
- end
101
-
102
- default_config :metadata_json_path do |provisioner|
103
- provisioner.calculate_path('metadata.json', :file)
104
- end
105
-
106
- default_config :manifests_path do |provisioner|
107
- provisioner.calculate_path('manifests', :directory)
108
- end
109
-
110
-
111
- default_config :puppet_debug, false
112
- default_config :puppet_verbose, false
113
- default_config :puppet_noop, false
114
- default_config :puppet_platform, ''
115
- default_config :update_package_repos, true
116
- default_config :custom_facts, {}
117
-
118
- def calculate_path(path, type = :directory)
119
- base = config[:test_base_path]
120
- candidates = []
121
- candidates << File.join(base, instance.suite.name, 'puppet', path)
122
- candidates << File.join(base, instance.suite.name, path)
123
- candidates << File.join(base, path)
124
- candidates << File.join(Dir.pwd, path)
125
-
126
- candidates.find do |c|
127
- type == :directory ? File.directory?(c) : File.file?(c)
128
- end
129
- end
130
-
131
- def install_command
132
- return unless config[:require_puppet_omnibus] or config[:require_puppet_repo]
133
- if config[:require_puppet_omnibus]
134
- info("Installing puppet using puppet omnibus")
135
- version = if !config[:puppet_version].nil?
136
- "-v #{config[:puppet_version]}"
137
- else
138
- ""
139
- end
140
- <<-INSTALL
141
- #{Util.shell_helpers}
142
-
143
- if [ ! -d "#{config[:puppet_omnibus_remote_path]}" ]; then
144
- echo "-----> Installing Puppet Omnibus"
145
- do_download #{config[:puppet_omnibus_url]} /tmp/puppet_install.sh
146
- #{sudo('sh')} /tmp/puppet_install.sh #{version}
147
- fi
148
- #{install_busser}
149
- INSTALL
150
- else
151
- case puppet_platform
152
- when "debian", "ubuntu"
153
- info("Installing puppet on #{puppet_platform}")
154
- <<-INSTALL
155
- if [ ! $(which puppet) ]; then
156
- #{sudo('wget')} #{puppet_apt_repo}
157
- #{sudo('dpkg')} -i #{puppet_apt_repo_file}
158
- #{update_packages_debian_cmd}
159
- #{sudo('apt-get')} -y install puppet-common#{puppet_debian_version}
160
- #{sudo('apt-get')} -y install puppet#{puppet_debian_version}
161
- fi
162
- #{install_busser}
163
- INSTALL
164
- when "redhat", "centos", "fedora"
165
- info("Installing puppet on #{puppet_platform}")
166
- <<-INSTALL
167
- if [ ! $(which puppet) ]; then
168
- #{sudo('rpm')} -ivh #{puppet_yum_repo}
169
- #{update_packages_redhat_cmd}
170
- #{sudo('yum')} -y install puppet#{puppet_redhat_version}
171
- fi
172
- #{install_busser}
173
- INSTALL
174
- else
175
- info("Installing puppet, will try to determine platform os")
176
- <<-INSTALL
177
- if [ ! $(which puppet) ]; then
178
- if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ]; then
179
- #{sudo('rpm')} -ivh #{puppet_yum_repo}
180
- #{update_packages_redhat_cmd}
181
- #{sudo('yum')} -y install puppet#{puppet_redhat_version}
182
- else
183
- #{sudo('wget')} #{puppet_apt_repo}
184
- #{sudo('dpkg')} -i #{puppet_apt_repo_file}
185
- #{update_packages_debian_cmd}
186
- #{sudo('apt-get')} -y install puppet-common#{puppet_debian_version}
187
- #{sudo('apt-get')} -y install puppet#{puppet_debian_version}
188
- fi
189
- fi
190
- #{install_busser}
191
- INSTALL
192
- end
193
- end
194
- end
195
-
196
- def install_busser
197
- if config[:require_chef_for_busser]
198
- <<-INSTALL
199
- #{Util.shell_helpers}
200
- # install chef omnibus so that busser works as this is needed to run tests :(
201
- # TODO: work out how to install enough ruby
202
- # and set busser: { :ruby_bindir => '/usr/bin/ruby' } so that we dont need the
203
- # whole chef client
204
- if [ ! -d "/opt/chef" ]
205
- then
206
- echo "-----> Installing Chef Omnibus to install busser to run tests"
207
- do_download #{chef_url} /tmp/install.sh
208
- #{sudo('sh')} /tmp/install.sh
209
- fi
210
- INSTALL
211
- end
212
- end
213
-
214
-
215
-
216
- def init_command
217
- dirs = %w{modules manifests files hiera hiera.yaml}.
218
- map { |dir| File.join(config[:root_path], dir) }.join(" ")
219
- cmd = "#{sudo('rm')} -rf #{dirs} #{hiera_data_remote_path} /etc/hiera.yaml /etc/puppet/hiera.yaml /etc/puppet/fileserver.conf;"
220
- cmd = cmd+" mkdir -p #{config[:root_path]}"
221
- debug(cmd)
222
- cmd
223
- end
224
-
225
- def create_sandbox
226
- super
227
- debug("Creating local sandbox in #{sandbox_path}")
228
-
229
- yield if block_given?
230
-
231
- prepare_modules
232
- prepare_manifests
233
- prepare_files
234
- prepare_puppet_config
235
- prepare_hiera_config
236
- prepare_fileserver_config
237
- prepare_hiera_data
238
- info('Finished Preparing files for transfer')
239
-
240
- end
241
-
242
- def cleanup_sandbox
243
- return if sandbox_path.nil?
244
- debug("Cleaning up local sandbox in #{sandbox_path}")
245
- FileUtils.rmtree(sandbox_path)
246
- end
247
-
248
- def prepare_command
249
- commands = []
250
-
251
- if puppet_config
252
- commands << [
253
- sudo('cp'),
254
- File.join(config[:root_path], 'puppet.conf'),
255
- '/etc/puppet',
256
- ].join(' ')
257
- end
258
-
259
- if hiera_config
260
- commands << [
261
- sudo('cp'), File.join(config[:root_path],'hiera.yaml'), '/etc/',
262
- ].join(' ')
263
-
264
- commands << [
265
- sudo('cp'), File.join(config[:root_path],'hiera.yaml'), '/etc/puppet/',
266
- ].join(' ')
267
- end
268
-
269
- if fileserver_config
270
- commands << [
271
- sudo('cp'),
272
- File.join(config[:root_path], 'fileserver.conf'),
273
- '/etc/puppet',
274
- ].join(' ')
275
- end
276
-
277
- if hiera_data and hiera_data_remote_path == '/var/lib/hiera'
278
- commands << [
279
- sudo('cp -r'), File.join(config[:root_path], 'hiera'), '/var/lib/'
280
- ].join(' ')
281
- end
282
-
283
- if hiera_data and hiera_data_remote_path != '/var/lib/hiera'
284
- commands << [
285
- sudo('mkdir -p'), hiera_data_remote_path
286
- ].join(' ')
287
- commands << [
288
- sudo('cp -r'), File.join(config[:root_path], 'hiera/*'), hiera_data_remote_path
289
- ].join(' ')
290
- end
291
-
292
- command = commands.join(' && ')
293
- debug(command)
294
- command
295
- end
296
-
297
- def run_command
298
- if !config[:puppet_apply_command].nil?
299
- return config[:puppet_apply_command]
300
- else
301
- [
302
- custom_facts,
303
- sudo('puppet'),
304
- 'apply',
305
- File.join(config[:root_path], 'manifests', manifest),
306
- "--modulepath=#{File.join(config[:root_path], 'modules')}",
307
- "--manifestdir=#{File.join(config[:root_path], 'manifests')}",
308
- "--fileserverconfig=#{File.join(config[:root_path], 'fileserver.conf')}",
309
- puppet_environment_flag,
310
- puppet_noop_flag,
311
- puppet_verbose_flag,
312
- puppet_debug_flag,
313
- ].join(" ")
314
- end
315
- end
316
-
317
- protected
318
-
319
- def load_needed_dependencies!
320
- if File.exists?(puppetfile) and config[:resolve_with_librarian_puppet]
321
- debug("Puppetfile found at #{puppetfile}, loading Librarian-Puppet")
322
- Puppet::Librarian.load!(logger)
323
- end
324
- end
325
-
326
- def tmpmodules_dir
327
- File.join(sandbox_path, 'modules')
328
- end
329
-
330
- def puppetfile
331
- config[:puppetfile_path] or ''
332
- end
333
-
334
- def modulefile
335
- config[:modulefile_path] or ''
336
- end
337
-
338
- def metadata_json
339
- config[:metadata_json_path] or ''
340
- end
341
-
342
- def manifest
343
- config[:manifest]
344
- end
345
-
346
- def manifests
347
- config[:manifests_path]
348
- end
349
-
350
- def modules
351
- config[:modules_path]
352
- end
353
-
354
- def files
355
- config[:files_path] || 'files'
356
- end
357
-
358
- def puppet_config
359
- config[:puppet_config_path]
360
- end
361
-
362
- def puppet_environment
363
- config[:puppet_environment]
364
- end
365
-
366
- def hiera_config
367
- config[:hiera_config_path]
368
- end
369
-
370
- def fileserver_config
371
- config[:fileserver_config_path]
372
- end
373
-
374
- def hiera_data
375
- config[:hiera_data_path]
376
- end
377
-
378
- def hiera_data_remote_path
379
- config[:hiera_data_remote_path]
380
- end
381
-
382
- def puppet_debian_version
383
- config[:puppet_version] ? "=#{config[:puppet_version]}" : nil
384
- end
385
-
386
- def puppet_redhat_version
387
- config[:puppet_version] ? "-#{config[:puppet_version]}" : nil
388
- end
389
-
390
- def puppet_environment_flag
391
- config[:puppet_environment] ? "--environment=#{config[:puppet_environment]}" : nil
392
- end
393
-
394
- def puppet_noop_flag
395
- config[:puppet_noop] ? '--noop' : nil
396
- end
397
-
398
- def puppet_debug_flag
399
- config[:puppet_debug] ? '-d' : nil
400
- end
401
-
402
- def puppet_verbose_flag
403
- config[:puppet_verbose] ? '-v' : nil
404
- end
405
-
406
- def puppet_platform
407
- config[:puppet_platform].to_s.downcase
408
- end
409
-
410
- def update_packages_debian_cmd
411
- config[:update_package_repos] ? "#{sudo('apt-get')} update" : nil
412
- end
413
-
414
- def update_packages_redhat_cmd
415
- config[:update_package_repos] ? "#{sudo('yum')} makecache" : nil
416
- end
417
-
418
- def custom_facts
419
- return nil if config[:custom_facts].none?
420
- bash_vars = config[:custom_facts].map { |k,v| "FACTER_#{k}=#{v}" }.join(" ")
421
- bash_vars = "export #{bash_vars};"
422
- debug(bash_vars)
423
- bash_vars
424
- end
425
-
426
- def puppet_apt_repo
427
- config[:puppet_apt_repo]
428
- end
429
-
430
- def puppet_apt_repo_file
431
- config[:puppet_apt_repo].split('/').last
432
- end
433
-
434
- def puppet_yum_repo
435
- config[:puppet_yum_repo]
436
- end
437
-
438
- def chef_url
439
- config[:chef_bootstrap_url]
440
- end
441
-
442
- def prepare_manifests
443
- info('Preparing manifests')
444
- debug("Using manifests from #{manifests}")
445
-
446
- tmp_manifests_dir = File.join(sandbox_path, 'manifests')
447
- FileUtils.mkdir_p(tmp_manifests_dir)
448
- FileUtils.cp_r(Dir.glob("#{manifests}/*"), tmp_manifests_dir)
449
- end
450
-
451
- def prepare_files
452
- info('Preparing files')
453
-
454
- unless File.directory?(files)
455
- info 'nothing to do for files'
456
- return
457
- end
458
-
459
- debug("Using files from #{files}")
460
-
461
- tmp_files_dir = File.join(sandbox_path, 'files')
462
- FileUtils.mkdir_p(tmp_files_dir)
463
- FileUtils.cp_r(Dir.glob("#{files}/*"), tmp_files_dir)
464
- end
465
-
466
- def prepare_modules
467
- info('Preparing modules')
468
-
469
- FileUtils.mkdir_p(tmpmodules_dir)
470
-
471
- resolve_with_librarian if File.exists?(puppetfile) and config[:resolve_with_librarian_puppet]
472
-
473
- if modules && File.directory?(modules)
474
- debug("Copying modules from #{modules} to #{tmpmodules_dir}")
475
- FileUtils.cp_r(Dir.glob("#{modules}/*"), tmpmodules_dir, remove_destination: true)
476
- else
477
- info 'nothing to do for modules'
478
- end
479
-
480
- copy_self_as_module
481
- end
482
-
483
- def copy_self_as_module
484
- if File.exists?(modulefile)
485
- warn('Modulefile found but this is depricated, ignoring it, see https://tickets.puppetlabs.com/browse/PUP-1188')
486
- end
487
-
488
- if File.exists?(metadata_json)
489
- module_name = nil
490
- begin
491
- module_name = JSON.parse(IO.read(metadata_json))['name'].split('/').last
492
- rescue
493
- error("not able to load or parse #{metadata_json_path} for the name of the module")
494
- end
495
-
496
- if module_name
497
- module_target_path = File.join(sandbox_path, 'modules', module_name)
498
- FileUtils.mkdir_p(module_target_path)
499
- FileUtils.cp_r(
500
- Dir.glob(File.join(config[:kitchen_root], '*')).reject { |entry| entry =~ /modules/ },
501
- module_target_path,
502
- remove_destination: true
503
- )
504
- end
505
- end
506
- end
507
-
508
- def prepare_puppet_config
509
- return unless puppet_config
510
-
511
- info('Preparing puppet.conf')
512
- debug("Using puppet config from #{puppet_config}")
513
-
514
- FileUtils.cp_r(puppet_config, File.join(sandbox_path, 'puppet.conf'))
515
- end
516
-
517
- def prepare_hiera_config
518
- return unless hiera_config
519
-
520
- info('Preparing hiera')
521
- debug("Using hiera from #{hiera_config}")
522
-
523
- FileUtils.cp_r(hiera_config, File.join(sandbox_path, 'hiera.yaml'))
524
- end
525
-
526
- def prepare_fileserver_config
527
- return unless fileserver_config
528
-
529
- info('Preparing fileserver')
530
- debug("Using fileserver config from #{fileserver_config}")
531
-
532
- FileUtils.cp_r(fileserver_config, File.join(sandbox_path, 'fileserver.conf'))
533
- end
534
-
535
- def prepare_hiera_data
536
- return unless hiera_data
537
- info('Preparing hiera data')
538
-
539
- tmp_hiera_dir = File.join(sandbox_path, 'hiera')
540
- debug("Copying hiera data from #{hiera_data} to #{tmp_hiera_dir}")
541
- FileUtils.mkdir_p(tmp_hiera_dir)
542
- FileUtils.cp_r(Dir.glob("#{hiera_data}/*"), tmp_hiera_dir)
543
- end
544
-
545
- def resolve_with_librarian
546
- Kitchen.mutex.synchronize do
547
- Puppet::Librarian.new(puppetfile, tmpmodules_dir, logger).resolve
548
- end
549
- end
550
- end
551
- end
552
- end
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # Author:: Chris Lundquist (<chris.lundquist@github.com>) Neill Turner (<neillwturner@gmail.com>)
4
+ #
5
+ # Copyright (C) 2013,2014 Chris Lundquist, Neill Turner
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+ # See https://github.com/neillturner/kitchen-puppet/blob/master/provisioner_options.md
20
+ # for documentation configuration parameters with puppet_apply provisioner.
21
+ #
22
+
23
+ require 'json'
24
+ require 'kitchen/provisioner/base'
25
+ require 'kitchen/provisioner/puppet/librarian'
26
+
27
+ module Kitchen
28
+
29
+ class Busser
30
+
31
+ def non_suite_dirs
32
+ %w{data data_bags environments nodes roles puppet}
33
+ end
34
+ end
35
+
36
+ module Provisioner
37
+ #
38
+ # Puppet Apply provisioner.
39
+ #
40
+ class PuppetApply < Base
41
+ attr_accessor :tmp_dir
42
+
43
+ default_config :require_puppet_omnibus, false
44
+ # TODO use something like https://github.com/fnichol/omnibus-puppet
45
+ default_config :puppet_omnibus_url, nil
46
+ default_config :puppet_omnibus_remote_path, '/opt/puppet'
47
+ default_config :puppet_version, nil
48
+ default_config :require_puppet_repo, true
49
+ default_config :require_chef_for_busser, true
50
+ default_config :resolve_with_librarian_puppet, true
51
+ default_config :puppet_environment, nil
52
+ default_config :puppet_apt_repo, "http://apt.puppetlabs.com/puppetlabs-release-precise.deb"
53
+ default_config :puppet_yum_repo, "https://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm"
54
+ default_config :chef_bootstrap_url, "https://www.getchef.com/chef/install.sh"
55
+
56
+ default_config :puppet_apply_command, nil
57
+
58
+ default_config :hiera_data_remote_path, '/var/lib/hiera'
59
+ default_config :manifest, 'site.pp'
60
+
61
+ default_config :manifests_path do |provisioner|
62
+ provisioner.calculate_path('manifests') or
63
+ raise 'No manifests_path detected. Please specify one in .kitchen.yml'
64
+ end
65
+
66
+ default_config :modules_path do |provisioner|
67
+ modules_path = provisioner.calculate_path('modules')
68
+ if modules_path.nil? && provisioner.calculate_path('Puppetfile', :file).nil?
69
+ raise 'No modules_path detected. Please specify one in .kitchen.yml'
70
+ end
71
+ modules_path
72
+ end
73
+
74
+ default_config :files_path do |provisioner|
75
+ provisioner.calculate_path('files') || 'files'
76
+ end
77
+
78
+ default_config :hiera_data_path do |provisioner|
79
+ provisioner.calculate_path('hiera')
80
+ end
81
+
82
+ default_config :puppet_config_path do |provisioner|
83
+ provisioner.calculate_path('puppet.conf', :file)
84
+ end
85
+
86
+ default_config :hiera_config_path do |provisioner|
87
+ provisioner.calculate_path('hiera.yaml', :file)
88
+ end
89
+
90
+ default_config :fileserver_config_path do |provisioner|
91
+ provisioner.calculate_path('fileserver.conf', :file)
92
+ end
93
+
94
+ default_config :puppetfile_path do |provisioner|
95
+ provisioner.calculate_path('Puppetfile', :file)
96
+ end
97
+
98
+ default_config :modulefile_path do |provisioner|
99
+ provisioner.calculate_path('Modulefile', :file)
100
+ end
101
+
102
+ default_config :metadata_json_path do |provisioner|
103
+ provisioner.calculate_path('metadata.json', :file)
104
+ end
105
+
106
+ default_config :manifests_path do |provisioner|
107
+ provisioner.calculate_path('manifests', :directory)
108
+ end
109
+
110
+
111
+ default_config :puppet_debug, false
112
+ default_config :puppet_verbose, false
113
+ default_config :puppet_noop, false
114
+ default_config :puppet_platform, ''
115
+ default_config :update_package_repos, true
116
+ default_config :remove_puppet_repo, false
117
+ default_config :custom_facts, {}
118
+
119
+ def calculate_path(path, type = :directory)
120
+ base = config[:test_base_path]
121
+ candidates = []
122
+ candidates << File.join(base, instance.suite.name, 'puppet', path)
123
+ candidates << File.join(base, instance.suite.name, path)
124
+ candidates << File.join(base, path)
125
+ candidates << File.join(Dir.pwd, path)
126
+
127
+ candidates.find do |c|
128
+ type == :directory ? File.directory?(c) : File.file?(c)
129
+ end
130
+ end
131
+
132
+ def install_command
133
+ return unless config[:require_puppet_omnibus] or config[:require_puppet_repo]
134
+ if config[:require_puppet_omnibus]
135
+ info("Installing puppet using puppet omnibus")
136
+ version = if !config[:puppet_version].nil?
137
+ "-v #{config[:puppet_version]}"
138
+ else
139
+ ""
140
+ end
141
+ <<-INSTALL
142
+ #{Util.shell_helpers}
143
+
144
+ if [ ! -d "#{config[:puppet_omnibus_remote_path]}" ]; then
145
+ echo "-----> Installing Puppet Omnibus"
146
+ do_download #{config[:puppet_omnibus_url]} /tmp/puppet_install.sh
147
+ #{sudo('sh')} /tmp/puppet_install.sh #{version}
148
+ fi
149
+ #{install_busser}
150
+ INSTALL
151
+ else
152
+ case puppet_platform
153
+ when "debian", "ubuntu"
154
+ info("Installing puppet on #{puppet_platform}")
155
+ <<-INSTALL
156
+ if [ ! $(which puppet) ]; then
157
+ #{sudo('wget')} #{puppet_apt_repo}
158
+ #{sudo('dpkg')} -i #{puppet_apt_repo_file}
159
+ #{update_packages_debian_cmd}
160
+ #{sudo('apt-get')} -y install puppet-common#{puppet_debian_version}
161
+ #{sudo('apt-get')} -y install puppet#{puppet_debian_version}
162
+ fi
163
+ #{install_busser}
164
+ INSTALL
165
+ when "redhat", "centos", "fedora"
166
+ info("Installing puppet on #{puppet_platform}")
167
+ <<-INSTALL
168
+ if [ ! $(which puppet) ]; then
169
+ #{sudo('rpm')} -ivh #{puppet_yum_repo}
170
+ #{update_packages_redhat_cmd}
171
+ #{sudo('yum')} -y install puppet#{puppet_redhat_version}
172
+ fi
173
+ #{install_busser}
174
+ INSTALL
175
+ else
176
+ info("Installing puppet, will try to determine platform os")
177
+ <<-INSTALL
178
+ if [ ! $(which puppet) ]; then
179
+ if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ]; then
180
+ #{sudo('rpm')} -ivh #{puppet_yum_repo}
181
+ #{update_packages_redhat_cmd}
182
+ #{sudo('yum')} -y install puppet#{puppet_redhat_version}
183
+ else
184
+ #{sudo('wget')} #{puppet_apt_repo}
185
+ #{sudo('dpkg')} -i #{puppet_apt_repo_file}
186
+ #{update_packages_debian_cmd}
187
+ #{sudo('apt-get')} -y install puppet-common#{puppet_debian_version}
188
+ #{sudo('apt-get')} -y install puppet#{puppet_debian_version}
189
+ fi
190
+ fi
191
+ #{install_busser}
192
+ INSTALL
193
+ end
194
+ end
195
+ end
196
+
197
+ def install_busser
198
+ if config[:require_chef_for_busser]
199
+ <<-INSTALL
200
+ #{Util.shell_helpers}
201
+ # install chef omnibus so that busser works as this is needed to run tests :(
202
+ # TODO: work out how to install enough ruby
203
+ # and set busser: { :ruby_bindir => '/usr/bin/ruby' } so that we dont need the
204
+ # whole chef client
205
+ if [ ! -d "/opt/chef" ]
206
+ then
207
+ echo "-----> Installing Chef Omnibus to install busser to run tests"
208
+ do_download #{chef_url} /tmp/install.sh
209
+ #{sudo('sh')} /tmp/install.sh
210
+ fi
211
+ INSTALL
212
+ end
213
+ end
214
+
215
+
216
+
217
+ def init_command
218
+ dirs = %w{modules manifests files hiera hiera.yaml}.
219
+ map { |dir| File.join(config[:root_path], dir) }.join(" ")
220
+ cmd = "#{sudo('rm')} -rf #{dirs} #{hiera_data_remote_path} /etc/hiera.yaml /etc/puppet/hiera.yaml /etc/puppet/fileserver.conf;"
221
+ cmd = cmd+" mkdir -p #{config[:root_path]}"
222
+ debug(cmd)
223
+ cmd
224
+ end
225
+
226
+ def create_sandbox
227
+ super
228
+ debug("Creating local sandbox in #{sandbox_path}")
229
+
230
+ yield if block_given?
231
+
232
+ prepare_modules
233
+ prepare_manifests
234
+ prepare_files
235
+ prepare_puppet_config
236
+ prepare_hiera_config
237
+ prepare_fileserver_config
238
+ prepare_hiera_data
239
+ info('Finished Preparing files for transfer')
240
+
241
+ end
242
+
243
+ def cleanup_sandbox
244
+ return if sandbox_path.nil?
245
+ debug("Cleaning up local sandbox in #{sandbox_path}")
246
+ FileUtils.rmtree(sandbox_path)
247
+ end
248
+
249
+ def prepare_command
250
+ commands = []
251
+
252
+ if puppet_config
253
+ commands << [
254
+ sudo('cp'),
255
+ File.join(config[:root_path], 'puppet.conf'),
256
+ '/etc/puppet',
257
+ ].join(' ')
258
+ end
259
+
260
+ if hiera_config
261
+ commands << [
262
+ sudo('cp'), File.join(config[:root_path],'hiera.yaml'), '/etc/',
263
+ ].join(' ')
264
+
265
+ commands << [
266
+ sudo('cp'), File.join(config[:root_path],'hiera.yaml'), '/etc/puppet/',
267
+ ].join(' ')
268
+ end
269
+
270
+ if fileserver_config
271
+ commands << [
272
+ sudo('cp'),
273
+ File.join(config[:root_path], 'fileserver.conf'),
274
+ '/etc/puppet',
275
+ ].join(' ')
276
+ end
277
+
278
+ if hiera_data and hiera_data_remote_path == '/var/lib/hiera'
279
+ commands << [
280
+ sudo('cp -r'), File.join(config[:root_path], 'hiera'), '/var/lib/'
281
+ ].join(' ')
282
+ end
283
+
284
+ if hiera_data and hiera_data_remote_path != '/var/lib/hiera'
285
+ commands << [
286
+ sudo('mkdir -p'), hiera_data_remote_path
287
+ ].join(' ')
288
+ commands << [
289
+ sudo('cp -r'), File.join(config[:root_path], 'hiera/*'), hiera_data_remote_path
290
+ ].join(' ')
291
+ end
292
+
293
+ command = commands.join(' && ')
294
+ debug(command)
295
+ command
296
+ end
297
+
298
+ def run_command
299
+ if !config[:puppet_apply_command].nil?
300
+ return config[:puppet_apply_command]
301
+ else
302
+ [
303
+ custom_facts,
304
+ sudo('puppet'),
305
+ 'apply',
306
+ File.join(config[:root_path], 'manifests', manifest),
307
+ "--modulepath=#{File.join(config[:root_path], 'modules')}",
308
+ "--manifestdir=#{File.join(config[:root_path], 'manifests')}",
309
+ "--fileserverconfig=#{File.join(config[:root_path], 'fileserver.conf')}",
310
+ puppet_environment_flag,
311
+ puppet_noop_flag,
312
+ puppet_verbose_flag,
313
+ puppet_debug_flag,
314
+ remove_repo
315
+ ].join(" ")
316
+ end
317
+ end
318
+
319
+ protected
320
+
321
+ def load_needed_dependencies!
322
+ if File.exists?(puppetfile) and config[:resolve_with_librarian_puppet]
323
+ debug("Puppetfile found at #{puppetfile}, loading Librarian-Puppet")
324
+ Puppet::Librarian.load!(logger)
325
+ end
326
+ end
327
+
328
+ def tmpmodules_dir
329
+ File.join(sandbox_path, 'modules')
330
+ end
331
+
332
+ def puppetfile
333
+ config[:puppetfile_path] or ''
334
+ end
335
+
336
+ def modulefile
337
+ config[:modulefile_path] or ''
338
+ end
339
+
340
+ def metadata_json
341
+ config[:metadata_json_path] or ''
342
+ end
343
+
344
+ def manifest
345
+ config[:manifest]
346
+ end
347
+
348
+ def manifests
349
+ config[:manifests_path]
350
+ end
351
+
352
+ def modules
353
+ config[:modules_path]
354
+ end
355
+
356
+ def files
357
+ config[:files_path] || 'files'
358
+ end
359
+
360
+ def puppet_config
361
+ config[:puppet_config_path]
362
+ end
363
+
364
+ def puppet_environment
365
+ config[:puppet_environment]
366
+ end
367
+
368
+ def hiera_config
369
+ config[:hiera_config_path]
370
+ end
371
+
372
+ def fileserver_config
373
+ config[:fileserver_config_path]
374
+ end
375
+
376
+ def hiera_data
377
+ config[:hiera_data_path]
378
+ end
379
+
380
+ def hiera_data_remote_path
381
+ config[:hiera_data_remote_path]
382
+ end
383
+
384
+ def puppet_debian_version
385
+ config[:puppet_version] ? "=#{config[:puppet_version]}" : nil
386
+ end
387
+
388
+ def puppet_redhat_version
389
+ config[:puppet_version] ? "-#{config[:puppet_version]}" : nil
390
+ end
391
+
392
+ def puppet_environment_flag
393
+ config[:puppet_environment] ? "--environment=#{config[:puppet_environment]}" : nil
394
+ end
395
+
396
+ def puppet_noop_flag
397
+ config[:puppet_noop] ? '--noop' : nil
398
+ end
399
+
400
+ def puppet_debug_flag
401
+ config[:puppet_debug] ? '-d' : nil
402
+ end
403
+
404
+ def puppet_verbose_flag
405
+ config[:puppet_verbose] ? '-v' : nil
406
+ end
407
+
408
+ def puppet_platform
409
+ config[:puppet_platform].to_s.downcase
410
+ end
411
+
412
+ def update_packages_debian_cmd
413
+ config[:update_package_repos] ? "#{sudo('apt-get')} update" : nil
414
+ end
415
+
416
+ def update_packages_redhat_cmd
417
+ config[:update_package_repos] ? "#{sudo('yum')} makecache" : nil
418
+ end
419
+
420
+ def remove_puppet_repo
421
+ config[:remove_puppet_repo]
422
+ end
423
+
424
+ def custom_facts
425
+ return nil if config[:custom_facts].none?
426
+ bash_vars = config[:custom_facts].map { |k,v| "FACTER_#{k}=#{v}" }.join(" ")
427
+ bash_vars = "export #{bash_vars};"
428
+ debug(bash_vars)
429
+ bash_vars
430
+ end
431
+
432
+ def remove_repo
433
+ if remove_puppet_repo
434
+ '; rm -rf /tmp/kitchen'
435
+ else
436
+ nil
437
+ end
438
+ end
439
+
440
+ def puppet_apt_repo
441
+ config[:puppet_apt_repo]
442
+ end
443
+
444
+ def puppet_apt_repo_file
445
+ config[:puppet_apt_repo].split('/').last
446
+ end
447
+
448
+ def puppet_yum_repo
449
+ config[:puppet_yum_repo]
450
+ end
451
+
452
+ def chef_url
453
+ config[:chef_bootstrap_url]
454
+ end
455
+
456
+ def prepare_manifests
457
+ info('Preparing manifests')
458
+ debug("Using manifests from #{manifests}")
459
+
460
+ tmp_manifests_dir = File.join(sandbox_path, 'manifests')
461
+ FileUtils.mkdir_p(tmp_manifests_dir)
462
+ FileUtils.cp_r(Dir.glob("#{manifests}/*"), tmp_manifests_dir)
463
+ end
464
+
465
+ def prepare_files
466
+ info('Preparing files')
467
+
468
+ unless File.directory?(files)
469
+ info 'nothing to do for files'
470
+ return
471
+ end
472
+
473
+ debug("Using files from #{files}")
474
+
475
+ tmp_files_dir = File.join(sandbox_path, 'files')
476
+ FileUtils.mkdir_p(tmp_files_dir)
477
+ FileUtils.cp_r(Dir.glob("#{files}/*"), tmp_files_dir)
478
+ end
479
+
480
+ def prepare_modules
481
+ info('Preparing modules')
482
+
483
+ FileUtils.mkdir_p(tmpmodules_dir)
484
+
485
+ resolve_with_librarian if File.exists?(puppetfile) and config[:resolve_with_librarian_puppet]
486
+
487
+ if modules && File.directory?(modules)
488
+ debug("Copying modules from #{modules} to #{tmpmodules_dir}")
489
+ FileUtils.cp_r(Dir.glob("#{modules}/*"), tmpmodules_dir, remove_destination: true)
490
+ else
491
+ info 'nothing to do for modules'
492
+ end
493
+
494
+ copy_self_as_module
495
+ end
496
+
497
+ def copy_self_as_module
498
+ if File.exists?(modulefile)
499
+ warn('Modulefile found but this is depricated, ignoring it, see https://tickets.puppetlabs.com/browse/PUP-1188')
500
+ end
501
+
502
+ if File.exists?(metadata_json)
503
+ module_name = nil
504
+ begin
505
+ module_name = JSON.parse(IO.read(metadata_json))['name'].split('-').last
506
+ rescue
507
+ error("not able to load or parse #{metadata_json_path} for the name of the module")
508
+ end
509
+
510
+ if module_name
511
+ module_target_path = File.join(sandbox_path, 'modules', module_name)
512
+ FileUtils.mkdir_p(module_target_path)
513
+ FileUtils.cp_r(
514
+ Dir.glob(File.join(config[:kitchen_root], '*')).reject { |entry| entry =~ /modules/ },
515
+ module_target_path,
516
+ remove_destination: true
517
+ )
518
+ end
519
+ end
520
+ end
521
+
522
+ def prepare_puppet_config
523
+ return unless puppet_config
524
+
525
+ info('Preparing puppet.conf')
526
+ debug("Using puppet config from #{puppet_config}")
527
+
528
+ FileUtils.cp_r(puppet_config, File.join(sandbox_path, 'puppet.conf'))
529
+ end
530
+
531
+ def prepare_hiera_config
532
+ return unless hiera_config
533
+
534
+ info('Preparing hiera')
535
+ debug("Using hiera from #{hiera_config}")
536
+
537
+ FileUtils.cp_r(hiera_config, File.join(sandbox_path, 'hiera.yaml'))
538
+ end
539
+
540
+ def prepare_fileserver_config
541
+ return unless fileserver_config
542
+
543
+ info('Preparing fileserver')
544
+ debug("Using fileserver config from #{fileserver_config}")
545
+
546
+ FileUtils.cp_r(fileserver_config, File.join(sandbox_path, 'fileserver.conf'))
547
+ end
548
+
549
+ def prepare_hiera_data
550
+ return unless hiera_data
551
+ info('Preparing hiera data')
552
+
553
+ tmp_hiera_dir = File.join(sandbox_path, 'hiera')
554
+ debug("Copying hiera data from #{hiera_data} to #{tmp_hiera_dir}")
555
+ FileUtils.mkdir_p(tmp_hiera_dir)
556
+ FileUtils.cp_r(Dir.glob("#{hiera_data}/*"), tmp_hiera_dir)
557
+ end
558
+
559
+ def resolve_with_librarian
560
+ Kitchen.mutex.synchronize do
561
+ Puppet::Librarian.new(puppetfile, tmpmodules_dir, logger).resolve
562
+ end
563
+ end
564
+ end
565
+ end
566
+ end