kitchen-puppet 1.0.36 → 1.0.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1006 +1,1014 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
- # Author:: Chris Lundquist (<chris.lundquist@github.com>) Neill Turner (<neillwturner@gmail.com>)
4
- #
5
- # Copyright (C) 2013,2014 Chris Lundquist, Neill Turner
6
- #
7
- # Licensed under the Apache License, Version 2.0 (the "License");
8
- # you may not use this file except in compliance with the License.
9
- # You may obtain a copy of the License at
10
- #
11
- # http://www.apache.org/licenses/LICENSE-2.0
12
- #
13
- # Unless required by applicable law or agreed to in writing, software
14
- # distributed under the License is distributed on an "AS IS" BASIS,
15
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- # See the License for the specific language governing permissions and
17
- # limitations under the License.
18
- #
19
- # See https://github.com/neillturner/kitchen-puppet/blob/master/provisioner_options.md
20
- # for documentation configuration parameters with puppet_apply provisioner.
21
- #
22
-
23
- require 'uri'
24
- require 'json'
25
- require 'kitchen'
26
- require 'kitchen/provisioner/puppet/librarian'
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, false
49
- default_config :puppet_yum_collections_repo, 'http://yum.puppetlabs.com/puppetlabs-release-pc1-el-6.noarch.rpm'
50
- default_config :puppet_apt_collections_repo, 'http://apt.puppetlabs.com/puppetlabs-release-pc1-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 :require_puppet_repo, true
58
- default_config :require_chef_for_busser, true
59
- default_config :resolve_with_librarian_puppet, true
60
- default_config :puppet_environment, nil
61
- default_config :puppet_apt_repo, 'http://apt.puppetlabs.com/puppetlabs-release-precise.deb'
62
- default_config :puppet_yum_repo, 'https://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm'
63
- default_config :chef_bootstrap_url, 'https://www.getchef.com/chef/install.sh'
64
- default_config :puppet_logdest, nil
65
- default_config :custom_install_command, nil
66
-
67
- default_config :puppet_apply_command, nil
68
-
69
- default_config :puppet_git_init, nil
70
- default_config :puppet_git_pr, nil
71
-
72
- default_config :http_proxy, nil
73
- default_config :https_proxy, nil
74
-
75
- default_config :hiera_data_remote_path, '/var/lib/hiera'
76
- default_config :manifest, 'site.pp'
77
-
78
- default_config :manifests_path do |provisioner|
79
- provisioner.calculate_path('manifests') ||
80
- raise('No manifests_path detected. Please specify one in .kitchen.yml')
81
- end
82
-
83
- default_config :modules_path do |provisioner|
84
- modules_path = provisioner.calculate_path('modules')
85
- if modules_path.nil? && provisioner.calculate_path('Puppetfile', :file).nil?
86
- raise('No modules_path detected. Please specify one in .kitchen.yml')
87
- end
88
- modules_path
89
- end
90
-
91
- default_config :files_path do |provisioner|
92
- provisioner.calculate_path('files') || 'files'
93
- end
94
-
95
- default_config :hiera_data_path do |provisioner|
96
- provisioner.calculate_path('hiera')
97
- end
98
-
99
- default_config :puppet_config_path do |provisioner|
100
- provisioner.calculate_path('puppet.conf', :file)
101
- end
102
-
103
- default_config :hiera_config_path do |provisioner|
104
- provisioner.calculate_path('hiera.yaml', :file)
105
- end
106
-
107
- default_config :fileserver_config_path do |provisioner|
108
- provisioner.calculate_path('fileserver.conf', :file)
109
- end
110
- default_config :puppetfile_path do |provisioner|
111
- provisioner.calculate_path('Puppetfile', :file)
112
- end
113
-
114
- default_config :modulefile_path do |provisioner|
115
- provisioner.calculate_path('Modulefile', :file)
116
- end
117
-
118
- default_config :metadata_json_path do |provisioner|
119
- provisioner.calculate_path('metadata.json', :file)
120
- end
121
-
122
- default_config :manifests_path do |provisioner|
123
- provisioner.calculate_path('manifests', :directory)
124
- end
125
-
126
- default_config :spec_files_path do |provisioner|
127
- provisioner.calculate_path('spec', :directory)
128
- end
129
-
130
- default_config :spec_files_remote_path, '/etc/puppet/spec'
131
-
132
- default_config :puppet_debug, false
133
- default_config :puppet_verbose, false
134
- default_config :puppet_noop, false
135
- default_config :platform, &:platform_name
136
- default_config :update_package_repos, true
137
- default_config :remove_puppet_repo, false
138
- default_config :install_custom_facts, false
139
- default_config :custom_facts, {}
140
- default_config :facterlib, nil
141
- default_config :puppet_detailed_exitcodes, nil
142
- default_config :facter_file, nil
143
- default_config :librarian_puppet_ssl_file, nil
144
-
145
- default_config :hiera_eyaml, false
146
- default_config :hiera_eyaml_key_remote_path, '/etc/puppet/secure/keys'
147
-
148
- default_config :hiera_eyaml_key_path do |provisioner|
149
- provisioner.calculate_path('hiera_keys')
150
- end
151
-
152
- default_config :hiera_deep_merge, false
153
-
154
- def calculate_path(path, type = :directory)
155
- base = config[:test_base_path]
156
- candidates = []
157
- candidates << File.join(base, instance.suite.name, 'puppet', path)
158
- candidates << File.join(base, instance.suite.name, path)
159
- candidates << File.join(base, path)
160
- candidates << File.join(Dir.pwd, path)
161
-
162
- candidates.find do |c|
163
- type == :directory ? File.directory?(c) : File.file?(c)
164
- end
165
- end
166
-
167
- def install_command
168
- return unless config[:require_puppet_collections] || config[:require_puppet_repo]
169
- if config[:require_puppet_collections]
170
- install_command_collections
171
- else
172
- case puppet_platform
173
- when 'debian', 'ubuntu'
174
- info("Installing puppet on #{config[:platform]}")
175
- <<-INSTALL
176
- if [ ! $(which puppet) ]; then
177
- #{sudo('apt-get')} -y install wget
178
- #{sudo('wget')} #{wget_proxy_parm} #{puppet_apt_repo}
179
- #{sudo('dpkg')} -i #{puppet_apt_repo_file}
180
- #{update_packages_debian_cmd}
181
- #{sudo_env('apt-get')} -y install facter#{facter_debian_version}
182
- #{sudo_env('apt-get')} -y install puppet-common#{puppet_debian_version}
183
- #{sudo_env('apt-get')} -y install puppet#{puppet_debian_version}
184
- #{install_hiera}
185
- fi
186
- #{install_eyaml}
187
- #{install_deep_merge}
188
- #{install_busser}
189
- #{custom_install_command}
190
- INSTALL
191
- when 'redhat', 'centos', 'fedora', 'oracle', 'amazon'
192
- info("Installing puppet from yum on #{puppet_platform}")
193
- <<-INSTALL
194
- if [ ! $(which puppet) ]; then
195
- #{install_puppet_yum_repo}
196
- fi
197
- #{install_eyaml}
198
- #{install_deep_merge}
199
- #{install_busser}
200
- #{custom_install_command}
201
- INSTALL
202
- else
203
- info('Installing puppet, will try to determine platform os')
204
- <<-INSTALL
205
- if [ ! $(which puppet) ]; then
206
- if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ]; then
207
- #{install_puppet_yum_repo}
208
- else
209
- if [ -f /etc/system-release ] || [ grep -q 'Amazon Linux' /etc/system-release ]; then
210
- #{install_puppet_yum_repo}
211
- else
212
- #{sudo('apt-get')} -y install wget
213
- #{sudo('wget')} #{wget_proxy_parm} #{puppet_apt_repo}
214
- #{sudo('dpkg')} -i #{puppet_apt_repo_file}
215
- #{update_packages_debian_cmd}
216
- #{sudo_env('apt-get')} -y install facter#{facter_debian_version}
217
- #{sudo_env('apt-get')} -y install puppet-common#{puppet_debian_version}
218
- #{sudo_env('apt-get')} -y install puppet#{puppet_debian_version}
219
- #{install_hiera}
220
- fi
221
- fi
222
- fi
223
- #{install_eyaml}
224
- #{install_deep_merge}
225
- #{install_busser}
226
- #{custom_install_command}
227
- INSTALL
228
- end
229
- end
230
- end
231
-
232
- def install_command_collections
233
- case puppet_platform
234
- when 'debian', 'ubuntu'
235
- info("Installing Puppet Collections on #{puppet_platform}")
236
- <<-INSTALL
237
- #{Util.shell_helpers}
238
- if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
239
- if [ ! -f "#{config[:puppet_apt_collections_repo]}" ]; then
240
- #{sudo('apt-get')} -y install wget
241
- #{sudo('wget')} #{wget_proxy_parm} #{config[:puppet_apt_collections_repo]}
242
- fi
243
- #{sudo('dpkg')} -i #{puppet_apt_coll_repo_file}
244
- #{sudo('apt-get')} update
245
- #{sudo_env('apt-get')} -y install puppet-agent#{puppet_debian_version}
246
- fi
247
- #{install_eyaml("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
248
- #{install_deep_merge}
249
- #{install_busser}
250
- #{custom_install_command}
251
- INSTALL
252
- when 'redhat', 'centos', 'fedora', 'oracle', 'amazon'
253
- info("Installing Puppet Collections on #{puppet_platform}")
254
- <<-INSTALL
255
- #{Util.shell_helpers}
256
- if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
257
- echo "-----> #{sudo_env('yum')} -y localinstall #{config[:puppet_yum_collections_repo]}"
258
- #{sudo_env('yum')} -y localinstall #{config[:puppet_yum_collections_repo]}
259
- #{sudo_env('yum')} -y install puppet-agent#{puppet_redhat_version}
260
- fi
261
- #{install_eyaml("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
262
- #{install_deep_merge}
263
- #{install_busser}
264
- #{custom_install_command}
265
- INSTALL
266
- else
267
- info('Installing Puppet Collections, will try to determine platform os')
268
- <<-INSTALL
269
- #{Util.shell_helpers}
270
- if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
271
- if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ] || \
272
- [ -f /etc/system-release ] || [ grep -q 'Amazon Linux' /etc/system-release ]; then
273
- echo "-----> #{sudo_env('yum')} -y localinstall #{config[:puppet_yum_collections_repo]}"
274
- #{sudo_env('yum')} -y localinstall #{config[:puppet_yum_collections_repo]}
275
- #{sudo_env('yum')} -y install puppet-agent#{puppet_redhat_version}
276
- else
277
- #{sudo('apt-get')} -y install wget
278
- #{sudo('wget')} #{wget_proxy_parm} #{config[:puppet_apt_collections_repo]}
279
- #{sudo('dpkg')} -i #{puppet_apt_coll_repo_file}
280
- #{sudo('apt-get')} update
281
- #{sudo_env('apt-get')} -y install puppet-agent#{puppet_debian_version}
282
- fi
283
- fi
284
- #{install_eyaml("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
285
- #{install_deep_merge}
286
- #{install_busser}
287
- #{custom_install_command}
288
- INSTALL
289
- end
290
- end
291
-
292
- def install_deep_merge
293
- return unless config[:hiera_deep_merge]
294
- <<-INSTALL
295
- # Support for hash merge lookups to recursively merge hash keys
296
- if [[ $(#{sudo('gem')} list deep_merge -i) == 'false' ]]; then
297
- echo '-----> Installing deep_merge to provide deep_merge of hiera hashes'
298
- #{sudo('gem')} install #{gem_proxy_parm} --no-ri --no-rdoc deep_merge
299
- fi
300
- INSTALL
301
- end
302
-
303
- def install_eyaml(gem_cmd = 'gem')
304
- return unless config[:hiera_eyaml]
305
- <<-INSTALL
306
- # A backend for Hiera that provides per-value asymmetric encryption of sensitive data
307
- if [[ $(#{sudo(gem_cmd)} list hiera-eyaml -i) == 'false' ]]; then
308
- echo '-----> Installing hiera-eyaml to provide encryption of hiera data'
309
- #{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc highline -v 1.6.21
310
- #{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc hiera-eyaml
311
- fi
312
- INSTALL
313
- end
314
-
315
- def install_busser
316
- return unless config[:require_chef_for_busser]
317
- <<-INSTALL
318
- #{Util.shell_helpers}
319
- # install chef omnibus so that busser works as this is needed to run tests :(
320
- # TODO: work out how to install enough ruby
321
- # and set busser: { :ruby_bindir => '/usr/bin/ruby' } so that we dont need the
322
- # whole chef client
323
- if [ ! -d "/opt/chef" ]
324
- then
325
- echo '-----> Installing Chef Omnibus to install busser to run tests'
326
- #{export_http_proxy_parm}
327
- #{export_https_proxy_parm}
328
- do_download #{chef_url} /tmp/install.sh
329
- #{sudo('sh')} /tmp/install.sh
330
- fi
331
- INSTALL
332
- end
333
-
334
- def install_hiera
335
- return unless config[:install_hiera]
336
- <<-INSTALL
337
- #{sudo_env('apt-get')} -y install #{hiera_package}
338
- INSTALL
339
- end
340
-
341
- def hiera_package
342
- "#{config[:hiera_package]}#{puppet_hiera_debian_version}"
343
- end
344
-
345
- # /bin/wget -P /etc/pki/rpm-gpg/ http://yum.puppetlabs.com/RPM-GPG-KEY-puppetlabs
346
- # changed to curl
347
-
348
- def install_puppet_yum_repo
349
- <<-INSTALL
350
- rhelversion=$(cat /etc/redhat-release | grep 'release 7')
351
- # For CentOS7/RHEL7 the rdo release contains puppetlabs repo, creating conflict. Create temp-repo
352
- #{sudo_env('curl')} -o /etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs http://yum.puppetlabs.com/RPM-GPG-KEY-puppetlabs
353
- if [ -n "$rhelversion" ]; then
354
- echo '[puppettemp-products]
355
- name=Puppet Labs Products - \$basearch
356
- baseurl=http://yum.puppetlabs.com/el/7/products/\$basearch
357
- gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
358
- enabled=0
359
- gpgcheck=1
360
- [puppettemp-deps]
361
- name=Puppet Labs Dependencies - \$basearch
362
- baseurl=http://yum.puppetlabs.com/el/7/dependencies/\$basearch
363
- gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
364
- enabled=0
365
- gpgcheck=1' | sudo tee /etc/yum.repos.d/puppettemp.repo > /dev/null
366
- sudo sed -i 's/^[ \t]*//' /etc/yum.repos.d/puppettemp.repo
367
- #{update_packages_redhat_cmd}
368
- #{sudo_env('yum')} -y --enablerepo=puppettemp-products --enablerepo=puppettemp-deps install puppet#{puppet_redhat_version}
369
- # Clean up temporary puppet repo
370
- sudo rm -rf /etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
371
- sudo rm -rf /etc/yum.repos.d/puppettemp.repo
372
- else
373
- #{sudo('rpm')} -ivh #{proxy_parm} #{puppet_yum_repo}
374
- #{update_packages_redhat_cmd}
375
- #{sudo_env('yum')} -y install puppet#{puppet_redhat_version}
376
- fi
377
- INSTALL
378
- end
379
-
380
- def custom_install_command
381
- <<-INSTALL
382
- #{config[:custom_install_command]}
383
- INSTALL
384
- end
385
-
386
- def init_command
387
- dirs = %w(modules manifests files hiera hiera.yaml facter spec)
388
- .map { |dir| File.join(config[:root_path], dir) }.join(' ')
389
- cmd = "#{sudo('rm')} -rf #{dirs} #{hiera_data_remote_path} \
390
- /etc/hiera.yaml #{puppet_dir}/hiera.yaml \
391
- #{spec_files_remote_path} \
392
- #{puppet_dir}/fileserver.conf;"
393
- cmd += config[:puppet_environment] ? "#{sudo('rm')} -f #{File.join(puppet_dir, config[:puppet_environment])};" : ''
394
- cmd += " mkdir -p #{config[:root_path]}; #{sudo('mkdir')} -p #{puppet_dir}"
395
- debug(cmd)
396
- cmd
397
- end
398
-
399
- def create_sandbox
400
- super
401
- debug("Creating local sandbox in #{sandbox_path}")
402
- yield if block_given?
403
- prepare_modules
404
- prepare_manifests
405
- prepare_files
406
- prepare_facter_file
407
- prepare_facts
408
- prepare_puppet_config
409
- prepare_hiera_config
410
- prepare_fileserver_config
411
- prepare_hiera_data
412
- prepare_spec_files
413
- info('Finished Preparing files for transfer')
414
- end
415
-
416
- def cleanup_sandbox
417
- return if sandbox_path.nil?
418
- debug("Cleaning up local sandbox in #{sandbox_path}")
419
- FileUtils.rmtree(sandbox_path)
420
- end
421
-
422
- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
423
- def prepare_command
424
- commands = []
425
- if puppet_git_init
426
- commands << [
427
- sudo('rm -rf'), '/etc/puppet'
428
- ].join(' ')
429
-
430
- commands << [
431
- sudo('git clone'), puppet_git_init, '/etc/puppet'
432
- ].join(' ')
433
- end
434
-
435
- if puppet_git_pr
436
- commands << [sudo('git'),
437
- '--git-dir=/etc/puppet/.git/',
438
- 'fetch -f',
439
- 'origin',
440
- "pull/#{puppet_git_pr}/head:pr_#{puppet_git_pr}"
441
- ].join(' ')
442
-
443
- commands << [sudo('git'), '--git-dir=/etc/puppet/.git/',
444
- '--work-tree=/etc/puppet/',
445
- 'checkout',
446
- "pr_#{puppet_git_pr}"
447
- ].join(' ')
448
- end
449
-
450
- if puppet_config
451
- commands << [
452
- sudo('cp'),
453
- File.join(config[:root_path], 'puppet.conf'),
454
- puppet_dir
455
- ].join(' ')
456
- end
457
-
458
- if hiera_config
459
- commands << [
460
- sudo('cp'), File.join(config[:root_path], 'hiera.yaml'), '/etc/'
461
- ].join(' ')
462
-
463
- commands << [
464
- sudo('cp'), File.join(config[:root_path], 'hiera.yaml'), puppet_dir
465
- ].join(' ')
466
- end
467
-
468
- if fileserver_config
469
- commands << [
470
- sudo('cp'),
471
- File.join(config[:root_path], 'fileserver.conf'),
472
- puppet_dir
473
- ].join(' ')
474
- end
475
-
476
- if hiera_data && hiera_data_remote_path == '/var/lib/hiera'
477
- commands << [
478
- sudo('cp -r'), File.join(config[:root_path], 'hiera'), '/var/lib/'
479
- ].join(' ')
480
- end
481
-
482
- if hiera_data && hiera_data_remote_path != '/var/lib/hiera'
483
- commands << [
484
- sudo('mkdir -p'), hiera_data_remote_path
485
- ].join(' ')
486
- commands << [
487
- sudo('cp -r'), File.join(config[:root_path], 'hiera/*'), hiera_data_remote_path
488
- ].join(' ')
489
- end
490
-
491
- if hiera_eyaml
492
- commands << [
493
- sudo('mkdir -p'), hiera_eyaml_key_remote_path
494
- ].join(' ')
495
- commands << [
496
- sudo('cp -r'), File.join(config[:root_path], 'hiera_keys/*'), hiera_eyaml_key_remote_path
497
- ].join(' ')
498
- end
499
-
500
- if puppet_environment
501
- commands << [
502
- sudo('ln -s '), config[:root_path], File.join(puppet_dir, config[:puppet_environment])
503
- ].join(' ')
504
- end
505
-
506
- if spec_files_path && spec_files_remote_path
507
- commands << [
508
- sudo('mkdir -p'), spec_files_remote_path
509
- ].join(' ')
510
- commands << [
511
- sudo('cp -r'), File.join(config[:root_path], 'spec/*'), spec_files_remote_path
512
- ].join(' ')
513
- end
514
-
515
- command = commands.join(' && ')
516
- debug(command)
517
- command
518
- end
519
-
520
- def run_command
521
- if !config[:puppet_apply_command].nil?
522
- return config[:puppet_apply_command]
523
- else
524
- result = [
525
- facterlib,
526
- custom_facts,
527
- puppet_manifestdir,
528
- puppet_cmd,
529
- 'apply',
530
- File.join(config[:root_path], 'manifests', manifest),
531
- "--modulepath=#{File.join(config[:root_path], 'modules')}",
532
- "--fileserverconfig=#{File.join(config[:root_path], 'fileserver.conf')}",
533
- custom_options,
534
- puppet_environment_flag,
535
- puppet_noop_flag,
536
- puppet_detailed_exitcodes_flag,
537
- puppet_verbose_flag,
538
- puppet_debug_flag,
539
- puppet_logdest_flag,
540
- remove_repo
541
- ].join(' ')
542
- info("Going to invoke puppet apply with: #{result}")
543
- result
544
- end
545
- end
546
-
547
- protected
548
-
549
- def load_needed_dependencies!
550
- return unless File.exist?(puppetfile)
551
- return unless config[:resolve_with_librarian_puppet]
552
- debug("Puppetfile found at #{puppetfile}, loading Librarian-Puppet")
553
- Puppet::Librarian.load!(logger)
554
- end
555
-
556
- def tmpmodules_dir
557
- File.join(sandbox_path, 'modules')
558
- end
559
-
560
- def puppetfile
561
- config[:puppetfile_path] || ''
562
- end
563
-
564
- def modulefile
565
- config[:modulefile_path] || ''
566
- end
567
-
568
- def metadata_json
569
- config[:metadata_json_path] || ''
570
- end
571
-
572
- def manifest
573
- config[:manifest]
574
- end
575
-
576
- def manifests
577
- config[:manifests_path]
578
- end
579
-
580
- def modules
581
- config[:modules_path]
582
- end
583
-
584
- def files
585
- config[:files_path] || 'files'
586
- end
587
-
588
- def puppet_config
589
- config[:puppet_config_path]
590
- end
591
-
592
- def puppet_environment
593
- config[:puppet_environment]
594
- end
595
-
596
- def puppet_git_init
597
- config[:puppet_git_init]
598
- end
599
-
600
- def puppet_git_pr
601
- config[:puppet_git_pr]
602
- end
603
-
604
- def hiera_config
605
- config[:hiera_config_path]
606
- end
607
-
608
- def fileserver_config
609
- config[:fileserver_config_path]
610
- end
611
-
612
- def hiera_data
613
- config[:hiera_data_path]
614
- end
615
-
616
- def hiera_data_remote_path
617
- config[:hiera_data_remote_path]
618
- end
619
-
620
- def hiera_eyaml
621
- config[:hiera_eyaml]
622
- end
623
-
624
- def hiera_eyaml_key_path
625
- config[:hiera_eyaml_key_path]
626
- end
627
-
628
- def hiera_eyaml_key_remote_path
629
- config[:hiera_eyaml_key_remote_path]
630
- end
631
-
632
- def hiera_deep_merge
633
- config[:hiera_deep_merge]
634
- end
635
-
636
- def librarian_puppet_ssl_file
637
- config[:librarian_puppet_ssl_file]
638
- end
639
-
640
- def puppet_cmd
641
- if config[:require_puppet_collections]
642
- sudo_env("#{config[:puppet_coll_remote_path]}/bin/puppet")
643
- else
644
- sudo_env('puppet')
645
- end
646
- end
647
-
648
- def puppet_dir
649
- if config[:require_puppet_collections]
650
- '/etc/puppetlabs/puppet'
651
- else
652
- '/etc/puppet'
653
- end
654
- end
655
-
656
- def puppet_debian_version
657
- config[:puppet_version] ? "=#{config[:puppet_version]}" : nil
658
- end
659
-
660
- def facter_debian_version
661
- config[:facter_version] ? "=#{config[:facter_version]}" : nil
662
- end
663
-
664
- def puppet_hiera_debian_version
665
- config[:hiera_version] ? "=#{config[:hiera_version]}" : nil
666
- end
667
-
668
- def puppet_redhat_version
669
- config[:puppet_version] ? "-#{config[:puppet_version]}" : nil
670
- end
671
-
672
- def puppet_environment_flag
673
- if config[:puppet_version] =~ /^2/
674
- config[:puppet_environment] ? "--environment=#{config[:puppet_environment]}" : nil
675
- else
676
- config[:puppet_environment] ? "--environment=#{config[:puppet_environment]} --environmentpath=#{puppet_dir}" : nil
677
- end
678
- end
679
-
680
- def puppet_manifestdir
681
- return nil if config[:require_puppet_collections]
682
- return nil if config[:puppet_environment]
683
- bash_vars = "export MANIFESTDIR='#{File.join(config[:root_path], 'manifests')}';"
684
- debug(bash_vars)
685
- bash_vars
686
- end
687
-
688
- def custom_options
689
- config[:custom_options] || ''
690
- end
691
-
692
- def puppet_noop_flag
693
- config[:puppet_noop] ? '--noop' : nil
694
- end
695
-
696
- def puppet_debug_flag
697
- config[:puppet_debug] ? '-d' : nil
698
- end
699
-
700
- def puppet_verbose_flag
701
- config[:puppet_verbose] ? '-v' : nil
702
- end
703
-
704
- def puppet_logdest_flag
705
- return nil unless config[:puppet_logdest]
706
- destinations = ''
707
- config[:puppet_logdest].each do |dest|
708
- destinations << "--logdest #{dest} "
709
- end
710
- destinations
711
- end
712
-
713
- def puppet_platform
714
- config[:platform].gsub(/-.*/, '')
715
- end
716
-
717
- def update_packages_debian_cmd
718
- config[:update_package_repos] ? "#{sudo_env('apt-get')} update" : nil
719
- end
720
-
721
- def update_packages_redhat_cmd
722
- # #{sudo('yum')}
723
- config[:update_package_repos] ? "#{sudo_env('yum')} makecache" : nil
724
- end
725
-
726
- def sudo_env(pm)
727
- s = https_proxy ? "https_proxy=#{https_proxy}" : nil
728
- p = http_proxy ? "http_proxy=#{http_proxy}" : nil
729
- p || s ? "#{sudo('env')} #{p} #{s} #{pm}" : sudo(pm).to_s
730
- end
731
-
732
- def remove_puppet_repo
733
- config[:remove_puppet_repo]
734
- end
735
-
736
- def spec_files_path
737
- config[:spec_files_path]
738
- end
739
-
740
- def spec_files_remote_path
741
- config[:spec_files_remote_path]
742
- end
743
-
744
- def facterlib
745
- factpath = nil
746
- factpath = File.join(config[:root_path], 'facter').to_s if config[:install_custom_facts] && !config[:custom_facts].none?
747
- factpath = File.join(config[:root_path], 'facter').to_s if config[:facter_file]
748
- factpath = "#{factpath}:" if config[:facterlib] && !factpath.nil?
749
- factpath = "#{factpath}#{config[:facterlib]}" if config[:facterlib]
750
- return nil if factpath.nil?
751
- bash_vars = "export FACTERLIB='#{factpath}';"
752
- debug(bash_vars)
753
- bash_vars
754
- end
755
-
756
- def custom_facts
757
- return nil if config[:custom_facts].none?
758
- return nil if config[:install_custom_facts]
759
- bash_vars = config[:custom_facts].map { |k, v| "FACTER_#{k}=#{v}" }.join(' ')
760
- bash_vars = "export #{bash_vars};"
761
- debug(bash_vars)
762
- bash_vars
763
- end
764
-
765
- def puppet_detailed_exitcodes_flag
766
- config[:puppet_detailed_exitcodes] ? '--detailed-exitcodes' : nil
767
- end
768
-
769
- def remove_repo
770
- remove_puppet_repo ? "; #{sudo('rm')} -rf /tmp/kitchen #{hiera_data_remote_path} #{hiera_eyaml_key_remote_path} #{puppet_dir}/* " : nil
771
- end
772
-
773
- def puppet_apt_repo
774
- platform_version = config[:platform].partition('-')[2]
775
- case puppet_platform
776
- when 'ubuntu'
777
- case platform_version
778
- when '14.10'
779
- # Utopic Repo
780
- 'https://apt.puppetlabs.com/puppetlabs-release-utopic.deb'
781
- when '14.04'
782
- # Trusty Repo
783
- 'https://apt.puppetlabs.com/puppetlabs-release-trusty.deb'
784
- when '12.04'
785
- # Precise Repo
786
- 'https://apt.puppetlabs.com/puppetlabs-release-precise.deb'
787
- else
788
- # Configured Repo
789
- config[:puppet_apt_repo]
790
- end
791
- when 'debian'
792
- case platform_version.gsub(/\..*/, '')
793
- when '8'
794
- # Debian Jessie
795
- 'https://apt.puppetlabs.com/puppetlabs-release-jessie.deb'
796
- when '7'
797
- # Debian Wheezy
798
- 'https://apt.puppetlabs.com/puppetlabs-release-wheezy.deb'
799
- when '6'
800
- # Debian Squeeze
801
- 'https://apt.puppetlabs.com/puppetlabs-release-squeeze.deb'
802
- else
803
- # Configured Repo
804
- config[:puppet_apt_repo]
805
- end
806
- else
807
- debug("Apt repo detection failed with platform - #{config[:platform]}")
808
- false
809
- end
810
- end
811
-
812
- def puppet_apt_repo_file
813
- puppet_apt_repo.split('/').last if puppet_apt_repo
814
- end
815
-
816
- def puppet_apt_coll_repo_file
817
- config[:puppet_apt_collections_repo].split('/').last
818
- end
819
-
820
- def puppet_yum_repo
821
- config[:puppet_yum_repo]
822
- end
823
-
824
- def proxy_parm
825
- http_proxy ? "--httpproxy #{URI.parse(http_proxy).host.downcase} --httpport #{URI.parse(http_proxy).port} " : nil
826
- end
827
-
828
- def gem_proxy_parm
829
- http_proxy ? "--http-proxy #{http_proxy}" : nil
830
- end
831
-
832
- def wget_proxy_parm
833
- p = http_proxy ? "-e http_proxy=#{http_proxy}" : nil
834
- s = https_proxy ? "-e https_proxy=#{https_proxy}" : nil
835
- p || s ? "-e use_proxy=yes #{p} #{s}" : nil
836
- end
837
-
838
- def export_http_proxy_parm
839
- http_proxy ? "export http_proxy=#{http_proxy}" : nil
840
- end
841
-
842
- def export_https_proxy_parm
843
- http_proxy ? "export https_proxy=#{http_proxy}" : nil
844
- end
845
-
846
- def http_proxy
847
- config[:http_proxy]
848
- end
849
-
850
- def https_proxy
851
- config[:https_proxy]
852
- end
853
-
854
- def chef_url
855
- config[:chef_bootstrap_url]
856
- end
857
-
858
- def prepare_manifests
859
- info('Preparing manifests')
860
- debug("Using manifests from #{manifests}")
861
-
862
- tmp_manifests_dir = File.join(sandbox_path, 'manifests')
863
- FileUtils.mkdir_p(tmp_manifests_dir)
864
- FileUtils.cp_r(Dir.glob("#{manifests}/*"), tmp_manifests_dir)
865
- end
866
-
867
- def prepare_files
868
- info('Preparing files')
869
- unless File.directory?(files)
870
- info 'nothing to do for files'
871
- return
872
- end
873
-
874
- debug("Using files from #{files}")
875
-
876
- tmp_files_dir = File.join(sandbox_path, 'files')
877
- FileUtils.mkdir_p(tmp_files_dir)
878
- FileUtils.cp_r(Dir.glob("#{files}/*"), tmp_files_dir)
879
- end
880
-
881
- def prepare_facter_file
882
- return unless config[:facter_file]
883
- info 'Copying facter file'
884
- facter_dir = File.join(sandbox_path, 'facter')
885
- FileUtils.mkdir_p(facter_dir)
886
- FileUtils.cp_r(config[:facter_file], facter_dir)
887
- end
888
-
889
- def prepare_facts
890
- return unless config[:install_custom_facts]
891
- return unless config[:custom_facts]
892
- info 'Installing custom facts'
893
- facter_dir = File.join(sandbox_path, 'facter')
894
- FileUtils.mkdir_p(facter_dir)
895
- tmp_facter_file = File.join(facter_dir, 'kitchen.rb')
896
- facter_facts = Hash[config[:custom_facts].map { |k, v| [k.to_s, v.to_s] }]
897
- File.open(tmp_facter_file, 'a') do |out|
898
- facter_facts.each do |k, v|
899
- out.write "\nFacter.add(:#{k}) do\n"
900
- out.write " setcode do\n"
901
- out.write " \"#{v}\"\n"
902
- out.write " end\n"
903
- out.write "end\n"
904
- end
905
- end
906
- end
907
-
908
- def prepare_modules
909
- info('Preparing modules')
910
-
911
- FileUtils.mkdir_p(tmpmodules_dir)
912
- resolve_with_librarian if File.exist?(puppetfile) && config[:resolve_with_librarian_puppet]
913
-
914
- if modules && File.directory?(modules)
915
- debug("Copying modules from #{modules} to #{tmpmodules_dir}")
916
- FileUtils.cp_r(Dir.glob("#{modules}/*"), tmpmodules_dir, remove_destination: true)
917
- else
918
- info 'nothing to do for modules'
919
- end
920
-
921
- copy_self_as_module
922
- end
923
-
924
- def copy_self_as_module
925
- if File.exist?(modulefile)
926
- warn('Modulefile found but this is depricated, ignoring it, see https://tickets.puppetlabs.com/browse/PUP-1188')
927
- end
928
-
929
- return unless File.exist?(metadata_json)
930
- module_name = nil
931
- begin
932
- module_name = JSON.parse(IO.read(metadata_json))['name'].split('-').last
933
- rescue
934
- error("not able to load or parse #{metadata_json_path} for the name of the module")
935
- end
936
-
937
- return unless module_name
938
- module_target_path = File.join(sandbox_path, 'modules', module_name)
939
- FileUtils.mkdir_p(module_target_path)
940
- FileUtils.cp_r(
941
- Dir.glob(File.join(config[:kitchen_root], '*')).reject { |entry| entry =~ /modules$|spec$|pkg$/ },
942
- module_target_path,
943
- remove_destination: true
944
- )
945
- end
946
-
947
- def prepare_puppet_config
948
- return unless puppet_config
949
-
950
- info('Preparing puppet.conf')
951
- debug("Using puppet config from #{puppet_config}")
952
-
953
- FileUtils.cp_r(puppet_config, File.join(sandbox_path, 'puppet.conf'))
954
- end
955
-
956
- def prepare_hiera_config
957
- return unless hiera_config
958
-
959
- info('Preparing hiera')
960
- debug("Using hiera from #{hiera_config}")
961
-
962
- FileUtils.cp_r(hiera_config, File.join(sandbox_path, 'hiera.yaml'))
963
- end
964
-
965
- def prepare_fileserver_config
966
- return unless fileserver_config
967
-
968
- info('Preparing fileserver')
969
- debug("Using fileserver config from #{fileserver_config}")
970
-
971
- FileUtils.cp_r(fileserver_config, File.join(sandbox_path, 'fileserver.conf'))
972
- end
973
-
974
- def prepare_hiera_data
975
- return unless hiera_data
976
- info('Preparing hiera data')
977
- tmp_hiera_dir = File.join(sandbox_path, 'hiera')
978
- debug("Copying hiera data from #{hiera_data} to #{tmp_hiera_dir}")
979
- FileUtils.mkdir_p(tmp_hiera_dir)
980
- FileUtils.cp_r(Dir.glob("#{hiera_data}/*"), tmp_hiera_dir)
981
- return unless hiera_eyaml_key_path
982
- tmp_hiera_key_dir = File.join(sandbox_path, 'hiera_keys')
983
- debug("Copying hiera eyaml keys from #{hiera_eyaml_key_path} to #{tmp_hiera_key_dir}")
984
- FileUtils.mkdir_p(tmp_hiera_key_dir)
985
- FileUtils.cp_r(Dir.glob("#{hiera_eyaml_key_path}/*"), tmp_hiera_key_dir)
986
- end
987
-
988
- def prepare_spec_files
989
- return unless spec_files_path
990
- info('Preparing spec files')
991
- tmp_spec_dir = File.join(sandbox_path, 'spec')
992
- debug("Copying specs from #{spec_files_path} to #{tmp_spec_dir}")
993
- FileUtils.mkdir_p(tmp_spec_dir)
994
- FileUtils.cp_r(Dir.glob("#{spec_files_path}/*"), tmp_spec_dir)
995
- end
996
-
997
- def resolve_with_librarian
998
- Kitchen.mutex.synchronize do
999
- ENV['SSL_CERT_FILE'] = librarian_puppet_ssl_file if librarian_puppet_ssl_file
1000
- Puppet::Librarian.new(puppetfile, tmpmodules_dir, logger).resolve
1001
- ENV['SSL_CERT_FILE'] = '' if librarian_puppet_ssl_file
1002
- end
1003
- end
1004
- end
1005
- end
1006
- end
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # Author:: Chris Lundquist (<chris.lundquist@github.com>) Neill Turner (<neillwturner@gmail.com>)
4
+ #
5
+ # Copyright (C) 2013,2014 Chris Lundquist, Neill Turner
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+ # See https://github.com/neillturner/kitchen-puppet/blob/master/provisioner_options.md
20
+ # for documentation configuration parameters with puppet_apply provisioner.
21
+ #
22
+
23
+ require 'uri'
24
+ require 'json'
25
+ require 'kitchen'
26
+ require 'kitchen/provisioner/puppet/librarian'
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, false
49
+ default_config :puppet_yum_collections_repo, 'http://yum.puppetlabs.com/puppetlabs-release-pc1-el-6.noarch.rpm'
50
+ default_config :puppet_apt_collections_repo, 'http://apt.puppetlabs.com/puppetlabs-release-pc1-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 :require_puppet_repo, true
58
+ default_config :require_chef_for_busser, true
59
+ default_config :resolve_with_librarian_puppet, true
60
+ default_config :puppet_environment, nil
61
+ default_config :puppet_apt_repo, 'http://apt.puppetlabs.com/puppetlabs-release-precise.deb'
62
+ default_config :puppet_yum_repo, 'https://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm'
63
+ default_config :chef_bootstrap_url, 'https://www.getchef.com/chef/install.sh'
64
+ default_config :puppet_logdest, nil
65
+ default_config :custom_install_command, nil
66
+
67
+ default_config :puppet_apply_command, nil
68
+
69
+ default_config :puppet_git_init, nil
70
+ default_config :puppet_git_pr, nil
71
+
72
+ default_config :http_proxy, nil
73
+ default_config :https_proxy, nil
74
+
75
+ default_config :hiera_data_remote_path, '/var/lib/hiera'
76
+ default_config :manifest, 'site.pp'
77
+
78
+ default_config :manifests_path do |provisioner|
79
+ provisioner.calculate_path('manifests') ||
80
+ raise('No manifests_path detected. Please specify one in .kitchen.yml')
81
+ end
82
+
83
+ default_config :modules_path do |provisioner|
84
+ modules_path = provisioner.calculate_path('modules')
85
+ if modules_path.nil? && provisioner.calculate_path('Puppetfile', :file).nil?
86
+ raise('No modules_path detected. Please specify one in .kitchen.yml')
87
+ end
88
+ modules_path
89
+ end
90
+
91
+ default_config :files_path do |provisioner|
92
+ provisioner.calculate_path('files') || 'files'
93
+ end
94
+
95
+ default_config :hiera_data_path do |provisioner|
96
+ provisioner.calculate_path('hiera')
97
+ end
98
+
99
+ default_config :puppet_config_path do |provisioner|
100
+ provisioner.calculate_path('puppet.conf', :file)
101
+ end
102
+
103
+ default_config :hiera_config_path do |provisioner|
104
+ provisioner.calculate_path('hiera.yaml', :file)
105
+ end
106
+
107
+ default_config :fileserver_config_path do |provisioner|
108
+ provisioner.calculate_path('fileserver.conf', :file)
109
+ end
110
+ default_config :puppetfile_path do |provisioner|
111
+ provisioner.calculate_path('Puppetfile', :file)
112
+ end
113
+
114
+ default_config :modulefile_path do |provisioner|
115
+ provisioner.calculate_path('Modulefile', :file)
116
+ end
117
+
118
+ default_config :metadata_json_path do |provisioner|
119
+ provisioner.calculate_path('metadata.json', :file)
120
+ end
121
+
122
+ default_config :manifests_path do |provisioner|
123
+ provisioner.calculate_path('manifests', :directory)
124
+ end
125
+
126
+ default_config :spec_files_path do |provisioner|
127
+ provisioner.calculate_path('spec', :directory)
128
+ end
129
+
130
+ default_config :spec_files_remote_path, '/etc/puppet/spec'
131
+
132
+ default_config :puppet_debug, false
133
+ default_config :puppet_verbose, false
134
+ default_config :puppet_noop, false
135
+ default_config :platform, &:platform_name
136
+ default_config :update_package_repos, true
137
+ default_config :remove_puppet_repo, false
138
+ default_config :install_custom_facts, false
139
+ default_config :custom_facts, {}
140
+ default_config :facterlib, nil
141
+ default_config :puppet_detailed_exitcodes, nil
142
+ default_config :facter_file, nil
143
+ default_config :librarian_puppet_ssl_file, nil
144
+
145
+ default_config :hiera_eyaml, false
146
+ default_config :hiera_eyaml_key_remote_path, '/etc/puppet/secure/keys'
147
+
148
+ default_config :hiera_eyaml_key_path do |provisioner|
149
+ provisioner.calculate_path('hiera_keys')
150
+ end
151
+
152
+ default_config :hiera_deep_merge, false
153
+
154
+ def calculate_path(path, type = :directory)
155
+ base = config[:test_base_path]
156
+ candidates = []
157
+ candidates << File.join(base, instance.suite.name, 'puppet', path)
158
+ candidates << File.join(base, instance.suite.name, path)
159
+ candidates << File.join(base, path)
160
+ candidates << File.join(Dir.pwd, path)
161
+
162
+ candidates.find do |c|
163
+ type == :directory ? File.directory?(c) : File.file?(c)
164
+ end
165
+ end
166
+
167
+ def install_command
168
+ return unless config[:require_puppet_collections] || config[:require_puppet_repo]
169
+ if config[:require_puppet_collections]
170
+ install_command_collections
171
+ else
172
+ case puppet_platform
173
+ when 'debian', 'ubuntu'
174
+ info("Installing puppet on #{config[:platform]}")
175
+ <<-INSTALL
176
+ if [ ! $(which puppet) ]; then
177
+ #{sudo('apt-get')} -y install wget
178
+ #{sudo('wget')} #{wget_proxy_parm} #{puppet_apt_repo}
179
+ #{sudo('dpkg')} -i #{puppet_apt_repo_file}
180
+ #{update_packages_debian_cmd}
181
+ #{sudo_env('apt-get')} -y install facter#{facter_debian_version}
182
+ #{sudo_env('apt-get')} -y install puppet-common#{puppet_debian_version}
183
+ #{sudo_env('apt-get')} -y install puppet#{puppet_debian_version}
184
+ #{install_hiera}
185
+ fi
186
+ #{install_eyaml}
187
+ #{install_deep_merge}
188
+ #{install_busser}
189
+ #{custom_install_command}
190
+ INSTALL
191
+ when 'redhat', 'centos', 'fedora', 'oracle', 'amazon'
192
+ info("Installing puppet from yum on #{puppet_platform}")
193
+ <<-INSTALL
194
+ if [ ! $(which puppet) ]; then
195
+ #{install_puppet_yum_repo}
196
+ fi
197
+ #{install_eyaml}
198
+ #{install_deep_merge}
199
+ #{install_busser}
200
+ #{custom_install_command}
201
+ INSTALL
202
+ else
203
+ info('Installing puppet, will try to determine platform os')
204
+ <<-INSTALL
205
+ if [ ! $(which puppet) ]; then
206
+ if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ]; then
207
+ #{install_puppet_yum_repo}
208
+ else
209
+ if [ -f /etc/system-release ] || [ grep -q 'Amazon Linux' /etc/system-release ]; then
210
+ #{install_puppet_yum_repo}
211
+ else
212
+ #{sudo('apt-get')} -y install wget
213
+ #{sudo('wget')} #{wget_proxy_parm} #{puppet_apt_repo}
214
+ #{sudo('dpkg')} -i #{puppet_apt_repo_file}
215
+ #{update_packages_debian_cmd}
216
+ #{sudo_env('apt-get')} -y install facter#{facter_debian_version}
217
+ #{sudo_env('apt-get')} -y install puppet-common#{puppet_debian_version}
218
+ #{sudo_env('apt-get')} -y install puppet#{puppet_debian_version}
219
+ #{install_hiera}
220
+ fi
221
+ fi
222
+ fi
223
+ #{install_eyaml}
224
+ #{install_deep_merge}
225
+ #{install_busser}
226
+ #{custom_install_command}
227
+ INSTALL
228
+ end
229
+ end
230
+ end
231
+
232
+ def install_command_collections
233
+ case puppet_platform
234
+ when 'debian', 'ubuntu'
235
+ info("Installing Puppet Collections on #{puppet_platform}")
236
+ <<-INSTALL
237
+ #{Util.shell_helpers}
238
+ if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
239
+ if [ ! -f "#{config[:puppet_apt_collections_repo]}" ]; then
240
+ #{sudo('apt-get')} -y install wget
241
+ #{sudo('wget')} #{wget_proxy_parm} #{config[:puppet_apt_collections_repo]}
242
+ fi
243
+ #{sudo('dpkg')} -i #{puppet_apt_coll_repo_file}
244
+ #{sudo('apt-get')} update
245
+ #{sudo_env('apt-get')} -y install puppet-agent#{puppet_debian_version}
246
+ fi
247
+ #{install_eyaml("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
248
+ #{install_deep_merge}
249
+ #{install_busser}
250
+ #{custom_install_command}
251
+ INSTALL
252
+ when 'redhat', 'centos', 'fedora', 'oracle', 'amazon'
253
+ info("Installing Puppet Collections on #{puppet_platform}")
254
+ <<-INSTALL
255
+ #{Util.shell_helpers}
256
+ if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
257
+ echo "-----> #{sudo_env('yum')} -y localinstall #{config[:puppet_yum_collections_repo]}"
258
+ #{sudo_env('yum')} -y localinstall #{config[:puppet_yum_collections_repo]}
259
+ #{sudo_env('yum')} -y install puppet-agent#{puppet_redhat_version}
260
+ fi
261
+ #{install_eyaml("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
262
+ #{install_deep_merge}
263
+ #{install_busser}
264
+ #{custom_install_command}
265
+ INSTALL
266
+ else
267
+ info('Installing Puppet Collections, will try to determine platform os')
268
+ <<-INSTALL
269
+ #{Util.shell_helpers}
270
+ if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
271
+ if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ] || \
272
+ [ -f /etc/system-release ] || [ grep -q 'Amazon Linux' /etc/system-release ]; then
273
+ echo "-----> #{sudo_env('yum')} -y localinstall #{config[:puppet_yum_collections_repo]}"
274
+ #{sudo_env('yum')} -y localinstall #{config[:puppet_yum_collections_repo]}
275
+ #{sudo_env('yum')} -y install puppet-agent#{puppet_redhat_version}
276
+ else
277
+ #{sudo('apt-get')} -y install wget
278
+ #{sudo('wget')} #{wget_proxy_parm} #{config[:puppet_apt_collections_repo]}
279
+ #{sudo('dpkg')} -i #{puppet_apt_coll_repo_file}
280
+ #{sudo('apt-get')} update
281
+ #{sudo_env('apt-get')} -y install puppet-agent#{puppet_debian_version}
282
+ fi
283
+ fi
284
+ #{install_eyaml("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
285
+ #{install_deep_merge}
286
+ #{install_busser}
287
+ #{custom_install_command}
288
+ INSTALL
289
+ end
290
+ end
291
+
292
+ def install_deep_merge
293
+ return unless config[:hiera_deep_merge]
294
+ <<-INSTALL
295
+ # Support for hash merge lookups to recursively merge hash keys
296
+ if [[ $(#{sudo('gem')} list deep_merge -i) == 'false' ]]; then
297
+ echo '-----> Installing deep_merge to provide deep_merge of hiera hashes'
298
+ #{sudo('gem')} install #{gem_proxy_parm} --no-ri --no-rdoc deep_merge
299
+ fi
300
+ INSTALL
301
+ end
302
+
303
+ def install_eyaml(gem_cmd = 'gem')
304
+ return unless config[:hiera_eyaml]
305
+ <<-INSTALL
306
+ # A backend for Hiera that provides per-value asymmetric encryption of sensitive data
307
+ if [[ $(#{sudo(gem_cmd)} list hiera-eyaml -i) == 'false' ]]; then
308
+ echo '-----> Installing hiera-eyaml to provide encryption of hiera data'
309
+ #{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc highline -v 1.6.21
310
+ #{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc hiera-eyaml
311
+ fi
312
+ INSTALL
313
+ end
314
+
315
+ def install_busser
316
+ return unless config[:require_chef_for_busser]
317
+ <<-INSTALL
318
+ #{Util.shell_helpers}
319
+ # install chef omnibus so that busser works as this is needed to run tests :(
320
+ # TODO: work out how to install enough ruby
321
+ # and set busser: { :ruby_bindir => '/usr/bin/ruby' } so that we dont need the
322
+ # whole chef client
323
+ if [ ! -d "/opt/chef" ]
324
+ then
325
+ echo '-----> Installing Chef Omnibus to install busser to run tests'
326
+ #{export_http_proxy_parm}
327
+ #{export_https_proxy_parm}
328
+ do_download #{chef_url} /tmp/install.sh
329
+ #{sudo('sh')} /tmp/install.sh
330
+ fi
331
+ INSTALL
332
+ end
333
+
334
+ def install_hiera
335
+ return unless config[:install_hiera]
336
+ <<-INSTALL
337
+ #{sudo_env('apt-get')} -y install #{hiera_package}
338
+ INSTALL
339
+ end
340
+
341
+ def hiera_package
342
+ "#{config[:hiera_package]}#{puppet_hiera_debian_version}"
343
+ end
344
+
345
+ # /bin/wget -P /etc/pki/rpm-gpg/ http://yum.puppetlabs.com/RPM-GPG-KEY-puppetlabs
346
+ # changed to curl
347
+
348
+ def install_puppet_yum_repo
349
+ <<-INSTALL
350
+ rhelversion=$(cat /etc/redhat-release | grep 'release 7')
351
+ # For CentOS7/RHEL7 the rdo release contains puppetlabs repo, creating conflict. Create temp-repo
352
+ #{sudo_env('curl')} -o /etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs http://yum.puppetlabs.com/RPM-GPG-KEY-puppetlabs
353
+ if [ -n "$rhelversion" ]; then
354
+ echo '[puppettemp-products]
355
+ name=Puppet Labs Products - \$basearch
356
+ baseurl=http://yum.puppetlabs.com/el/7/products/\$basearch
357
+ gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
358
+ enabled=0
359
+ gpgcheck=1
360
+ [puppettemp-deps]
361
+ name=Puppet Labs Dependencies - \$basearch
362
+ baseurl=http://yum.puppetlabs.com/el/7/dependencies/\$basearch
363
+ gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
364
+ enabled=0
365
+ gpgcheck=1' | sudo tee /etc/yum.repos.d/puppettemp.repo > /dev/null
366
+ sudo sed -i 's/^[ \t]*//' /etc/yum.repos.d/puppettemp.repo
367
+ #{update_packages_redhat_cmd}
368
+ #{sudo_env('yum')} -y --enablerepo=puppettemp-products --enablerepo=puppettemp-deps install puppet#{puppet_redhat_version}
369
+ # Clean up temporary puppet repo
370
+ sudo rm -rf /etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
371
+ sudo rm -rf /etc/yum.repos.d/puppettemp.repo
372
+ else
373
+ #{sudo('rpm')} -ivh #{proxy_parm} #{puppet_yum_repo}
374
+ #{update_packages_redhat_cmd}
375
+ #{sudo_env('yum')} -y install puppet#{puppet_redhat_version}
376
+ fi
377
+ INSTALL
378
+ end
379
+
380
+ def custom_install_command
381
+ <<-INSTALL
382
+ #{config[:custom_install_command]}
383
+ INSTALL
384
+ end
385
+
386
+ def init_command
387
+ dirs = %w(modules manifests files hiera hiera.yaml facter spec)
388
+ .map { |dir| File.join(config[:root_path], dir) }.join(' ')
389
+ cmd = "#{sudo('rm')} -rf #{dirs} #{hiera_data_remote_path} \
390
+ /etc/hiera.yaml #{puppet_dir}/hiera.yaml \
391
+ #{spec_files_remote_path} \
392
+ #{puppet_dir}/fileserver.conf;"
393
+ cmd += config[:puppet_environment] ? "#{sudo('rm')} -f #{File.join(puppet_dir, config[:puppet_environment])};" : ''
394
+ cmd += " mkdir -p #{config[:root_path]}; #{sudo('mkdir')} -p #{puppet_dir}"
395
+ debug(cmd)
396
+ cmd
397
+ end
398
+
399
+ def create_sandbox
400
+ super
401
+ debug("Creating local sandbox in #{sandbox_path}")
402
+ yield if block_given?
403
+ prepare_modules
404
+ prepare_manifests
405
+ prepare_files
406
+ prepare_facter_file
407
+ prepare_facts
408
+ prepare_puppet_config
409
+ prepare_hiera_config
410
+ prepare_fileserver_config
411
+ prepare_hiera_data
412
+ prepare_spec_files
413
+ info('Finished Preparing files for transfer')
414
+ end
415
+
416
+ def cleanup_sandbox
417
+ return if sandbox_path.nil?
418
+ debug("Cleaning up local sandbox in #{sandbox_path}")
419
+ FileUtils.rmtree(sandbox_path)
420
+ end
421
+
422
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
423
+ def prepare_command
424
+ commands = []
425
+ if puppet_git_init
426
+ commands << [
427
+ sudo('rm -rf'), '/etc/puppet'
428
+ ].join(' ')
429
+
430
+ commands << [
431
+ sudo('git clone'), puppet_git_init, '/etc/puppet'
432
+ ].join(' ')
433
+ end
434
+
435
+ if puppet_git_pr
436
+ commands << [sudo('git'),
437
+ '--git-dir=/etc/puppet/.git/',
438
+ 'fetch -f',
439
+ 'origin',
440
+ "pull/#{puppet_git_pr}/head:pr_#{puppet_git_pr}"
441
+ ].join(' ')
442
+
443
+ commands << [sudo('git'), '--git-dir=/etc/puppet/.git/',
444
+ '--work-tree=/etc/puppet/',
445
+ 'checkout',
446
+ "pr_#{puppet_git_pr}"
447
+ ].join(' ')
448
+ end
449
+
450
+ if puppet_config
451
+ commands << [
452
+ sudo('cp'),
453
+ File.join(config[:root_path], 'puppet.conf'),
454
+ puppet_dir
455
+ ].join(' ')
456
+ end
457
+
458
+ if hiera_config
459
+ commands << [
460
+ sudo('cp'), File.join(config[:root_path], 'hiera.yaml'), '/etc/'
461
+ ].join(' ')
462
+
463
+ commands << [
464
+ sudo('cp'), File.join(config[:root_path], 'hiera.yaml'), hiera_config_dir
465
+ ].join(' ')
466
+ end
467
+
468
+ if fileserver_config
469
+ commands << [
470
+ sudo('cp'),
471
+ File.join(config[:root_path], 'fileserver.conf'),
472
+ puppet_dir
473
+ ].join(' ')
474
+ end
475
+
476
+ if hiera_data && hiera_data_remote_path == '/var/lib/hiera'
477
+ commands << [
478
+ sudo('cp -r'), File.join(config[:root_path], 'hiera'), '/var/lib/'
479
+ ].join(' ')
480
+ end
481
+
482
+ if hiera_data && hiera_data_remote_path != '/var/lib/hiera'
483
+ commands << [
484
+ sudo('mkdir -p'), hiera_data_remote_path
485
+ ].join(' ')
486
+ commands << [
487
+ sudo('cp -r'), File.join(config[:root_path], 'hiera/*'), hiera_data_remote_path
488
+ ].join(' ')
489
+ end
490
+
491
+ if hiera_eyaml
492
+ commands << [
493
+ sudo('mkdir -p'), hiera_eyaml_key_remote_path
494
+ ].join(' ')
495
+ commands << [
496
+ sudo('cp -r'), File.join(config[:root_path], 'hiera_keys/*'), hiera_eyaml_key_remote_path
497
+ ].join(' ')
498
+ end
499
+
500
+ if puppet_environment
501
+ commands << [
502
+ sudo('ln -s '), config[:root_path], File.join(puppet_dir, config[:puppet_environment])
503
+ ].join(' ')
504
+ end
505
+
506
+ if spec_files_path && spec_files_remote_path
507
+ commands << [
508
+ sudo('mkdir -p'), spec_files_remote_path
509
+ ].join(' ')
510
+ commands << [
511
+ sudo('cp -r'), File.join(config[:root_path], 'spec/*'), spec_files_remote_path
512
+ ].join(' ')
513
+ end
514
+
515
+ command = commands.join(' && ')
516
+ debug(command)
517
+ command
518
+ end
519
+
520
+ def run_command
521
+ if !config[:puppet_apply_command].nil?
522
+ return config[:puppet_apply_command]
523
+ else
524
+ result = [
525
+ facterlib,
526
+ custom_facts,
527
+ puppet_manifestdir,
528
+ puppet_cmd,
529
+ 'apply',
530
+ File.join(config[:root_path], 'manifests', manifest),
531
+ "--modulepath=#{File.join(config[:root_path], 'modules')}",
532
+ "--fileserverconfig=#{File.join(config[:root_path], 'fileserver.conf')}",
533
+ custom_options,
534
+ puppet_environment_flag,
535
+ puppet_noop_flag,
536
+ puppet_detailed_exitcodes_flag,
537
+ puppet_verbose_flag,
538
+ puppet_debug_flag,
539
+ puppet_logdest_flag,
540
+ remove_repo
541
+ ].join(' ')
542
+ info("Going to invoke puppet apply with: #{result}")
543
+ result
544
+ end
545
+ end
546
+
547
+ protected
548
+
549
+ def load_needed_dependencies!
550
+ return unless File.exist?(puppetfile)
551
+ return unless config[:resolve_with_librarian_puppet]
552
+ debug("Puppetfile found at #{puppetfile}, loading Librarian-Puppet")
553
+ Puppet::Librarian.load!(logger)
554
+ end
555
+
556
+ def tmpmodules_dir
557
+ File.join(sandbox_path, 'modules')
558
+ end
559
+
560
+ def puppetfile
561
+ config[:puppetfile_path] || ''
562
+ end
563
+
564
+ def modulefile
565
+ config[:modulefile_path] || ''
566
+ end
567
+
568
+ def metadata_json
569
+ config[:metadata_json_path] || ''
570
+ end
571
+
572
+ def manifest
573
+ config[:manifest]
574
+ end
575
+
576
+ def manifests
577
+ config[:manifests_path]
578
+ end
579
+
580
+ def modules
581
+ config[:modules_path]
582
+ end
583
+
584
+ def files
585
+ config[:files_path] || 'files'
586
+ end
587
+
588
+ def puppet_config
589
+ config[:puppet_config_path]
590
+ end
591
+
592
+ def puppet_environment
593
+ config[:puppet_environment]
594
+ end
595
+
596
+ def puppet_git_init
597
+ config[:puppet_git_init]
598
+ end
599
+
600
+ def puppet_git_pr
601
+ config[:puppet_git_pr]
602
+ end
603
+
604
+ def hiera_config
605
+ config[:hiera_config_path]
606
+ end
607
+
608
+ def fileserver_config
609
+ config[:fileserver_config_path]
610
+ end
611
+
612
+ def hiera_data
613
+ config[:hiera_data_path]
614
+ end
615
+
616
+ def hiera_data_remote_path
617
+ config[:hiera_data_remote_path]
618
+ end
619
+
620
+ def hiera_eyaml
621
+ config[:hiera_eyaml]
622
+ end
623
+
624
+ def hiera_eyaml_key_path
625
+ config[:hiera_eyaml_key_path]
626
+ end
627
+
628
+ def hiera_eyaml_key_remote_path
629
+ config[:hiera_eyaml_key_remote_path]
630
+ end
631
+
632
+ def hiera_deep_merge
633
+ config[:hiera_deep_merge]
634
+ end
635
+
636
+ def librarian_puppet_ssl_file
637
+ config[:librarian_puppet_ssl_file]
638
+ end
639
+
640
+ def puppet_cmd
641
+ if config[:require_puppet_collections]
642
+ sudo_env("#{config[:puppet_coll_remote_path]}/bin/puppet")
643
+ else
644
+ sudo_env('puppet')
645
+ end
646
+ end
647
+
648
+ def puppet_dir
649
+ if config[:require_puppet_collections]
650
+ '/etc/puppetlabs/puppet'
651
+ else
652
+ '/etc/puppet'
653
+ end
654
+ end
655
+
656
+ def hiera_config_dir
657
+ if config[:require_puppet_collections]
658
+ '/etc/puppetlabs/code'
659
+ else
660
+ '/etc/puppet'
661
+ end
662
+ end
663
+
664
+ def puppet_debian_version
665
+ config[:puppet_version] ? "=#{config[:puppet_version]}" : nil
666
+ end
667
+
668
+ def facter_debian_version
669
+ config[:facter_version] ? "=#{config[:facter_version]}" : nil
670
+ end
671
+
672
+ def puppet_hiera_debian_version
673
+ config[:hiera_version] ? "=#{config[:hiera_version]}" : nil
674
+ end
675
+
676
+ def puppet_redhat_version
677
+ config[:puppet_version] ? "-#{config[:puppet_version]}" : nil
678
+ end
679
+
680
+ def puppet_environment_flag
681
+ if config[:puppet_version] =~ /^2/
682
+ config[:puppet_environment] ? "--environment=#{config[:puppet_environment]}" : nil
683
+ else
684
+ config[:puppet_environment] ? "--environment=#{config[:puppet_environment]} --environmentpath=#{puppet_dir}" : nil
685
+ end
686
+ end
687
+
688
+ def puppet_manifestdir
689
+ return nil if config[:require_puppet_collections]
690
+ return nil if config[:puppet_environment]
691
+ bash_vars = "export MANIFESTDIR='#{File.join(config[:root_path], 'manifests')}';"
692
+ debug(bash_vars)
693
+ bash_vars
694
+ end
695
+
696
+ def custom_options
697
+ config[:custom_options] || ''
698
+ end
699
+
700
+ def puppet_noop_flag
701
+ config[:puppet_noop] ? '--noop' : nil
702
+ end
703
+
704
+ def puppet_debug_flag
705
+ config[:puppet_debug] ? '-d' : nil
706
+ end
707
+
708
+ def puppet_verbose_flag
709
+ config[:puppet_verbose] ? '-v' : nil
710
+ end
711
+
712
+ def puppet_logdest_flag
713
+ return nil unless config[:puppet_logdest]
714
+ destinations = ''
715
+ config[:puppet_logdest].each do |dest|
716
+ destinations << "--logdest #{dest} "
717
+ end
718
+ destinations
719
+ end
720
+
721
+ def puppet_platform
722
+ config[:platform].gsub(/-.*/, '')
723
+ end
724
+
725
+ def update_packages_debian_cmd
726
+ config[:update_package_repos] ? "#{sudo_env('apt-get')} update" : nil
727
+ end
728
+
729
+ def update_packages_redhat_cmd
730
+ # #{sudo('yum')}
731
+ config[:update_package_repos] ? "#{sudo_env('yum')} makecache" : nil
732
+ end
733
+
734
+ def sudo_env(pm)
735
+ s = https_proxy ? "https_proxy=#{https_proxy}" : nil
736
+ p = http_proxy ? "http_proxy=#{http_proxy}" : nil
737
+ p || s ? "#{sudo('env')} #{p} #{s} #{pm}" : sudo(pm).to_s
738
+ end
739
+
740
+ def remove_puppet_repo
741
+ config[:remove_puppet_repo]
742
+ end
743
+
744
+ def spec_files_path
745
+ config[:spec_files_path]
746
+ end
747
+
748
+ def spec_files_remote_path
749
+ config[:spec_files_remote_path]
750
+ end
751
+
752
+ def facterlib
753
+ factpath = nil
754
+ factpath = File.join(config[:root_path], 'facter').to_s if config[:install_custom_facts] && !config[:custom_facts].none?
755
+ factpath = File.join(config[:root_path], 'facter').to_s if config[:facter_file]
756
+ factpath = "#{factpath}:" if config[:facterlib] && !factpath.nil?
757
+ factpath = "#{factpath}#{config[:facterlib]}" if config[:facterlib]
758
+ return nil if factpath.nil?
759
+ bash_vars = "export FACTERLIB='#{factpath}';"
760
+ debug(bash_vars)
761
+ bash_vars
762
+ end
763
+
764
+ def custom_facts
765
+ return nil if config[:custom_facts].none?
766
+ return nil if config[:install_custom_facts]
767
+ bash_vars = config[:custom_facts].map { |k, v| "FACTER_#{k}=#{v}" }.join(' ')
768
+ bash_vars = "export #{bash_vars};"
769
+ debug(bash_vars)
770
+ bash_vars
771
+ end
772
+
773
+ def puppet_detailed_exitcodes_flag
774
+ config[:puppet_detailed_exitcodes] ? '--detailed-exitcodes' : nil
775
+ end
776
+
777
+ def remove_repo
778
+ remove_puppet_repo ? "; #{sudo('rm')} -rf /tmp/kitchen #{hiera_data_remote_path} #{hiera_eyaml_key_remote_path} #{puppet_dir}/* " : nil
779
+ end
780
+
781
+ def puppet_apt_repo
782
+ platform_version = config[:platform].partition('-')[2]
783
+ case puppet_platform
784
+ when 'ubuntu'
785
+ case platform_version
786
+ when '14.10'
787
+ # Utopic Repo
788
+ 'https://apt.puppetlabs.com/puppetlabs-release-utopic.deb'
789
+ when '14.04'
790
+ # Trusty Repo
791
+ 'https://apt.puppetlabs.com/puppetlabs-release-trusty.deb'
792
+ when '12.04'
793
+ # Precise Repo
794
+ 'https://apt.puppetlabs.com/puppetlabs-release-precise.deb'
795
+ else
796
+ # Configured Repo
797
+ config[:puppet_apt_repo]
798
+ end
799
+ when 'debian'
800
+ case platform_version.gsub(/\..*/, '')
801
+ when '8'
802
+ # Debian Jessie
803
+ 'https://apt.puppetlabs.com/puppetlabs-release-jessie.deb'
804
+ when '7'
805
+ # Debian Wheezy
806
+ 'https://apt.puppetlabs.com/puppetlabs-release-wheezy.deb'
807
+ when '6'
808
+ # Debian Squeeze
809
+ 'https://apt.puppetlabs.com/puppetlabs-release-squeeze.deb'
810
+ else
811
+ # Configured Repo
812
+ config[:puppet_apt_repo]
813
+ end
814
+ else
815
+ debug("Apt repo detection failed with platform - #{config[:platform]}")
816
+ false
817
+ end
818
+ end
819
+
820
+ def puppet_apt_repo_file
821
+ puppet_apt_repo.split('/').last if puppet_apt_repo
822
+ end
823
+
824
+ def puppet_apt_coll_repo_file
825
+ config[:puppet_apt_collections_repo].split('/').last
826
+ end
827
+
828
+ def puppet_yum_repo
829
+ config[:puppet_yum_repo]
830
+ end
831
+
832
+ def proxy_parm
833
+ http_proxy ? "--httpproxy #{URI.parse(http_proxy).host.downcase} --httpport #{URI.parse(http_proxy).port} " : nil
834
+ end
835
+
836
+ def gem_proxy_parm
837
+ http_proxy ? "--http-proxy #{http_proxy}" : nil
838
+ end
839
+
840
+ def wget_proxy_parm
841
+ p = http_proxy ? "-e http_proxy=#{http_proxy}" : nil
842
+ s = https_proxy ? "-e https_proxy=#{https_proxy}" : nil
843
+ p || s ? "-e use_proxy=yes #{p} #{s}" : nil
844
+ end
845
+
846
+ def export_http_proxy_parm
847
+ http_proxy ? "export http_proxy=#{http_proxy}" : nil
848
+ end
849
+
850
+ def export_https_proxy_parm
851
+ http_proxy ? "export https_proxy=#{http_proxy}" : nil
852
+ end
853
+
854
+ def http_proxy
855
+ config[:http_proxy]
856
+ end
857
+
858
+ def https_proxy
859
+ config[:https_proxy]
860
+ end
861
+
862
+ def chef_url
863
+ config[:chef_bootstrap_url]
864
+ end
865
+
866
+ def prepare_manifests
867
+ info('Preparing manifests')
868
+ debug("Using manifests from #{manifests}")
869
+
870
+ tmp_manifests_dir = File.join(sandbox_path, 'manifests')
871
+ FileUtils.mkdir_p(tmp_manifests_dir)
872
+ FileUtils.cp_r(Dir.glob("#{manifests}/*"), tmp_manifests_dir)
873
+ end
874
+
875
+ def prepare_files
876
+ info('Preparing files')
877
+ unless File.directory?(files)
878
+ info 'nothing to do for files'
879
+ return
880
+ end
881
+
882
+ debug("Using files from #{files}")
883
+
884
+ tmp_files_dir = File.join(sandbox_path, 'files')
885
+ FileUtils.mkdir_p(tmp_files_dir)
886
+ FileUtils.cp_r(Dir.glob("#{files}/*"), tmp_files_dir)
887
+ end
888
+
889
+ def prepare_facter_file
890
+ return unless config[:facter_file]
891
+ info 'Copying facter file'
892
+ facter_dir = File.join(sandbox_path, 'facter')
893
+ FileUtils.mkdir_p(facter_dir)
894
+ FileUtils.cp_r(config[:facter_file], facter_dir)
895
+ end
896
+
897
+ def prepare_facts
898
+ return unless config[:install_custom_facts]
899
+ return unless config[:custom_facts]
900
+ info 'Installing custom facts'
901
+ facter_dir = File.join(sandbox_path, 'facter')
902
+ FileUtils.mkdir_p(facter_dir)
903
+ tmp_facter_file = File.join(facter_dir, 'kitchen.rb')
904
+ facter_facts = Hash[config[:custom_facts].map { |k, v| [k.to_s, v.to_s] }]
905
+ File.open(tmp_facter_file, 'a') do |out|
906
+ facter_facts.each do |k, v|
907
+ out.write "\nFacter.add(:#{k}) do\n"
908
+ out.write " setcode do\n"
909
+ out.write " \"#{v}\"\n"
910
+ out.write " end\n"
911
+ out.write "end\n"
912
+ end
913
+ end
914
+ end
915
+
916
+ def prepare_modules
917
+ info('Preparing modules')
918
+
919
+ FileUtils.mkdir_p(tmpmodules_dir)
920
+ resolve_with_librarian if File.exist?(puppetfile) && config[:resolve_with_librarian_puppet]
921
+
922
+ if modules && File.directory?(modules)
923
+ debug("Copying modules from #{modules} to #{tmpmodules_dir}")
924
+ FileUtils.cp_r(Dir.glob("#{modules}/*"), tmpmodules_dir, remove_destination: true)
925
+ else
926
+ info 'nothing to do for modules'
927
+ end
928
+
929
+ copy_self_as_module
930
+ end
931
+
932
+ def copy_self_as_module
933
+ if File.exist?(modulefile)
934
+ warn('Modulefile found but this is depricated, ignoring it, see https://tickets.puppetlabs.com/browse/PUP-1188')
935
+ end
936
+
937
+ return unless File.exist?(metadata_json)
938
+ module_name = nil
939
+ begin
940
+ module_name = JSON.parse(IO.read(metadata_json))['name'].split('-').last
941
+ rescue
942
+ error("not able to load or parse #{metadata_json_path} for the name of the module")
943
+ end
944
+
945
+ return unless module_name
946
+ module_target_path = File.join(sandbox_path, 'modules', module_name)
947
+ FileUtils.mkdir_p(module_target_path)
948
+ FileUtils.cp_r(
949
+ Dir.glob(File.join(config[:kitchen_root], '*')).reject { |entry| entry =~ /modules$|spec$|pkg$/ },
950
+ module_target_path,
951
+ remove_destination: true
952
+ )
953
+ end
954
+
955
+ def prepare_puppet_config
956
+ return unless puppet_config
957
+
958
+ info('Preparing puppet.conf')
959
+ debug("Using puppet config from #{puppet_config}")
960
+
961
+ FileUtils.cp_r(puppet_config, File.join(sandbox_path, 'puppet.conf'))
962
+ end
963
+
964
+ def prepare_hiera_config
965
+ return unless hiera_config
966
+
967
+ info('Preparing hiera')
968
+ debug("Using hiera from #{hiera_config}")
969
+
970
+ FileUtils.cp_r(hiera_config, File.join(sandbox_path, 'hiera.yaml'))
971
+ end
972
+
973
+ def prepare_fileserver_config
974
+ return unless fileserver_config
975
+
976
+ info('Preparing fileserver')
977
+ debug("Using fileserver config from #{fileserver_config}")
978
+
979
+ FileUtils.cp_r(fileserver_config, File.join(sandbox_path, 'fileserver.conf'))
980
+ end
981
+
982
+ def prepare_hiera_data
983
+ return unless hiera_data
984
+ info('Preparing hiera data')
985
+ tmp_hiera_dir = File.join(sandbox_path, 'hiera')
986
+ debug("Copying hiera data from #{hiera_data} to #{tmp_hiera_dir}")
987
+ FileUtils.mkdir_p(tmp_hiera_dir)
988
+ FileUtils.cp_r(Dir.glob("#{hiera_data}/*"), tmp_hiera_dir)
989
+ return unless hiera_eyaml_key_path
990
+ tmp_hiera_key_dir = File.join(sandbox_path, 'hiera_keys')
991
+ debug("Copying hiera eyaml keys from #{hiera_eyaml_key_path} to #{tmp_hiera_key_dir}")
992
+ FileUtils.mkdir_p(tmp_hiera_key_dir)
993
+ FileUtils.cp_r(Dir.glob("#{hiera_eyaml_key_path}/*"), tmp_hiera_key_dir)
994
+ end
995
+
996
+ def prepare_spec_files
997
+ return unless spec_files_path
998
+ info('Preparing spec files')
999
+ tmp_spec_dir = File.join(sandbox_path, 'spec')
1000
+ debug("Copying specs from #{spec_files_path} to #{tmp_spec_dir}")
1001
+ FileUtils.mkdir_p(tmp_spec_dir)
1002
+ FileUtils.cp_r(Dir.glob("#{spec_files_path}/*"), tmp_spec_dir)
1003
+ end
1004
+
1005
+ def resolve_with_librarian
1006
+ Kitchen.mutex.synchronize do
1007
+ ENV['SSL_CERT_FILE'] = librarian_puppet_ssl_file if librarian_puppet_ssl_file
1008
+ Puppet::Librarian.new(puppetfile, tmpmodules_dir, logger).resolve
1009
+ ENV['SSL_CERT_FILE'] = '' if librarian_puppet_ssl_file
1010
+ end
1011
+ end
1012
+ end
1013
+ end
1014
+ end