-
Notifications
You must be signed in to change notification settings - Fork 86
/
puppet_apply.rb
1503 lines (1310 loc) · 54.3 KB
/
puppet_apply.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- encoding: utf-8 -*-
#
# Author:: Chris Lundquist (<chris.lundquist@github.com>) Neill Turner (<neillwturner@gmail.com>)
#
# Copyright (C) 2013,2014 Chris Lundquist, Neill Turner
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# See https://github.com/neillturner/kitchen-puppet/blob/master/provisioner_options.md
# for documentation configuration parameters with puppet_apply provisioner.
#
require 'uri'
require 'json'
require 'kitchen'
module Kitchen
class Busser
def non_suite_dirs
%w[data data_bags environments nodes roles puppet]
end
end
module Configurable
def platform_name
instance.platform.name
end
end
module Provisioner
#
# Puppet Apply provisioner.
#
class PuppetApply < Base
attr_accessor :tmp_dir
default_config :require_puppet_collections, true
default_config :puppet_yum_collections_repo, 'https://yum.puppetlabs.com/puppet5/puppet5-release-el-6.noarch.rpm'
default_config :puppet_apt_collections_repo, 'http://apt.puppetlabs.com/puppet5-release-wheezy.deb'
default_config :puppet_coll_remote_path, '/opt/puppetlabs'
default_config :puppet_version, nil
default_config :facter_version, nil
default_config :hiera_version, nil
default_config :install_hiera, false
default_config :hiera_package, 'hiera-puppet'
default_config :hiera_writer_files, nil
default_config :require_puppet_repo, true
default_config :require_chef_for_busser, true
default_config :resolve_with_librarian_puppet, true
default_config :resolve_with_r10k, false
default_config :puppet_environment, nil
default_config :puppet_environment_config_path do |provisioner|
provisioner.calculate_path('environment.conf')
end
default_config :puppet_environment_remote_modules_path, 'modules'
default_config :puppet_environment_remote_manifests_path, 'manifests'
default_config :puppet_environment_remote_hieradata_path, 'hieradata'
default_config :puppet_environment_hiera_config_path do |provisioner|
provisioner.calculate_path('hiera.yaml', :file)
end
default_config :puppet_apt_repo, 'http://apt.puppetlabs.com/puppetlabs-release-precise.deb'
default_config :puppet_yum_repo, 'https://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm'
default_config :chef_bootstrap_url, 'https://www.chef.io/chef/install.sh'
default_config :puppet_windows_msi_url, nil
default_config :puppet_logdest, nil
default_config :custom_install_command, nil
default_config :custom_pre_install_command, nil
default_config :custom_pre_apply_command, nil
default_config :custom_post_apply_command, nil
default_config :puppet_whitelist_exit_code, nil
default_config :require_puppet_omnibus, false
default_config :puppet_omnibus_url, 'https://raw.githubusercontent.com/petems/puppet-install-shell/master/install_puppet_6_agent.sh'
default_config :puppet_enc, nil
default_config :ignore_spec_fixtures, false
default_config :puppet_apply_command, nil
default_config :puppet_git_init, nil
default_config :puppet_git_pr, nil
default_config :http_proxy, nil
default_config :https_proxy, nil
default_config :no_proxy, nil
default_config :ignored_paths_from_root, ['spec']
default_config :hiera_data_remote_path, nil
default_config :manifest, ''
default_config :puppet_binary, 'puppet'
default_config :manifests_path do |provisioner|
provisioner.calculate_path('manifests') ||
raise('No manifests_path detected. Please specify one in .kitchen.yml')
end
default_config :modules_path do |provisioner|
modules_path = provisioner.calculate_path('modules')
if modules_path.nil? && provisioner.calculate_path('Puppetfile', :file).nil?
raise('No modules_path detected. Please specify one in .kitchen.yml')
end
modules_path
end
default_config :files_path do |provisioner|
provisioner.calculate_path('files') || 'files'
end
default_config :hiera_data_path do |provisioner|
provisioner.calculate_path('hiera')
end
default_config :puppet_config_path do |provisioner|
provisioner.calculate_path('puppet.conf', :file)
end
default_config :hiera_config_path do |provisioner|
provisioner.calculate_path('hiera.global.yaml', :file) ||
provisioner.calculate_path('hiera.yaml', :file)
end
default_config :fileserver_config_path do |provisioner|
provisioner.calculate_path('fileserver.conf', :file)
end
default_config :puppetfile_path do |provisioner|
provisioner.calculate_path('Puppetfile', :file)
end
default_config :modulefile_path do |provisioner|
provisioner.calculate_path('Modulefile', :file)
end
default_config :metadata_json_path do |provisioner|
provisioner.calculate_path('metadata.json', :file)
end
default_config :manifests_path do |provisioner|
provisioner.calculate_path('manifests', :directory)
end
default_config :spec_files_path do |provisioner|
provisioner.calculate_path('spec', :directory)
end
default_config :spec_files_remote_path, '/etc/puppet/spec'
default_config :puppet_debug, false
default_config :puppet_verbose, false
default_config :puppet_noop, false
default_config :puppet_show_diff, false
default_config :puppet_future_parser, false
default_config :platform, &:platform_name
default_config :update_package_repos, true
default_config :remove_puppet_repo, false
default_config :install_custom_facts, false
default_config :custom_facts, {}
default_config :facterlib, nil
default_config :puppet_detailed_exitcodes, nil
default_config :facter_file, nil
default_config :librarian_puppet_ssl_file, nil
default_config :r10k_ssl_file, nil
default_config :hiera_eyaml, false
default_config :hiera_eyaml_key_remote_path, '/etc/puppet/secure/keys'
default_config :puppet_environmentpath_remote_path, nil
default_config :hiera_eyaml_gpg, false
default_config :hiera_eyaml_gpg_recipients, false
default_config :hiera_eyaml_gpg_secring, false
default_config :hiera_eyaml_gpg_pubring, false
default_config :hiera_eyaml_gpg_remote_path, '/home/vagrant/.gnupg'
default_config :hiera_eyaml_key_path do |provisioner|
provisioner.calculate_path('hiera_keys')
end
default_config :hiera_deep_merge, false
default_config :puppet_no_sudo, false
def calculate_path(path, type = :directory)
base = config[:test_base_path]
candidates = []
candidates << File.join(base, instance.suite.name, 'puppet', path)
candidates << File.join(base, instance.suite.name, path)
candidates << File.join(base, path)
candidates << File.join(Dir.pwd, path)
candidates.find do |c|
type == :directory ? File.directory?(c) : File.file?(c)
end
end
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def install_command
return unless config[:require_puppet_collections] || config[:require_puppet_repo] || config[:require_puppet_omnibus]
if config[:require_puppet_omnibus]
install_omnibus_command
elsif config[:require_puppet_collections]
install_command_collections
else
case puppet_platform
when 'debian', 'ubuntu'
info("Installing puppet on #{config[:platform]}")
# need to add a CR to avoid trouble with proxy settings concatenation
<<-INSTALL
#{custom_pre_install_command}
if [ ! $(which puppet) ]; then
#{sudo('apt-get')} -y install wget
#{sudo('apt-get')} -y install apt-transport-https
#{sudo('wget')} #{wget_proxy_parm} #{puppet_apt_repo}
#{sudo('dpkg')} -i #{puppet_apt_repo_file}
#{update_packages_debian_cmd}
#{sudo_env('apt-get')} -y install facter#{facter_debian_version}
#{sudo_env('apt-get')} -y install puppet-common#{puppet_debian_version}
#{sudo_env('apt-get')} -y install puppet#{puppet_debian_version}
#{install_hiera}
fi
#{install_eyaml}
#{install_eyaml_gpg}
#{install_deep_merge}
#{install_busser}
#{custom_install_command}
INSTALL
when 'redhat', 'centos', 'fedora', 'oracle', 'amazon'
info("Installing puppet from yum on #{puppet_platform}")
# need to add a CR to avoid trouble with proxy settings concatenation
<<-INSTALL
#{custom_pre_install_command}
if [ ! $(which puppet) ]; then
#{install_puppet_yum_repo}
fi
#{install_eyaml}
#{install_eyaml_gpg}
#{install_deep_merge}
#{install_busser}
#{custom_install_command}
INSTALL
when /^windows.*/
info("Installing puppet on #{puppet_platform}")
info('Powershell is not recognised by core test-kitchen assuming it is present') unless powershell_shell?
<<-INSTALL
#{custom_pre_install_command}
if(Get-Command puppet -ErrorAction 0) { return; }
if( '#{puppet_windows_msi_url}' -ne '') {
$MsiUrl = '#{puppet_windows_msi_url}'
}
else
{
$architecture = if( [Environment]::Is64BitOperatingSystem ) { 'x64' } else { 'x86' }
if( '#{puppet_windows_version}' -eq 'latest' ) {
$MsiUrl = "https://downloads.puppetlabs.com/windows/puppet-agent-${architecture}-latest.msi"
} elseif( '#{puppet_windows_version}' -match '(\\d)\\.' ) {
$MsiUrl = "https://downloads.puppetlabs.com/windows/puppet$($Matches[1])/puppet-agent-#{puppet_windows_version}-${architecture}.msi"
} else {
$MsiUrl = "https://downloads.puppetlabs.com/windows/puppet-#{puppet_windows_version}${architecture}.msi"
}
}
Invoke-WebRequest $MsiUrl -UseBasicParsing -OutFile "C:/puppet.msi" #{posh_proxy_parm}
$process = Start-Process -FilePath msiexec.exe -Wait -PassThru -ArgumentList '/qn', '/norestart', '/i', 'C:\\puppet.msi'
if ($process.ExitCode -ne 0) {
Write-Host "Installer failed."
Exit 1
}
#{install_busser}
#{custom_install_command}
INSTALL
else
info('Installing puppet, will try to determine platform os')
# need to add a CR to avoid trouble with proxy settings concatenation
<<-INSTALL
#{custom_pre_install_command}
if [ ! $(which puppet) ]; then
if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ]; then
#{install_puppet_yum_repo}
else
if [ -f /etc/system-release ] || grep -q 'Amazon Linux' /etc/system-release; then
#{install_puppet_yum_repo}
else
#{sudo('apt-get')} -y install wget
#{sudo('apt-get')} -y install apt-transport-https
#{sudo('wget')} #{wget_proxy_parm} #{puppet_apt_repo}
#{sudo('dpkg')} -i #{puppet_apt_repo_file}
#{update_packages_debian_cmd}
#{sudo_env('apt-get')} -y install facter#{facter_debian_version}
#{sudo_env('apt-get')} -y install puppet-common#{puppet_debian_version}
#{sudo_env('apt-get')} -y install puppet#{puppet_debian_version}
#{install_hiera}
fi
fi
fi
#{install_eyaml}
#{install_eyaml_gpg}
#{install_deep_merge}
#{install_busser}
#{custom_install_command}
INSTALL
end
end
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def install_command_collections
case puppet_platform
when 'debian', 'ubuntu'
info("Installing Puppet Collections on #{puppet_platform}")
<<-INSTALL
#{Util.shell_helpers}
#{custom_pre_install_command}
if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
if [ ! -f "#{config[:puppet_apt_collections_repo]}" ]; then
#{sudo('apt-get')} -y install wget
#{sudo('apt-get')} -y install apt-transport-https
#{sudo('wget')} #{wget_proxy_parm} #{config[:puppet_apt_collections_repo]}
fi
#{sudo('dpkg')} -i #{puppet_apt_coll_repo_file}
#{sudo('apt-get')} update
#{sudo_env('apt-get')} -y install puppet-agent#{puppet_debian_version}
fi
#{install_eyaml("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
#{install_eyaml_gpg("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
#{install_deep_merge}
#{install_busser}
#{custom_install_command}
INSTALL
when 'redhat', 'centos', 'fedora', 'oracle', 'amazon'
info("Installing Puppet Collections on #{puppet_platform}")
<<-INSTALL
#{Util.shell_helpers}
#{custom_pre_install_command}
if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
echo "-----> #{sudo_env('yum')} -y --nogpgcheck install #{config[:puppet_yum_collections_repo]}"
#{sudo_env('yum')} clean all
#{sudo_env('yum')} -y --nogpgcheck install #{config[:puppet_yum_collections_repo]}
#{sudo_env('yum')} -y --nogpgcheck install puppet-agent#{puppet_redhat_version}
fi
#{install_eyaml("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
#{install_eyaml_gpg("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
#{install_deep_merge}
#{install_busser}
#{custom_install_command}
INSTALL
when /^windows.*/
info("Installing Puppet Collections on #{puppet_platform}")
info('Powershell is not recognised by core test-kitchen assuming it is present') unless powershell_shell?
<<-INSTALL
#{custom_pre_install_command}
if(Get-Command puppet -ErrorAction 0) { return; }
if( '#{puppet_windows_msi_url}' -ne '') {
$MsiUrl = '#{puppet_windows_msi_url}'
}
else
{
$architecture = if( [Environment]::Is64BitOperatingSystem ) { 'x64' } else { 'x86' }
if( '#{puppet_windows_version}' -eq 'latest' ) {
$MsiUrl = "https://downloads.puppetlabs.com/windows/puppet-agent-${architecture}-latest.msi"
} elseif( '#{puppet_windows_version}' -match '(\\d)\\.' ) {
$MsiUrl = "https://downloads.puppetlabs.com/windows/puppet$($Matches[1])/puppet-agent-#{puppet_windows_version}-${architecture}.msi"
} else {
$MsiUrl = "https://downloads.puppetlabs.com/windows/puppet-agent-#{puppet_windows_version}-${architecture}.msi"
}
}
Invoke-WebRequest $MsiUrl -UseBasicParsing -OutFile "C:/puppet-agent.msi" #{posh_proxy_parm}
$process = Start-Process -FilePath msiexec.exe -Wait -PassThru -ArgumentList '/qn', '/norestart', '/i', 'C:\\puppet-agent.msi'
if ($process.ExitCode -ne 0) {
Write-Host "Installer failed."
Exit 1
}
#{install_busser}
#{custom_install_command}
INSTALL
else
info('Installing Puppet Collections, will try to determine platform os')
<<-INSTALL
#{Util.shell_helpers}
#{custom_pre_install_command}
if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ] || \
[ -f /etc/system-release ] || grep -q 'Amazon Linux' /etc/system-release; then
echo "-----> #{sudo_env('yum')} -y --nogpgcheck install #{config[:puppet_yum_collections_repo]}"
#{sudo_env('yum')} -y --nogpgcheck install #{config[:puppet_yum_collections_repo]}
#{sudo_env('yum')} -y --nogpgcheck install puppet-agent#{puppet_redhat_version}
else
#{sudo('apt-get')} -y install wget
#{sudo('apt-get')} -y install apt-transport-https
#{sudo('wget')} #{wget_proxy_parm} #{config[:puppet_apt_collections_repo]}
#{sudo('dpkg')} -i #{puppet_apt_coll_repo_file}
#{sudo('apt-get')} update
#{sudo_env('apt-get')} -y install puppet-agent#{puppet_debian_version}
fi
fi
#{install_eyaml("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
#{install_eyaml_gpg("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
#{install_deep_merge}
#{install_busser}
#{custom_install_command}
INSTALL
end
end
def install_deep_merge
return unless config[:hiera_deep_merge]
<<-INSTALL
# Support for hash merge lookups to recursively merge hash keys
if [[ $(#{sudo('gem')} list deep_merge -i) == 'false' ]]; then
echo '-----> Installing deep_merge to provide deep_merge of hiera hashes'
#{sudo('gem')} install #{gem_proxy_parm} --no-ri --no-rdoc deep_merge
fi
INSTALL
end
def install_eyaml(gem_cmd = 'gem')
return unless config[:hiera_eyaml]
<<-INSTALL
# A backend for Hiera that provides per-value asymmetric encryption of sensitive data
if [[ $(#{sudo(gem_cmd)} list hiera-eyaml -i) == 'false' ]]; then
echo '-----> Installing hiera-eyaml to provide encryption of hiera data'
#{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc highline -v 1.6.21
#{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc hiera-eyaml
fi
INSTALL
end
def install_eyaml_gpg(gem_cmd = 'gem')
return unless config[:hiera_eyaml_gpg]
<<-INSTALL
# A backend for Hiera that provides per-value asymmetric encryption of sensitive data
if [[ $(#{sudo(gem_cmd)} list hiera-eyaml-gpg -i) == 'false' ]]; then
echo '-----> Installing hiera-eyaml-gpg to provide encryption of hiera data'
#{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc highline -v 1.6.21
#{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc hiera-eyaml
#{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc hiera-eyaml-gpg
#{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc ruby_gpg
fi
INSTALL
end
def install_busser
return unless config[:require_chef_for_busser]
info("Install busser on #{puppet_platform}")
case puppet_platform
when /^windows.*/
# https://raw.githubusercontent.com/opscode/knife-windows/master/lib/chef/knife/bootstrap/windows-chef-client-msi.erb
<<-INSTALL
$webclient = New-Object System.Net.WebClient; $webclient.DownloadFile('https://opscode-omnibus-packages.s3.amazonaws.com/windows/2008r2/x86_64/chef-windows-11.12.8-1.windows.msi','chef-windows-11.12.8-1.windows.msi')
msiexec /qn /i chef-windows-11.12.8-1.windows.msi
cmd.exe /C "SET PATH=%PATH%;`"C:\\opscode\\chef\\embedded\\bin`";`"C:\\tmp\\busser\\gems\\bin`""
INSTALL
else
<<-INSTALL
#{Util.shell_helpers}
# install chef omnibus so that busser works as this is needed to run tests :(
# TODO: work out how to install enough ruby
# and set busser: { :ruby_bindir => '/usr/bin/ruby' } so that we dont need the
# whole chef client
if [ ! -d "/opt/chef" ]
then
echo '-----> Installing Chef Omnibus to install busser to run tests'
#{export_http_proxy_parm}
#{export_https_proxy_parm}
#{export_no_proxy_parm}
do_download #{chef_url} /tmp/install.sh
#{sudo('sh')} /tmp/install.sh
fi
INSTALL
end
end
def install_omnibus_command
info('Installing puppet using puppet omnibus')
version = ''
version = "-v #{config[:puppet_version]}" unless config[:puppet_version].nil?
<<-INSTALL
#{Util.shell_helpers}
if [ ! $(which puppet) ]; then
echo "-----> Installing Puppet Omnibus"
#{export_http_proxy_parm}
#{export_https_proxy_parm}
#{export_no_proxy_parm}
do_download #{config[:puppet_omnibus_url]} /tmp/install_puppet.sh
#{sudo_env('sh')} /tmp/install_puppet.sh #{version}
fi
INSTALL
end
def install_hiera
return unless config[:install_hiera]
<<-INSTALL
#{sudo_env('apt-get')} -y install #{hiera_package}
INSTALL
end
def hiera_package
"#{config[:hiera_package]}#{puppet_hiera_debian_version}"
end
# /bin/wget -P /etc/pki/rpm-gpg/ http://yum.puppetlabs.com/RPM-GPG-KEY-puppetlabs
# changed to curl
def install_puppet_yum_repo
<<-INSTALL
rhelversion=$(cat /etc/redhat-release | grep 'release 7')
# For CentOS7/RHEL7 the rdo release contains puppetlabs repo, creating conflict. Create temp-repo
#{sudo_env('curl')} -o /etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs http://yum.puppetlabs.com/RPM-GPG-KEY-puppetlabs
if [ -n "$rhelversion" ]; then
echo '[puppettemp-products]
name=Puppet Labs Products - \$basearch
baseurl=http://yum.puppetlabs.com/el/7/products/\$basearch
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
enabled=0
gpgcheck=1
[puppettemp-deps]
name=Puppet Labs Dependencies - \$basearch
baseurl=http://yum.puppetlabs.com/el/7/dependencies/\$basearch
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
enabled=0
gpgcheck=1' | sudo tee /etc/yum.repos.d/puppettemp.repo > /dev/null
sudo sed -i 's/^[ \t]*//' /etc/yum.repos.d/puppettemp.repo
#{update_packages_redhat_cmd}
#{sudo_env('yum')} -y --enablerepo=puppettemp-products --enablerepo=puppettemp-deps install puppet#{puppet_redhat_version}
# Clean up temporary puppet repo
sudo rm -rf /etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
sudo rm -rf /etc/yum.repos.d/puppettemp.repo
else
#{sudo('rpm')} -ivh #{proxy_parm} #{puppet_yum_repo}
#{update_packages_redhat_cmd}
#{sudo_env('yum')} -y --nogpgcheck install puppet#{puppet_redhat_version}
fi
INSTALL
end
def custom_pre_install_command
<<-INSTALL
#{config[:custom_pre_install_command]}
INSTALL
end
def custom_install_command
<<-INSTALL
#{config[:custom_install_command]}
INSTALL
end
def init_command
todelete = %w[modules manifests files hiera hiera.yaml hiera.global.yaml facter spec enc environment]
.map { |dir| File.join(config[:root_path], dir) }
todelete += [hiera_data_remote_path,
'/etc/hiera.yaml',
"#{puppet_dir}/hiera.yaml",
spec_files_remote_path.to_s,
"#{puppet_dir}/fileserver.conf"]
todelete << File.join(puppet_dir, puppet_environment) if puppet_environment
todelete << File.join(puppet_environmentpath_remote_path, puppet_environment) if puppet_environment_config && puppet_environment
cmd = "#{sudo(rm_command_paths(todelete))};"
cmd += " #{mkdir_command} #{config[:root_path]};"
cmd += " #{sudo(mkdir_command)} #{puppet_dir}"
debug(cmd)
cmd
end
def create_sandbox
super
debug("Creating local sandbox in #{sandbox_path}")
yield if block_given?
prepare_modules
prepare_manifests
prepare_files
prepare_facter_file
prepare_facts
prepare_puppet_config
prepare_hiera_config
prepare_puppet_environment
prepare_fileserver_config
prepare_hiera_data
prepare_enc
prepare_spec_files
info('Finished Preparing files for transfer')
end
def cleanup_sandbox
return if sandbox_path.nil?
debug("Cleaning up local sandbox in #{sandbox_path}")
FileUtils.rmtree(sandbox_path)
return if remove_repo.nil?
debug("Cleaning up remote sandbox: #{remove_repo}")
instance.remote_exec remove_repo
end
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def prepare_command
commands = []
if puppet_git_init
commands << [
sudo('rm -rf'), '/etc/puppet'
].join(' ')
commands << [
sudo('git clone'), puppet_git_init, '/etc/puppet'
].join(' ')
end
if puppet_git_pr
commands << [
sudo('git'),
'--git-dir=/etc/puppet/.git/',
'fetch -f',
'origin',
"pull/#{puppet_git_pr}/head:pr_#{puppet_git_pr}"
].join(' ')
commands << [
sudo('git'),
'--git-dir=/etc/puppet/.git/',
'--work-tree=/etc/puppet/',
'checkout',
"pr_#{puppet_git_pr}"
].join(' ')
end
if puppet_config
commands << [
sudo(cp_command),
File.join(config[:root_path], 'puppet.conf'),
puppet_dir
].join(' ')
end
if fileserver_config
commands << [
sudo(cp_command),
File.join(config[:root_path], 'fileserver.conf'),
puppet_dir
].join(' ')
end
if hiera_data
commands << [
sudo(mkdir_command), hiera_data_remote_path
].join(' ')
commands << [
sudo("#{cp_command} -r"), File.join(config[:root_path], 'hiera/*'), hiera_data_remote_path
].join(' ')
end
if hiera_eyaml
commands << [
sudo(mkdir_command), hiera_eyaml_key_remote_path
].join(' ')
commands << [
sudo("#{cp_command} -r"), File.join(config[:root_path], 'hiera_keys/*'), hiera_eyaml_key_remote_path
].join(' ')
end
if hiera_eyaml_gpg
commands << [
sudo('mkdir -p'), hiera_eyaml_gpg_remote_path
].join(' ')
commands << [
sudo('cp -r'), File.join(config[:root_path], hiera_eyaml_gpg_recipients), hiera_eyaml_gpg_remote_path
].join(' ')
commands << [
sudo('cp -r'), File.join(config[:root_path], hiera_eyaml_gpg_secring), hiera_eyaml_gpg_remote_path
].join(' ')
commands << [
sudo('cp -r'), File.join(config[:root_path], hiera_eyaml_gpg_pubring), hiera_eyaml_gpg_remote_path
].join(' ')
end
if puppet_environment
commands << [
sudo('ln -s '), config[:root_path], File.join(puppet_dir, puppet_environment)
].join(' ')
end
if puppet_environment_config && puppet_environment
commands << [
sudo(mkdir_command), puppet_environmentpath_remote_path
].join(' ')
commands << [
sudo(mkdir_command), File.join(puppet_environmentpath_remote_path, puppet_environment)
].join(' ')
commands << [
sudo('ln -s '), File.join(config[:root_path], 'modules'), File.join(puppet_environmentpath_remote_path, puppet_environment, puppet_environment_remote_modules_path)
].join(' ')
commands << [
sudo('ln -s '), File.join(config[:root_path], 'manifests'), File.join(puppet_environmentpath_remote_path, puppet_environment, puppet_environment_remote_manifests_path)
].join(' ')
commands << [
sudo('ln -s '), File.join(config[:root_path], 'hiera'), File.join(puppet_environmentpath_remote_path, puppet_environment, puppet_environment_remote_hieradata_path)
].join(' ')
commands << [
sudo('cp'), File.join(config[:root_path], 'environment', 'environment.conf'), File.join(puppet_environmentpath_remote_path, puppet_environment, 'environment.conf')
].join(' ')
commands << [
sudo('cp'), File.join(config[:root_path], 'environment', 'hiera.yaml'), File.join(puppet_environmentpath_remote_path, puppet_environment, 'hiera.yaml')
].join(' ')
end
if spec_files_path && spec_files_remote_path
commands << [
sudo(mkdir_command), spec_files_remote_path
].join(' ')
commands << [
sudo("#{cp_command} -r"), File.join(config[:root_path], 'spec/*'), spec_files_remote_path
].join(' ')
end
if config[:puppet_enc]
commands << [
sudo('chmod 755'), File.join("#{config[:root_path]}/enc", File.basename(config[:puppet_enc]))
].join(' ')
end
command = powershell? ? commands.join('; ') : commands.join(' && ')
debug(command)
command
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def run_command
return config[:puppet_apply_command] unless config[:puppet_apply_command].nil?
result = [
facterlib,
custom_facts,
puppet_manifestdir,
puppet_cmd,
'apply',
File.join(config[:root_path], 'manifests', manifest),
"--modulepath=#{File.join(config[:root_path], 'modules')}",
"--fileserverconfig=#{File.join(config[:root_path], 'fileserver.conf')}",
custom_options,
puppet_environment_flag,
puppet_noop_flag,
puppet_enc_flag,
puppet_hiera_flag,
puppet_detailed_exitcodes_flag,
puppet_verbose_flag,
puppet_debug_flag,
puppet_logdest_flag,
puppet_future_parser_flag,
puppet_show_diff_flag,
puppet_whitelist_exit_code
].join(' ')
if config[:custom_post_apply_command]
custom_post_apply_trap = <<-TRAP
function custom_post_apply_command {
#{config[:custom_post_apply_command]}
}
trap custom_post_apply_command EXIT
TRAP
end
result = <<-RUN
#{config[:custom_pre_apply_command]}
#{custom_post_apply_trap}
#{result}
RUN
info("Going to invoke puppet apply with: #{result}")
result
end
protected
def load_needed_dependencies!
return unless File.exist?(puppetfile)
return unless config[:resolve_with_librarian_puppet] || config[:resolve_with_r10k]
if config[:resolve_with_librarian_puppet]
require 'kitchen/provisioner/puppet/librarian'
debug("Puppetfile found at #{puppetfile}, loading Librarian-Puppet")
Puppet::Librarian.load!(logger)
elsif config[:resolve_with_r10k]
require 'kitchen/provisioner/puppet/r10k'
debug("Puppetfile found at #{puppetfile}, loading R10K")
Puppet::R10K.load!(logger)
end
end
def tmpmodules_dir
File.join(sandbox_path, 'modules')
end
def puppetfile
config[:puppetfile_path] || ''
end
def modulefile
config[:modulefile_path] || ''
end
def metadata_json
config[:metadata_json_path] || ''
end
def manifest
config[:manifest]
end
def manifests
config[:manifests_path]
end
def modules
config[:modules_path]
end
def files
config[:files_path] || 'files'
end
def puppet_config
config[:puppet_config_path]
end
def puppet_environment
config[:puppet_environment]
end
def puppet_environment_config
if config[:puppet_environment_config_path] && !puppet_environment
raise("ERROR: found environment config '#{config[:puppet_environment_config_path]}', however no 'puppet_environment' is specified. Please specify 'puppet_environment' or unset 'puppet_environment_config_path' in .kitchen.yml")
end
config[:puppet_environment_config_path]
end
def puppet_environment_remote_modules_path
config[:puppet_environment_remote_modules_path]
end
def puppet_environment_remote_manifests_path
config[:puppet_environment_remote_manifests_path]
end
def puppet_environment_remote_hieradata_path
config[:puppet_environment_remote_hieradata_path]
end
def puppet_git_init
config[:puppet_git_init]
end
def puppet_git_pr
config[:puppet_git_pr]
end
def hiera_config
config[:hiera_config_path]
end
def puppet_environment_hiera_config
config[:puppet_environment_hiera_config_path]
end
def fileserver_config
config[:fileserver_config_path]
end
def hiera_data
config[:hiera_data_path]
end
def hiera_data_remote_path
return config[:hiera_data_remote_path] if config[:hiera_data_remote_path]
if config[:require_puppet_collections]
powershell? ? 'C:/ProgramData/PuppetLabs/code/environments/production/hieradata' : '/etc/puppetlabs/code/environments/production/hieradata'
else
powershell? ? 'C:/ProgramData/PuppetLabs/hiera/var' : '/var/lib/hiera'
end
end
def hiera_writer
config[:hiera_writer_files]
end
def hiera_eyaml
config[:hiera_eyaml]
end
def hiera_eyaml_gpg
config[:hiera_eyaml_gpg]
end
def hiera_eyaml_gpg_recipients
config[:hiera_eyaml_gpg_recipients]
end
def hiera_eyaml_gpg_secring
config[:hiera_eyaml_gpg_secring]
end
def hiera_eyaml_gpg_pubring
config[:hiera_eyaml_gpg_pubring]
end
def hiera_eyaml_gpg_remote_path
config[:hiera_eyaml_gpg_remote_path]
end
def hiera_eyaml_key_path
config[:hiera_eyaml_key_path]
end
def hiera_eyaml_key_remote_path
config[:hiera_eyaml_key_remote_path]
end
def hiera_deep_merge
config[:hiera_deep_merge]
end
def librarian_puppet_ssl_file
config[:librarian_puppet_ssl_file]
end
def r10k_ssl_file
config[:r10k_puppet_ssl_file] || config[:librarian_puppet_ssl_file]
end
def puppet_cmd
return '& "C:\Program Files\Puppet Labs\Puppet\bin\puppet"' if powershell?
puppet_bin = config[:require_puppet_collections] ? "#{config[:puppet_coll_remote_path]}/bin/puppet" : config[:puppet_binary]
if config[:puppet_no_sudo]
puppet_bin
else
sudo_env(puppet_bin)
end
end
def puppet_dir
return 'C:/ProgramData/PuppetLabs/puppet/etc' if powershell?
config[:require_puppet_collections] ? '/etc/puppetlabs/puppet' : '/etc/puppet'
end
def puppet_environmentpath_remote_path
return config[:puppet_environmentpath_remote_path] if config[:puppet_environmentpath_remote_path]
if config[:puppet_version] =~ /^3/
powershell? ? 'C:/ProgramData/PuppetLabs/puppet/etc' : '/etc/puppet/environments'
else
powershell? ? 'C:/ProgramData/PuppetLabs/code/environments' : '/etc/puppetlabs/code/environments'
end
end
def hiera_config_dir
return 'C:/ProgramData/PuppetLabs/puppet/etc' if powershell?
config[:require_puppet_collections] ? '/etc/puppetlabs/code' : '/etc/puppet'
end
def puppet_debian_version
config[:puppet_version] ? "=#{config[:puppet_version]}" : nil
end
def facter_debian_version
config[:facter_version] ? "=#{config[:facter_version]}" : nil
end
def puppet_hiera_debian_version
config[:hiera_version] ? "=#{config[:hiera_version]}" : nil
end
def puppet_redhat_version
if puppet_platform == 'amazon'
config[:puppet_version]
else
config[:puppet_version] ? "-#{config[:puppet_version]}" : nil
end
end
def puppet_windows_version
config[:puppet_version] ? config[:puppet_version].to_s : 'latest'
end
def puppet_windows_msi_url
config[:puppet_windows_msi_url]
end
def puppet_environment_flag
if config[:puppet_version] =~ /^2/
config[:puppet_environment] ? "--environment=#{puppet_environment}" : nil
else
config[:puppet_environment] ? "--environment=#{puppet_environment} --environmentpath=#{puppet_environmentpath_remote_path}" : nil
end