This repository has been archived by the owner on May 26, 2020. It is now read-only.
/
provisioning_seeder.rb
1120 lines (987 loc) · 42.9 KB
/
provisioning_seeder.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
require 'ipaddr'
class ProvisioningSeeder < BaseSeeder
attr_accessor :domain, :fqdn
def initialize(kafo)
super
@domain = kafo.param('foreman_proxy', 'dns_zone').value
@environment = kafo.param('foreman', 'environment').value
@netmask = kafo.param('foreman_plugin_staypuft', 'netmask').value
@network = kafo.param('foreman_plugin_staypuft', 'network').value
@ip = kafo.param('foreman_proxy', 'tftp_servername').value
@from = kafo.param('foreman_plugin_staypuft', 'from').value
@to = kafo.param('foreman_plugin_staypuft', 'to').value
@gateway = kafo.param('foreman_proxy', 'dhcp_gateway').value
@kernel = kafo.param('foreman_plugin_discovery', 'kernel').value
@initrd = kafo.param('foreman_plugin_discovery', 'initrd').value
@discovery_env_name = 'discovery'
@default_root_pass = kafo.param('foreman_plugin_staypuft', 'root_password').instance_variable_get('@value')
@default_ssh_public_key = kafo.param('foreman_plugin_staypuft', 'ssh_public_key').value
@ntp_host = kafo.param('foreman_plugin_staypuft', 'ntp_host').value
@timezone = kafo.param('foreman_plugin_staypuft', 'timezone').value
begin
pub_key_path = kafo.param('sshkeypair', 'foreman_proxy_home').value + '/.ssh/id_rsa.pub'
@pub_key = File.read(pub_key_path).split(' ')[1]
rescue => e
say "Could not read SSH public key from #{pub_key_path} - #{e.message}, answer file will be <%= color('broken', :bad) %>"
@pub_key = 'broken'
end
end
def seed
say HighLine.color("Starting to seed provisioning data", :good)
default_proxy = find_default_proxy
default_environment = find_default_environment
foreman_host = find_foreman_host
default_domain = @foreman.domain.show_or_ensure({'id' => @domain},
{'name' => @domain,
'fullname' => 'Default domain used for provisioning',
'dns_id' => default_proxy['id']})
default_subnet = @foreman.subnet.find_or_ensure('name=default',
{'name' => 'default',
'mask' => @netmask,
'network' => @network,
'dns_primary' => @ip,
'from' => @from,
'to' => @to,
'gateway' => @gateway,
'domain_ids' => [default_domain['id']],
'dns_id' => default_proxy['id'],
'dhcp_id' => default_proxy['id'],
'tftp_id' => default_proxy['id'],
'boot_mode' => 'DHCP'})
kinds = @foreman.template_kind.index
provisioning = kinds.detect { |k| k['name'] == 'provision' }
pxe_linux = kinds.detect { |k| k['name'] == 'PXELinux' }
@foreman.config_template.show_or_ensure({'id' => 'redhat_register'},
{'template' => redhat_register_snippet, 'snippet' => '1', 'name' => 'redhat_register'})
@foreman.config_template.show_or_ensure({'id' => 'custom_deployment_repositories'},
{'template' => custom_deployment_repositories_snippet, 'snippet' => '1', 'name' => 'custom_deployment_repositories'})
@foreman.config_template.show_or_ensure({'id' => 'staypuft-client-installer-answers-yaml'},
{'template' => staypuft_staypuft_answers_snippet, 'name' => 'staypuft-client-installer-answers-yaml', 'snippet' => '1'})
@foreman.config_template.show_or_ensure({'id' => 'staypuft_client_bootstrap'},
{'template' => staypuft_bootstrap_snippet, 'name' => 'staypuft_client_bootstrap', 'snippet' => '1'})
@foreman.config_template.show_or_ensure({'id' => 'Kickstart RHEL default'},
{'template' => kickstart_rhel_default, 'template_kind_id' => provisioning['id'], 'name' => 'Kickstart RHEL default'})
@foreman.config_template.show_or_ensure({'id' => 'Kickstart default'},
{'template' => kickstart_default, 'template_kind_id' => provisioning['id'], 'name' => 'Kickstart default'})
@foreman.config_template.show_or_ensure({'id' => 'Kickstart default PXELinux'},
{'template' => kickstart_default_pxelinux, 'template_kind_id' => pxe_linux['id'], 'name' => 'Kickstart default PXELinux'})
@foreman.config_template.show_or_ensure({'id' => 'ssh_public_key'},
{'template' => ssh_public_key_snippet, 'snippet' => '1', 'name' => 'ssh_public_key'})
@foreman.config_template.show_or_ensure({'id' => 'kickstart_networking_setup'},
{'template' => kickstart_networking_setup_snippet, 'snippet' => '1', 'name' => 'kickstart_networking_setup'})
@foreman.partition_table.show_or_ensure({'id' => 'LVM with cinder-volumes',
'name' => 'LVM with cinder-volumes',
'layout' => lvm_w_cinder_volumes,
'os_family' => 'Redhat'}, {})
@foreman.partition_table.show_or_ensure({'id' => 'OpenStack Default',
'name' => 'OpenStack Default',
'layout' => openstack_lvm,
'os_family' => 'Redhat'}, {})
name = 'PXELinux global default'
pxe_template = @foreman.config_template.show_or_ensure({'id' => name},
{'template' => template})
@foreman.config_template.build_pxe_default
puppet_klass = @foreman.puppetclass.search('name = foreman::puppet::agent::service')['foreman'].first
smart_parameter = @foreman.smart_class_parameter.first('puppetclass = foreman::puppet::agent::service and key = runmode')
@foreman.smart_class_parameter.show_or_ensure({'id' => smart_parameter['id']},
{'override' => true, 'default_value' => 'none'})
staypuft_client_klass = @foreman.puppetclass.search('name = foreman::plugin::staypuft_client')['foreman'].first
smart_parameter = @foreman.smart_class_parameter.first('puppetclass = foreman::plugin::staypuft_client and key = staypuft_ssh_public_key')
@foreman.smart_class_parameter.show_or_ensure({'id' => smart_parameter['id']},
{'override' => true, 'default_value' => @pub_key})
klasses = [puppet_klass, staypuft_client_klass]
puppet_class_ids = klasses.map { |klass| klass['id'] }
@hostgroups = []
oses = find_default_oses(foreman_host)
oses.each do |os|
next if os['name'] == 'RedHat' && os['major'] == '6' # we don's support RHEL6 for provisioning anymore
next if os['name'] == 'CentOS' && os['major'] == '6' # we don's support CentOS6 for provisioning anymore
group_id = "base_#{os['name']}_#{os['major']}"
medium = @foreman.medium.index('search' => "name ~ #{os['name']}").first
if os['architectures'].nil? || os['architectures'].empty?
@foreman.operating_system.update 'id' => os['id'],
'operatingsystem' => {'architecture_ids' => [foreman_host['architecture_id']]}
end
if os['media'].nil? || os['media'].empty?
if medium.nil?
say HighLine.color("Installation medium for #{os['name']} not found, provisioning will not work for hostgroup #{group_id} unless you create it manually", :info)
else
@foreman.operating_system.update 'id' => os['id'], 'operatingsystem' => {'medium_ids' => [medium['id']]}
end
end
assign_provisioning_templates(os)
ptable = assign_partition_tables(os)
hostgroup_attrs = {'name' => group_id,
'architecture_id' => foreman_host['architecture_id'],
'domain_id' => default_domain['id'],
'environment_id' => default_environment['id'],
'operatingsystem_id' => os['id'],
'ptable_id' => ptable['id'],
'puppet_ca_proxy_id' => default_proxy['id'],
'puppet_proxy_id' => default_proxy['id'],
'subnet_id' => default_subnet['id'],
'puppetclass_ids' => puppet_class_ids}
hostgroup_attrs['medium_id'] = medium['id'] unless medium.nil?
hostgroup = @foreman.hostgroup.show_or_ensure({'id' => group_id}, hostgroup_attrs)
@foreman.parameter.show_or_ensure({'id' => 'staypuft_ssh_public_key', 'operatingsystem_id' => os['id']},
{
'name' => 'staypuft_ssh_public_key',
'value' => @pub_key,
})
if !@default_ssh_public_key.nil? && !@default_ssh_public_key.empty?
@foreman.parameter.show_or_ensure({'id' => 'ssh_public_key', 'operatingsystem_id' => os['id']},
{
'name' => 'ssh_public_key',
'value' => @default_ssh_public_key,
})
end
@foreman.parameter.show_or_ensure({'id' => 'ntp-server', 'operatingsystem_id' => os['id']},
{
'name' => 'ntp-server',
'value' => @ntp_host.split(',').first,
})
@foreman.parameter.show_or_ensure({'id' => 'ntp-servers', 'operatingsystem_id' => os['id']},
{
'name' => 'ntp-servers',
'value' => @ntp_host,
})
@foreman.parameter.show_or_ensure({'id' => 'time-zone', 'operatingsystem_id' => os['id']},
{
'name' => 'time-zone',
'value' => @timezone,
})
@hostgroups.push hostgroup
end
default_hostgroup = @hostgroups.last
setup_setting(default_hostgroup)
setup_idle_timeout
setup_default_root_pass
setup_ignore_puppet_facts_for_provisioning
setup_puppetrun
create_discovery_env(pxe_template)
say HighLine.color("Use '#{default_hostgroup['name']}' hostgroup for provisioning", :good)
end
private
def create_discovery_env(template)
env = @foreman.environment.show_or_ensure({'id' => @discovery_env_name},
{'name' => @discovery_env_name})
# if the template has combination already, we don't update it
unless template['template_combinations'].any? { |c| c['environment_id'] == env['id'].to_i and c['hostgroup_id'].nil? }
@foreman.config_template.update 'id' => template['name'],
'config_template' => {'template_combinations_attributes' => [{'environment_id' => env['id']}]}
end
end
def setup_setting(default_hostgroup)
adjust_setting('base_hostgroup', default_hostgroup['name'].to_s)
end
def setup_idle_timeout
adjust_setting('idle_timeout', @foreman.version.start_with?('1.6') ? 180 : '180')
end
def setup_ignore_puppet_facts_for_provisioning
@foreman.setting.show_or_ensure({'id' => 'ignore_puppet_facts_for_provisioning'},
{'value' => @foreman.version.start_with?('1.6') ? true : 'true'})
rescue NoMethodError => e
@logger.error "Setting with name 'ignore_puppet_facts_for_provisioning' not found, you must run 'foreman-rake db:seed' " +
"and rerun installer to fix this issue."
end
def setup_default_root_pass
adjust_setting('root_pass', @default_root_pass)
end
def setup_puppetrun
adjust_setting('puppetrun', @foreman.version.start_with?('1.6') ? true : 'true')
end
def adjust_setting(id, value)
@foreman.setting.show_or_ensure({'id' => id},
{'value' => value})
rescue NoMethodError => e
@logger.error "Setting with name '#{id}' not found, you must run 'foreman-rake db:seed' " +
"and rerun installer to fix this issue."
end
def assign_partition_tables(os)
if os['family'] == 'Redhat'
default_ptable_name = 'OpenStack Default'
additional_ptables_names = ['LVM with cinder-volumes']
elsif os['family'] == 'Debian'
default_ptable_name = 'Preseed default'
additional_ptable_names = []
end
default_ptable = nil
additional_ptables_names.push(default_ptable_name).each do |ptable_name|
ptable = @foreman.partition_table.first! %Q(name ~ "#{ptable_name}*")
default_ptable = ptable if default_ptable_name == ptable['name']
if os['ptables'].nil? || os['ptables'].empty?
ids = @foreman.partition_table.show!('id' => ptable['id'])['operatingsystems'].map { |o| o['id'] }
@foreman.partition_table.update 'id' => ptable['id'], 'ptable' => {'operatingsystem_ids' => (ids + [os['id']]).uniq}
end
end
default_ptable
end
def assign_provisioning_templates(os)
# Default values used for provision template searching, some were renamed after 1.4
if os['family'] == 'Redhat'
tmpl_name = 'Kickstart default'
provision_tmpl_name = os['name'] == 'RedHat' ? 'Kickstart RHEL default' : tmpl_name
ipxe_tmpl_name = 'Kickstart'
elsif os['family'] == 'Debian'
tmpl_name = provision_tmpl_name = 'Preseed'
ipxe_tmpl_name = nil
end
{'provision' => provision_tmpl_name, 'PXELinux' => tmpl_name, 'iPXE' => ipxe_tmpl_name}.each do |kind_name, tmpl_name|
next if tmpl_name.nil?
kinds = @foreman.template_kind.index
kind = kinds.detect { |k| k['name'] == kind_name }
# we prefer foreman_bootdisk templates
tmpls = @foreman.config_template.search "name ~ \"#{tmpl_name}*\" and kind = #{kind_name}"
tmpl = tmpls.detect { |t| t['name'] =~ /.*sboot disk.*s/ } || tmpls.first
raise StandardError, "no template found by search 'name ~ \"#{tmpl_name}*\"'" if tmpl.nil?
# if there's no provisioning template for this os family found it means, it's not associated so we add relation
# otherwise we still must check that it's assigned for right os not just family
assigned_tmpl = @foreman.config_template.first %Q(name ~ "#{tmpl_name}*" and kind = #{kind_name} and operatingsystem = "#{os['name']}")
if assigned_tmpl.nil?
@foreman.config_template.update 'id' => tmpl['id'], 'config_template' => {'operatingsystem_ids' => [os['id']]}
else
assigned_os_ids = @foreman.config_template.show!('id' => tmpl['id'])['operatingsystems'].map { |o| o['id'] }
if !assigned_os_ids.include?(os['id'])
@foreman.config_template.update 'id' => tmpl['id'], 'config_template' => {'operatingsystem_ids' => assigned_os_ids + [os['id']]}
end
end
# finally we setup default template from possible values we assigned in previous steps
os_tmpls = @foreman.os_default_template.index 'operatingsystem_id' => os['id']
os_tmpl = os_tmpls.detect { |t| t['template_kind_name'] == kind_name }
if os_tmpl.nil?
@foreman.os_default_template.create 'os_default_template' => {'config_template_id' => tmpl['id'], 'template_kind_id' => kind['id']},
'operatingsystem_id' => os['id']
end
end
end
def find_default_environment
@foreman.environment.show! 'id' => @environment,
:error_message => "environment #{@environment} not found in foreman, puppet haven't run yet?"
end
def find_default_proxy
@foreman.smart_proxy.show! 'id' => @fqdn,
:error_message => "smart proxy #{@fqdn} haven't been registered in foreman yet, installer failure?"
end
def kickstart_rhel_default
<<'EOS'
<%#
kind: provision
name: Kickstart RHEL default
oses:
- RedHat 4
- RedHat 5
- RedHat 6
- RedHat 7
%>
<%
os_major = @host.operatingsystem.major.to_i
# safemode renderer does not support unary negation
pm_set = @host.puppetmaster.empty? ? false : true
puppet_enabled = pm_set || @host.params['force-puppet']
%>
install
<%= @mediapath %>
lang en_US.UTF-8
selinux --enforcing
keyboard us
skipx
<% subnet = @host.subnet -%>
<% dhcp = subnet.dhcp_boot_mode? -%>
network --bootproto <%= dhcp ? 'dhcp' : "static --ip=#{@host.ip} --netmask=#{subnet.mask} --gateway=#{subnet.gateway} --nameserver=#{[subnet.dns_primary, subnet.dns_secondary].select(&:present?).join(',')}" %> --device=<%= @host.mac -%> --hostname <%= @host %>
rootpw --iscrypted <%= root_pass %>
authconfig --useshadow --passalgo=sha256 --kickstart
timezone --utc <%= @host.params['time-zone'] || 'UTC' %>
<% if os_major >= 7 && @host.info["parameters"]["realm"] && @host.otp && @host.realm -%>
realm join --one-time-password=<%= @host.otp %> <%= @host.realm %>
<% end -%>
<% if os_major > 4 -%>
services --enabled iptables --disabled autofs,gpm,sendmail,cups,iptables,ip6tables,auditd,arptables_jf,xfs,pcmcia,isdn,rawdevices,hpoj,bluetooth,openibd,avahi-daemon,avahi-dnsconfd,hidd,hplip,pcscd,restorecond,mcstrans,rhnsd,yum-updatesd
<% if puppet_enabled && @host.params['enable-puppetlabs-repo'] && @host.params['enable-puppetlabs-repo'] == 'true' -%>
repo --name=puppetlabs-products --baseurl=http://yum.puppetlabs.com/el/<%= @host.operatingsystem.major %>/products/<%= @host.architecture %>
repo --name=puppetlabs-deps --baseurl=http://yum.puppetlabs.com/el/<%= @host.operatingsystem.major %>/dependencies/<%= @host.architecture %>
<% end -%>
<% end -%>
bootloader --location=mbr --append="nofb quiet splash=quiet" <%= grub_pass %>
<% if os_major == 5 -%>
key --skip
<% end -%>
%include /tmp/diskpart.cfg
text
reboot
%packages --ignoremissing
yum
dhclient
ntp
wget
@Core
iptables-services
-firewalld
<% if puppet_enabled %>
puppet
<% if @host.params['enable-puppetlabs-repo'] && @host.params['enable-puppetlabs-repo'] == 'true' -%>
puppetlabs-release
<% end -%>
<% end -%>
%end
%pre
cat > /tmp/diskpart.cfg << EOF
<%= @host.diskLayout %>
EOF
# ensures a valid disk is addressed in the partition table layout
# sda is assumed and replaced if it is not correct
sed -i "s/sda/$(cat /proc/partitions | awk '{ print $4 }' | grep -e "^.d.$" | grep -vw fd0 | sort | head -1)/" /tmp/diskpart.cfg
%end
%post --nochroot
exec < /dev/tty3 > /dev/tty3
#changing to VT 3 so that we can see whats going on....
/usr/bin/chvt 3
(
cp -va /etc/resolv.conf /mnt/sysimage/etc/resolv.conf
/usr/bin/chvt 1
) 2>&1 | tee /mnt/sysimage/root/install.postnochroot.log
%end
%post
logger "Starting anaconda <%= @host %> postinstall"
exec < /dev/tty3 > /dev/tty3
#changing to VT 3 so that we can see whats going on....
/usr/bin/chvt 3
(
<%= snippet 'kickstart_networking_setup' %>
#update local time
echo "updating system time"
/usr/sbin/ntpdate -sub <%= @host.params['ntp-server'] || '0.fedora.pool.ntp.org' %>
/usr/sbin/hwclock --systohc
#disable NetworkManager and enable network
chkconfig NetworkManager off
chkconfig network on
# setup SSH key for root user
<%= snippet 'ssh_public_key' %>
<%= snippet 'redhat_register' %>
<%= snippet 'custom_deployment_repositories' %>
<% if @host.info["parameters"]["realm"] && @host.otp && @host.realm && @host.realm.realm_type == "Red Hat Directory Server" && os_major <= 6 -%>
<%= snippet "freeipa_register" %>
<% end -%>
# update all the base packages from the updates repository
yum -t -y -e 0 update
<% if puppet_enabled %>
# and add the puppet package
yum -t -y -e 0 install puppet
# we reuse our machine registerer instead
<%= snippet 'staypuft_client_bootstrap' %>
<% end -%>
sync
# Inform the build system that we are done.
echo "Informing Foreman that we are built"
wget -q -O /dev/null --no-check-certificate <%= foreman_url %>
# Sleeping an hour for debug
) 2>&1 | tee /root/install.post.log
exit 0
%end
EOS
end
def kickstart_default
<<'EOS'
<%#
kind: provision
name: Kickstart default
oses:
- CentOS 4
- CentOS 5
- CentOS 6
- CentOS 7
- Fedora 16
- Fedora 17
- Fedora 18
- Fedora 19
- Fedora 20
%>
<%
rhel_compatible = @host.operatingsystem.family == 'Redhat' && @host.operatingsystem.name != 'Fedora'
os_major = @host.operatingsystem.major.to_i
realm_compatible = (@host.operatingsystem.name == "Fedora" && os_major >= 20) || (rhel_compatible && os_major >= 7)
# safemode renderer does not support unary negation
realm_incompatible = (@host.operatingsystem.name == "Fedora" && os_major < 20) || (rhel_compatible && os_major < 7)
pm_set = @host.puppetmaster.empty? ? false : true
puppet_enabled = pm_set || @host.params['force-puppet']
%>
install
<%= @mediapath %>
lang en_US.UTF-8
selinux --enforcing
keyboard us
skipx
<% subnet = @host.subnet -%>
<% dhcp = subnet.dhcp_boot_mode? -%>
network --bootproto <%= dhcp ? 'dhcp' : "static --ip=#{@host.ip} --netmask=#{subnet.mask} --gateway=#{subnet.gateway} --nameserver=#{[subnet.dns_primary, subnet.dns_secondary].select(&:present?).join(',')}" %> --device=<%= @host.mac -%> --hostname <%= @host %>
rootpw --iscrypted <%= root_pass %>
firewall --<%= os_major >= 6 ? 'service=' : '' %>ssh
authconfig --useshadow --passalgo=sha256 --kickstart
timezone --utc <%= @host.params['time-zone'] || 'UTC' %>
<% if rhel_compatible && os_major > 4 -%>
services --disabled autofs,gpm,sendmail,cups,iptables,ip6tables,auditd,arptables_jf,xfs,pcmcia,isdn,rawdevices,hpoj,bluetooth,openibd,avahi-daemon,avahi-dnsconfd,hidd,hplip,pcscd,restorecond,mcstrans,rhnsd,yum-updatesd
<% end -%>
<% if realm_compatible && @host.info["parameters"]["realm"] && @host.otp && @host.realm -%>
realm join --one-time-password='<%= @host.otp %>' <%= @host.realm %>
<% end -%>
<% if @host.operatingsystem.name == 'Fedora' -%>
repo --name=fedora-everything --mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=fedora-<%= @host.operatingsystem.major %>&arch=<%= @host.architecture %>
repo --name=foreman-nightly --baseurl=http://yum.theforeman.org/plugins/nightly/f<%= @host.operatingsystem.major %>/<%= @host.architecture %>
<% if puppet_enabled && @host.params['enable-puppetlabs-repo'] && @host.params['enable-puppetlabs-repo'] == 'true' -%>
repo --name=puppetlabs-products --baseurl=http://yum.puppetlabs.com/fedora/f<%= @host.operatingsystem.major %>/products/<%= @host.architecture %>
repo --name=puppetlabs-deps --baseurl=http://yum.puppetlabs.com/fedora/f<%= @host.operatingsystem.major %>/dependencies/<%= @host.architecture %>
<% end -%>
<% elsif rhel_compatible && os_major > 4 -%>
repo --name="Extra Packages for Enterprise Linux" --mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-<%= @host.operatingsystem.major %>&arch=<%= @host.architecture %>
repo --name=foreman-nightly --baseurl=http://yum.theforeman.org/plugins/nightly/el<%= @host.operatingsystem.major %>/<%= @host.architecture %>
<% if puppet_enabled && @host.params['enable-puppetlabs-repo'] && @host.params['enable-puppetlabs-repo'] == 'true' -%>
repo --name=puppetlabs-products --baseurl=http://yum.puppetlabs.com/el/<%= @host.operatingsystem.major %>/products/<%= @host.architecture %>
repo --name=puppetlabs-deps --baseurl=http://yum.puppetlabs.com/el/<%= @host.operatingsystem.major %>/dependencies/<%= @host.architecture %>
<% end -%>
<% end -%>
<% if @host.operatingsystem.name == 'Fedora' and os_major <= 16 -%>
# Bootloader exception for Fedora 16:
bootloader --append="nofb quiet splash=quiet <%=ks_console%>" <%= grub_pass %>
part biosboot --fstype=biosboot --size=1
<% else -%>
bootloader --location=mbr --append="nofb quiet splash=quiet" <%= grub_pass %>
<% end -%>
%include /tmp/diskpart.cfg
text
reboot
%packages --ignoremissing
yum
dhclient
ntp
wget
@Core
epel-release
<% if puppet_enabled %>
puppet
<% if @host.params['enable-puppetlabs-repo'] && @host.params['enable-puppetlabs-repo'] == 'true' -%>
puppetlabs-release
<% end -%>
<% end -%>
%end
%pre
cat > /tmp/diskpart.cfg << EOF
<%= @host.diskLayout %>
EOF
# ensures a valid disk is addressed in the partition table layout
# sda is assumed and replaced if it is not correct
sed -i "s/sda/$(cat /proc/partitions | awk '{ print $4 }' | grep -e "^.d.$" | sort | head -1)/" /tmp/diskpart.cfg
%end
%post --nochroot
exec < /dev/tty3 > /dev/tty3
#changing to VT 3 so that we can see whats going on....
/usr/bin/chvt 3
(
cp -va /etc/resolv.conf /mnt/sysimage/etc/resolv.conf
/usr/bin/chvt 1
) 2>&1 | tee /mnt/sysimage/root/install.postnochroot.log
%end
%post
logger "Starting anaconda <%= @host %> postinstall"
exec < /dev/tty3 > /dev/tty3
#changing to VT 3 so that we can see whats going on....
/usr/bin/chvt 3
(
<%= snippet 'kickstart_networking_setup' %>
<%= snippet 'custom_deployment_repositories' %>
# get name of provisioning interface
PROVISION_IFACE=$(ip route | awk '$1 == "default" {print $5}' | head -1)
echo "found provisioning interface = $PROVISION_IFACE"
<% if @host.hostgroup.to_s.include?("Controller") %>
echo "setting DEFROUTE=no on $PROVISION_IFACE"
sed -i '
/DEFROUTE/ d
$ a\DEFROUTE=no
' /etc/sysconfig/network-scripts/ifcfg-$PROVISION_IFACE
<% end -%>
#update local time
echo "updating system time"
/usr/sbin/ntpdate -sub <%= @host.params['ntp-server'] || '0.fedora.pool.ntp.org' %>
/usr/sbin/hwclock --systohc
# setup SSH key for root user
<%= snippet 'ssh_public_key' %>
<% if realm_incompatible && @host.info["parameters"]["realm"] && @host.otp && @host.realm && @host.realm.realm_type == "Red Hat Directory Server" -%>
<%= snippet "freeipa_register" %>
<% end -%>
# update all the base packages from the updates repository
yum -t -y -e 0 update
# ensure firewalld is absent (BZ#1125075)
yum -t -y -e 0 remove firewalld
<% if puppet_enabled %>
echo "Configuring puppet"
#cat > /etc/puppet/puppet.conf << EOF
#<% # snippet 'puppet.conf' %>
#EOF
#
## Setup puppet to run on system reboot
#/sbin/chkconfig --level 345 puppet on
#
#/usr/bin/puppet agent --config /etc/puppet/puppet.conf -o --tags no_such_tag <%= @host.puppetmaster.blank? ? '' : "--server #{@host.puppetmaster}" %> --no-daemonize
# we reuse our machine registerer instead
<%= snippet 'staypuft_client_bootstrap' %>
<% end -%>
sync
# Inform the build system that we are done.
echo "Informing Foreman that we are built"
wget -q -O /dev/null --no-check-certificate <%= foreman_url %>
# Sleeping an hour for debug
) 2>&1 | tee /root/install.post.log
exit 0
%end
EOS
end
def staypuft_bootstrap_snippet
<<EOS
yum install -t -e 0 -y foreman-installer-staypuft-client
cat > /etc/foreman/staypuft-client-installer.answers.yaml << EOF
<%= snippet 'staypuft-client-installer-answers-yaml' %>
EOF
# Run the installer twice to mitigate potential pluginsync timeouts
# https://github.com/theforeman/staypuft/pull/414#issuecomment-71828263
staypuft-client-installer
staypuft-client-installer
EOS
end
def staypuft_staypuft_answers_snippet
<<EOS
---
puppet:
server: false
runmode: none
puppetmaster: <%= @host.puppetmaster %>
foreman::plugin::staypuft_client:
staypuft_ssh_public_key: <%= @host.params['staypuft_ssh_public_key'] || 'missing' %>
EOS
end
def template
<<EOS
DEFAULT menu
PROMPT 0
MENU TITLE PXE Menu
TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT discovery
LABEL discovery
MENU LABEL Foreman Discovery
KERNEL boot/#{@kernel}
APPEND rootflags=loop initrd=boot/#{@initrd} root=live:/foreman.iso rootfstype=auto ro rd.live.image rd.live.check rd.lvm=0 rootflags=ro crashkernel=128M elevator=deadline max_loop=256 rd.luks=0 rd.md=0 rd.dm=0 foreman.url=#{@foreman_url} nomodeset selinux=0 stateless biosdevname=0
IPAPPEND 2
EOS
end
def ssh_public_key_snippet
<<'EOS'
mkdir --mode=700 /root/.ssh
cat >> /root/.ssh/authorized_keys << PUBLIC_KEY
<%= @host.params['ssh_public_key'] %>
PUBLIC_KEY
chmod 600 /root/.ssh/authorized_keys
EOS
end
def custom_deployment_repositories_snippet
<<'EOS'
# custom deployment repositories
<% if @host.deployment.has_custom_repos? -%>
yum -t -y -e 0 install yum-plugin-priorities
<% i = 0 -%>
<% @host.deployment.custom_repos_paths.each do |path| -%>
<% i +=1 -%>
cat > /etc/yum.repos.d/staypuft_custom_<%= i -%>.repo << EOF
[staypuft_custom_<%= i -%>]
name=Staypuft custom repository <%= i %>
baseurl=<%= path %>
gpgcheck=0
priority=50
enabled=1
EOF
<% end %>
<% end %>
EOS
end
def kickstart_networking_setup_snippet
<<'EOS'
<%#
kind: snippet
name: kickstart_networking_setup
description: this will configure your host networking, it configures your primary interface as well
as other configures NICs. It supports physical, VLAN and Alias interfaces. It's intended to be
called from %post in your kickstart template
%>
<% subnet = @host.subnet -%>
<% dhcp = subnet.dhcp_boot_mode? -%>
<% bonds = @host.bond_interfaces -%>
<% mtu = @host.network_query.mtu_for_tenant_subnet -%>
<% tenant_subnet = @host.network_query.tenant_subnet?(subnet, @host) -%>
<% tenant_interface = @host.network_query.tenant_interface?(@host.primary_interface) -%>
# primary interface
real=`ip -o link | grep <%= @host.mac -%> | awk '{print $2;}' | sed s/://`
<% if @host.has_primary_interface? %>
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-$real
<% unless dhcp || @host.primary_interface_is_bonded? -%>
IPADDR="<%= @host.ip -%>"
NETMASK="<%= subnet.mask -%>"
GATEWAY="<%= subnet.gateway -%>"
<% end -%>
DEVICE="$real"
HWADDR="<%= @host.mac -%>"
BOOTPROTO="<%= (dhcp && !@host.primary_interface_is_bonded?) ? 'dhcp' : 'none' -%>"
ONBOOT=yes
DEFROUTE=no
<% if (tenant_subnet || tenant_interface) && mtu -%>
MTU=<%= mtu %>
<% end -%>
<% bonds.each do |bond| -%>
<% next if !bond.attached_devices_identifiers.include? @host.primary_interface -%>
MASTER=<%= bond.identifier %>
SLAVE=yes
NM_CONTROLLED=no
PEERDNS=no
PEERROUTES=no
<% end -%>
EOF
<% end -%>
<% bonded_interfaces = [] -%>
<% bonds.each do |bond| %>
<% subnet = bond.subnet -%>
<% dhcp = subnet.nil? ? false : subnet.dhcp_boot_mode? -%>
<% tenant_subnet = @host.network_query.tenant_subnet?(subnet, @host) -%>
<% tenant_interface = @host.network_query.tenant_interface?(bond.identifier) -%>
# <%= bond.identifier %> interface
real="<%= bond.identifier -%>"
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-$real
BOOTPROTO="<%= dhcp ? 'dhcp' : 'none' -%>"
<% unless dhcp || subnet.nil? -%>
IPADDR="<%= bond.ip -%>"
NETMASK="<%= subnet.mask -%>"
GATEWAY="<%= subnet.gateway -%>"
<% end -%>
DEVICE="$real"
ONBOOT=yes
PEERDNS=no
PEERROUTES=no
DEFROUTE=no
TYPE=Bond
BONDING_OPTS="<%= bond.bond_options -%> mode=<%= bond.mode -%>"
BONDING_MASTER=yes
NM_CONTROLLED=no
<% if (tenant_subnet || tenant_interface) && mtu -%>
MTU=<%= mtu %>
<% end -%>
EOF
<% @host.interfaces_with_identifier(bond.attached_devices_identifiers).each do |interface| -%>
<% next if !interface.managed? -%>
<% subnet = interface.subnet -%>
<% virtual = interface.virtual? -%>
<% vlan = virtual && subnet.has_vlanid? -%>
<% alias_type = virtual && !subnet.nil? && !subnet.has_vlanid? && interface.identifier.include?(':') -%>
<% dhcp = !subnet.nil? && subnet.dhcp_boot_mode? -%>
<% tenant_subnet = @host.network_query.tenant_subnet?(subnet, @host) -%>
<% tenant_interface = @host.network_query.tenant_interface?(interface.identifier) -%>
# <%= interface.identifier %> interface
real=`ip -o link | grep <%= interface.mac -%> | awk '{print $2;}' | sed s/:$//`
<% if virtual -%>
real=`echo <%= interface.identifier -%> | sed s/<%= interface.attached_to -%>/$real/`
<% end -%>
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-$real
BOOTPROTO="none"
DEVICE="$real"
<% unless virtual -%>
HWADDR="<%= interface.mac -%>"
<% end -%>
ONBOOT=yes
PEERDNS=no
PEERROUTES=no
<% if vlan -%>
VLAN=yes
<% elsif alias_type -%>
TYPE=Alias
<% end -%>
NM_CONTROLLED=no
MASTER=<%= bond.identifier %>
SLAVE=yes
DEFROUTE=no
<% if (tenant_subnet || tenant_interface) && mtu -%>
MTU=<%= mtu %>
<% end -%>
EOF
<% bonded_interfaces.push(interface.identifier) -%>
<% end %>
<% end %>
<% @host.managed_interfaces.each do |interface| %>
<% next if !interface.managed? -%>
<% next if bonded_interfaces.include?(interface.identifier) -%>
<% subnet = interface.subnet -%>
<% virtual = interface.virtual? -%>
<% vlan = subnet.nil? ? false : virtual && subnet.has_vlanid? -%>
<% alias_type = subnet.nil? ? false : virtual && !subnet.has_vlanid? && interface.identifier.include?(':') -%>
<% dhcp = subnet.nil? ? false : subnet.dhcp_boot_mode? -%>
<% tenant_subnet = @host.network_query.tenant_subnet?(subnet, @host) -%>
<% tenant_interface = @host.network_query.tenant_interface?(interface.identifier) -%>
# <%= interface.identifier %> interface
<% unless virtual -%>
real=`ip -o link | grep <%= interface.mac -%> | awk '{print $2;}' | sed s/:$//`
<% else -%>
real="<%= interface.identifier -%>"
<% end -%>
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-$real
BOOTPROTO="<%= dhcp ? 'dhcp' : 'none' -%>"
<% unless dhcp || subnet.nil? -%>
IPADDR="<%= interface.ip -%>"
NETMASK="<%= subnet.mask -%>"
GATEWAY="<%= subnet.gateway -%>"
<% end -%>
DEVICE="$real"
<% unless virtual -%>
HWADDR="<%= interface.mac -%>"
<% end -%>
ONBOOT=<%= subnet.nil? ? "no" : "yes" %>
PEERDNS=no
PEERROUTES=no
<% if vlan -%>
VLAN=yes
<% elsif alias_type -%>
TYPE=Alias
<% end -%>
NM_CONTROLLED=no
DEFROUTE=no
<% if (tenant_subnet || tenant_interface) && mtu -%>
MTU=<%= mtu %>
<% end -%>
EOF
<% end %>
# get name of provisioning interface
PROVISION_IFACE=$(ip route | awk '$1 == "default" {print $5}' | head -1)
echo "found provisioning interface = $PROVISION_IFACE"
DEFROUTE_IFACE=`ip -o link | grep <%= @host.network_query.gateway_interface_mac -%> | awk '{print $2;}' | sed s/:$//`
echo "found interface with default gateway = $DEFROUTE_IFACE"
<% gateway_interface = @host.network_query.gateway_interface
gateway_is_vlan = @host.network_query.gateway_subnet.has_vlanid?
bond_gateway_map = @host.bond_interfaces.map { |bond| bond.identifier == gateway_interface }
gateway_is_bond = false
bond_gateway_map.each do |is_gateway|
next if gateway_is_bond
gateway_is_bond = is_gateway
end -%>
IFACES=$(ls -d /sys/class/net/* | while read iface; do readlink $iface | grep -q virtual || echo ${iface##*/}; done)
<% if gateway_is_vlan or gateway_is_bond -%>
IFACES="$IFACES <%= gateway_interface %>"
DEFROUTE_IFACE="<%= gateway_interface %>"
echo "gateway interface is a vlan and/or bond = $DEFROUTE_IFACE"
<% end -%>
for i in $IFACES; do
if [ "$i" != "$PROVISION_IFACE" ]; then
echo "setting PEERDNS=no on $i"
sed -i '
/PEERDNS/ d
$ a\PEERDNS=no
' /etc/sysconfig/network-scripts/ifcfg-$i
fi
if [ "$i" = "$DEFROUTE_IFACE" ]; then
echo "setting DEFROUTE=yes on $i"
sed -i '
/DEFROUTE/ d
$ a\DEFROUTE=yes
' /etc/sysconfig/network-scripts/ifcfg-$i
fi
done
EOS
end
def lvm_w_cinder_volumes
<<'EOS'
#Dynamic
zerombr
clearpart --all --initlabel
part biosboot --fstype=biosboot --size=1 --ondisk=sda
part /boot --fstype ext3 --size=500 --ondisk=sda
part swap --size=1024 --ondisk=sda
part pv.01 --size=1024 --grow --maxsize=102400 --ondisk=sda
part pv.02 --size=1024 --grow --ondisk=sda
volgroup vg_root pv.01
volgroup cinder-volumes pv.02
logvol / --vgname=vg_root --size=1 --grow --name=lv_root
EOS
end
def openstack_lvm
<<'EOS'
#Dynamic
zerombr
clearpart --all --initlabel
part biosboot --fstype=biosboot --size=1 --ondisk=sda
part /boot --fstype ext3 --size=500 --ondisk=sda
part swap --size=1024 --ondisk=sda
part pv.01 --size=1024 --grow --ondisk=sda
volgroup vg_root pv.01
logvol / --vgname=vg_root --size=1 --grow --name=lv_root
EOS
end
def redhat_register_snippet
<<'EOS'
<%#
kind: snippet
name: redhat_register
%>
# Red Hat Registration Snippet
#
# Set these parameters if you're using rhnreg_ks:
#
# spacewalk_type = 'site' (local Spacewalk/Satellite server)
# = 'hosted' (RHN hosted)
# spacewalk_host = <hostname> (hostname of Spacewalk server, optional for
# RHN hosted)
#
# Set these parameters if you're using subscription-manager:
#
# subscription_manager = 'true' (you're going to use subscription-manager)
#
# subscription_manager_username = <username> (if using hosted RHN)
#
# subscription_manager_password = <password> (if using hosted RHN)
#
# subscription_manager_host = <hostname> (hostname of SAM/Katello
# installation, if using SAM)
#
# subscription_manager_org = <org name> (organization name, if using
# SAM/Katello)
#
# subscription_manager_repos = <repos> (comma separated list of repos (like
# rhel-6-server-optional-rpms) to
# enable after registration)
#
# subscription_manager_pool = <pool> (specific pool to be used for
# registration)
#
# http-proxy = <host> (proxy hostname to be used for registration)
#
# http-proxy-port = <port> (proxy port to be used for registration)
#
# http-proxy-user = <user> (proxy user to be used for registration)
#
# http-proxy-password = <password> (proxy password to be
# used for registration)
#
# Set this parameter regardless of which registration method you're using:
#
# activation_key = <key> (activation key string, not needed if using
# subscription-manager with hosted RHN)
#
<% unless @host.params['subscription_manager'] %>
<% type = @host.params['spacewalk_type'] || 'hosted' %>
<% if @host.params['activation_key'] %>
# Discovered Activation Key <%= @host.params['activation_key'] %>
rhn_activation_key="<%= @host.params['activation_key'] -%>"
<% if type == "site" -%>
satellite_hostname="<%= @host.params['spacewalk_host'] -%>"