kitchen-puppet 3.4.1 → 3.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,1464 +1,1465 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- #
4
- # Author:: Chris Lundquist (<chris.lundquist@github.com>) Neill Turner (<neillwturner@gmail.com>)
5
- #
6
- # Copyright (C) 2013,2014 Chris Lundquist, Neill Turner
7
- #
8
- # Licensed under the Apache License, Version 2.0 (the "License");
9
- # you may not use this file except in compliance with the License.
10
- # You may obtain a copy of the License at
11
- #
12
- # http://www.apache.org/licenses/LICENSE-2.0
13
- #
14
- # Unless required by applicable law or agreed to in writing, software
15
- # distributed under the License is distributed on an "AS IS" BASIS,
16
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
- # See the License for the specific language governing permissions and
18
- # limitations under the License.
19
- #
20
- # See https://github.com/neillturner/kitchen-puppet/blob/master/provisioner_options.md
21
- # for documentation configuration parameters with puppet_apply provisioner.
22
- #
23
-
24
- require 'uri'
25
- require 'json'
26
- require 'kitchen'
27
-
28
- module Kitchen
29
- class Busser
30
- def non_suite_dirs
31
- %w[data data_bags environments nodes roles puppet]
32
- end
33
- end
34
-
35
- module Configurable
36
- def platform_name
37
- instance.platform.name
38
- end
39
- end
40
-
41
- module Provisioner
42
- #
43
- # Puppet Apply provisioner.
44
- #
45
- class PuppetApply < Base
46
- attr_accessor :tmp_dir
47
-
48
- default_config :require_puppet_collections, true
49
- default_config :puppet_yum_collections_repo, 'http://yum.puppetlabs.com/puppet5/puppet-release-el-6.noarch.rpm'
50
- default_config :puppet_apt_collections_repo, 'http://apt.puppetlabs.com/puppet5-release-wheezy.deb'
51
- default_config :puppet_coll_remote_path, '/opt/puppetlabs'
52
- default_config :puppet_version, nil
53
- default_config :facter_version, nil
54
- default_config :hiera_version, nil
55
- default_config :install_hiera, false
56
- default_config :hiera_package, 'hiera-puppet'
57
- default_config :hiera_writer_files, nil
58
- default_config :require_puppet_repo, true
59
- default_config :require_chef_for_busser, true
60
- default_config :resolve_with_librarian_puppet, true
61
- default_config :resolve_with_r10k, false
62
- default_config :puppet_environment, nil
63
- default_config :puppet_environment_config_path do |provisioner|
64
- provisioner.calculate_path('environment.conf')
65
- end
66
- default_config :puppet_environment_remote_modules_path, 'modules'
67
- default_config :puppet_environment_remote_manifests_path, 'manifests'
68
- default_config :puppet_environment_remote_hieradata_path, 'hieradata'
69
- default_config :puppet_environment_hiera_config_path do |provisioner|
70
- provisioner.calculate_path('hiera.yaml', :file)
71
- end
72
- default_config :puppet_apt_repo, 'http://apt.puppetlabs.com/puppetlabs-release-precise.deb'
73
- default_config :puppet_yum_repo, 'https://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm'
74
- default_config :chef_bootstrap_url, 'https://www.chef.io/chef/install.sh'
75
- default_config :puppet_logdest, nil
76
- default_config :custom_install_command, nil
77
- default_config :custom_pre_install_command, nil
78
- default_config :custom_pre_apply_command, nil
79
- default_config :custom_post_apply_command, nil
80
- default_config :puppet_whitelist_exit_code, nil
81
- default_config :require_puppet_omnibus, false
82
- default_config :puppet_omnibus_url, 'https://raw.githubusercontent.com/petems/puppet-install-shell/master/install_puppet_5_agent.sh'
83
- default_config :puppet_enc, nil
84
- default_config :ignore_spec_fixtures, false
85
-
86
- default_config :puppet_apply_command, nil
87
-
88
- default_config :puppet_git_init, nil
89
- default_config :puppet_git_pr, nil
90
-
91
- default_config :http_proxy, nil
92
- default_config :https_proxy, nil
93
- default_config :no_proxy, nil
94
-
95
- default_config :ignored_paths_from_root, ['spec']
96
- default_config :hiera_data_remote_path, nil
97
- default_config :manifest, ''
98
- default_config :puppet_binary, 'puppet'
99
-
100
- default_config :manifests_path do |provisioner|
101
- provisioner.calculate_path('manifests') ||
102
- raise('No manifests_path detected. Please specify one in .kitchen.yml')
103
- end
104
-
105
- default_config :modules_path do |provisioner|
106
- modules_path = provisioner.calculate_path('modules')
107
- if modules_path.nil? && provisioner.calculate_path('Puppetfile', :file).nil?
108
- raise('No modules_path detected. Please specify one in .kitchen.yml')
109
- end
110
- modules_path
111
- end
112
-
113
- default_config :files_path do |provisioner|
114
- provisioner.calculate_path('files') || 'files'
115
- end
116
-
117
- default_config :hiera_data_path do |provisioner|
118
- provisioner.calculate_path('hiera')
119
- end
120
-
121
- default_config :puppet_config_path do |provisioner|
122
- provisioner.calculate_path('puppet.conf', :file)
123
- end
124
-
125
- default_config :hiera_config_path do |provisioner|
126
- provisioner.calculate_path('hiera.global.yaml', :file) ||
127
- provisioner.calculate_path('hiera.yaml', :file)
128
- end
129
-
130
- default_config :fileserver_config_path do |provisioner|
131
- provisioner.calculate_path('fileserver.conf', :file)
132
- end
133
- default_config :puppetfile_path do |provisioner|
134
- provisioner.calculate_path('Puppetfile', :file)
135
- end
136
-
137
- default_config :modulefile_path do |provisioner|
138
- provisioner.calculate_path('Modulefile', :file)
139
- end
140
-
141
- default_config :metadata_json_path do |provisioner|
142
- provisioner.calculate_path('metadata.json', :file)
143
- end
144
-
145
- default_config :manifests_path do |provisioner|
146
- provisioner.calculate_path('manifests', :directory)
147
- end
148
-
149
- default_config :spec_files_path do |provisioner|
150
- provisioner.calculate_path('spec', :directory)
151
- end
152
-
153
- default_config :spec_files_remote_path, '/etc/puppet/spec'
154
-
155
- default_config :puppet_debug, false
156
- default_config :puppet_verbose, false
157
- default_config :puppet_noop, false
158
- default_config :puppet_show_diff, false
159
- default_config :puppet_future_parser, false
160
- default_config :platform, &:platform_name
161
- default_config :update_package_repos, true
162
- default_config :remove_puppet_repo, false
163
- default_config :install_custom_facts, false
164
- default_config :custom_facts, {}
165
- default_config :facterlib, nil
166
- default_config :puppet_detailed_exitcodes, nil
167
- default_config :facter_file, nil
168
- default_config :librarian_puppet_ssl_file, nil
169
- default_config :r10k_ssl_file, nil
170
-
171
- default_config :hiera_eyaml, false
172
- default_config :hiera_eyaml_key_remote_path, '/etc/puppet/secure/keys'
173
- default_config :puppet_environmentpath_remote_path, nil
174
-
175
- default_config :hiera_eyaml_gpg, false
176
- default_config :hiera_eyaml_gpg_recipients, false
177
- default_config :hiera_eyaml_gpg_secring, false
178
- default_config :hiera_eyaml_gpg_pubring, false
179
- default_config :hiera_eyaml_gpg_remote_path, '/home/vagrant/.gnupg'
180
-
181
- default_config :hiera_eyaml_key_path do |provisioner|
182
- provisioner.calculate_path('hiera_keys')
183
- end
184
-
185
- default_config :hiera_deep_merge, false
186
- default_config :puppet_no_sudo, false
187
-
188
- def calculate_path(path, type = :directory)
189
- base = config[:test_base_path]
190
- candidates = []
191
- candidates << File.join(base, instance.suite.name, 'puppet', path)
192
- candidates << File.join(base, instance.suite.name, path)
193
- candidates << File.join(base, path)
194
- candidates << File.join(Dir.pwd, path)
195
-
196
- candidates.find do |c|
197
- type == :directory ? File.directory?(c) : File.file?(c)
198
- end
199
- end
200
-
201
- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
202
- def install_command
203
- return unless config[:require_puppet_collections] || config[:require_puppet_repo] || config[:require_puppet_omnibus]
204
- if config[:require_puppet_omnibus]
205
- install_omnibus_command
206
- elsif config[:require_puppet_collections]
207
- install_command_collections
208
- else
209
- case puppet_platform
210
- when 'debian', 'ubuntu'
211
- info("Installing puppet on #{config[:platform]}")
212
- # need to add a CR to avoid trouble with proxy settings concatenation
213
- <<-INSTALL
214
-
215
- #{custom_pre_install_command}
216
- if [ ! $(which puppet) ]; then
217
- #{sudo('apt-get')} -y install wget
218
- #{sudo('wget')} #{wget_proxy_parm} #{puppet_apt_repo}
219
- #{sudo('dpkg')} -i #{puppet_apt_repo_file}
220
- #{update_packages_debian_cmd}
221
- #{sudo_env('apt-get')} -y install facter#{facter_debian_version}
222
- #{sudo_env('apt-get')} -y install puppet-common#{puppet_debian_version}
223
- #{sudo_env('apt-get')} -y install puppet#{puppet_debian_version}
224
- #{install_hiera}
225
- fi
226
- #{install_eyaml}
227
- #{install_eyaml_gpg}
228
- #{install_deep_merge}
229
- #{install_busser}
230
- #{custom_install_command}
231
- INSTALL
232
- when 'redhat', 'centos', 'fedora', 'oracle', 'amazon'
233
- info("Installing puppet from yum on #{puppet_platform}")
234
- # need to add a CR to avoid trouble with proxy settings concatenation
235
- <<-INSTALL
236
-
237
- #{custom_pre_install_command}
238
- if [ ! $(which puppet) ]; then
239
- #{install_puppet_yum_repo}
240
- fi
241
- #{install_eyaml}
242
- #{install_eyaml_gpg}
243
- #{install_deep_merge}
244
- #{install_busser}
245
- #{custom_install_command}
246
- INSTALL
247
- when /^windows.*/
248
- info("Installing puppet on #{puppet_platform}")
249
- info('Powershell is not recognised by core test-kitchen assuming it is present') unless powershell_shell?
250
- <<-INSTALL
251
- if(Get-Command puppet -ErrorAction 0) { return; }
252
- $architecture = if( [Environment]::Is64BitOperatingSystem ) { '-x64' } else { '' }
253
- if( '#{puppet_windows_version}' -eq 'latest' ) {
254
- $MsiUrl = "https://downloads.puppetlabs.com/windows/puppet-agent-${architecture}-latest.msi"
255
- } elseif( '#{puppet_windows_version}' -like '5.*' ) {
256
- $MsiUrl = "https://downloads.puppetlabs.com/windows/puppet5/puppet-agent-#{puppet_windows_version}-${architecture}.msi"
257
- } else {
258
- $MsiUrl = "https://downloads.puppetlabs.com/windows/puppet-#{puppet_windows_version}${architecture}.msi"
259
- }
260
- Invoke-WebRequest $MsiUrl -UseBasicParsing -OutFile "C:/puppet.msi" #{posh_proxy_parm}
261
- $process = Start-Process -FilePath msiexec.exe -Wait -PassThru -ArgumentList '/qn', '/norestart', '/i', 'C:\\puppet.msi'
262
- if ($process.ExitCode -ne 0) {
263
- Write-Host "Installer failed."
264
- Exit 1
265
- }
266
-
267
- #{install_busser}
268
- INSTALL
269
- else
270
- info('Installing puppet, will try to determine platform os')
271
- # need to add a CR to avoid trouble with proxy settings concatenation
272
- <<-INSTALL
273
-
274
- #{custom_pre_install_command}
275
- if [ ! $(which puppet) ]; then
276
- if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ]; then
277
- #{install_puppet_yum_repo}
278
- else
279
- if [ -f /etc/system-release ] || [ grep -q 'Amazon Linux' /etc/system-release ]; then
280
- #{install_puppet_yum_repo}
281
- else
282
- #{sudo('apt-get')} -y install wget
283
- #{sudo('wget')} #{wget_proxy_parm} #{puppet_apt_repo}
284
- #{sudo('dpkg')} -i #{puppet_apt_repo_file}
285
- #{update_packages_debian_cmd}
286
- #{sudo_env('apt-get')} -y install facter#{facter_debian_version}
287
- #{sudo_env('apt-get')} -y install puppet-common#{puppet_debian_version}
288
- #{sudo_env('apt-get')} -y install puppet#{puppet_debian_version}
289
- #{install_hiera}
290
- fi
291
- fi
292
- fi
293
- #{install_eyaml}
294
- #{install_eyaml_gpg}
295
- #{install_deep_merge}
296
- #{install_busser}
297
- #{custom_install_command}
298
- INSTALL
299
- end
300
- end
301
- end
302
- # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
303
-
304
- def install_command_collections
305
- case puppet_platform
306
- when 'debian', 'ubuntu'
307
- info("Installing Puppet Collections on #{puppet_platform}")
308
- <<-INSTALL
309
-
310
- #{Util.shell_helpers}
311
- #{custom_pre_install_command}
312
- if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
313
- if [ ! -f "#{config[:puppet_apt_collections_repo]}" ]; then
314
- #{sudo('apt-get')} -y install wget
315
- #{sudo('wget')} #{wget_proxy_parm} #{config[:puppet_apt_collections_repo]}
316
- fi
317
- #{sudo('dpkg')} -i #{puppet_apt_coll_repo_file}
318
- #{sudo('apt-get')} update
319
- #{sudo_env('apt-get')} -y install puppet-agent#{puppet_debian_version}
320
- fi
321
- #{install_eyaml("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
322
- #{install_eyaml_gpg("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
323
- #{install_deep_merge}
324
- #{install_busser}
325
- #{custom_install_command}
326
- INSTALL
327
- when 'redhat', 'centos', 'fedora', 'oracle', 'amazon'
328
- info("Installing Puppet Collections on #{puppet_platform}")
329
- <<-INSTALL
330
-
331
- #{Util.shell_helpers}
332
- #{custom_pre_install_command}
333
- if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
334
- echo "-----> #{sudo_env('yum')} -y install #{config[:puppet_yum_collections_repo]}"
335
- #{sudo_env('yum')} -y install #{config[:puppet_yum_collections_repo]}
336
- #{sudo_env('yum')} -y install puppet-agent#{puppet_redhat_version}
337
- fi
338
- #{install_eyaml("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
339
- #{install_eyaml_gpg("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
340
- #{install_deep_merge}
341
- #{install_busser}
342
- #{custom_install_command}
343
- INSTALL
344
- when /^windows.*/
345
- info("Installing Puppet Collections on #{puppet_platform}")
346
- info('Powershell is not recognised by core test-kitchen assuming it is present') unless powershell_shell?
347
- <<-INSTALL
348
- if(Get-Command puppet -ErrorAction 0) { return; }
349
- $architecture = if( [Environment]::Is64BitOperatingSystem ) { 'x64' } else { 'x86' }
350
- if( '#{puppet_windows_version}' -eq 'latest' ) {
351
- $MsiUrl = "https://downloads.puppetlabs.com/windows/puppet-agent-${architecture}-latest.msi"
352
- } elseif( '#{puppet_windows_version}' -like '5.*' ) {
353
- $MsiUrl = "https://downloads.puppetlabs.com/windows/puppet5/puppet-agent-#{puppet_windows_version}-${architecture}.msi"
354
- } else {
355
- $MsiUrl = "https://downloads.puppetlabs.com/windows/puppet-agent-#{puppet_windows_version}${architecture}.msi"
356
- }
357
- Invoke-WebRequest $MsiUrl -UseBasicParsing -OutFile "C:/puppet-agent.msi" #{posh_proxy_parm}
358
- $process = Start-Process -FilePath msiexec.exe -Wait -PassThru -ArgumentList '/qn', '/norestart', '/i', 'C:\\puppet-agent.msi'
359
- if ($process.ExitCode -ne 0) {
360
- Write-Host "Installer failed."
361
- Exit 1
362
- }
363
-
364
- #{install_busser}
365
- INSTALL
366
- else
367
- info('Installing Puppet Collections, will try to determine platform os')
368
- <<-INSTALL
369
-
370
- #{Util.shell_helpers}
371
- #{custom_pre_install_command}
372
- if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
373
- if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ] || \
374
- [ -f /etc/system-release ] || [ grep -q 'Amazon Linux' /etc/system-release ]; then
375
- echo "-----> #{sudo_env('yum')} -y install #{config[:puppet_yum_collections_repo]}"
376
- #{sudo_env('yum')} -y install #{config[:puppet_yum_collections_repo]}
377
- #{sudo_env('yum')} -y install puppet-agent#{puppet_redhat_version}
378
- else
379
- #{sudo('apt-get')} -y install wget
380
- #{sudo('wget')} #{wget_proxy_parm} #{config[:puppet_apt_collections_repo]}
381
- #{sudo('dpkg')} -i #{puppet_apt_coll_repo_file}
382
- #{sudo('apt-get')} update
383
- #{sudo_env('apt-get')} -y install puppet-agent#{puppet_debian_version}
384
- fi
385
- fi
386
- #{install_eyaml("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
387
- #{install_eyaml_gpg("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
388
- #{install_deep_merge}
389
- #{install_busser}
390
- #{custom_install_command}
391
- INSTALL
392
- end
393
- end
394
-
395
- def install_deep_merge
396
- return unless config[:hiera_deep_merge]
397
- <<-INSTALL
398
- # Support for hash merge lookups to recursively merge hash keys
399
- if [[ $(#{sudo('gem')} list deep_merge -i) == 'false' ]]; then
400
- echo '-----> Installing deep_merge to provide deep_merge of hiera hashes'
401
- #{sudo('gem')} install #{gem_proxy_parm} --no-ri --no-rdoc deep_merge
402
- fi
403
- INSTALL
404
- end
405
-
406
- def install_eyaml(gem_cmd = 'gem')
407
- return unless config[:hiera_eyaml]
408
- <<-INSTALL
409
- # A backend for Hiera that provides per-value asymmetric encryption of sensitive data
410
- if [[ $(#{sudo(gem_cmd)} list hiera-eyaml -i) == 'false' ]]; then
411
- echo '-----> Installing hiera-eyaml to provide encryption of hiera data'
412
- #{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc highline -v 1.6.21
413
- #{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc hiera-eyaml
414
- fi
415
- INSTALL
416
- end
417
-
418
- def install_eyaml_gpg(gem_cmd = 'gem')
419
- return unless config[:hiera_eyaml_gpg]
420
- <<-INSTALL
421
- # A backend for Hiera that provides per-value asymmetric encryption of sensitive data
422
- if [[ $(#{sudo(gem_cmd)} list hiera-eyaml-gpg -i) == 'false' ]]; then
423
- echo '-----> Installing hiera-eyaml-gpg to provide encryption of hiera data'
424
- #{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc highline -v 1.6.21
425
- #{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc hiera-eyaml
426
- #{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc hiera-eyaml-gpg
427
- #{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc ruby_gpg
428
- fi
429
- INSTALL
430
- end
431
-
432
- def install_busser
433
- return unless config[:require_chef_for_busser]
434
- info("Install busser on #{puppet_platform}")
435
- case puppet_platform
436
- when /^windows.*/
437
- # https://raw.githubusercontent.com/opscode/knife-windows/master/lib/chef/knife/bootstrap/windows-chef-client-msi.erb
438
- <<-INSTALL
439
- $webclient = New-Object System.Net.WebClient; $webclient.DownloadFile('https://opscode-omnibus-packages.s3.amazonaws.com/windows/2008r2/x86_64/chef-windows-11.12.8-1.windows.msi','chef-windows-11.12.8-1.windows.msi')
440
- msiexec /qn /i chef-windows-11.12.8-1.windows.msi
441
-
442
- cmd.exe /C "SET PATH=%PATH%;`"C:\\opscode\\chef\\embedded\\bin`";`"C:\\tmp\\busser\\gems\\bin`""
443
-
444
- INSTALL
445
- else
446
- <<-INSTALL
447
- #{Util.shell_helpers}
448
- # install chef omnibus so that busser works as this is needed to run tests :(
449
- # TODO: work out how to install enough ruby
450
- # and set busser: { :ruby_bindir => '/usr/bin/ruby' } so that we dont need the
451
- # whole chef client
452
- if [ ! -d "/opt/chef" ]
453
- then
454
- echo '-----> Installing Chef Omnibus to install busser to run tests'
455
- #{export_http_proxy_parm}
456
- #{export_https_proxy_parm}
457
- #{export_no_proxy_parm}
458
- do_download #{chef_url} /tmp/install.sh
459
- #{sudo('sh')} /tmp/install.sh
460
- fi
461
- INSTALL
462
- end
463
- end
464
-
465
- def install_omnibus_command
466
- info('Installing puppet using puppet omnibus')
467
-
468
- version = ''
469
- version = "-v #{config[:puppet_version]}" unless config[:puppet_version].nil?
470
-
471
- <<-INSTALL
472
- #{Util.shell_helpers}
473
- if [ ! $(which puppet) ]; then
474
- echo "-----> Installing Puppet Omnibus"
475
- #{export_http_proxy_parm}
476
- #{export_https_proxy_parm}
477
- #{export_no_proxy_parm}
478
- do_download #{config[:puppet_omnibus_url]} /tmp/install_puppet.sh
479
- #{sudo_env('sh')} /tmp/install_puppet.sh #{version}
480
- fi
481
- INSTALL
482
- end
483
-
484
- def install_hiera
485
- return unless config[:install_hiera]
486
- <<-INSTALL
487
- #{sudo_env('apt-get')} -y install #{hiera_package}
488
- INSTALL
489
- end
490
-
491
- def hiera_package
492
- "#{config[:hiera_package]}#{puppet_hiera_debian_version}"
493
- end
494
-
495
- # /bin/wget -P /etc/pki/rpm-gpg/ http://yum.puppetlabs.com/RPM-GPG-KEY-puppetlabs
496
- # changed to curl
497
-
498
- def install_puppet_yum_repo
499
- <<-INSTALL
500
- rhelversion=$(cat /etc/redhat-release | grep 'release 7')
501
- # For CentOS7/RHEL7 the rdo release contains puppetlabs repo, creating conflict. Create temp-repo
502
- #{sudo_env('curl')} -o /etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs http://yum.puppetlabs.com/RPM-GPG-KEY-puppetlabs
503
- if [ -n "$rhelversion" ]; then
504
- echo '[puppettemp-products]
505
- name=Puppet Labs Products - \$basearch
506
- baseurl=http://yum.puppetlabs.com/el/7/products/\$basearch
507
- gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
508
- enabled=0
509
- gpgcheck=1
510
- [puppettemp-deps]
511
- name=Puppet Labs Dependencies - \$basearch
512
- baseurl=http://yum.puppetlabs.com/el/7/dependencies/\$basearch
513
- gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
514
- enabled=0
515
- gpgcheck=1' | sudo tee /etc/yum.repos.d/puppettemp.repo > /dev/null
516
- sudo sed -i 's/^[ \t]*//' /etc/yum.repos.d/puppettemp.repo
517
- #{update_packages_redhat_cmd}
518
- #{sudo_env('yum')} -y --enablerepo=puppettemp-products --enablerepo=puppettemp-deps install puppet#{puppet_redhat_version}
519
- # Clean up temporary puppet repo
520
- sudo rm -rf /etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
521
- sudo rm -rf /etc/yum.repos.d/puppettemp.repo
522
- else
523
- #{sudo('rpm')} -ivh #{proxy_parm} #{puppet_yum_repo}
524
- #{update_packages_redhat_cmd}
525
- #{sudo_env('yum')} -y install puppet#{puppet_redhat_version}
526
- fi
527
- INSTALL
528
- end
529
-
530
- def custom_pre_install_command
531
- <<-INSTALL
532
- #{config[:custom_pre_install_command]}
533
- INSTALL
534
- end
535
-
536
- def custom_install_command
537
- <<-INSTALL
538
- #{config[:custom_install_command]}
539
- INSTALL
540
- end
541
-
542
- def init_command
543
- todelete = %w[modules manifests files hiera hiera.yaml hiera.global.yaml facter spec enc environment]
544
- .map { |dir| File.join(config[:root_path], dir) }
545
- todelete += [hiera_data_remote_path,
546
- '/etc/hiera.yaml',
547
- "#{puppet_dir}/hiera.yaml",
548
- spec_files_remote_path.to_s,
549
- "#{puppet_dir}/fileserver.conf"]
550
- todelete << File.join(puppet_dir, puppet_environment) if puppet_environment
551
- todelete << File.join(puppet_environmentpath_remote_path, puppet_environment) if puppet_environment_config && puppet_environment
552
- cmd = "#{sudo(rm_command_paths(todelete))};"
553
- cmd += " #{mkdir_command} #{config[:root_path]};"
554
- cmd += " #{sudo(mkdir_command)} #{puppet_dir}"
555
- debug(cmd)
556
- cmd
557
- end
558
-
559
- def create_sandbox
560
- super
561
- debug("Creating local sandbox in #{sandbox_path}")
562
- yield if block_given?
563
- prepare_modules
564
- prepare_manifests
565
- prepare_files
566
- prepare_facter_file
567
- prepare_facts
568
- prepare_puppet_config
569
- prepare_hiera_config
570
- prepare_puppet_environment
571
- prepare_fileserver_config
572
- prepare_hiera_data
573
- prepare_enc
574
- prepare_spec_files
575
- info('Finished Preparing files for transfer')
576
- end
577
-
578
- def cleanup_sandbox
579
- return if sandbox_path.nil?
580
- debug("Cleaning up local sandbox in #{sandbox_path}")
581
- FileUtils.rmtree(sandbox_path)
582
- return if remove_repo.nil?
583
- debug("Cleaning up remote sandbox: #{remove_repo}")
584
- instance.remote_exec remove_repo
585
- end
586
-
587
- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
588
- def prepare_command
589
- commands = []
590
- if puppet_git_init
591
- commands << [
592
- sudo('rm -rf'), '/etc/puppet'
593
- ].join(' ')
594
-
595
- commands << [
596
- sudo('git clone'), puppet_git_init, '/etc/puppet'
597
- ].join(' ')
598
- end
599
-
600
- if puppet_git_pr
601
- commands << [
602
- sudo('git'),
603
- '--git-dir=/etc/puppet/.git/',
604
- 'fetch -f',
605
- 'origin',
606
- "pull/#{puppet_git_pr}/head:pr_#{puppet_git_pr}"
607
- ].join(' ')
608
-
609
- commands << [
610
- sudo('git'),
611
- '--git-dir=/etc/puppet/.git/',
612
- '--work-tree=/etc/puppet/',
613
- 'checkout',
614
- "pr_#{puppet_git_pr}"
615
- ].join(' ')
616
- end
617
-
618
- if puppet_config
619
- commands << [
620
- sudo(cp_command),
621
- File.join(config[:root_path], 'puppet.conf'),
622
- puppet_dir
623
- ].join(' ')
624
- end
625
-
626
- if fileserver_config
627
- commands << [
628
- sudo(cp_command),
629
- File.join(config[:root_path], 'fileserver.conf'),
630
- puppet_dir
631
- ].join(' ')
632
- end
633
-
634
- if hiera_data
635
- commands << [
636
- sudo(mkdir_command), hiera_data_remote_path
637
- ].join(' ')
638
- commands << [
639
- sudo("#{cp_command} -r"), File.join(config[:root_path], 'hiera/*'), hiera_data_remote_path
640
- ].join(' ')
641
- end
642
-
643
- if hiera_eyaml
644
- commands << [
645
- sudo(mkdir_command), hiera_eyaml_key_remote_path
646
- ].join(' ')
647
- commands << [
648
- sudo("#{cp_command} -r"), File.join(config[:root_path], 'hiera_keys/*'), hiera_eyaml_key_remote_path
649
- ].join(' ')
650
- end
651
-
652
- if hiera_eyaml_gpg
653
- commands << [
654
- sudo('mkdir -p'), hiera_eyaml_gpg_remote_path
655
- ].join(' ')
656
- commands << [
657
- sudo('cp -r'), File.join(config[:root_path], hiera_eyaml_gpg_recipients), hiera_eyaml_gpg_remote_path
658
- ].join(' ')
659
- commands << [
660
- sudo('cp -r'), File.join(config[:root_path], hiera_eyaml_gpg_secring), hiera_eyaml_gpg_remote_path
661
- ].join(' ')
662
- commands << [
663
- sudo('cp -r'), File.join(config[:root_path], hiera_eyaml_gpg_pubring), hiera_eyaml_gpg_remote_path
664
- ].join(' ')
665
- end
666
-
667
- if puppet_environment
668
- commands << [
669
- sudo('ln -s '), config[:root_path], File.join(puppet_dir, puppet_environment)
670
- ].join(' ')
671
- end
672
-
673
- if puppet_environment_config && puppet_environment
674
- commands << [
675
- sudo(mkdir_command), puppet_environmentpath_remote_path
676
- ].join(' ')
677
- commands << [
678
- sudo(mkdir_command), File.join(puppet_environmentpath_remote_path, puppet_environment)
679
- ].join(' ')
680
- commands << [
681
- sudo('ln -s '), File.join(config[:root_path], 'modules'), File.join(puppet_environmentpath_remote_path, puppet_environment, puppet_environment_remote_modules_path)
682
- ].join(' ')
683
- commands << [
684
- sudo('ln -s '), File.join(config[:root_path], 'manifests'), File.join(puppet_environmentpath_remote_path, puppet_environment, puppet_environment_remote_manifests_path)
685
- ].join(' ')
686
- commands << [
687
- sudo('ln -s '), File.join(config[:root_path], 'hiera'), File.join(puppet_environmentpath_remote_path, puppet_environment, puppet_environment_remote_hieradata_path)
688
- ].join(' ')
689
- commands << [
690
- sudo('cp'), File.join(config[:root_path], 'environment', 'environment.conf'), File.join(puppet_environmentpath_remote_path, puppet_environment, 'environment.conf')
691
- ].join(' ')
692
- commands << [
693
- sudo('cp'), File.join(config[:root_path], 'environment', 'hiera.yaml'), File.join(puppet_environmentpath_remote_path, puppet_environment, 'hiera.yaml')
694
- ].join(' ')
695
- end
696
-
697
- if spec_files_path && spec_files_remote_path
698
- commands << [
699
- sudo(mkdir_command), spec_files_remote_path
700
- ].join(' ')
701
- commands << [
702
- sudo("#{cp_command} -r"), File.join(config[:root_path], 'spec/*'), spec_files_remote_path
703
- ].join(' ')
704
- end
705
-
706
- if config[:puppet_enc]
707
- commands << [
708
- sudo('chmod 755'), File.join("#{config[:root_path]}/enc", File.basename(config[:puppet_enc]))
709
- ].join(' ')
710
- end
711
-
712
- command = powershell? ? commands.join('; ') : commands.join(' && ')
713
- debug(command)
714
- command
715
- end
716
- # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
717
-
718
- def run_command
719
- return config[:puppet_apply_command] unless config[:puppet_apply_command].nil?
720
- result = [
721
- facterlib,
722
- custom_facts,
723
- puppet_manifestdir,
724
- puppet_cmd,
725
- 'apply',
726
- File.join(config[:root_path], 'manifests', manifest),
727
- "--modulepath=#{File.join(config[:root_path], 'modules')}",
728
- "--fileserverconfig=#{File.join(config[:root_path], 'fileserver.conf')}",
729
- custom_options,
730
- puppet_environment_flag,
731
- puppet_noop_flag,
732
- puppet_enc_flag,
733
- puppet_hiera_flag,
734
- puppet_detailed_exitcodes_flag,
735
- puppet_verbose_flag,
736
- puppet_debug_flag,
737
- puppet_logdest_flag,
738
- puppet_future_parser_flag,
739
- puppet_show_diff_flag,
740
- puppet_whitelist_exit_code
741
- ].join(' ')
742
- if config[:custom_post_apply_command]
743
- custom_post_apply_trap = <<-TRAP
744
- function custom_post_apply_command {
745
- #{config[:custom_post_apply_command]}
746
- }
747
- trap custom_post_apply_command EXIT
748
- TRAP
749
- end
750
- result = <<-RUN
751
- #{config[:custom_pre_apply_command]}
752
- #{custom_post_apply_trap}
753
- #{result}
754
- RUN
755
- info("Going to invoke puppet apply with: #{result}")
756
- result
757
- end
758
-
759
- protected
760
-
761
- def load_needed_dependencies!
762
- return unless File.exist?(puppetfile)
763
- return unless config[:resolve_with_librarian_puppet] || config[:resolve_with_r10k]
764
- if config[:resolve_with_librarian_puppet]
765
- require 'kitchen/provisioner/puppet/librarian'
766
- debug("Puppetfile found at #{puppetfile}, loading Librarian-Puppet")
767
- Puppet::Librarian.load!(logger)
768
- elsif config[:resolve_with_r10k]
769
- require 'kitchen/provisioner/puppet/r10k'
770
- debug("Puppetfile found at #{puppetfile}, loading R10K")
771
- Puppet::R10K.load!(logger)
772
- end
773
- end
774
-
775
- def tmpmodules_dir
776
- File.join(sandbox_path, 'modules')
777
- end
778
-
779
- def puppetfile
780
- config[:puppetfile_path] || ''
781
- end
782
-
783
- def modulefile
784
- config[:modulefile_path] || ''
785
- end
786
-
787
- def metadata_json
788
- config[:metadata_json_path] || ''
789
- end
790
-
791
- def manifest
792
- config[:manifest]
793
- end
794
-
795
- def manifests
796
- config[:manifests_path]
797
- end
798
-
799
- def modules
800
- config[:modules_path]
801
- end
802
-
803
- def files
804
- config[:files_path] || 'files'
805
- end
806
-
807
- def puppet_config
808
- config[:puppet_config_path]
809
- end
810
-
811
- def puppet_environment
812
- config[:puppet_environment]
813
- end
814
-
815
- def puppet_environment_config
816
- if config[:puppet_environment_config_path] && !puppet_environment
817
- raise("ERROR: found environment config '#{config[:puppet_environment_config_path]}', however no 'puppet_environment' is specified. Please specify 'puppet_environment' or unset 'puppet_environment_config_path' in .kitchen.yml")
818
- end
819
- config[:puppet_environment_config_path]
820
- end
821
-
822
- def puppet_environment_remote_modules_path
823
- config[:puppet_environment_remote_modules_path]
824
- end
825
-
826
- def puppet_environment_remote_manifests_path
827
- config[:puppet_environment_remote_manifests_path]
828
- end
829
-
830
- def puppet_environment_remote_hieradata_path
831
- config[:puppet_environment_remote_hieradata_path]
832
- end
833
-
834
- def puppet_git_init
835
- config[:puppet_git_init]
836
- end
837
-
838
- def puppet_git_pr
839
- config[:puppet_git_pr]
840
- end
841
-
842
- def hiera_config
843
- config[:hiera_config_path]
844
- end
845
-
846
- def puppet_environment_hiera_config
847
- config[:puppet_environment_hiera_config_path]
848
- end
849
-
850
- def fileserver_config
851
- config[:fileserver_config_path]
852
- end
853
-
854
- def hiera_data
855
- config[:hiera_data_path]
856
- end
857
-
858
- def hiera_data_remote_path
859
- return config[:hiera_data_remote_path] if config[:hiera_data_remote_path]
860
-
861
- if config[:require_puppet_collections]
862
- powershell? ? 'C:/ProgramData/PuppetLabs/code/environments/production/hieradata' : '/etc/puppetlabs/code/environments/production/hieradata'
863
- else
864
- powershell? ? 'C:/ProgramData/PuppetLabs/hiera/var' : '/var/lib/hiera'
865
- end
866
- end
867
-
868
- def hiera_writer
869
- config[:hiera_writer_files]
870
- end
871
-
872
- def hiera_eyaml
873
- config[:hiera_eyaml]
874
- end
875
-
876
- def hiera_eyaml_gpg
877
- config[:hiera_eyaml_gpg]
878
- end
879
-
880
- def hiera_eyaml_gpg_recipients
881
- config[:hiera_eyaml_gpg_recipients]
882
- end
883
-
884
- def hiera_eyaml_gpg_secring
885
- config[:hiera_eyaml_gpg_secring]
886
- end
887
-
888
- def hiera_eyaml_gpg_pubring
889
- config[:hiera_eyaml_gpg_pubring]
890
- end
891
-
892
- def hiera_eyaml_gpg_remote_path
893
- config[:hiera_eyaml_gpg_remote_path]
894
- end
895
-
896
- def hiera_eyaml_key_path
897
- config[:hiera_eyaml_key_path]
898
- end
899
-
900
- def hiera_eyaml_key_remote_path
901
- config[:hiera_eyaml_key_remote_path]
902
- end
903
-
904
- def hiera_deep_merge
905
- config[:hiera_deep_merge]
906
- end
907
-
908
- def librarian_puppet_ssl_file
909
- config[:librarian_puppet_ssl_file]
910
- end
911
-
912
- def r10k_ssl_file
913
- config[:r10k_puppet_ssl_file] || config[:librarian_puppet_ssl_file]
914
- end
915
-
916
- def puppet_cmd
917
- return '& "C:\Program Files\Puppet Labs\Puppet\bin\puppet"' if powershell?
918
-
919
- puppet_bin = config[:require_puppet_collections] ? "#{config[:puppet_coll_remote_path]}/bin/puppet" : config[:puppet_binary]
920
-
921
- if config[:puppet_no_sudo]
922
- puppet_bin
923
- else
924
- sudo_env(puppet_bin)
925
- end
926
- end
927
-
928
- def puppet_dir
929
- return 'C:/ProgramData/PuppetLabs/puppet/etc' if powershell?
930
- config[:require_puppet_collections] ? '/etc/puppetlabs/puppet' : '/etc/puppet'
931
- end
932
-
933
- def puppet_environmentpath_remote_path
934
- return config[:puppet_environmentpath_remote_path] if config[:puppet_environmentpath_remote_path]
935
- if config[:puppet_version] =~ /^3/
936
- powershell? ? 'C:/ProgramData/PuppetLabs/puppet/etc' : '/etc/puppet/environments'
937
- else
938
- powershell? ? 'C:/ProgramData/PuppetLabs/code/environments' : '/etc/puppetlabs/code/environments'
939
- end
940
- end
941
-
942
- def hiera_config_dir
943
- return 'C:/ProgramData/PuppetLabs/puppet/etc' if powershell?
944
- config[:require_puppet_collections] ? '/etc/puppetlabs/code' : '/etc/puppet'
945
- end
946
-
947
- def puppet_debian_version
948
- config[:puppet_version] ? "=#{config[:puppet_version]}" : nil
949
- end
950
-
951
- def facter_debian_version
952
- config[:facter_version] ? "=#{config[:facter_version]}" : nil
953
- end
954
-
955
- def puppet_hiera_debian_version
956
- config[:hiera_version] ? "=#{config[:hiera_version]}" : nil
957
- end
958
-
959
- def puppet_redhat_version
960
- if puppet_platform == 'amazon'
961
- config[:puppet_version]
962
- else
963
- config[:puppet_version] ? "-#{config[:puppet_version]}" : nil
964
- end
965
- end
966
-
967
- def puppet_windows_version
968
- config[:puppet_version] ? config[:puppet_version].to_s : 'latest'
969
- end
970
-
971
- def puppet_environment_flag
972
- if config[:puppet_version] =~ /^2/
973
- config[:puppet_environment] ? "--environment=#{puppet_environment}" : nil
974
- else
975
- config[:puppet_environment] ? "--environment=#{puppet_environment} --environmentpath=#{puppet_environmentpath_remote_path}" : nil
976
- end
977
- end
978
-
979
- def puppet_manifestdir
980
- return nil if config[:require_puppet_collections]
981
- return nil if config[:puppet_environment]
982
- return nil if powershell?
983
- bash_vars = "export MANIFESTDIR='#{File.join(config[:root_path], 'manifests')}';"
984
- debug(bash_vars)
985
- bash_vars
986
- end
987
-
988
- def custom_options
989
- config[:custom_options] || ''
990
- end
991
-
992
- def puppet_noop_flag
993
- config[:puppet_noop] ? '--noop' : nil
994
- end
995
-
996
- def puppet_debug_flag
997
- config[:puppet_debug] ? '-d' : nil
998
- end
999
-
1000
- def puppet_verbose_flag
1001
- config[:puppet_verbose] ? '-v' : nil
1002
- end
1003
-
1004
- def puppet_show_diff_flag
1005
- config[:puppet_show_diff] ? '--show_diff' : nil
1006
- end
1007
-
1008
- def puppet_future_parser_flag
1009
- config[:puppet_future_parser] ? '--parser=future' : nil
1010
- end
1011
-
1012
- def puppet_logdest_flag
1013
- return nil unless config[:puppet_logdest]
1014
- destinations = ''
1015
- config[:puppet_logdest].each do |dest|
1016
- destinations << "--logdest #{dest} "
1017
- end
1018
- destinations
1019
- end
1020
-
1021
- def puppet_platform
1022
- config[:platform].gsub(/-.*/, '')
1023
- end
1024
-
1025
- def update_packages_debian_cmd
1026
- config[:update_package_repos] ? "#{sudo_env('apt-get')} update" : nil
1027
- end
1028
-
1029
- def update_packages_redhat_cmd
1030
- # #{sudo('yum')}
1031
- config[:update_package_repos] ? "#{sudo_env('yum')} makecache" : nil
1032
- end
1033
-
1034
- def sudo_env(pm)
1035
- s = https_proxy ? "https_proxy=#{https_proxy}" : nil
1036
- p = http_proxy ? "http_proxy=#{http_proxy}" : nil
1037
- n = no_proxy ? "no_proxy=#{no_proxy}" : nil
1038
- p || s ? "#{sudo('env')} #{p} #{s} #{n} #{pm}" : sudo(pm).to_s
1039
- end
1040
-
1041
- def remove_puppet_repo
1042
- config[:remove_puppet_repo]
1043
- end
1044
-
1045
- def spec_files_path
1046
- config[:spec_files_path]
1047
- end
1048
-
1049
- def spec_files_remote_path
1050
- config[:spec_files_remote_path]
1051
- end
1052
-
1053
- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
1054
- def facterlib
1055
- factpath = nil
1056
- factpath = File.join(config[:root_path], 'facter').to_s if config[:install_custom_facts] && config[:custom_facts].any?
1057
- factpath = File.join(config[:root_path], 'facter').to_s if config[:facter_file]
1058
- factpath = "#{factpath}:" if config[:facterlib] && !factpath.nil?
1059
- factpath = "#{factpath}#{config[:facterlib]}" if config[:facterlib]
1060
- return nil if factpath.nil?
1061
- bash_vars = "export FACTERLIB='#{factpath}';"
1062
- debug(bash_vars)
1063
- bash_vars
1064
- end
1065
- # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
1066
-
1067
- def custom_facts
1068
- return nil if config[:custom_facts].none?
1069
- return nil if config[:install_custom_facts]
1070
- if powershell?
1071
- environment_vars = config[:custom_facts].map { |k, v| "$env:FACTER_#{k}='#{v}'" }.join('; ')
1072
- environment_vars = "#{environment_vars};"
1073
- else
1074
- environment_vars = config[:custom_facts].map { |k, v| "FACTER_#{k}=#{v}" }.join(' ')
1075
- environment_vars = "export #{environment_vars};"
1076
- end
1077
- debug(environment_vars)
1078
- environment_vars
1079
- end
1080
-
1081
- def puppet_enc_flag
1082
- config[:puppet_enc] ? "--node_terminus=exec --external_nodes=#{config[:root_path]}/enc/#{File.basename(config[:puppet_enc])}" : nil
1083
- end
1084
-
1085
- def puppet_hiera_flag
1086
- hiera_config ? "--hiera_config=#{config[:root_path]}/hiera.global.yaml" : nil
1087
- end
1088
-
1089
- def puppet_detailed_exitcodes_flag
1090
- config[:puppet_detailed_exitcodes] ? '--detailed-exitcodes' : nil
1091
- end
1092
-
1093
- def remove_repo
1094
- remove_puppet_repo ? "#{sudo('rm')} -rf /tmp/kitchen #{hiera_data_remote_path} #{hiera_eyaml_key_remote_path} #{puppet_dir}/* " : nil
1095
- end
1096
-
1097
- def puppet_whitelist_exit_code
1098
- if config[:puppet_whitelist_exit_code].nil?
1099
- powershell? ? '; exit $LASTEXITCODE' : nil
1100
- elsif powershell?
1101
- "; if(@(#{[config[:puppet_whitelist_exit_code]].join(', ')}) -contains $LASTEXITCODE) {exit 0} else {exit $LASTEXITCODE}"
1102
- else
1103
- '; RC=$?; [ ' + [config[:puppet_whitelist_exit_code]].flatten.map { |x| "\$RC -eq #{x}" }.join(' -o ') + ' ] && exit 0; exit $RC'
1104
- end
1105
- end
1106
-
1107
- # rubocop:disable Metrics/CyclomaticComplexity
1108
- def puppet_apt_repo
1109
- platform_version = config[:platform].partition('-')[2]
1110
- case puppet_platform
1111
- when 'ubuntu'
1112
- case platform_version
1113
- when '14.10'
1114
- # Utopic Repo
1115
- 'https://apt.puppetlabs.com/puppetlabs-release-utopic.deb'
1116
- when '14.04'
1117
- # Trusty Repo
1118
- 'https://apt.puppetlabs.com/puppetlabs-release-trusty.deb'
1119
- when '12.04'
1120
- # Precise Repo
1121
- 'https://apt.puppetlabs.com/puppetlabs-release-precise.deb'
1122
- else
1123
- # Configured Repo
1124
- config[:puppet_apt_repo]
1125
- end
1126
- when 'debian'
1127
- case platform_version.gsub(/\..*/, '')
1128
- when '8'
1129
- # Debian Jessie
1130
- 'https://apt.puppetlabs.com/puppetlabs-release-jessie.deb'
1131
- when '7'
1132
- # Debian Wheezy
1133
- 'https://apt.puppetlabs.com/puppetlabs-release-wheezy.deb'
1134
- when '6'
1135
- # Debian Squeeze
1136
- 'https://apt.puppetlabs.com/puppetlabs-release-squeeze.deb'
1137
- else
1138
- # Configured Repo
1139
- config[:puppet_apt_repo]
1140
- end
1141
- else
1142
- debug("Apt repo detection failed with platform - #{config[:platform]}")
1143
- false
1144
- end
1145
- end
1146
- # rubocop:enable Metrics/CyclomaticComplexity
1147
-
1148
- def puppet_apt_repo_file
1149
- puppet_apt_repo.split('/').last if puppet_apt_repo
1150
- end
1151
-
1152
- def puppet_apt_coll_repo_file
1153
- config[:puppet_apt_collections_repo].split('/').last
1154
- end
1155
-
1156
- def puppet_yum_repo
1157
- config[:puppet_yum_repo]
1158
- end
1159
-
1160
- def proxy_parm
1161
- http_proxy ? "--httpproxy #{URI.parse(http_proxy).host.downcase} --httpport #{URI.parse(http_proxy).port} " : nil
1162
- end
1163
-
1164
- def gem_proxy_parm
1165
- p = http_proxy ? "--http-proxy #{http_proxy}" : nil
1166
- n = no_proxy ? "--no-http-proxy #{no_proxy}" : nil
1167
- p || n ? "#{p} #{n}" : nil
1168
- end
1169
-
1170
- def wget_proxy_parm
1171
- p = http_proxy ? "-e http_proxy=#{http_proxy}" : nil
1172
- s = https_proxy ? "-e https_proxy=#{https_proxy}" : nil
1173
- n = no_proxy ? "-e no_proxy=#{no_proxy}" : nil
1174
- p || s ? "-e use_proxy=yes #{p} #{s} #{n}" : nil
1175
- end
1176
-
1177
- def posh_proxy_parm
1178
- http_proxy ? "-Proxy #{http_proxy}" : nil
1179
- end
1180
-
1181
- def powershell?
1182
- return true if powershell_shell?
1183
- return true if puppet_platform =~ /^windows.*/
1184
- false
1185
- end
1186
-
1187
- def export_http_proxy_parm
1188
- http_proxy ? "export http_proxy=#{http_proxy}" : nil
1189
- end
1190
-
1191
- def export_https_proxy_parm
1192
- https_proxy ? "export https_proxy=#{https_proxy}" : nil
1193
- end
1194
-
1195
- def export_no_proxy_parm
1196
- no_proxy ? "export no_proxy=#{no_proxy}" : nil
1197
- end
1198
-
1199
- def http_proxy
1200
- config[:http_proxy]
1201
- end
1202
-
1203
- def https_proxy
1204
- config[:https_proxy]
1205
- end
1206
-
1207
- def no_proxy
1208
- config[:no_proxy]
1209
- end
1210
-
1211
- def chef_url
1212
- config[:chef_bootstrap_url]
1213
- end
1214
-
1215
- def prepare_manifests
1216
- info('Preparing manifests')
1217
- debug("Using manifests from #{manifests}")
1218
-
1219
- tmp_manifests_dir = File.join(sandbox_path, 'manifests')
1220
- FileUtils.mkdir_p(tmp_manifests_dir)
1221
- FileUtils.cp_r(Dir.glob("#{manifests}/*"), tmp_manifests_dir)
1222
- end
1223
-
1224
- def prepare_files
1225
- info('Preparing files')
1226
- unless File.directory?(files)
1227
- info 'nothing to do for files'
1228
- return
1229
- end
1230
-
1231
- debug("Using files from #{files}")
1232
-
1233
- tmp_files_dir = File.join(sandbox_path, 'files')
1234
- FileUtils.mkdir_p(tmp_files_dir)
1235
- FileUtils.cp_r(Dir.glob("#{files}/*"), tmp_files_dir)
1236
- end
1237
-
1238
- def prepare_facter_file
1239
- return unless config[:facter_file]
1240
- info 'Copying facter file'
1241
- facter_dir = File.join(sandbox_path, 'facter')
1242
- FileUtils.mkdir_p(facter_dir)
1243
- FileUtils.cp_r(config[:facter_file], facter_dir)
1244
- end
1245
-
1246
- def prepare_facts
1247
- return unless config[:install_custom_facts]
1248
- return unless config[:custom_facts]
1249
- info 'Installing custom facts'
1250
- facter_dir = File.join(sandbox_path, 'facter')
1251
- FileUtils.mkdir_p(facter_dir)
1252
- tmp_facter_file = File.join(facter_dir, 'kitchen.rb')
1253
- facter_facts = Hash[config[:custom_facts]]
1254
- File.open(tmp_facter_file, 'a') do |out|
1255
- facter_facts.each do |k, v|
1256
- out.write "\nFacter.add(:#{k}) do\n"
1257
- out.write " setcode do\n"
1258
- if [Array, Hash].include? v.class
1259
- out.write " #{v}\n"
1260
- else
1261
- out.write " \"#{v}\"\n"
1262
- end
1263
- out.write " end\n"
1264
- out.write "end\n"
1265
- end
1266
- end
1267
- end
1268
-
1269
- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
1270
- def prepare_modules
1271
- info('Preparing modules')
1272
-
1273
- FileUtils.mkdir_p(tmpmodules_dir)
1274
- resolve_with_librarian if File.exist?(puppetfile) && config[:resolve_with_librarian_puppet]
1275
- resolve_with_r10k if File.exist?(puppetfile) && config[:resolve_with_r10k] && !config[:resolve_with_librarian_puppet]
1276
- modules_to_copy = {}
1277
-
1278
- # If root dir (.) is a module, add it for copying
1279
- self_name = read_self_module_name
1280
- modules_to_copy[self_name] = '.' if self_name
1281
-
1282
- if modules
1283
- modules_array = modules.split(':')
1284
- modules_array.each do |m_path|
1285
- Dir.glob("#{m_path}/*").each do |m|
1286
- name = File.basename(m)
1287
- if modules_to_copy.include? name
1288
- debug("Found duplicated module: #{name}. The path taking precedence: '#{modules_to_copy[name]}', ignoring '#{m}'")
1289
- else
1290
- modules_to_copy[name] = m
1291
- end
1292
- end
1293
- end
1294
- end
1295
-
1296
- if modules_to_copy.empty?
1297
- info 'Nothing to do for modules'
1298
- else
1299
- copy_modules(modules_to_copy, tmpmodules_dir)
1300
- end
1301
- end
1302
- # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
1303
-
1304
- def copy_modules(modules, destination)
1305
- excluded_paths = %w[modules pkg] + config[:ignored_paths_from_root]
1306
- debug("Copying modules to directory: #{destination}")
1307
- modules.each do |name, source|
1308
- next unless File.directory?(source)
1309
- debug("Copying module #{name} from #{source}...")
1310
- target = "#{destination}/#{name}"
1311
- FileUtils.mkdir_p(target) unless File.exist? target
1312
- FileUtils.cp_r(
1313
- Dir.glob("#{source}/*").reject { |entry| entry =~ /#{excluded_paths.join('$|')}$/ },
1314
- target,
1315
- remove_destination: true
1316
- )
1317
- end
1318
- end
1319
-
1320
- def read_self_module_name
1321
- if File.exist?(modulefile)
1322
- warn('Modulefile found but this is deprecated, ignoring it, see https://tickets.puppetlabs.com/browse/PUP-1188')
1323
- end
1324
-
1325
- return unless File.exist?(metadata_json)
1326
- module_name = nil
1327
- begin
1328
- module_name = JSON.parse(IO.read(metadata_json))['name'].split('-').last
1329
- rescue JSON::ParserError
1330
- error("not able to load or parse #{metadata_json} for the name of the module")
1331
- end
1332
-
1333
- module_name
1334
- end
1335
-
1336
- def prepare_puppet_config
1337
- return unless puppet_config
1338
-
1339
- info('Preparing puppet.conf')
1340
- debug("Using puppet config from #{puppet_config}")
1341
-
1342
- FileUtils.cp_r(puppet_config, File.join(sandbox_path, 'puppet.conf'))
1343
- end
1344
-
1345
- def prepare_enc
1346
- return unless config[:puppet_enc]
1347
- info 'Copying enc file'
1348
- enc_dir = File.join(sandbox_path, 'enc')
1349
- FileUtils.mkdir_p(enc_dir)
1350
- FileUtils.cp_r(config[:puppet_enc], File.join(enc_dir, '/'))
1351
- end
1352
-
1353
- def prepare_puppet_environment
1354
- return unless puppet_environment_config
1355
-
1356
- info('Preparing Environment Config')
1357
- environment_dir = File.join(sandbox_path, 'environment')
1358
- FileUtils.mkdir_p(environment_dir)
1359
- debug("Using Environment Config environment.conf from #{puppet_environment_config}")
1360
- FileUtils.cp_r(puppet_environment_config, File.join(environment_dir, 'environment.conf'))
1361
- if puppet_environment_hiera_config
1362
- debug("Using Environment Hiera Config hiera.yaml from #{puppet_environment_hiera_config}")
1363
- FileUtils.cp_r(puppet_environment_hiera_config, File.join(environment_dir, 'hiera.yaml'))
1364
- else
1365
- info('No Environment hiera.yaml found')
1366
- end
1367
- end
1368
-
1369
- def prepare_hiera_config
1370
- return unless hiera_config
1371
-
1372
- info('Preparing hiera (global layer)')
1373
- debug("Using hiera from #{hiera_config}")
1374
-
1375
- FileUtils.cp_r(hiera_config, File.join(sandbox_path, 'hiera.global.yaml'))
1376
- end
1377
-
1378
- def prepare_fileserver_config
1379
- return unless fileserver_config
1380
-
1381
- info('Preparing fileserver')
1382
- debug("Using fileserver config from #{fileserver_config}")
1383
-
1384
- FileUtils.cp_r(fileserver_config, File.join(sandbox_path, 'fileserver.conf'))
1385
- end
1386
-
1387
- def prepare_hiera_data
1388
- return unless hiera_data
1389
- info('Preparing hiera data')
1390
- tmp_hiera_dir = File.join(sandbox_path, 'hiera')
1391
- debug("Copying hiera data from #{hiera_data} to #{tmp_hiera_dir}")
1392
- FileUtils.mkdir_p(tmp_hiera_dir)
1393
- FileUtils.cp_r(Dir.glob("#{hiera_data}/*"), tmp_hiera_dir)
1394
- if hiera_writer
1395
- hiera_writer.each do |file|
1396
- file.each do |filename, hiera_hash|
1397
- debug("Creating hiera yaml file #{tmp_hiera_dir}/#{filename}")
1398
- dir = File.join(tmp_hiera_dir, File.dirname(filename.to_s))
1399
- FileUtils.mkdir_p(dir)
1400
- output_file = open(File.join(dir, File.basename(filename.to_s)), 'w')
1401
- # convert json and back before converting to yaml to recursively convert symbols to strings, heh
1402
- output_file.write JSON[hiera_hash.to_json].to_yaml
1403
- output_file.close
1404
- end
1405
- end
1406
- end
1407
- return unless hiera_eyaml_key_path
1408
- tmp_hiera_key_dir = File.join(sandbox_path, 'hiera_keys')
1409
- debug("Copying hiera eyaml keys from #{hiera_eyaml_key_path} to #{tmp_hiera_key_dir}")
1410
- FileUtils.mkdir_p(tmp_hiera_key_dir)
1411
- FileUtils.cp_r(Dir.glob("#{hiera_eyaml_key_path}/*"), tmp_hiera_key_dir)
1412
- end
1413
-
1414
- def prepare_spec_files
1415
- return unless spec_files_path
1416
- info('Preparing spec files')
1417
- tmp_spec_dir = File.join(sandbox_path, 'spec')
1418
- debug("Copying specs from #{spec_files_path} to #{tmp_spec_dir}")
1419
- FileUtils.mkdir_p(tmp_spec_dir)
1420
- FileUtils.cp_r(Dir.glob(File.join(spec_files_path, '*')).reject { |entry| entry =~ /fixtures$/ }, tmp_spec_dir) if config[:ignore_spec_fixtures]
1421
- FileUtils.cp_r(Dir.glob("#{spec_files_path}/*"), tmp_spec_dir) unless config[:ignore_spec_fixtures]
1422
- end
1423
-
1424
- def resolve_with_librarian
1425
- require 'kitchen/provisioner/puppet/librarian'
1426
- Kitchen.mutex.synchronize do
1427
- ENV['SSL_CERT_FILE'] = librarian_puppet_ssl_file if librarian_puppet_ssl_file
1428
- Puppet::Librarian.new(puppetfile, tmpmodules_dir, logger).resolve
1429
- ENV['SSL_CERT_FILE'] = '' if librarian_puppet_ssl_file
1430
- end
1431
- end
1432
-
1433
- def resolve_with_r10k
1434
- require 'kitchen/provisioner/puppet/r10k'
1435
- Kitchen.mutex.synchronize do
1436
- ENV['SSL_CERT_FILE'] = r10k_ssl_file if r10k_ssl_file
1437
- Puppet::R10K.new(puppetfile, tmpmodules_dir, logger).resolve
1438
- ENV['SSL_CERT_FILE'] = '' if r10k_ssl_file
1439
- end
1440
- end
1441
-
1442
- def cp_command
1443
- return 'cp -force' if powershell?
1444
- 'cp'
1445
- end
1446
-
1447
- def rm_command
1448
- return 'rm -force -recurse' if powershell?
1449
- 'rm -rf'
1450
- end
1451
-
1452
- def mkdir_command
1453
- return 'mkdir -force -path' if powershell?
1454
- 'mkdir -p'
1455
- end
1456
-
1457
- def rm_command_paths(paths)
1458
- return :nil if paths.length.zero?
1459
- return "#{rm_command} \"#{paths.join('", "')}\"" if powershell?
1460
- "#{rm_command} #{paths.join(' ')}"
1461
- end
1462
- end
1463
- end
1464
- end
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ #
4
+ # Author:: Chris Lundquist (<chris.lundquist@github.com>) Neill Turner (<neillwturner@gmail.com>)
5
+ #
6
+ # Copyright (C) 2013,2014 Chris Lundquist, Neill Turner
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+ #
20
+ # See https://github.com/neillturner/kitchen-puppet/blob/master/provisioner_options.md
21
+ # for documentation configuration parameters with puppet_apply provisioner.
22
+ #
23
+
24
+ require 'uri'
25
+ require 'json'
26
+ require 'kitchen'
27
+
28
+ module Kitchen
29
+ class Busser
30
+ def non_suite_dirs
31
+ %w[data data_bags environments nodes roles puppet]
32
+ end
33
+ end
34
+
35
+ module Configurable
36
+ def platform_name
37
+ instance.platform.name
38
+ end
39
+ end
40
+
41
+ module Provisioner
42
+ #
43
+ # Puppet Apply provisioner.
44
+ #
45
+ class PuppetApply < Base
46
+ attr_accessor :tmp_dir
47
+
48
+ default_config :require_puppet_collections, true
49
+ default_config :puppet_yum_collections_repo, 'http://yum.puppetlabs.com/puppet5/puppet5-release-el-6.noarch.rpm'
50
+ default_config :puppet_apt_collections_repo, 'http://apt.puppetlabs.com/puppet5-release-wheezy.deb'
51
+ default_config :puppet_coll_remote_path, '/opt/puppetlabs'
52
+ default_config :puppet_version, nil
53
+ default_config :facter_version, nil
54
+ default_config :hiera_version, nil
55
+ default_config :install_hiera, false
56
+ default_config :hiera_package, 'hiera-puppet'
57
+ default_config :hiera_writer_files, nil
58
+ default_config :require_puppet_repo, true
59
+ default_config :require_chef_for_busser, true
60
+ default_config :resolve_with_librarian_puppet, true
61
+ default_config :resolve_with_r10k, false
62
+ default_config :puppet_environment, nil
63
+ default_config :puppet_environment_config_path do |provisioner|
64
+ provisioner.calculate_path('environment.conf')
65
+ end
66
+ default_config :puppet_environment_remote_modules_path, 'modules'
67
+ default_config :puppet_environment_remote_manifests_path, 'manifests'
68
+ default_config :puppet_environment_remote_hieradata_path, 'hieradata'
69
+ default_config :puppet_environment_hiera_config_path do |provisioner|
70
+ provisioner.calculate_path('hiera.yaml', :file)
71
+ end
72
+ default_config :puppet_apt_repo, 'http://apt.puppetlabs.com/puppetlabs-release-precise.deb'
73
+ default_config :puppet_yum_repo, 'https://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm'
74
+ default_config :chef_bootstrap_url, 'https://www.chef.io/chef/install.sh'
75
+ default_config :puppet_logdest, nil
76
+ default_config :custom_install_command, nil
77
+ default_config :custom_pre_install_command, nil
78
+ default_config :custom_pre_apply_command, nil
79
+ default_config :custom_post_apply_command, nil
80
+ default_config :puppet_whitelist_exit_code, nil
81
+ default_config :require_puppet_omnibus, false
82
+ default_config :puppet_omnibus_url, 'https://raw.githubusercontent.com/petems/puppet-install-shell/master/install_puppet_5_agent.sh'
83
+ default_config :puppet_enc, nil
84
+ default_config :ignore_spec_fixtures, false
85
+
86
+ default_config :puppet_apply_command, nil
87
+
88
+ default_config :puppet_git_init, nil
89
+ default_config :puppet_git_pr, nil
90
+
91
+ default_config :http_proxy, nil
92
+ default_config :https_proxy, nil
93
+ default_config :no_proxy, nil
94
+
95
+ default_config :ignored_paths_from_root, ['spec']
96
+ default_config :hiera_data_remote_path, nil
97
+ default_config :manifest, ''
98
+ default_config :puppet_binary, 'puppet'
99
+
100
+ default_config :manifests_path do |provisioner|
101
+ provisioner.calculate_path('manifests') ||
102
+ raise('No manifests_path detected. Please specify one in .kitchen.yml')
103
+ end
104
+
105
+ default_config :modules_path do |provisioner|
106
+ modules_path = provisioner.calculate_path('modules')
107
+ if modules_path.nil? && provisioner.calculate_path('Puppetfile', :file).nil?
108
+ raise('No modules_path detected. Please specify one in .kitchen.yml')
109
+ end
110
+ modules_path
111
+ end
112
+
113
+ default_config :files_path do |provisioner|
114
+ provisioner.calculate_path('files') || 'files'
115
+ end
116
+
117
+ default_config :hiera_data_path do |provisioner|
118
+ provisioner.calculate_path('hiera')
119
+ end
120
+
121
+ default_config :puppet_config_path do |provisioner|
122
+ provisioner.calculate_path('puppet.conf', :file)
123
+ end
124
+
125
+ default_config :hiera_config_path do |provisioner|
126
+ provisioner.calculate_path('hiera.global.yaml', :file) ||
127
+ provisioner.calculate_path('hiera.yaml', :file)
128
+ end
129
+
130
+ default_config :fileserver_config_path do |provisioner|
131
+ provisioner.calculate_path('fileserver.conf', :file)
132
+ end
133
+ default_config :puppetfile_path do |provisioner|
134
+ provisioner.calculate_path('Puppetfile', :file)
135
+ end
136
+
137
+ default_config :modulefile_path do |provisioner|
138
+ provisioner.calculate_path('Modulefile', :file)
139
+ end
140
+
141
+ default_config :metadata_json_path do |provisioner|
142
+ provisioner.calculate_path('metadata.json', :file)
143
+ end
144
+
145
+ default_config :manifests_path do |provisioner|
146
+ provisioner.calculate_path('manifests', :directory)
147
+ end
148
+
149
+ default_config :spec_files_path do |provisioner|
150
+ provisioner.calculate_path('spec', :directory)
151
+ end
152
+
153
+ default_config :spec_files_remote_path, '/etc/puppet/spec'
154
+
155
+ default_config :puppet_debug, false
156
+ default_config :puppet_verbose, false
157
+ default_config :puppet_noop, false
158
+ default_config :puppet_show_diff, false
159
+ default_config :puppet_future_parser, false
160
+ default_config :platform, &:platform_name
161
+ default_config :update_package_repos, true
162
+ default_config :remove_puppet_repo, false
163
+ default_config :install_custom_facts, false
164
+ default_config :custom_facts, {}
165
+ default_config :facterlib, nil
166
+ default_config :puppet_detailed_exitcodes, nil
167
+ default_config :facter_file, nil
168
+ default_config :librarian_puppet_ssl_file, nil
169
+ default_config :r10k_ssl_file, nil
170
+
171
+ default_config :hiera_eyaml, false
172
+ default_config :hiera_eyaml_key_remote_path, '/etc/puppet/secure/keys'
173
+ default_config :puppet_environmentpath_remote_path, nil
174
+
175
+ default_config :hiera_eyaml_gpg, false
176
+ default_config :hiera_eyaml_gpg_recipients, false
177
+ default_config :hiera_eyaml_gpg_secring, false
178
+ default_config :hiera_eyaml_gpg_pubring, false
179
+ default_config :hiera_eyaml_gpg_remote_path, '/home/vagrant/.gnupg'
180
+
181
+ default_config :hiera_eyaml_key_path do |provisioner|
182
+ provisioner.calculate_path('hiera_keys')
183
+ end
184
+
185
+ default_config :hiera_deep_merge, false
186
+ default_config :puppet_no_sudo, false
187
+
188
+ def calculate_path(path, type = :directory)
189
+ base = config[:test_base_path]
190
+ candidates = []
191
+ candidates << File.join(base, instance.suite.name, 'puppet', path)
192
+ candidates << File.join(base, instance.suite.name, path)
193
+ candidates << File.join(base, path)
194
+ candidates << File.join(Dir.pwd, path)
195
+
196
+ candidates.find do |c|
197
+ type == :directory ? File.directory?(c) : File.file?(c)
198
+ end
199
+ end
200
+
201
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
202
+ def install_command
203
+ return unless config[:require_puppet_collections] || config[:require_puppet_repo] || config[:require_puppet_omnibus]
204
+ if config[:require_puppet_omnibus]
205
+ install_omnibus_command
206
+ elsif config[:require_puppet_collections]
207
+ install_command_collections
208
+ else
209
+ case puppet_platform
210
+ when 'debian', 'ubuntu'
211
+ info("Installing puppet on #{config[:platform]}")
212
+ # need to add a CR to avoid trouble with proxy settings concatenation
213
+ <<-INSTALL
214
+
215
+ #{custom_pre_install_command}
216
+ if [ ! $(which puppet) ]; then
217
+ #{sudo('apt-get')} -y install wget
218
+ #{sudo('wget')} #{wget_proxy_parm} #{puppet_apt_repo}
219
+ #{sudo('dpkg')} -i #{puppet_apt_repo_file}
220
+ #{update_packages_debian_cmd}
221
+ #{sudo_env('apt-get')} -y install facter#{facter_debian_version}
222
+ #{sudo_env('apt-get')} -y install puppet-common#{puppet_debian_version}
223
+ #{sudo_env('apt-get')} -y install puppet#{puppet_debian_version}
224
+ #{install_hiera}
225
+ fi
226
+ #{install_eyaml}
227
+ #{install_eyaml_gpg}
228
+ #{install_deep_merge}
229
+ #{install_busser}
230
+ #{custom_install_command}
231
+ INSTALL
232
+ when 'redhat', 'centos', 'fedora', 'oracle', 'amazon'
233
+ info("Installing puppet from yum on #{puppet_platform}")
234
+ # need to add a CR to avoid trouble with proxy settings concatenation
235
+ <<-INSTALL
236
+
237
+ #{custom_pre_install_command}
238
+ if [ ! $(which puppet) ]; then
239
+ #{install_puppet_yum_repo}
240
+ fi
241
+ #{install_eyaml}
242
+ #{install_eyaml_gpg}
243
+ #{install_deep_merge}
244
+ #{install_busser}
245
+ #{custom_install_command}
246
+ INSTALL
247
+ when /^windows.*/
248
+ info("Installing puppet on #{puppet_platform}")
249
+ info('Powershell is not recognised by core test-kitchen assuming it is present') unless powershell_shell?
250
+ <<-INSTALL
251
+ if(Get-Command puppet -ErrorAction 0) { return; }
252
+ $architecture = if( [Environment]::Is64BitOperatingSystem ) { '-x64' } else { '' }
253
+ if( '#{puppet_windows_version}' -eq 'latest' ) {
254
+ $MsiUrl = "https://downloads.puppetlabs.com/windows/puppet-agent-${architecture}-latest.msi"
255
+ } elseif( '#{puppet_windows_version}' -like '5.*' ) {
256
+ $MsiUrl = "https://downloads.puppetlabs.com/windows/puppet5/puppet-agent-#{puppet_windows_version}-${architecture}.msi"
257
+ } else {
258
+ $MsiUrl = "https://downloads.puppetlabs.com/windows/puppet-#{puppet_windows_version}${architecture}.msi"
259
+ }
260
+ Invoke-WebRequest $MsiUrl -UseBasicParsing -OutFile "C:/puppet.msi" #{posh_proxy_parm}
261
+ $process = Start-Process -FilePath msiexec.exe -Wait -PassThru -ArgumentList '/qn', '/norestart', '/i', 'C:\\puppet.msi'
262
+ if ($process.ExitCode -ne 0) {
263
+ Write-Host "Installer failed."
264
+ Exit 1
265
+ }
266
+
267
+ #{install_busser}
268
+ INSTALL
269
+ else
270
+ info('Installing puppet, will try to determine platform os')
271
+ # need to add a CR to avoid trouble with proxy settings concatenation
272
+ <<-INSTALL
273
+
274
+ #{custom_pre_install_command}
275
+ if [ ! $(which puppet) ]; then
276
+ if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ]; then
277
+ #{install_puppet_yum_repo}
278
+ else
279
+ if [ -f /etc/system-release ] || [ grep -q 'Amazon Linux' /etc/system-release ]; then
280
+ #{install_puppet_yum_repo}
281
+ else
282
+ #{sudo('apt-get')} -y install wget
283
+ #{sudo('wget')} #{wget_proxy_parm} #{puppet_apt_repo}
284
+ #{sudo('dpkg')} -i #{puppet_apt_repo_file}
285
+ #{update_packages_debian_cmd}
286
+ #{sudo_env('apt-get')} -y install facter#{facter_debian_version}
287
+ #{sudo_env('apt-get')} -y install puppet-common#{puppet_debian_version}
288
+ #{sudo_env('apt-get')} -y install puppet#{puppet_debian_version}
289
+ #{install_hiera}
290
+ fi
291
+ fi
292
+ fi
293
+ #{install_eyaml}
294
+ #{install_eyaml_gpg}
295
+ #{install_deep_merge}
296
+ #{install_busser}
297
+ #{custom_install_command}
298
+ INSTALL
299
+ end
300
+ end
301
+ end
302
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
303
+
304
+ def install_command_collections
305
+ case puppet_platform
306
+ when 'debian', 'ubuntu'
307
+ info("Installing Puppet Collections on #{puppet_platform}")
308
+ <<-INSTALL
309
+
310
+ #{Util.shell_helpers}
311
+ #{custom_pre_install_command}
312
+ if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
313
+ if [ ! -f "#{config[:puppet_apt_collections_repo]}" ]; then
314
+ #{sudo('apt-get')} -y install wget
315
+ #{sudo('wget')} #{wget_proxy_parm} #{config[:puppet_apt_collections_repo]}
316
+ fi
317
+ #{sudo('dpkg')} -i #{puppet_apt_coll_repo_file}
318
+ #{sudo('apt-get')} update
319
+ #{sudo_env('apt-get')} -y install puppet-agent#{puppet_debian_version}
320
+ fi
321
+ #{install_eyaml("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
322
+ #{install_eyaml_gpg("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
323
+ #{install_deep_merge}
324
+ #{install_busser}
325
+ #{custom_install_command}
326
+ INSTALL
327
+ when 'redhat', 'centos', 'fedora', 'oracle', 'amazon'
328
+ info("Installing Puppet Collections on #{puppet_platform}")
329
+ <<-INSTALL
330
+
331
+ #{Util.shell_helpers}
332
+ #{custom_pre_install_command}
333
+ if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
334
+ echo "-----> #{sudo_env('yum')} -y --nogpgcheck install #{config[:puppet_yum_collections_repo]}"
335
+ #{sudo_env('yum')} clean all
336
+ #{sudo_env('yum')} -y --nogpgcheck install #{config[:puppet_yum_collections_repo]}
337
+ #{sudo_env('yum')} -y --nogpgcheck install puppet-agent#{puppet_redhat_version}
338
+ fi
339
+ #{install_eyaml("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
340
+ #{install_eyaml_gpg("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
341
+ #{install_deep_merge}
342
+ #{install_busser}
343
+ #{custom_install_command}
344
+ INSTALL
345
+ when /^windows.*/
346
+ info("Installing Puppet Collections on #{puppet_platform}")
347
+ info('Powershell is not recognised by core test-kitchen assuming it is present') unless powershell_shell?
348
+ <<-INSTALL
349
+ if(Get-Command puppet -ErrorAction 0) { return; }
350
+ $architecture = if( [Environment]::Is64BitOperatingSystem ) { 'x64' } else { 'x86' }
351
+ if( '#{puppet_windows_version}' -eq 'latest' ) {
352
+ $MsiUrl = "https://downloads.puppetlabs.com/windows/puppet-agent-${architecture}-latest.msi"
353
+ } elseif( '#{puppet_windows_version}' -like '5.*' ) {
354
+ $MsiUrl = "https://downloads.puppetlabs.com/windows/puppet5/puppet-agent-#{puppet_windows_version}-${architecture}.msi"
355
+ } else {
356
+ $MsiUrl = "https://downloads.puppetlabs.com/windows/puppet-agent-#{puppet_windows_version}${architecture}.msi"
357
+ }
358
+ Invoke-WebRequest $MsiUrl -UseBasicParsing -OutFile "C:/puppet-agent.msi" #{posh_proxy_parm}
359
+ $process = Start-Process -FilePath msiexec.exe -Wait -PassThru -ArgumentList '/qn', '/norestart', '/i', 'C:\\puppet-agent.msi'
360
+ if ($process.ExitCode -ne 0) {
361
+ Write-Host "Installer failed."
362
+ Exit 1
363
+ }
364
+
365
+ #{install_busser}
366
+ INSTALL
367
+ else
368
+ info('Installing Puppet Collections, will try to determine platform os')
369
+ <<-INSTALL
370
+
371
+ #{Util.shell_helpers}
372
+ #{custom_pre_install_command}
373
+ if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
374
+ if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ] || \
375
+ [ -f /etc/system-release ] || [ grep -q 'Amazon Linux' /etc/system-release ]; then
376
+ echo "-----> #{sudo_env('yum')} -y --nogpgcheck install #{config[:puppet_yum_collections_repo]}"
377
+ #{sudo_env('yum')} -y --nogpgcheck install #{config[:puppet_yum_collections_repo]}
378
+ #{sudo_env('yum')} -y --nogpgcheck install puppet-agent#{puppet_redhat_version}
379
+ else
380
+ #{sudo('apt-get')} -y install wget
381
+ #{sudo('wget')} #{wget_proxy_parm} #{config[:puppet_apt_collections_repo]}
382
+ #{sudo('dpkg')} -i #{puppet_apt_coll_repo_file}
383
+ #{sudo('apt-get')} update
384
+ #{sudo_env('apt-get')} -y install puppet-agent#{puppet_debian_version}
385
+ fi
386
+ fi
387
+ #{install_eyaml("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
388
+ #{install_eyaml_gpg("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
389
+ #{install_deep_merge}
390
+ #{install_busser}
391
+ #{custom_install_command}
392
+ INSTALL
393
+ end
394
+ end
395
+
396
+ def install_deep_merge
397
+ return unless config[:hiera_deep_merge]
398
+ <<-INSTALL
399
+ # Support for hash merge lookups to recursively merge hash keys
400
+ if [[ $(#{sudo('gem')} list deep_merge -i) == 'false' ]]; then
401
+ echo '-----> Installing deep_merge to provide deep_merge of hiera hashes'
402
+ #{sudo('gem')} install #{gem_proxy_parm} --no-ri --no-rdoc deep_merge
403
+ fi
404
+ INSTALL
405
+ end
406
+
407
+ def install_eyaml(gem_cmd = 'gem')
408
+ return unless config[:hiera_eyaml]
409
+ <<-INSTALL
410
+ # A backend for Hiera that provides per-value asymmetric encryption of sensitive data
411
+ if [[ $(#{sudo(gem_cmd)} list hiera-eyaml -i) == 'false' ]]; then
412
+ echo '-----> Installing hiera-eyaml to provide encryption of hiera data'
413
+ #{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc highline -v 1.6.21
414
+ #{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc hiera-eyaml
415
+ fi
416
+ INSTALL
417
+ end
418
+
419
+ def install_eyaml_gpg(gem_cmd = 'gem')
420
+ return unless config[:hiera_eyaml_gpg]
421
+ <<-INSTALL
422
+ # A backend for Hiera that provides per-value asymmetric encryption of sensitive data
423
+ if [[ $(#{sudo(gem_cmd)} list hiera-eyaml-gpg -i) == 'false' ]]; then
424
+ echo '-----> Installing hiera-eyaml-gpg to provide encryption of hiera data'
425
+ #{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc highline -v 1.6.21
426
+ #{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc hiera-eyaml
427
+ #{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc hiera-eyaml-gpg
428
+ #{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc ruby_gpg
429
+ fi
430
+ INSTALL
431
+ end
432
+
433
+ def install_busser
434
+ return unless config[:require_chef_for_busser]
435
+ info("Install busser on #{puppet_platform}")
436
+ case puppet_platform
437
+ when /^windows.*/
438
+ # https://raw.githubusercontent.com/opscode/knife-windows/master/lib/chef/knife/bootstrap/windows-chef-client-msi.erb
439
+ <<-INSTALL
440
+ $webclient = New-Object System.Net.WebClient; $webclient.DownloadFile('https://opscode-omnibus-packages.s3.amazonaws.com/windows/2008r2/x86_64/chef-windows-11.12.8-1.windows.msi','chef-windows-11.12.8-1.windows.msi')
441
+ msiexec /qn /i chef-windows-11.12.8-1.windows.msi
442
+
443
+ cmd.exe /C "SET PATH=%PATH%;`"C:\\opscode\\chef\\embedded\\bin`";`"C:\\tmp\\busser\\gems\\bin`""
444
+
445
+ INSTALL
446
+ else
447
+ <<-INSTALL
448
+ #{Util.shell_helpers}
449
+ # install chef omnibus so that busser works as this is needed to run tests :(
450
+ # TODO: work out how to install enough ruby
451
+ # and set busser: { :ruby_bindir => '/usr/bin/ruby' } so that we dont need the
452
+ # whole chef client
453
+ if [ ! -d "/opt/chef" ]
454
+ then
455
+ echo '-----> Installing Chef Omnibus to install busser to run tests'
456
+ #{export_http_proxy_parm}
457
+ #{export_https_proxy_parm}
458
+ #{export_no_proxy_parm}
459
+ do_download #{chef_url} /tmp/install.sh
460
+ #{sudo('sh')} /tmp/install.sh
461
+ fi
462
+ INSTALL
463
+ end
464
+ end
465
+
466
+ def install_omnibus_command
467
+ info('Installing puppet using puppet omnibus')
468
+
469
+ version = ''
470
+ version = "-v #{config[:puppet_version]}" unless config[:puppet_version].nil?
471
+
472
+ <<-INSTALL
473
+ #{Util.shell_helpers}
474
+ if [ ! $(which puppet) ]; then
475
+ echo "-----> Installing Puppet Omnibus"
476
+ #{export_http_proxy_parm}
477
+ #{export_https_proxy_parm}
478
+ #{export_no_proxy_parm}
479
+ do_download #{config[:puppet_omnibus_url]} /tmp/install_puppet.sh
480
+ #{sudo_env('sh')} /tmp/install_puppet.sh #{version}
481
+ fi
482
+ INSTALL
483
+ end
484
+
485
+ def install_hiera
486
+ return unless config[:install_hiera]
487
+ <<-INSTALL
488
+ #{sudo_env('apt-get')} -y install #{hiera_package}
489
+ INSTALL
490
+ end
491
+
492
+ def hiera_package
493
+ "#{config[:hiera_package]}#{puppet_hiera_debian_version}"
494
+ end
495
+
496
+ # /bin/wget -P /etc/pki/rpm-gpg/ http://yum.puppetlabs.com/RPM-GPG-KEY-puppetlabs
497
+ # changed to curl
498
+
499
+ def install_puppet_yum_repo
500
+ <<-INSTALL
501
+ rhelversion=$(cat /etc/redhat-release | grep 'release 7')
502
+ # For CentOS7/RHEL7 the rdo release contains puppetlabs repo, creating conflict. Create temp-repo
503
+ #{sudo_env('curl')} -o /etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs http://yum.puppetlabs.com/RPM-GPG-KEY-puppetlabs
504
+ if [ -n "$rhelversion" ]; then
505
+ echo '[puppettemp-products]
506
+ name=Puppet Labs Products - \$basearch
507
+ baseurl=http://yum.puppetlabs.com/el/7/products/\$basearch
508
+ gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
509
+ enabled=0
510
+ gpgcheck=1
511
+ [puppettemp-deps]
512
+ name=Puppet Labs Dependencies - \$basearch
513
+ baseurl=http://yum.puppetlabs.com/el/7/dependencies/\$basearch
514
+ gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
515
+ enabled=0
516
+ gpgcheck=1' | sudo tee /etc/yum.repos.d/puppettemp.repo > /dev/null
517
+ sudo sed -i 's/^[ \t]*//' /etc/yum.repos.d/puppettemp.repo
518
+ #{update_packages_redhat_cmd}
519
+ #{sudo_env('yum')} -y --enablerepo=puppettemp-products --enablerepo=puppettemp-deps install puppet#{puppet_redhat_version}
520
+ # Clean up temporary puppet repo
521
+ sudo rm -rf /etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
522
+ sudo rm -rf /etc/yum.repos.d/puppettemp.repo
523
+ else
524
+ #{sudo('rpm')} -ivh #{proxy_parm} #{puppet_yum_repo}
525
+ #{update_packages_redhat_cmd}
526
+ #{sudo_env('yum')} -y --nogpgcheck install puppet#{puppet_redhat_version}
527
+ fi
528
+ INSTALL
529
+ end
530
+
531
+ def custom_pre_install_command
532
+ <<-INSTALL
533
+ #{config[:custom_pre_install_command]}
534
+ INSTALL
535
+ end
536
+
537
+ def custom_install_command
538
+ <<-INSTALL
539
+ #{config[:custom_install_command]}
540
+ INSTALL
541
+ end
542
+
543
+ def init_command
544
+ todelete = %w[modules manifests files hiera hiera.yaml hiera.global.yaml facter spec enc environment]
545
+ .map { |dir| File.join(config[:root_path], dir) }
546
+ todelete += [hiera_data_remote_path,
547
+ '/etc/hiera.yaml',
548
+ "#{puppet_dir}/hiera.yaml",
549
+ spec_files_remote_path.to_s,
550
+ "#{puppet_dir}/fileserver.conf"]
551
+ todelete << File.join(puppet_dir, puppet_environment) if puppet_environment
552
+ todelete << File.join(puppet_environmentpath_remote_path, puppet_environment) if puppet_environment_config && puppet_environment
553
+ cmd = "#{sudo(rm_command_paths(todelete))};"
554
+ cmd += " #{mkdir_command} #{config[:root_path]};"
555
+ cmd += " #{sudo(mkdir_command)} #{puppet_dir}"
556
+ debug(cmd)
557
+ cmd
558
+ end
559
+
560
+ def create_sandbox
561
+ super
562
+ debug("Creating local sandbox in #{sandbox_path}")
563
+ yield if block_given?
564
+ prepare_modules
565
+ prepare_manifests
566
+ prepare_files
567
+ prepare_facter_file
568
+ prepare_facts
569
+ prepare_puppet_config
570
+ prepare_hiera_config
571
+ prepare_puppet_environment
572
+ prepare_fileserver_config
573
+ prepare_hiera_data
574
+ prepare_enc
575
+ prepare_spec_files
576
+ info('Finished Preparing files for transfer')
577
+ end
578
+
579
+ def cleanup_sandbox
580
+ return if sandbox_path.nil?
581
+ debug("Cleaning up local sandbox in #{sandbox_path}")
582
+ FileUtils.rmtree(sandbox_path)
583
+ return if remove_repo.nil?
584
+ debug("Cleaning up remote sandbox: #{remove_repo}")
585
+ instance.remote_exec remove_repo
586
+ end
587
+
588
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
589
+ def prepare_command
590
+ commands = []
591
+ if puppet_git_init
592
+ commands << [
593
+ sudo('rm -rf'), '/etc/puppet'
594
+ ].join(' ')
595
+
596
+ commands << [
597
+ sudo('git clone'), puppet_git_init, '/etc/puppet'
598
+ ].join(' ')
599
+ end
600
+
601
+ if puppet_git_pr
602
+ commands << [
603
+ sudo('git'),
604
+ '--git-dir=/etc/puppet/.git/',
605
+ 'fetch -f',
606
+ 'origin',
607
+ "pull/#{puppet_git_pr}/head:pr_#{puppet_git_pr}"
608
+ ].join(' ')
609
+
610
+ commands << [
611
+ sudo('git'),
612
+ '--git-dir=/etc/puppet/.git/',
613
+ '--work-tree=/etc/puppet/',
614
+ 'checkout',
615
+ "pr_#{puppet_git_pr}"
616
+ ].join(' ')
617
+ end
618
+
619
+ if puppet_config
620
+ commands << [
621
+ sudo(cp_command),
622
+ File.join(config[:root_path], 'puppet.conf'),
623
+ puppet_dir
624
+ ].join(' ')
625
+ end
626
+
627
+ if fileserver_config
628
+ commands << [
629
+ sudo(cp_command),
630
+ File.join(config[:root_path], 'fileserver.conf'),
631
+ puppet_dir
632
+ ].join(' ')
633
+ end
634
+
635
+ if hiera_data
636
+ commands << [
637
+ sudo(mkdir_command), hiera_data_remote_path
638
+ ].join(' ')
639
+ commands << [
640
+ sudo("#{cp_command} -r"), File.join(config[:root_path], 'hiera/*'), hiera_data_remote_path
641
+ ].join(' ')
642
+ end
643
+
644
+ if hiera_eyaml
645
+ commands << [
646
+ sudo(mkdir_command), hiera_eyaml_key_remote_path
647
+ ].join(' ')
648
+ commands << [
649
+ sudo("#{cp_command} -r"), File.join(config[:root_path], 'hiera_keys/*'), hiera_eyaml_key_remote_path
650
+ ].join(' ')
651
+ end
652
+
653
+ if hiera_eyaml_gpg
654
+ commands << [
655
+ sudo('mkdir -p'), hiera_eyaml_gpg_remote_path
656
+ ].join(' ')
657
+ commands << [
658
+ sudo('cp -r'), File.join(config[:root_path], hiera_eyaml_gpg_recipients), hiera_eyaml_gpg_remote_path
659
+ ].join(' ')
660
+ commands << [
661
+ sudo('cp -r'), File.join(config[:root_path], hiera_eyaml_gpg_secring), hiera_eyaml_gpg_remote_path
662
+ ].join(' ')
663
+ commands << [
664
+ sudo('cp -r'), File.join(config[:root_path], hiera_eyaml_gpg_pubring), hiera_eyaml_gpg_remote_path
665
+ ].join(' ')
666
+ end
667
+
668
+ if puppet_environment
669
+ commands << [
670
+ sudo('ln -s '), config[:root_path], File.join(puppet_dir, puppet_environment)
671
+ ].join(' ')
672
+ end
673
+
674
+ if puppet_environment_config && puppet_environment
675
+ commands << [
676
+ sudo(mkdir_command), puppet_environmentpath_remote_path
677
+ ].join(' ')
678
+ commands << [
679
+ sudo(mkdir_command), File.join(puppet_environmentpath_remote_path, puppet_environment)
680
+ ].join(' ')
681
+ commands << [
682
+ sudo('ln -s '), File.join(config[:root_path], 'modules'), File.join(puppet_environmentpath_remote_path, puppet_environment, puppet_environment_remote_modules_path)
683
+ ].join(' ')
684
+ commands << [
685
+ sudo('ln -s '), File.join(config[:root_path], 'manifests'), File.join(puppet_environmentpath_remote_path, puppet_environment, puppet_environment_remote_manifests_path)
686
+ ].join(' ')
687
+ commands << [
688
+ sudo('ln -s '), File.join(config[:root_path], 'hiera'), File.join(puppet_environmentpath_remote_path, puppet_environment, puppet_environment_remote_hieradata_path)
689
+ ].join(' ')
690
+ commands << [
691
+ sudo('cp'), File.join(config[:root_path], 'environment', 'environment.conf'), File.join(puppet_environmentpath_remote_path, puppet_environment, 'environment.conf')
692
+ ].join(' ')
693
+ commands << [
694
+ sudo('cp'), File.join(config[:root_path], 'environment', 'hiera.yaml'), File.join(puppet_environmentpath_remote_path, puppet_environment, 'hiera.yaml')
695
+ ].join(' ')
696
+ end
697
+
698
+ if spec_files_path && spec_files_remote_path
699
+ commands << [
700
+ sudo(mkdir_command), spec_files_remote_path
701
+ ].join(' ')
702
+ commands << [
703
+ sudo("#{cp_command} -r"), File.join(config[:root_path], 'spec/*'), spec_files_remote_path
704
+ ].join(' ')
705
+ end
706
+
707
+ if config[:puppet_enc]
708
+ commands << [
709
+ sudo('chmod 755'), File.join("#{config[:root_path]}/enc", File.basename(config[:puppet_enc]))
710
+ ].join(' ')
711
+ end
712
+
713
+ command = powershell? ? commands.join('; ') : commands.join(' && ')
714
+ debug(command)
715
+ command
716
+ end
717
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
718
+
719
+ def run_command
720
+ return config[:puppet_apply_command] unless config[:puppet_apply_command].nil?
721
+ result = [
722
+ facterlib,
723
+ custom_facts,
724
+ puppet_manifestdir,
725
+ puppet_cmd,
726
+ 'apply',
727
+ File.join(config[:root_path], 'manifests', manifest),
728
+ "--modulepath=#{File.join(config[:root_path], 'modules')}",
729
+ "--fileserverconfig=#{File.join(config[:root_path], 'fileserver.conf')}",
730
+ custom_options,
731
+ puppet_environment_flag,
732
+ puppet_noop_flag,
733
+ puppet_enc_flag,
734
+ puppet_hiera_flag,
735
+ puppet_detailed_exitcodes_flag,
736
+ puppet_verbose_flag,
737
+ puppet_debug_flag,
738
+ puppet_logdest_flag,
739
+ puppet_future_parser_flag,
740
+ puppet_show_diff_flag,
741
+ puppet_whitelist_exit_code
742
+ ].join(' ')
743
+ if config[:custom_post_apply_command]
744
+ custom_post_apply_trap = <<-TRAP
745
+ function custom_post_apply_command {
746
+ #{config[:custom_post_apply_command]}
747
+ }
748
+ trap custom_post_apply_command EXIT
749
+ TRAP
750
+ end
751
+ result = <<-RUN
752
+ #{config[:custom_pre_apply_command]}
753
+ #{custom_post_apply_trap}
754
+ #{result}
755
+ RUN
756
+ info("Going to invoke puppet apply with: #{result}")
757
+ result
758
+ end
759
+
760
+ protected
761
+
762
+ def load_needed_dependencies!
763
+ return unless File.exist?(puppetfile)
764
+ return unless config[:resolve_with_librarian_puppet] || config[:resolve_with_r10k]
765
+ if config[:resolve_with_librarian_puppet]
766
+ require 'kitchen/provisioner/puppet/librarian'
767
+ debug("Puppetfile found at #{puppetfile}, loading Librarian-Puppet")
768
+ Puppet::Librarian.load!(logger)
769
+ elsif config[:resolve_with_r10k]
770
+ require 'kitchen/provisioner/puppet/r10k'
771
+ debug("Puppetfile found at #{puppetfile}, loading R10K")
772
+ Puppet::R10K.load!(logger)
773
+ end
774
+ end
775
+
776
+ def tmpmodules_dir
777
+ File.join(sandbox_path, 'modules')
778
+ end
779
+
780
+ def puppetfile
781
+ config[:puppetfile_path] || ''
782
+ end
783
+
784
+ def modulefile
785
+ config[:modulefile_path] || ''
786
+ end
787
+
788
+ def metadata_json
789
+ config[:metadata_json_path] || ''
790
+ end
791
+
792
+ def manifest
793
+ config[:manifest]
794
+ end
795
+
796
+ def manifests
797
+ config[:manifests_path]
798
+ end
799
+
800
+ def modules
801
+ config[:modules_path]
802
+ end
803
+
804
+ def files
805
+ config[:files_path] || 'files'
806
+ end
807
+
808
+ def puppet_config
809
+ config[:puppet_config_path]
810
+ end
811
+
812
+ def puppet_environment
813
+ config[:puppet_environment]
814
+ end
815
+
816
+ def puppet_environment_config
817
+ if config[:puppet_environment_config_path] && !puppet_environment
818
+ raise("ERROR: found environment config '#{config[:puppet_environment_config_path]}', however no 'puppet_environment' is specified. Please specify 'puppet_environment' or unset 'puppet_environment_config_path' in .kitchen.yml")
819
+ end
820
+ config[:puppet_environment_config_path]
821
+ end
822
+
823
+ def puppet_environment_remote_modules_path
824
+ config[:puppet_environment_remote_modules_path]
825
+ end
826
+
827
+ def puppet_environment_remote_manifests_path
828
+ config[:puppet_environment_remote_manifests_path]
829
+ end
830
+
831
+ def puppet_environment_remote_hieradata_path
832
+ config[:puppet_environment_remote_hieradata_path]
833
+ end
834
+
835
+ def puppet_git_init
836
+ config[:puppet_git_init]
837
+ end
838
+
839
+ def puppet_git_pr
840
+ config[:puppet_git_pr]
841
+ end
842
+
843
+ def hiera_config
844
+ config[:hiera_config_path]
845
+ end
846
+
847
+ def puppet_environment_hiera_config
848
+ config[:puppet_environment_hiera_config_path]
849
+ end
850
+
851
+ def fileserver_config
852
+ config[:fileserver_config_path]
853
+ end
854
+
855
+ def hiera_data
856
+ config[:hiera_data_path]
857
+ end
858
+
859
+ def hiera_data_remote_path
860
+ return config[:hiera_data_remote_path] if config[:hiera_data_remote_path]
861
+
862
+ if config[:require_puppet_collections]
863
+ powershell? ? 'C:/ProgramData/PuppetLabs/code/environments/production/hieradata' : '/etc/puppetlabs/code/environments/production/hieradata'
864
+ else
865
+ powershell? ? 'C:/ProgramData/PuppetLabs/hiera/var' : '/var/lib/hiera'
866
+ end
867
+ end
868
+
869
+ def hiera_writer
870
+ config[:hiera_writer_files]
871
+ end
872
+
873
+ def hiera_eyaml
874
+ config[:hiera_eyaml]
875
+ end
876
+
877
+ def hiera_eyaml_gpg
878
+ config[:hiera_eyaml_gpg]
879
+ end
880
+
881
+ def hiera_eyaml_gpg_recipients
882
+ config[:hiera_eyaml_gpg_recipients]
883
+ end
884
+
885
+ def hiera_eyaml_gpg_secring
886
+ config[:hiera_eyaml_gpg_secring]
887
+ end
888
+
889
+ def hiera_eyaml_gpg_pubring
890
+ config[:hiera_eyaml_gpg_pubring]
891
+ end
892
+
893
+ def hiera_eyaml_gpg_remote_path
894
+ config[:hiera_eyaml_gpg_remote_path]
895
+ end
896
+
897
+ def hiera_eyaml_key_path
898
+ config[:hiera_eyaml_key_path]
899
+ end
900
+
901
+ def hiera_eyaml_key_remote_path
902
+ config[:hiera_eyaml_key_remote_path]
903
+ end
904
+
905
+ def hiera_deep_merge
906
+ config[:hiera_deep_merge]
907
+ end
908
+
909
+ def librarian_puppet_ssl_file
910
+ config[:librarian_puppet_ssl_file]
911
+ end
912
+
913
+ def r10k_ssl_file
914
+ config[:r10k_puppet_ssl_file] || config[:librarian_puppet_ssl_file]
915
+ end
916
+
917
+ def puppet_cmd
918
+ return '& "C:\Program Files\Puppet Labs\Puppet\bin\puppet"' if powershell?
919
+
920
+ puppet_bin = config[:require_puppet_collections] ? "#{config[:puppet_coll_remote_path]}/bin/puppet" : config[:puppet_binary]
921
+
922
+ if config[:puppet_no_sudo]
923
+ puppet_bin
924
+ else
925
+ sudo_env(puppet_bin)
926
+ end
927
+ end
928
+
929
+ def puppet_dir
930
+ return 'C:/ProgramData/PuppetLabs/puppet/etc' if powershell?
931
+ config[:require_puppet_collections] ? '/etc/puppetlabs/puppet' : '/etc/puppet'
932
+ end
933
+
934
+ def puppet_environmentpath_remote_path
935
+ return config[:puppet_environmentpath_remote_path] if config[:puppet_environmentpath_remote_path]
936
+ if config[:puppet_version] =~ /^3/
937
+ powershell? ? 'C:/ProgramData/PuppetLabs/puppet/etc' : '/etc/puppet/environments'
938
+ else
939
+ powershell? ? 'C:/ProgramData/PuppetLabs/code/environments' : '/etc/puppetlabs/code/environments'
940
+ end
941
+ end
942
+
943
+ def hiera_config_dir
944
+ return 'C:/ProgramData/PuppetLabs/puppet/etc' if powershell?
945
+ config[:require_puppet_collections] ? '/etc/puppetlabs/code' : '/etc/puppet'
946
+ end
947
+
948
+ def puppet_debian_version
949
+ config[:puppet_version] ? "=#{config[:puppet_version]}" : nil
950
+ end
951
+
952
+ def facter_debian_version
953
+ config[:facter_version] ? "=#{config[:facter_version]}" : nil
954
+ end
955
+
956
+ def puppet_hiera_debian_version
957
+ config[:hiera_version] ? "=#{config[:hiera_version]}" : nil
958
+ end
959
+
960
+ def puppet_redhat_version
961
+ if puppet_platform == 'amazon'
962
+ config[:puppet_version]
963
+ else
964
+ config[:puppet_version] ? "-#{config[:puppet_version]}" : nil
965
+ end
966
+ end
967
+
968
+ def puppet_windows_version
969
+ config[:puppet_version] ? config[:puppet_version].to_s : 'latest'
970
+ end
971
+
972
+ def puppet_environment_flag
973
+ if config[:puppet_version] =~ /^2/
974
+ config[:puppet_environment] ? "--environment=#{puppet_environment}" : nil
975
+ else
976
+ config[:puppet_environment] ? "--environment=#{puppet_environment} --environmentpath=#{puppet_environmentpath_remote_path}" : nil
977
+ end
978
+ end
979
+
980
+ def puppet_manifestdir
981
+ return nil if config[:require_puppet_collections]
982
+ return nil if config[:puppet_environment]
983
+ return nil if powershell?
984
+ bash_vars = "export MANIFESTDIR='#{File.join(config[:root_path], 'manifests')}';"
985
+ debug(bash_vars)
986
+ bash_vars
987
+ end
988
+
989
+ def custom_options
990
+ config[:custom_options] || ''
991
+ end
992
+
993
+ def puppet_noop_flag
994
+ config[:puppet_noop] ? '--noop' : nil
995
+ end
996
+
997
+ def puppet_debug_flag
998
+ config[:puppet_debug] ? '-d' : nil
999
+ end
1000
+
1001
+ def puppet_verbose_flag
1002
+ config[:puppet_verbose] ? '-v' : nil
1003
+ end
1004
+
1005
+ def puppet_show_diff_flag
1006
+ config[:puppet_show_diff] ? '--show_diff' : nil
1007
+ end
1008
+
1009
+ def puppet_future_parser_flag
1010
+ config[:puppet_future_parser] ? '--parser=future' : nil
1011
+ end
1012
+
1013
+ def puppet_logdest_flag
1014
+ return nil unless config[:puppet_logdest]
1015
+ destinations = ''
1016
+ config[:puppet_logdest].each do |dest|
1017
+ destinations << "--logdest #{dest} "
1018
+ end
1019
+ destinations
1020
+ end
1021
+
1022
+ def puppet_platform
1023
+ config[:platform].gsub(/-.*/, '')
1024
+ end
1025
+
1026
+ def update_packages_debian_cmd
1027
+ config[:update_package_repos] ? "#{sudo_env('apt-get')} update" : nil
1028
+ end
1029
+
1030
+ def update_packages_redhat_cmd
1031
+ # #{sudo('yum')}
1032
+ config[:update_package_repos] ? "#{sudo_env('yum')} makecache" : nil
1033
+ end
1034
+
1035
+ def sudo_env(pm)
1036
+ s = https_proxy ? "https_proxy=#{https_proxy}" : nil
1037
+ p = http_proxy ? "http_proxy=#{http_proxy}" : nil
1038
+ n = no_proxy ? "no_proxy=#{no_proxy}" : nil
1039
+ p || s ? "#{sudo('env')} #{p} #{s} #{n} #{pm}" : sudo(pm).to_s
1040
+ end
1041
+
1042
+ def remove_puppet_repo
1043
+ config[:remove_puppet_repo]
1044
+ end
1045
+
1046
+ def spec_files_path
1047
+ config[:spec_files_path]
1048
+ end
1049
+
1050
+ def spec_files_remote_path
1051
+ config[:spec_files_remote_path]
1052
+ end
1053
+
1054
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
1055
+ def facterlib
1056
+ factpath = nil
1057
+ factpath = File.join(config[:root_path], 'facter').to_s if config[:install_custom_facts] && config[:custom_facts].any?
1058
+ factpath = File.join(config[:root_path], 'facter').to_s if config[:facter_file]
1059
+ factpath = "#{factpath}:" if config[:facterlib] && !factpath.nil?
1060
+ factpath = "#{factpath}#{config[:facterlib]}" if config[:facterlib]
1061
+ return nil if factpath.nil?
1062
+ bash_vars = "export FACTERLIB='#{factpath}';"
1063
+ debug(bash_vars)
1064
+ bash_vars
1065
+ end
1066
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
1067
+
1068
+ def custom_facts
1069
+ return nil if config[:custom_facts].none?
1070
+ return nil if config[:install_custom_facts]
1071
+ if powershell?
1072
+ environment_vars = config[:custom_facts].map { |k, v| "$env:FACTER_#{k}='#{v}'" }.join('; ')
1073
+ environment_vars = "#{environment_vars};"
1074
+ else
1075
+ environment_vars = config[:custom_facts].map { |k, v| "FACTER_#{k}=#{v}" }.join(' ')
1076
+ environment_vars = "export #{environment_vars};"
1077
+ end
1078
+ debug(environment_vars)
1079
+ environment_vars
1080
+ end
1081
+
1082
+ def puppet_enc_flag
1083
+ config[:puppet_enc] ? "--node_terminus=exec --external_nodes=#{config[:root_path]}/enc/#{File.basename(config[:puppet_enc])}" : nil
1084
+ end
1085
+
1086
+ def puppet_hiera_flag
1087
+ hiera_config ? "--hiera_config=#{config[:root_path]}/hiera.global.yaml" : nil
1088
+ end
1089
+
1090
+ def puppet_detailed_exitcodes_flag
1091
+ config[:puppet_detailed_exitcodes] ? '--detailed-exitcodes' : nil
1092
+ end
1093
+
1094
+ def remove_repo
1095
+ remove_puppet_repo ? "#{sudo('rm')} -rf /tmp/kitchen #{hiera_data_remote_path} #{hiera_eyaml_key_remote_path} #{puppet_dir}/* " : nil
1096
+ end
1097
+
1098
+ def puppet_whitelist_exit_code
1099
+ if config[:puppet_whitelist_exit_code].nil?
1100
+ powershell? ? '; exit $LASTEXITCODE' : nil
1101
+ elsif powershell?
1102
+ "; if(@(#{[config[:puppet_whitelist_exit_code]].join(', ')}) -contains $LASTEXITCODE) {exit 0} else {exit $LASTEXITCODE}"
1103
+ else
1104
+ '; RC=$?; [ ' + [config[:puppet_whitelist_exit_code]].flatten.map { |x| "\$RC -eq #{x}" }.join(' -o ') + ' ] && exit 0; exit $RC'
1105
+ end
1106
+ end
1107
+
1108
+ # rubocop:disable Metrics/CyclomaticComplexity
1109
+ def puppet_apt_repo
1110
+ platform_version = config[:platform].partition('-')[2]
1111
+ case puppet_platform
1112
+ when 'ubuntu'
1113
+ case platform_version
1114
+ when '14.10'
1115
+ # Utopic Repo
1116
+ 'https://apt.puppetlabs.com/puppetlabs-release-utopic.deb'
1117
+ when '14.04'
1118
+ # Trusty Repo
1119
+ 'https://apt.puppetlabs.com/puppetlabs-release-trusty.deb'
1120
+ when '12.04'
1121
+ # Precise Repo
1122
+ 'https://apt.puppetlabs.com/puppetlabs-release-precise.deb'
1123
+ else
1124
+ # Configured Repo
1125
+ config[:puppet_apt_repo]
1126
+ end
1127
+ when 'debian'
1128
+ case platform_version.gsub(/\..*/, '')
1129
+ when '8'
1130
+ # Debian Jessie
1131
+ 'https://apt.puppetlabs.com/puppetlabs-release-jessie.deb'
1132
+ when '7'
1133
+ # Debian Wheezy
1134
+ 'https://apt.puppetlabs.com/puppetlabs-release-wheezy.deb'
1135
+ when '6'
1136
+ # Debian Squeeze
1137
+ 'https://apt.puppetlabs.com/puppetlabs-release-squeeze.deb'
1138
+ else
1139
+ # Configured Repo
1140
+ config[:puppet_apt_repo]
1141
+ end
1142
+ else
1143
+ debug("Apt repo detection failed with platform - #{config[:platform]}")
1144
+ false
1145
+ end
1146
+ end
1147
+ # rubocop:enable Metrics/CyclomaticComplexity
1148
+
1149
+ def puppet_apt_repo_file
1150
+ puppet_apt_repo.split('/').last if puppet_apt_repo
1151
+ end
1152
+
1153
+ def puppet_apt_coll_repo_file
1154
+ config[:puppet_apt_collections_repo].split('/').last
1155
+ end
1156
+
1157
+ def puppet_yum_repo
1158
+ config[:puppet_yum_repo]
1159
+ end
1160
+
1161
+ def proxy_parm
1162
+ http_proxy ? "--httpproxy #{URI.parse(http_proxy).host.downcase} --httpport #{URI.parse(http_proxy).port} " : nil
1163
+ end
1164
+
1165
+ def gem_proxy_parm
1166
+ p = http_proxy ? "--http-proxy #{http_proxy}" : nil
1167
+ n = no_proxy ? "--no-http-proxy #{no_proxy}" : nil
1168
+ p || n ? "#{p} #{n}" : nil
1169
+ end
1170
+
1171
+ def wget_proxy_parm
1172
+ p = http_proxy ? "-e http_proxy=#{http_proxy}" : nil
1173
+ s = https_proxy ? "-e https_proxy=#{https_proxy}" : nil
1174
+ n = no_proxy ? "-e no_proxy=#{no_proxy}" : nil
1175
+ p || s ? "-e use_proxy=yes #{p} #{s} #{n}" : nil
1176
+ end
1177
+
1178
+ def posh_proxy_parm
1179
+ http_proxy ? "-Proxy #{http_proxy}" : nil
1180
+ end
1181
+
1182
+ def powershell?
1183
+ return true if powershell_shell?
1184
+ return true if puppet_platform =~ /^windows.*/
1185
+ false
1186
+ end
1187
+
1188
+ def export_http_proxy_parm
1189
+ http_proxy ? "export http_proxy=#{http_proxy}" : nil
1190
+ end
1191
+
1192
+ def export_https_proxy_parm
1193
+ https_proxy ? "export https_proxy=#{https_proxy}" : nil
1194
+ end
1195
+
1196
+ def export_no_proxy_parm
1197
+ no_proxy ? "export no_proxy=#{no_proxy}" : nil
1198
+ end
1199
+
1200
+ def http_proxy
1201
+ config[:http_proxy]
1202
+ end
1203
+
1204
+ def https_proxy
1205
+ config[:https_proxy]
1206
+ end
1207
+
1208
+ def no_proxy
1209
+ config[:no_proxy]
1210
+ end
1211
+
1212
+ def chef_url
1213
+ config[:chef_bootstrap_url]
1214
+ end
1215
+
1216
+ def prepare_manifests
1217
+ info('Preparing manifests')
1218
+ debug("Using manifests from #{manifests}")
1219
+
1220
+ tmp_manifests_dir = File.join(sandbox_path, 'manifests')
1221
+ FileUtils.mkdir_p(tmp_manifests_dir)
1222
+ FileUtils.cp_r(Dir.glob("#{manifests}/*"), tmp_manifests_dir)
1223
+ end
1224
+
1225
+ def prepare_files
1226
+ info('Preparing files')
1227
+ unless File.directory?(files)
1228
+ info 'nothing to do for files'
1229
+ return
1230
+ end
1231
+
1232
+ debug("Using files from #{files}")
1233
+
1234
+ tmp_files_dir = File.join(sandbox_path, 'files')
1235
+ FileUtils.mkdir_p(tmp_files_dir)
1236
+ FileUtils.cp_r(Dir.glob("#{files}/*"), tmp_files_dir)
1237
+ end
1238
+
1239
+ def prepare_facter_file
1240
+ return unless config[:facter_file]
1241
+ info 'Copying facter file'
1242
+ facter_dir = File.join(sandbox_path, 'facter')
1243
+ FileUtils.mkdir_p(facter_dir)
1244
+ FileUtils.cp_r(config[:facter_file], facter_dir)
1245
+ end
1246
+
1247
+ def prepare_facts
1248
+ return unless config[:install_custom_facts]
1249
+ return unless config[:custom_facts]
1250
+ info 'Installing custom facts'
1251
+ facter_dir = File.join(sandbox_path, 'facter')
1252
+ FileUtils.mkdir_p(facter_dir)
1253
+ tmp_facter_file = File.join(facter_dir, 'kitchen.rb')
1254
+ facter_facts = Hash[config[:custom_facts]]
1255
+ File.open(tmp_facter_file, 'a') do |out|
1256
+ facter_facts.each do |k, v|
1257
+ out.write "\nFacter.add(:#{k}) do\n"
1258
+ out.write " setcode do\n"
1259
+ if [Array, Hash].include? v.class
1260
+ out.write " #{v}\n"
1261
+ else
1262
+ out.write " \"#{v}\"\n"
1263
+ end
1264
+ out.write " end\n"
1265
+ out.write "end\n"
1266
+ end
1267
+ end
1268
+ end
1269
+
1270
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
1271
+ def prepare_modules
1272
+ info('Preparing modules')
1273
+
1274
+ FileUtils.mkdir_p(tmpmodules_dir)
1275
+ resolve_with_librarian if File.exist?(puppetfile) && config[:resolve_with_librarian_puppet]
1276
+ resolve_with_r10k if File.exist?(puppetfile) && config[:resolve_with_r10k] && !config[:resolve_with_librarian_puppet]
1277
+ modules_to_copy = {}
1278
+
1279
+ # If root dir (.) is a module, add it for copying
1280
+ self_name = read_self_module_name
1281
+ modules_to_copy[self_name] = '.' if self_name
1282
+
1283
+ if modules
1284
+ modules_array = modules.split(':')
1285
+ modules_array.each do |m_path|
1286
+ Dir.glob("#{m_path}/*").each do |m|
1287
+ name = File.basename(m)
1288
+ if modules_to_copy.include? name
1289
+ debug("Found duplicated module: #{name}. The path taking precedence: '#{modules_to_copy[name]}', ignoring '#{m}'")
1290
+ else
1291
+ modules_to_copy[name] = m
1292
+ end
1293
+ end
1294
+ end
1295
+ end
1296
+
1297
+ if modules_to_copy.empty?
1298
+ info 'Nothing to do for modules'
1299
+ else
1300
+ copy_modules(modules_to_copy, tmpmodules_dir)
1301
+ end
1302
+ end
1303
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
1304
+
1305
+ def copy_modules(modules, destination)
1306
+ excluded_paths = %w[modules pkg] + config[:ignored_paths_from_root]
1307
+ debug("Copying modules to directory: #{destination}")
1308
+ modules.each do |name, source|
1309
+ next unless File.directory?(source)
1310
+ debug("Copying module #{name} from #{source}...")
1311
+ target = "#{destination}/#{name}"
1312
+ FileUtils.mkdir_p(target) unless File.exist? target
1313
+ FileUtils.cp_r(
1314
+ Dir.glob("#{source}/*").reject { |entry| entry =~ /#{excluded_paths.join('$|')}$/ },
1315
+ target,
1316
+ remove_destination: true
1317
+ )
1318
+ end
1319
+ end
1320
+
1321
+ def read_self_module_name
1322
+ if File.exist?(modulefile)
1323
+ warn('Modulefile found but this is deprecated, ignoring it, see https://tickets.puppetlabs.com/browse/PUP-1188')
1324
+ end
1325
+
1326
+ return unless File.exist?(metadata_json)
1327
+ module_name = nil
1328
+ begin
1329
+ module_name = JSON.parse(IO.read(metadata_json))['name'].split('-').last
1330
+ rescue JSON::ParserError
1331
+ error("not able to load or parse #{metadata_json} for the name of the module")
1332
+ end
1333
+
1334
+ module_name
1335
+ end
1336
+
1337
+ def prepare_puppet_config
1338
+ return unless puppet_config
1339
+
1340
+ info('Preparing puppet.conf')
1341
+ debug("Using puppet config from #{puppet_config}")
1342
+
1343
+ FileUtils.cp_r(puppet_config, File.join(sandbox_path, 'puppet.conf'))
1344
+ end
1345
+
1346
+ def prepare_enc
1347
+ return unless config[:puppet_enc]
1348
+ info 'Copying enc file'
1349
+ enc_dir = File.join(sandbox_path, 'enc')
1350
+ FileUtils.mkdir_p(enc_dir)
1351
+ FileUtils.cp_r(config[:puppet_enc], File.join(enc_dir, '/'))
1352
+ end
1353
+
1354
+ def prepare_puppet_environment
1355
+ return unless puppet_environment_config
1356
+
1357
+ info('Preparing Environment Config')
1358
+ environment_dir = File.join(sandbox_path, 'environment')
1359
+ FileUtils.mkdir_p(environment_dir)
1360
+ debug("Using Environment Config environment.conf from #{puppet_environment_config}")
1361
+ FileUtils.cp_r(puppet_environment_config, File.join(environment_dir, 'environment.conf'))
1362
+ if puppet_environment_hiera_config
1363
+ debug("Using Environment Hiera Config hiera.yaml from #{puppet_environment_hiera_config}")
1364
+ FileUtils.cp_r(puppet_environment_hiera_config, File.join(environment_dir, 'hiera.yaml'))
1365
+ else
1366
+ info('No Environment hiera.yaml found')
1367
+ end
1368
+ end
1369
+
1370
+ def prepare_hiera_config
1371
+ return unless hiera_config
1372
+
1373
+ info('Preparing hiera (global layer)')
1374
+ debug("Using hiera from #{hiera_config}")
1375
+
1376
+ FileUtils.cp_r(hiera_config, File.join(sandbox_path, 'hiera.global.yaml'))
1377
+ end
1378
+
1379
+ def prepare_fileserver_config
1380
+ return unless fileserver_config
1381
+
1382
+ info('Preparing fileserver')
1383
+ debug("Using fileserver config from #{fileserver_config}")
1384
+
1385
+ FileUtils.cp_r(fileserver_config, File.join(sandbox_path, 'fileserver.conf'))
1386
+ end
1387
+
1388
+ def prepare_hiera_data
1389
+ return unless hiera_data
1390
+ info('Preparing hiera data')
1391
+ tmp_hiera_dir = File.join(sandbox_path, 'hiera')
1392
+ debug("Copying hiera data from #{hiera_data} to #{tmp_hiera_dir}")
1393
+ FileUtils.mkdir_p(tmp_hiera_dir)
1394
+ FileUtils.cp_r(Dir.glob("#{hiera_data}/*"), tmp_hiera_dir)
1395
+ if hiera_writer
1396
+ hiera_writer.each do |file|
1397
+ file.each do |filename, hiera_hash|
1398
+ debug("Creating hiera yaml file #{tmp_hiera_dir}/#{filename}")
1399
+ dir = File.join(tmp_hiera_dir, File.dirname(filename.to_s))
1400
+ FileUtils.mkdir_p(dir)
1401
+ output_file = open(File.join(dir, File.basename(filename.to_s)), 'w')
1402
+ # convert json and back before converting to yaml to recursively convert symbols to strings, heh
1403
+ output_file.write JSON[hiera_hash.to_json].to_yaml
1404
+ output_file.close
1405
+ end
1406
+ end
1407
+ end
1408
+ return unless hiera_eyaml_key_path
1409
+ tmp_hiera_key_dir = File.join(sandbox_path, 'hiera_keys')
1410
+ debug("Copying hiera eyaml keys from #{hiera_eyaml_key_path} to #{tmp_hiera_key_dir}")
1411
+ FileUtils.mkdir_p(tmp_hiera_key_dir)
1412
+ FileUtils.cp_r(Dir.glob("#{hiera_eyaml_key_path}/*"), tmp_hiera_key_dir)
1413
+ end
1414
+
1415
+ def prepare_spec_files
1416
+ return unless spec_files_path
1417
+ info('Preparing spec files')
1418
+ tmp_spec_dir = File.join(sandbox_path, 'spec')
1419
+ debug("Copying specs from #{spec_files_path} to #{tmp_spec_dir}")
1420
+ FileUtils.mkdir_p(tmp_spec_dir)
1421
+ FileUtils.cp_r(Dir.glob(File.join(spec_files_path, '*')).reject { |entry| entry =~ /fixtures$/ }, tmp_spec_dir) if config[:ignore_spec_fixtures]
1422
+ FileUtils.cp_r(Dir.glob("#{spec_files_path}/*"), tmp_spec_dir) unless config[:ignore_spec_fixtures]
1423
+ end
1424
+
1425
+ def resolve_with_librarian
1426
+ require 'kitchen/provisioner/puppet/librarian'
1427
+ Kitchen.mutex.synchronize do
1428
+ ENV['SSL_CERT_FILE'] = librarian_puppet_ssl_file if librarian_puppet_ssl_file
1429
+ Puppet::Librarian.new(puppetfile, tmpmodules_dir, logger).resolve
1430
+ ENV['SSL_CERT_FILE'] = '' if librarian_puppet_ssl_file
1431
+ end
1432
+ end
1433
+
1434
+ def resolve_with_r10k
1435
+ require 'kitchen/provisioner/puppet/r10k'
1436
+ Kitchen.mutex.synchronize do
1437
+ ENV['SSL_CERT_FILE'] = r10k_ssl_file if r10k_ssl_file
1438
+ Puppet::R10K.new(puppetfile, tmpmodules_dir, logger).resolve
1439
+ ENV['SSL_CERT_FILE'] = '' if r10k_ssl_file
1440
+ end
1441
+ end
1442
+
1443
+ def cp_command
1444
+ return 'cp -force' if powershell?
1445
+ 'cp'
1446
+ end
1447
+
1448
+ def rm_command
1449
+ return 'rm -force -recurse' if powershell?
1450
+ 'rm -rf'
1451
+ end
1452
+
1453
+ def mkdir_command
1454
+ return 'mkdir -force -path' if powershell?
1455
+ 'mkdir -p'
1456
+ end
1457
+
1458
+ def rm_command_paths(paths)
1459
+ return :nil if paths.length.zero?
1460
+ return "#{rm_command} \"#{paths.join('", "')}\"" if powershell?
1461
+ "#{rm_command} #{paths.join(' ')}"
1462
+ end
1463
+ end
1464
+ end
1465
+ end