kitchen-puppet 1.0.35 → 1.0.36

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,998 +1,1006 @@
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
- fail('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
- fail '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
- # TODO: refactor for smaller cyclomatic complexity and perceived complexity
168
- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
169
- def install_command
170
- return unless config[:require_puppet_collections] || config[:require_puppet_repo]
171
- if config[:require_puppet_collections]
172
- install_command_collections
173
- else
174
- case puppet_platform
175
- when 'debian', 'ubuntu'
176
- info("Installing puppet on #{config[:platform]}")
177
- <<-INSTALL
178
- if [ ! $(which puppet) ]; then
179
- #{sudo('apt-get')} -y install wget
180
- #{sudo('wget')} #{wget_proxy_parm} #{puppet_apt_repo}
181
- #{sudo('dpkg')} -i #{puppet_apt_repo_file}
182
- #{update_packages_debian_cmd}
183
- #{sudo_env('apt-get')} -y install facter#{facter_debian_version}
184
- #{sudo_env('apt-get')} -y install puppet-common#{puppet_debian_version}
185
- #{sudo_env('apt-get')} -y install puppet#{puppet_debian_version}
186
- #{install_hiera}
187
- fi
188
- #{install_eyaml}
189
- #{install_deep_merge}
190
- #{install_busser}
191
- #{custom_install_command}
192
- INSTALL
193
- when 'redhat', 'centos', 'fedora', 'oracle', 'amazon'
194
- info("Installing puppet from yum on #{puppet_platform}")
195
- <<-INSTALL
196
- if [ ! $(which puppet) ]; then
197
- #{install_puppet_yum_repo}
198
- fi
199
- #{install_eyaml}
200
- #{install_deep_merge}
201
- #{install_busser}
202
- #{custom_install_command}
203
- INSTALL
204
- else
205
- info('Installing puppet, will try to determine platform os')
206
- <<-INSTALL
207
- if [ ! $(which puppet) ]; then
208
- if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ]; then
209
- #{install_puppet_yum_repo}
210
- else
211
- if [ -f /etc/system-release ] || [ grep -q 'Amazon Linux' /etc/system-release ]; then
212
- #{install_puppet_yum_repo}
213
- else
214
- #{sudo('apt-get')} -y install wget
215
- #{sudo('wget')} #{wget_proxy_parm} #{puppet_apt_repo}
216
- #{sudo('dpkg')} -i #{puppet_apt_repo_file}
217
- #{update_packages_debian_cmd}
218
- #{sudo_env('apt-get')} -y install facter#{facter_debian_version}
219
- #{sudo_env('apt-get')} -y install puppet-common#{puppet_debian_version}
220
- #{sudo_env('apt-get')} -y install puppet#{puppet_debian_version}
221
- #{install_hiera}
222
- fi
223
- fi
224
- fi
225
- #{install_eyaml}
226
- #{install_deep_merge}
227
- #{install_busser}
228
- #{custom_install_command}
229
- INSTALL
230
- end
231
- end
232
- end
233
-
234
- def install_command_collections
235
- case puppet_platform
236
- when 'debian', 'ubuntu'
237
- info("Installing Puppet Collections on #{puppet_platform}")
238
- <<-INSTALL
239
- #{sudo('apt-get')} -y install wget
240
- #{sudo('wget')} #{wget_proxy_parm} #{config[:puppet_apt_collections_repo]}
241
- #{sudo('dpkg')} -i #{puppet_apt_coll_repo_file}
242
- #{custom_install_command}
243
- INSTALL
244
- when 'redhat', 'centos', 'fedora', 'oracle', 'amazon'
245
- info("Installing Puppet Collections on #{puppet_platform}")
246
- <<-INSTALL
247
- #{Util.shell_helpers}
248
- if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
249
- echo "-----> #{sudo_env('yum')} -y localinstall #{config[:puppet_yum_collections_repo]}"
250
- #{sudo_env('yum')} -y localinstall #{config[:puppet_yum_collections_repo]}
251
- #{sudo_env('yum')} -y install puppet
252
- fi
253
- #{install_eyaml("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
254
- #{install_deep_merge}
255
- #{install_busser}
256
- #{custom_install_command}
257
- INSTALL
258
- else
259
- info('Installing Puppet Collections, will try to determine platform os')
260
- <<-INSTALL
261
- if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
262
- if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ]; then
263
- #{Util.shell_helpers}
264
- if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
265
- echo "-----> #{sudo_env('yum')} -y localinstall #{config[:puppet_yum_collections_repo]}"
266
- #{sudo_env('yum')} -y localinstall #{config[:puppet_yum_collections_repo]}
267
- #{sudo_env('yum')} -y install puppet
268
- fi
269
- else
270
- if [ -f /etc/system-release ] || [ grep -q 'Amazon Linux' /etc/system-release ]; then
271
- #{Util.shell_helpers}
272
- if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; 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
276
- fi
277
- else
278
- #{sudo('apt-get')} -y install wget
279
- #{sudo('wget')} #{wget_proxy_parm} #{config[:puppet_apt_collections_repo]}
280
- #{sudo('dpkg')} -i #{puppet_apt_coll_repo_file}
281
- fi
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
- do_download #{chef_url} /tmp/install.sh
327
- #{sudo('sh')} /tmp/install.sh
328
- fi
329
- INSTALL
330
- end
331
-
332
- def install_hiera
333
- return unless config[:install_hiera]
334
- <<-INSTALL
335
- #{sudo_env('apt-get')} -y install #{hiera_package}
336
- INSTALL
337
- end
338
-
339
- def hiera_package
340
- "#{config[:hiera_package]}#{puppet_hiera_debian_version}"
341
- end
342
-
343
- # /bin/wget -P /etc/pki/rpm-gpg/ http://yum.puppetlabs.com/RPM-GPG-KEY-puppetlabs
344
- # changed to curl
345
-
346
- def install_puppet_yum_repo
347
- <<-INSTALL
348
- rhelversion=$(cat /etc/redhat-release | grep 'release 7')
349
- # For CentOS7/RHEL7 the rdo release contains puppetlabs repo, creating conflict. Create temp-repo
350
- #{sudo_env('curl')} -o /etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs http://yum.puppetlabs.com/RPM-GPG-KEY-puppetlabs
351
- if [ -n "$rhelversion" ]; then
352
- echo '[puppettemp-products]
353
- name=Puppet Labs Products - \$basearch
354
- baseurl=http://yum.puppetlabs.com/el/7/products/\$basearch
355
- gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
356
- enabled=0
357
- gpgcheck=1
358
- [puppettemp-deps]
359
- name=Puppet Labs Dependencies - \$basearch
360
- baseurl=http://yum.puppetlabs.com/el/7/dependencies/\$basearch
361
- gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
362
- enabled=0
363
- gpgcheck=1' | sudo tee /etc/yum.repos.d/puppettemp.repo > /dev/null
364
- sudo sed -i 's/^[ \t]*//' /etc/yum.repos.d/puppettemp.repo
365
- #{update_packages_redhat_cmd}
366
- #{sudo_env('yum')} -y --enablerepo=puppettemp-products --enablerepo=puppettemp-deps install puppet#{puppet_redhat_version}
367
- # Clean up temporary puppet repo
368
- sudo rm -rf /etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
369
- sudo rm -rf /etc/yum.repos.d/puppettemp.repo
370
- else
371
- #{sudo('rpm')} -ivh #{proxy_parm} #{puppet_yum_repo}
372
- #{update_packages_redhat_cmd}
373
- #{sudo_env('yum')} -y install puppet#{puppet_redhat_version}
374
- fi
375
- INSTALL
376
- end
377
-
378
- def custom_install_command
379
- <<-INSTALL
380
- #{config[:custom_install_command]}
381
- INSTALL
382
- end
383
-
384
- def init_command
385
- dirs = %w(modules manifests files hiera hiera.yaml facter spec)
386
- .map { |dir| File.join(config[:root_path], dir) }.join(' ')
387
- cmd = "#{sudo('rm')} -rf #{dirs} #{hiera_data_remote_path} \
388
- /etc/hiera.yaml #{puppet_dir}/hiera.yaml \
389
- #{spec_files_remote_path} \
390
- #{puppet_dir}/fileserver.conf;"
391
- cmd += config[:puppet_environment] ? "#{sudo('rm')} -f #{File.join(puppet_dir, config[:puppet_environment])};" : ''
392
- cmd += " mkdir -p #{config[:root_path]}; #{sudo('mkdir')} -p #{puppet_dir}"
393
- debug(cmd)
394
- cmd
395
- end
396
-
397
- def create_sandbox
398
- super
399
- debug("Creating local sandbox in #{sandbox_path}")
400
- yield if block_given?
401
- prepare_modules
402
- prepare_manifests
403
- prepare_files
404
- prepare_facter_file
405
- prepare_facts
406
- prepare_puppet_config
407
- prepare_hiera_config
408
- prepare_fileserver_config
409
- prepare_hiera_data
410
- prepare_spec_files
411
- info('Finished Preparing files for transfer')
412
- end
413
-
414
- def cleanup_sandbox
415
- return if sandbox_path.nil?
416
- debug("Cleaning up local sandbox in #{sandbox_path}")
417
- FileUtils.rmtree(sandbox_path)
418
- end
419
-
420
- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
421
- def prepare_command
422
- commands = []
423
- if puppet_git_init
424
- commands << [
425
- sudo('rm -rf'), '/etc/puppet'
426
- ].join(' ')
427
-
428
- commands << [
429
- sudo('git clone'), puppet_git_init, '/etc/puppet'
430
- ].join(' ')
431
- end
432
-
433
- if puppet_git_pr
434
- commands << [sudo('git'),
435
- '--git-dir=/etc/puppet/.git/',
436
- 'fetch -f',
437
- 'origin',
438
- "pull/#{puppet_git_pr}/head:pr_#{puppet_git_pr}"
439
- ].join(' ')
440
-
441
- commands << [sudo('git'), '--git-dir=/etc/puppet/.git/',
442
- '--work-tree=/etc/puppet/',
443
- 'checkout',
444
- "pr_#{puppet_git_pr}"
445
- ].join(' ')
446
- end
447
-
448
- if puppet_config
449
- commands << [
450
- sudo('cp'),
451
- File.join(config[:root_path], 'puppet.conf'),
452
- puppet_dir
453
- ].join(' ')
454
- end
455
-
456
- if hiera_config
457
- commands << [
458
- sudo('cp'), File.join(config[:root_path], 'hiera.yaml'), '/etc/'
459
- ].join(' ')
460
-
461
- commands << [
462
- sudo('cp'), File.join(config[:root_path], 'hiera.yaml'), puppet_dir
463
- ].join(' ')
464
- end
465
-
466
- if fileserver_config
467
- commands << [
468
- sudo('cp'),
469
- File.join(config[:root_path], 'fileserver.conf'),
470
- puppet_dir
471
- ].join(' ')
472
- end
473
-
474
- if hiera_data && hiera_data_remote_path == '/var/lib/hiera'
475
- commands << [
476
- sudo('cp -r'), File.join(config[:root_path], 'hiera'), '/var/lib/'
477
- ].join(' ')
478
- end
479
-
480
- if hiera_data && hiera_data_remote_path != '/var/lib/hiera'
481
- commands << [
482
- sudo('mkdir -p'), hiera_data_remote_path
483
- ].join(' ')
484
- commands << [
485
- sudo('cp -r'), File.join(config[:root_path], 'hiera/*'), hiera_data_remote_path
486
- ].join(' ')
487
- end
488
-
489
- if hiera_eyaml
490
- commands << [
491
- sudo('mkdir -p'), hiera_eyaml_key_remote_path
492
- ].join(' ')
493
- commands << [
494
- sudo('cp -r'), File.join(config[:root_path], 'hiera_keys/*'), hiera_eyaml_key_remote_path
495
- ].join(' ')
496
- end
497
-
498
- if puppet_environment
499
- commands << [
500
- sudo('ln -s '), config[:root_path], File.join(puppet_dir, config[:puppet_environment])
501
- ].join(' ')
502
- end
503
-
504
- if spec_files_path && spec_files_remote_path
505
- commands << [
506
- sudo('mkdir -p'), spec_files_remote_path
507
- ].join(' ')
508
- commands << [
509
- sudo('cp -r'), File.join(config[:root_path], 'spec/*'), spec_files_remote_path
510
- ].join(' ')
511
- end
512
-
513
- command = commands.join(' && ')
514
- debug(command)
515
- command
516
- end
517
- # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
518
-
519
- def run_command
520
- if !config[:puppet_apply_command].nil?
521
- return config[:puppet_apply_command]
522
- else
523
- result = [
524
- facterlib,
525
- custom_facts,
526
- puppet_manifestdir,
527
- puppet_cmd,
528
- 'apply',
529
- File.join(config[:root_path], 'manifests', manifest),
530
- "--modulepath=#{File.join(config[:root_path], 'modules')}",
531
- "--fileserverconfig=#{File.join(config[:root_path], 'fileserver.conf')}",
532
- custom_options,
533
- puppet_environment_flag,
534
- puppet_noop_flag,
535
- puppet_detailed_exitcodes_flag,
536
- puppet_verbose_flag,
537
- puppet_debug_flag,
538
- puppet_logdest_flag,
539
- remove_repo
540
- ].join(' ')
541
- info("Going to invoke puppet apply with: #{result}")
542
- result
543
- end
544
- end
545
-
546
- protected
547
-
548
- def load_needed_dependencies!
549
- return unless File.exist?(puppetfile)
550
- return unless config[:resolve_with_librarian_puppet]
551
- debug("Puppetfile found at #{puppetfile}, loading Librarian-Puppet")
552
- Puppet::Librarian.load!(logger)
553
- end
554
-
555
- def tmpmodules_dir
556
- File.join(sandbox_path, 'modules')
557
- end
558
-
559
- def puppetfile
560
- config[:puppetfile_path] || ''
561
- end
562
-
563
- def modulefile
564
- config[:modulefile_path] || ''
565
- end
566
-
567
- def metadata_json
568
- config[:metadata_json_path] || ''
569
- end
570
-
571
- def manifest
572
- config[:manifest]
573
- end
574
-
575
- def manifests
576
- config[:manifests_path]
577
- end
578
-
579
- def modules
580
- config[:modules_path]
581
- end
582
-
583
- def files
584
- config[:files_path] || 'files'
585
- end
586
-
587
- def puppet_config
588
- config[:puppet_config_path]
589
- end
590
-
591
- def puppet_environment
592
- config[:puppet_environment]
593
- end
594
-
595
- def puppet_git_init
596
- config[:puppet_git_init]
597
- end
598
-
599
- def puppet_git_pr
600
- config[:puppet_git_pr]
601
- end
602
-
603
- def hiera_config
604
- config[:hiera_config_path]
605
- end
606
-
607
- def fileserver_config
608
- config[:fileserver_config_path]
609
- end
610
-
611
- def hiera_data
612
- config[:hiera_data_path]
613
- end
614
-
615
- def hiera_data_remote_path
616
- config[:hiera_data_remote_path]
617
- end
618
-
619
- def hiera_eyaml
620
- config[:hiera_eyaml]
621
- end
622
-
623
- def hiera_eyaml_key_path
624
- config[:hiera_eyaml_key_path]
625
- end
626
-
627
- def hiera_eyaml_key_remote_path
628
- config[:hiera_eyaml_key_remote_path]
629
- end
630
-
631
- def hiera_deep_merge
632
- config[:hiera_deep_merge]
633
- end
634
-
635
- def librarian_puppet_ssl_file
636
- config[:librarian_puppet_ssl_file]
637
- end
638
-
639
- def puppet_cmd
640
- if config[:require_puppet_collections]
641
- sudo_env("#{config[:puppet_coll_remote_path]}/bin/puppet")
642
- else
643
- sudo_env('puppet')
644
- end
645
- end
646
-
647
- def puppet_dir
648
- if config[:require_puppet_collections]
649
- '/etc/puppetlabs/puppet'
650
- else
651
- '/etc/puppet'
652
- end
653
- end
654
-
655
- def puppet_debian_version
656
- config[:puppet_version] ? "=#{config[:puppet_version]}" : nil
657
- end
658
-
659
- def facter_debian_version
660
- config[:facter_version] ? "=#{config[:facter_version]}" : nil
661
- end
662
-
663
- def puppet_hiera_debian_version
664
- config[:hiera_version] ? "=#{config[:hiera_version]}" : nil
665
- end
666
-
667
- def puppet_redhat_version
668
- config[:puppet_version] ? "-#{config[:puppet_version]}" : nil
669
- end
670
-
671
- def puppet_environment_flag
672
- if config[:puppet_version] =~ /^2/
673
- config[:puppet_environment] ? "--environment=#{config[:puppet_environment]}" : nil
674
- else
675
- config[:puppet_environment] ? "--environment=#{config[:puppet_environment]} --environmentpath=#{puppet_dir}" : nil
676
- end
677
- end
678
-
679
- def puppet_manifestdir
680
- return nil if config[:require_puppet_collections]
681
- return nil if config[:puppet_environment]
682
- bash_vars = "export MANIFESTDIR='#{File.join(config[:root_path], 'manifests')}';"
683
- debug(bash_vars)
684
- bash_vars
685
- end
686
-
687
- def custom_options
688
- config[:custom_options] || ''
689
- end
690
-
691
- def puppet_noop_flag
692
- config[:puppet_noop] ? '--noop' : nil
693
- end
694
-
695
- def puppet_debug_flag
696
- config[:puppet_debug] ? '-d' : nil
697
- end
698
-
699
- def puppet_verbose_flag
700
- config[:puppet_verbose] ? '-v' : nil
701
- end
702
-
703
- def puppet_logdest_flag
704
- return nil unless config[:puppet_logdest]
705
- destinations = ''
706
- config[:puppet_logdest].each do |dest|
707
- destinations << "--logdest #{dest} "
708
- end
709
- destinations
710
- end
711
-
712
- def puppet_platform
713
- config[:platform].gsub(/-.*/, '')
714
- end
715
-
716
- def update_packages_debian_cmd
717
- config[:update_package_repos] ? "#{sudo_env('apt-get')} update" : nil
718
- end
719
-
720
- def update_packages_redhat_cmd
721
- # #{sudo('yum')}
722
- config[:update_package_repos] ? "#{sudo_env('yum')} makecache" : nil
723
- end
724
-
725
- def sudo_env(pm)
726
- s = https_proxy ? "https_proxy=#{https_proxy}" : nil
727
- p = http_proxy ? "http_proxy=#{http_proxy}" : nil
728
- p || s ? "#{sudo('env')} #{p} #{s} #{pm}" : "#{sudo(pm)}"
729
- end
730
-
731
- def remove_puppet_repo
732
- config[:remove_puppet_repo]
733
- end
734
-
735
- def spec_files_path
736
- config[:spec_files_path]
737
- end
738
-
739
- def spec_files_remote_path
740
- config[:spec_files_remote_path]
741
- end
742
-
743
- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
744
- def facterlib
745
- factpath = nil
746
- factpath = "#{File.join(config[:root_path], 'facter')}" if config[:install_custom_facts] && !config[:custom_facts].none?
747
- factpath = "#{File.join(config[:root_path], 'facter')}" 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 http_proxy=#{http_proxy}" : nil
835
- p || s ? "-e use_proxy=yes #{p} #{s}" : nil
836
- end
837
-
838
- def http_proxy
839
- config[:http_proxy]
840
- end
841
-
842
- def https_proxy
843
- config[:https_proxy]
844
- end
845
-
846
- def chef_url
847
- config[:chef_bootstrap_url]
848
- end
849
-
850
- def prepare_manifests
851
- info('Preparing manifests')
852
- debug("Using manifests from #{manifests}")
853
-
854
- tmp_manifests_dir = File.join(sandbox_path, 'manifests')
855
- FileUtils.mkdir_p(tmp_manifests_dir)
856
- FileUtils.cp_r(Dir.glob("#{manifests}/*"), tmp_manifests_dir)
857
- end
858
-
859
- def prepare_files
860
- info('Preparing files')
861
- unless File.directory?(files)
862
- info 'nothing to do for files'
863
- return
864
- end
865
-
866
- debug("Using files from #{files}")
867
-
868
- tmp_files_dir = File.join(sandbox_path, 'files')
869
- FileUtils.mkdir_p(tmp_files_dir)
870
- FileUtils.cp_r(Dir.glob("#{files}/*"), tmp_files_dir)
871
- end
872
-
873
- def prepare_facter_file
874
- return unless config[:facter_file]
875
- info 'Copying facter file'
876
- facter_dir = File.join(sandbox_path, 'facter')
877
- FileUtils.mkdir_p(facter_dir)
878
- FileUtils.cp_r(config[:facter_file], facter_dir)
879
- end
880
-
881
- def prepare_facts
882
- return unless config[:install_custom_facts]
883
- return unless config[:custom_facts]
884
- info 'Installing custom facts'
885
- facter_dir = File.join(sandbox_path, 'facter')
886
- FileUtils.mkdir_p(facter_dir)
887
- tmp_facter_file = File.join(facter_dir, 'kitchen.rb')
888
- facter_facts = Hash[config[:custom_facts].map { |k, v| [k.to_s, v.to_s] }]
889
- File.open(tmp_facter_file, 'a') do |out|
890
- facter_facts.each do |k, v|
891
- out.write "\nFacter.add(:#{k}) do\n"
892
- out.write " setcode do\n"
893
- out.write " \"#{v}\"\n"
894
- out.write " end\n"
895
- out.write "end\n"
896
- end
897
- end
898
- end
899
-
900
- def prepare_modules
901
- info('Preparing modules')
902
-
903
- FileUtils.mkdir_p(tmpmodules_dir)
904
- resolve_with_librarian if File.exist?(puppetfile) && config[:resolve_with_librarian_puppet]
905
-
906
- if modules && File.directory?(modules)
907
- debug("Copying modules from #{modules} to #{tmpmodules_dir}")
908
- FileUtils.cp_r(Dir.glob("#{modules}/*"), tmpmodules_dir, remove_destination: true)
909
- else
910
- info 'nothing to do for modules'
911
- end
912
-
913
- copy_self_as_module
914
- end
915
-
916
- def copy_self_as_module
917
- if File.exist?(modulefile)
918
- warn('Modulefile found but this is depricated, ignoring it, see https://tickets.puppetlabs.com/browse/PUP-1188')
919
- end
920
-
921
- return unless File.exist?(metadata_json)
922
- module_name = nil
923
- begin
924
- module_name = JSON.parse(IO.read(metadata_json))['name'].split('-').last
925
- rescue
926
- error("not able to load or parse #{metadata_json_path} for the name of the module")
927
- end
928
-
929
- return unless module_name
930
- module_target_path = File.join(sandbox_path, 'modules', module_name)
931
- FileUtils.mkdir_p(module_target_path)
932
- FileUtils.cp_r(
933
- Dir.glob(File.join(config[:kitchen_root], '*')).reject { |entry| entry =~ /modules$|spec$|pkg$/ },
934
- module_target_path,
935
- remove_destination: true
936
- )
937
- end
938
-
939
- def prepare_puppet_config
940
- return unless puppet_config
941
-
942
- info('Preparing puppet.conf')
943
- debug("Using puppet config from #{puppet_config}")
944
-
945
- FileUtils.cp_r(puppet_config, File.join(sandbox_path, 'puppet.conf'))
946
- end
947
-
948
- def prepare_hiera_config
949
- return unless hiera_config
950
-
951
- info('Preparing hiera')
952
- debug("Using hiera from #{hiera_config}")
953
-
954
- FileUtils.cp_r(hiera_config, File.join(sandbox_path, 'hiera.yaml'))
955
- end
956
-
957
- def prepare_fileserver_config
958
- return unless fileserver_config
959
-
960
- info('Preparing fileserver')
961
- debug("Using fileserver config from #{fileserver_config}")
962
-
963
- FileUtils.cp_r(fileserver_config, File.join(sandbox_path, 'fileserver.conf'))
964
- end
965
-
966
- def prepare_hiera_data
967
- return unless hiera_data
968
- info('Preparing hiera data')
969
- tmp_hiera_dir = File.join(sandbox_path, 'hiera')
970
- debug("Copying hiera data from #{hiera_data} to #{tmp_hiera_dir}")
971
- FileUtils.mkdir_p(tmp_hiera_dir)
972
- FileUtils.cp_r(Dir.glob("#{hiera_data}/*"), tmp_hiera_dir)
973
- return unless hiera_eyaml_key_path
974
- tmp_hiera_key_dir = File.join(sandbox_path, 'hiera_keys')
975
- debug("Copying hiera eyaml keys from #{hiera_eyaml_key_path} to #{tmp_hiera_key_dir}")
976
- FileUtils.mkdir_p(tmp_hiera_key_dir)
977
- FileUtils.cp_r(Dir.glob("#{hiera_eyaml_key_path}/*"), tmp_hiera_key_dir)
978
- end
979
-
980
- def prepare_spec_files
981
- return unless spec_files_path
982
- info('Preparing spec files')
983
- tmp_spec_dir = File.join(sandbox_path, 'spec')
984
- debug("Copying specs from #{spec_files_path} to #{tmp_spec_dir}")
985
- FileUtils.mkdir_p(tmp_spec_dir)
986
- FileUtils.cp_r(Dir.glob("#{spec_files_path}/*"), tmp_spec_dir)
987
- end
988
-
989
- def resolve_with_librarian
990
- Kitchen.mutex.synchronize do
991
- ENV['SSL_CERT_FILE'] = librarian_puppet_ssl_file if librarian_puppet_ssl_file
992
- Puppet::Librarian.new(puppetfile, tmpmodules_dir, logger).resolve
993
- ENV['SSL_CERT_FILE'] = '' if librarian_puppet_ssl_file
994
- end
995
- end
996
- end
997
- end
998
- 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'), 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