-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraform_deploy_v2.sh
1913 lines (1789 loc) · 92.8 KB
/
terraform_deploy_v2.sh
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
#!/bin/bash
########################################################### {COPYRIGHT-TOP} ####
# Licensed Materials - Property of IBM
# CloudOE
#
# (C) Copyright IBM Corp. 2019
#
# US Government Users Restricted Rights - Use, duplication, or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
########################################################### {COPYRIGHT-END} ####
## This script is used to launch FCI VMs (kube master, kube worker, hdp vms, etc) via Terraform, and do the customization (disk partition, software installation,etc) on the VMs
#variables statement
cur_dir=$(cd `dirname $0` && pwd)
terraform_factory_dir="${cur_dir}/terraform_factory"
terraform_shared_tfvars="${terraform_factory_dir}/terraform.tfvars.shared.config"
terraform="/usr/local/bin/terraform"
terraform_plugin_work_dir="${cur_dir}/plugin"
terraform_plugin_dir="${cur_dir}/plugin/.terraform"
terraform_tf_vars_name="terraform.tfvars"
build_tf_name="build.tf"
variables_tf_name="variables.tf"
#templates variable statement
tf_config_dir=${cur_dir}/config
tf_json_config_file=${tf_config_dir}/terraform.tfvars.json
custom_properties_file=${tf_config_dir}/custom.properties
tf_shared_templates_dir=${cur_dir}/templates/
#templates files for generating build.tf, variables.tf and terraform.tfvars files
tf_build_tf_template=${tf_shared_templates_dir}/build.tf.template
tf_build_tag_template=${tf_shared_templates_dir}/build.tf.tag.template
build_tf_disk_template=${tf_shared_templates_dir}/build.tf.disk.template
build_tf_internal_network_customization_template=${tf_shared_templates_dir}/build.tf.internal.network.customization.template
build_tf_internal_network_definition_template=${tf_shared_templates_dir}/build.tf.internal.network.definition.template
build_tf_internal_network_data_template=${tf_shared_templates_dir}/build.tf.internal.network.data.template
build_tf_compute_cluster_data_template=${tf_shared_templates_dir}/build.tf.compute.cluster.data.template
build_tf_compute_cluster_resource_pool_id_template=${tf_shared_templates_dir}/build.tf.compute.cluster.resource.pool.id.definition.template
build_tf_compute_cluster_host_system_id_template=${tf_shared_templates_dir}/build.tf.compute.cluster.host.system.id.definition.template
build_tf_compute_cluster_vm_host_rules_resource_template=${tf_shared_templates_dir}/build.tf.compute.cluster.vm.host.rules.resource.template
build_tf_host_data_definition_template=${tf_shared_templates_dir}/build.tf.host.data.template
build_tf_single_host_resource_pool_id_template=${tf_shared_templates_dir}/build.tf.single.host.resource.pool.id.definition.template
build_tf_resource_pool_data_template=${tf_shared_templates_dir}/build.tf.vsphere.resource.pool.data.template
build_tf_connection_template=${tf_shared_templates_dir}/build.tf.connection.template
build_tf_connection_hosts_file_provisioner_template=${tf_shared_templates_dir}/build.tf.connection.hosts.file.provisioner.template
build_tf_connection_hdpa_hosts_prop_file_provisioner_template=${tf_shared_templates_dir}/build.tf.connection.hdpa.hosts.prop.file.provisioner.template
build_tf_connection_km_hosts_prop_file_provisioner_template=${tf_shared_templates_dir}/build.tf.connection.km.hosts.prop.file.provisioner.template
build_tf_connection_hostname_change_provisioner_template=${tf_shared_templates_dir}/build.tf.connection.hostname.change.provisioner.template
build_tf_connection_customization_provisioner_template=${tf_shared_templates_dir}/build.tf.connection.customization.provisioner.template
build_tf_tag_data_template=${tf_shared_templates_dir}/build.tf.data.vm.tag.template
build_tf_tag_resource_template=${tf_shared_templates_dir}/build.tf.resource.vm.tag.template
mgmt_hosts_file_template=${tf_shared_templates_dir}/mgmt.hosts.file.template
mgmt_reverse_hosts_file_template=${tf_shared_templates_dir}/mgmt.reverse.hosts.file.template
haproxy_cfg_file_template=${tf_shared_templates_dir}/haproxy.cfg.template
variables_tf_shared_template=${tf_shared_templates_dir}/variables.tf.template
variables_tf_internal_network_template=${tf_shared_templates_dir}/variables.tf.internal.network.template
variables_tf_compute_cluster_template=${tf_shared_templates_dir}/variable.tf.compute.cluster.template
variables_tf_compute_cluster_vm_host_template=${tf_shared_templates_dir}/variable.tf.compute.cluster.vm.host.template
variables_tf_single_host_template=${tf_shared_templates_dir}/variable.tf.single.host.template
terraform_tfvars_distinct_config_template=${tf_shared_templates_dir}/terraform.tfvars.distinct.config.template
terraform_tfvars_shared_config_template=${tf_shared_templates_dir}/terraform.tfvars.shared.config.template
# definition keywords to be replaced in build.tf and variables.tf template file
disk_definition_keywords_in_build_tf_template="#disks_definition"
kube_internal_nw_definition_keywords_in_build_tf_template="#kube_internal_network_definition"
kube_internal_nw_customization_keywords_in_build_tf_template="#kube_internal_network_customization"
kube_internal_nw_data_keywords_in_build_tf_template="#kube_internal_network_data"
connection_keywords_in_build_tf_template="#connection_definition"
vsphere_compute_cluster_data_keywords_in_build_tf_template="#vsphere_compute_cluster_data_definition"
vsphere_host_data_keywords_in_build_tf_template="#vsphere_host_data_definition"
vsphere_resource_pool_data_keywords_in_build_tf_template="#vsphere_resource_pool_data_definition"
resource_pool_id_keywords_in_build_tf_template="#resource_pool_id_definition"
host_system_id_keywords_in_build_tf_template="#host_system_id_definition"
kube_internal_nw_vars_keywords_in_variables_tf_template="kube_internal_network_variables"
compute_cluster_vm_host_vars_keywords_in_variables_tf_template="compute_cluster_vm_host_variables"
host_file_provisioner_keywords_in_build_tf_connection_template="#hosts_file_provisioner"
km_hosts_properties_file_provisioner_keywords_in_build_tf_connection_template="#km_hosts_prop_file_provisioner"
hdpa_hosts_properties_file_provisioner_keywords_in_build_tf_connection_template="#hdpa_hosts_prop_file_provisioner"
hostname_change_provisioner_keywords_in_build_tf_connection_template="#hostname_change_provisioner"
customization_provisioner_keywords_in_build_tf_connection_template="#customization_provisioner"
tag_vm_resource_data_section_in_build_tf_template="#tag_vm_resource_data_section"
vm_tags_list_section_in_build_tf_template="#vm_tags_list_section"
#files directory, which stores artifacts for remote executing, i.e scripts, packages, etc
terraform_files_dir=${cur_dir}/files
terraform_files_secrets_dir=${cur_dir}/files/fci_keystore
terraform_customization_script_template="$terraform_files_dir/customization.sh.template"
terraform_customization_script_name="customization.sh"
terraform_hdp_customization_script_template="$terraform_files_dir/customization.hdp.sh.template"
terraform_hdp_customization_script_name="customization.hdp.sh"
terraform_customization_kube_platform_script_name="customization.kube.platform.sh"
terraform_customization_dd_script_name="customization.dd.sh"
terraform_customization_insurance_script_name="customization.insurance.sh"
terraform_mgmt_files_dir="${terraform_factory_dir}/mgmt/files"
terraform_mgmt_host_file="${terraform_mgmt_files_dir}/wfss.ibm.com.hosts.template"
terraform_km_files_dir="${terraform_factory_dir}/km/files"
terraform_km_hosts_file="${terraform_km_files_dir}/install.hosts.properties.template"
terraform_hdpa_files_dir="${terraform_factory_dir}/hdpa/files"
terraform_hdpa_hosts_properties_file="${terraform_hdpa_files_dir}/fci-hadoop.prod.hosts.properties"
terraform_haproxy_files_dir="${terraform_factory_dir}/haproxy/files"
terraform_haproxy_cfg_file="${terraform_haproxy_files_dir}/haproxy.cfg.template"
terraform_ms1_files_dir="${terraform_factory_dir}/ms1/files"
terraform_vm_tag_folder="${terraform_factory_dir}/tag"
terraform_vm_tag_terraform_build_file="${terraform_vm_tag_folder}/build.tf"
terraform_vm_tag_terraform_tfvars_file="${terraform_vm_tag_folder}/terraform.tfvars"
terraform_vm_tag_terraform_variables_file="${terraform_vm_tag_folder}/variables.tf"
#json file for vms in a specific datacenter. generated by calling pyvmomi
vms_in_dc_file="${cur_dir}/vms_in_dc.json"
# Usage statement
usage() {
echo
echo "This script is used to launch FCI VMs (kube master, kube worker, hdp vms, etc) via Terraform, and do the customization (disk partition, software installation,etc) on the VMs"
echo
echo "Usage: $0 [OPTIONS]...[ARGS]"
echo
echo " -a|--all"
echo " whether need to launch all VMs defined via Terraform"
echo
echo " -km|--kube_master"
echo " whether need to launch kube master via Terraform"
echo
echo " -kw|--kube_worker"
echo " whether need to launch kube worker via Terraform"
echo
echo " -db|--database"
echo " whether need to launch db via Terraform"
echo
echo " -docreg|--docker_registry"
echo " whether need to launch docker registry via Terraform"
echo
echo " -kwdd|--kube_worker_dd"
echo " whether need to launch kube worker dd via Terraform"
echo
echo " -mgmt|--management"
echo " whether need to launch management vm via Terraform"
echo
echo " -nfs1|--nfs1"
echo " whether need to launch nfs1 via Terraform"
echo
echo " -nfs2|--nfs2"
echo " whether need to launch nfs2 via Terraform"
echo
echo " -nfs3|--nfs3"
echo " whether need to launch nfs3 via Terraform"
echo
echo " -web|--web"
echo " whether need to launch web vm via Terraform"
echo
echo " -wexc|--wexc"
echo " whether need to launch wexc via Terraform"
echo
echo " -wexfp|--wexfp"
echo " whether need to launch wexfp via Terraform"
echo
echo " -hdpa|--hdpa"
echo " whether need to launch hadoop ambari via Terraform"
echo
echo " -hdpg|--hdpg"
echo " whether need to launch hadoop gateway via Terraform"
echo
echo " -hdpm|--hdpm"
echo " whether need to launch hadoop master via Terraform"
echo
echo " -hdps1|--hdps1"
echo " whether need to launch hadoop slave1 via Terraform"
echo
echo " -hdps2|--hdps2"
echo " whether need to launch hadoop slave2 via Terraform"
echo
echo " -hdpsec|--hdpsec"
echo " whether need to launch hadoop security via Terraform"
echo
echo " -ms|--ms"
echo " whether need to launch ICP4D ms via Terraform"
echo
echo " -str|--stream"
echo " whether need to launch stream vms via Terraform"
echo
echo " -zkp|--zkp"
echo " whether need to launch zookeeper vm via Terraform"
echo
echo " -wexm|--wexm"
echo " whether need to launch wexm via Terraform"
echo
echo " -api|--api"
echo " whether need to launch api server via Terraform"
echo
echo " -st|--storage"
echo " whether need to launch storage vm via Terraform"
echo
echo " -worker|--worker"
echo " whether need to launch worker vm via Terraform"
echo
echo " -landing|--landing"
echo " whether need to launch landing vm via Terraform"
echo
echo " -bigdata|--bigdata"
echo " whether need to launch bigdata vm via Terraform"
echo
echo " -haproxy|--haproxy"
echo " whether need to launch haproxy vm via Terraform"
echo
echo " -portworx|--portworx"
echo " whether need to launch haproxy vm via Terraform"
echo
echo " -disable_validator|--disable_validator"
echo " whether need to disable validator or NOT"
echo
echo " -ansible|--ansible"
echo " whether need to generate ansible hosts file via Terraform"
echo
echo " -zabbix|--zabbix"
echo " whether need to integrate with zabbix via Terraform"
echo
echo " -deploy|--deploy"
echo " whether launch terraform deploy or not"
echo
}
if [ $# -lt 1 ]; then
usage
exit 1
fi
### PARAMETER ###
is_all=0
is_kube_master=0
is_kube_worker=0
is_db=0
is_docreg=0
is_kwdd=0
is_mgmt=0
is_nfs1=0
is_nfs2=0
is_nfs3=0
is_web=0
is_wexc=0
is_wexfp=0
is_wexm=0
is_hdpa=0
is_hdpg=0
is_hdpm=0
is_hdps1=0
is_hdps2=0
is_hdpsec=0
is_ms=0
is_zkp=0
is_str=0
is_api=0
is_storage=0
is_worker=0
is_landing=0
is_bigdata=0
is_haproxy=0
is_portworx=0
is_ansible=0
disable_validator=0
is_zabbix=0
terraform_deploy=0
while [[ $# > 0 ]]
do
key="$1"
shift
case ${key} in
-a|--all)
is_all=1
;;
-km|--kube_master)
is_kube_master=1
;;
-kw|--kube_worker)
is_kube_worker=1
;;
-db|--database)
is_db=1
;;
-docreg|--docker_registry)
is_docreg=1
;;
-kwdd|--kube_worker_dd)
is_kwdd=1
;;
-mgmt|--management)
is_mgmt=1
;;
-nfs1|--nfs1)
is_nfs1=1
;;
-nfs2|--nfs2)
is_nfs2=1
;;
-nfs3|--nfs3)
is_nfs3=1
;;
-web|--web)
is_web=1
;;
-wexc|--wexc)
is_wexc=1
;;
-wexfp|--wexfp)
is_wexfp=1
;;
-wexm|--wexm)
is_wexm=1
;;
-hdpa|--hdpa)
is_hdpa=1
;;
-hdpg|--hdpg)
is_hdpg=1
;;
-hdpm|--hdpm)
is_hdpm=1
;;
-hdps1|--hdps1)
is_hdps1=1
;;
-hdps2|--hdps2)
is_hdps2=1
;;
-hdpsec|--hdpsec)
is_hdpsec=1
;;
-ms|--ms)
is_ms=1
;;
-zkp|--zkp)
is_zkp=1
;;
-str|--stream)
is_str=1
;;
-api|--api)
is_api=1
;;
-st|--storage)
is_storage=1
;;
-worker|--worker)
is_worker=1
;;
-landing|--landing)
is_landing=1
;;
-bigdata|--bigdata)
is_bigdata=1
;;
-haproxy|--haproxy)
is_haproxy=1
;;
-portworx|--portworx)
is_portworx=1
;;
-disable_validator|--disable_validator)
disable_validator=1
;;
-ansible|--ansible)
is_ansible=1
;;
-zabbix|--zabbix)
is_zabbix=1
;;
-deploy|--deploy)
terraform_deploy=1
;;
-h|--help)
usage
exit 1
;;
*)
echo -e "\nUnknow Argument"
usage
exit 1
;;
esac
done
if [ ! -f $terraform ]; then
echo "$terraform does NOT exist. Please install it first. Exiting..."
exit 1
fi
if [ $is_all -eq 1 ]; then
echo "to provision an environment by cloning existing template VMs"
is_kube_master=1
is_kube_worker=1
is_db=1
is_docreg=1
is_kwdd=1
is_mgmt=1
is_nfs1=1
is_nfs2=1
is_nfs3=1
is_web=1
is_wexc=1
is_wexfp=1
is_wexm=1
is_hdpa=1
is_hdpg=1
is_hdpm=1
is_hdps1=1
is_hdps2=1
is_hdpsec=1
is_ms=1
is_zkp=1
is_str=1
is_api=1
is_storage=1
is_worker=1
is_landing=1
is_bigdata=1
is_haproxy=1
is_portworx=1
fi
#function to add ip by 1, for example, for ip 10.0.80.11, after adding by 1, it will be 10.0.80.12
function add_ip_by_one() {
local original_ip="$1"
local ip_prefix="`echo ${original_ip} | awk -F "." '{print $1}'`.`echo ${original_ip} | awk -F "." '{print $2}'`.`echo ${original_ip} | awk -F "." '{print $3}'`."
local ip_start_number=`echo ${original_ip} | awk -F "." '{print $4}'`
local ip_postfix=""
((ip_postfix=ip_start_number+1))
echo "${ip_prefix}${ip_postfix}"
}
# function to increment ip address by any number
function increment_ip () {
local ip="$1"
# subtract one from the counter to give the increment
local increment=$(( $2 - 1 ))
local baseaddr="$(echo $ip | cut -d. -f1-3)"
local octet4="$(echo $ip | cut -d. -f4)"
new_octet4=$(( $octet4 + $increment ))
echo $baseaddr.$new_octet4
}
#generate shared tfvars file, which can be used for all types of vms
function generate_shared_tfvars(){
local vm_it=0
if [ ! -d $terraform_factory_dir ]; then
mkdir -p $terraform_factory_dir
fi
if [ -f ${terraform_shared_tfvars} ]; then
rm ${terraform_shared_tfvars}
fi
local vsphere_vcenter=$(jq -r ".vsphere_vcenter" $tf_json_config_file)
echo "vsphere_vcenter = \"$vsphere_vcenter\"" > ${terraform_shared_tfvars}
local vsphere_user=$(jq -r ".vsphere_user" $tf_json_config_file)
echo "vsphere_user = \"$vsphere_user\"" >> ${terraform_shared_tfvars}
local vsphere_password=$(jq -r ".vsphere_password" $tf_json_config_file)
echo "vsphere_password = \"$vsphere_password\"" >> ${terraform_shared_tfvars}
local vsphere_source_datacenter=$(jq -r ".vsphere_source_datacenter" $tf_json_config_file)
echo "vsphere_source_datacenter = \"$vsphere_source_datacenter\"" >> ${terraform_shared_tfvars}
local vsphere_source_vm_folder=$(jq -r ".vsphere_source_vm_folder" $tf_json_config_file)
echo "vsphere_source_vm_folder = \"$vsphere_source_vm_folder\"" >> ${terraform_shared_tfvars}
local vsphere_target_datacenter=$(jq -r ".vsphere_target_datacenter" $tf_json_config_file)
echo "vsphere_target_datacenter = \"$vsphere_target_datacenter\"" >> ${terraform_shared_tfvars}
local vsphere_compute_cluster=$(jq -r ".vsphere_compute_cluster" $tf_json_config_file)
if [ ! -z "${vsphere_compute_cluster// }" ] && [ "$vsphere_compute_cluster" != "null" ]; then
echo "vsphere_compute_cluster = \"$vsphere_compute_cluster\"" >> ${terraform_shared_tfvars}
fi
local vsphere_host=$(jq -r ".vsphere_host" $tf_json_config_file)
if [ ! -z "${vsphere_host// }" ] && [ "$vsphere_host" != "null" ]; then
echo "vsphere_host = \"$vsphere_host\"" >> ${terraform_shared_tfvars}
fi
local vsphere_host_folder=$(jq -r ".vsphere_host_folder" $tf_json_config_file)
if [ ! -z "${vsphere_host_folder// }" ] && [ "$vsphere_host_folder" != "null" ]; then
echo "vsphere_host_folder = \"$vsphere_host_folder\"" >> ${terraform_shared_tfvars}
fi
local vsphere_target_datastore=$(jq -r ".vsphere_target_datastore" $tf_json_config_file)
if [ ! -z "${vsphere_target_datastore// }" ] && [ "$vsphere_target_datastore" != "null" ]; then
echo "vsphere_target_datastore = \"$vsphere_target_datastore\"" >> ${terraform_shared_tfvars}
fi
local vsphere_target_vm_folder=$(jq -r ".vsphere_target_vm_folder" $tf_json_config_file)
echo "vsphere_target_vm_folder = \"$vsphere_target_vm_folder\"" >> ${terraform_shared_tfvars}
local vsphere_sl_private_network_name=$(jq -r ".vsphere_sl_private_network_name" $tf_json_config_file)
if [ ! -z "${vsphere_sl_private_network_name// }" ] && [ "$vsphere_sl_private_network_name" != "null" ]; then
echo "vsphere_sl_private_network_name = \"$vsphere_sl_private_network_name\"" >> ${terraform_shared_tfvars}
fi
local vm_sl_private_netmask=$(jq -r ".vm_sl_private_netmask" $tf_json_config_file)
if [ ! -z "${vm_sl_private_netmask// }" ] && [ "$vm_sl_private_netmask" != "null" ]; then
echo "vm_sl_private_netmask = \"$vm_sl_private_netmask\"" >> ${terraform_shared_tfvars}
fi
local vm_network_gateway=$(jq -r ".vm_network_gateway" $tf_json_config_file)
if [ ! -z "${vm_network_gateway// }" ] && [ "$vm_network_gateway" != "null" ]; then
echo "vm_network_gateway = \"$vm_network_gateway\"" >> ${terraform_shared_tfvars}
fi
# generate vsphere_dns_servers list in terraform.tfvars. such as: [ "10.0.80.11", "10.0.80.12" ]
local vsphere_dns_servers=$(jq -r ".vsphere_dns_servers" $tf_json_config_file)
#if dns server not defined, will use mgmt vm's ip
local mgmt_sl_private_ip=""
if [ "$vsphere_dns_servers" == "null" ]; then
for vm_it in $(jq '.vms_meta | keys | .[]' $tf_json_config_file); do
local vm_type=$(jq -r ".vms_meta[$vm_it].vm_type" $tf_json_config_file)
if [ "$vm_type" == "mgmt" ]; then
mgmt_sl_private_ip=$(jq -r ".vms_meta[$vm_it].vm_sl_private_ip" $tf_json_config_file)
break
fi
done
echo "vsphere_dns_servers = [ \"$mgmt_sl_private_ip\" ]" >> ${terraform_shared_tfvars}
#if dns server defined by "vsphere_dns_servers", will use the corresponding value of it
else
echo "vsphere_dns_servers = $vsphere_dns_servers" >> ${terraform_shared_tfvars}
dns_servers_num=`echo $vsphere_dns_servers | awk -F "," '{print NF}'`
for (( j=1; j<=$dns_servers_num; j++ ))
do
vsphere_dns_server=`echo $vsphere_dns_servers | awk -F "," "{print $"$j"}" | awk '{print $1}'`
sed -i "/^vsphere_dns_server/s/\b$vsphere_dns_server\b/\"$vsphere_dns_server\"/g" ${terraform_shared_tfvars}
done
vsphere_dns_servers_str=`grep -E "^vsphere_dns_servers" ${terraform_shared_tfvars} | awk -F "vsphere_dns_servers = " '{print $2}'`
vsphere_dns_servers_new_str="[ $vsphere_dns_servers_str ]"
sed -i "/^vsphere_dns_server/s/$vsphere_dns_servers_str/$vsphere_dns_servers_new_str/g" ${terraform_shared_tfvars}
fi
# generate virtual_machine_search_domain list in terraform.tfvars. such as: [ "wfss.ibm.com", "fss.ibm.com" ]
local virtual_machine_search_domain=$(jq -r ".virtual_machine_search_domain" $tf_json_config_file)
echo "virtual_machine_search_domain = $virtual_machine_search_domain" >> ${terraform_shared_tfvars}
search_domain_num=`echo $virtual_machine_search_domain | awk -F "," '{print NF}'`
for (( k=1; k<=$search_domain_num; k++ ))
do
search_domain=`echo $virtual_machine_search_domain | awk -F "," "{print $"$k"}" | awk '{print $1}'`
sed -i "/^virtual_machine_search_domain/s/\b$search_domain\b/\"$search_domain\"/g" ${terraform_shared_tfvars}
done
virtual_machine_search_domain_str=`grep -E "^virtual_machine_search_domain" ${terraform_shared_tfvars} | awk -F "virtual_machine_search_domain = " '{print $2}'`
virtual_machine_search_domain_new_str="[ $virtual_machine_search_domain_str ]"
sed -i "/^virtual_machine_search_domain/s/$virtual_machine_search_domain_str/$virtual_machine_search_domain_new_str/g" ${terraform_shared_tfvars}
local vsphere_dns_domain=$(jq -r ".vsphere_dns_domain" $tf_json_config_file)
echo "vsphere_dns_domain = \"$vsphere_dns_domain\"" >> ${terraform_shared_tfvars}
}
function get_subnet() {
local netmask="$1"
local gateway_ip="$2"
local network_ip=$(increment_ip "${gateway_ip}" "0")
echo "${network_ip}/${netmask}"
}
function generate_nfs_export_options() {
local vm_sl_private_netmask=$(jq -r ".vm_sl_private_netmask" $tf_json_config_file)
local vm_network_gateway=$(jq -r ".vm_network_gateway" $tf_json_config_file)
local first_subnet="$(get_subnet $vm_sl_private_netmask $vm_network_gateway)"
local nfs_exports_props="$(echo "${first_subnet}(rw,sync,no_root_squash)" )"
for vm_it in $(jq '.vms_meta | keys | .[]' $tf_json_config_file); do
local vm_it_private_netmask=$(jq -r ".vms_meta[$vm_it].vm_sl_private_netmask" $tf_json_config_file)
local vm_it_network_gateway=$(jq -r ".vms_meta[$vm_it].vm_network_gateway" $tf_json_config_file)
if [ ! -z "${vm_it_private_netmask// }" ] && [ ! -z "${vm_it_network_gateway// }" ] && [ "$vm_it_private_netmask" != "null" ] && [ "$vm_it_network_gateway" != "null" ]; then
local next_subnet="$(get_subnet $vm_it_private_netmask $vm_it_network_gateway)"
local next_nfs_exports_props="${next_subnet}(rw,sync,no_root_squash)"
local nfs_exports_props="$(echo "${nfs_exports_props} ${next_nfs_exports_props}" )"
fi
done
echo "${nfs_exports_props}"
}
full_nfs_export_options="$(generate_nfs_export_options)"
#function for generating vm customization script:
function generate_vm_customization_script() {
local vm_hostname="$1"
local vm_targetname="$2"
local terraform_customization_file_folder="$3"
local product_type=$(jq -r ".product_initials" $tf_json_config_file)
local vmuser=$(jq -r ".vmuser" $tf_json_config_file)
local nfs_export_options=$(echo $full_nfs_export_options | sed -e 's/[]\/$*.^[]/\\&/g')
#copy all the script templates file to the corresponding terraform folder, and rename them to the name without ".template" as the postfix
for file in `ls $terraform_files_dir/*.sh.template`
do
file_basename=$(basename -- $file)
new_file_basename=$(echo "$file_basename" | awk -F ".template" '{print $1}')
cp $file "${terraform_customization_file_folder}/${new_file_basename}"
grep -q "{vsphere_vm_hostname}" "${terraform_customization_file_folder}/${new_file_basename}"
if [ $? -eq 0 ]; then
sed -i "s/{vsphere_vm_hostname}/$vm_hostname/g" "${terraform_customization_file_folder}/${new_file_basename}"
fi
grep -q "{vsphere_target_vm_name}" "${terraform_customization_file_folder}/${new_file_basename}"
if [ $? -eq 0 ]; then
sed -i "s/{vsphere_target_vm_name}/$vm_targetname/g" "${terraform_customization_file_folder}/${new_file_basename}"
fi
grep -q "{product_type}" "${terraform_customization_file_folder}/${new_file_basename}"
if [ $? -eq 0 ]; then
sed -i "s/{product_type}/$product_type/g" "${terraform_customization_file_folder}/${new_file_basename}"
fi
grep -q "{full_nfs_export_options}" "${terraform_customization_file_folder}/${new_file_basename}"
if [ $? -eq 0 ]; then
sed -i "s/{full_nfs_export_options}/${nfs_export_options}/g" "${terraform_customization_file_folder}/${new_file_basename}"
fi
done
grep -q "{kwdd_vm_count}" "${terraform_customization_file_folder}/${terraform_customization_kube_platform_script_name}"
if [ $? -eq 0 ]; then
local kwdd_vm_count=3
local vm_it=0
grep -q "\"vm_type\": \"kwdd\"" $tf_json_config_file
if [ $? -eq 0 ]; then
for vm_it in $(jq '.vms_meta | keys | .[]' $tf_json_config_file); do
local vm_type=$(jq -r ".vms_meta[$vm_it].vm_type" $tf_json_config_file)
if [ "$vm_type" == "kwdd" ]; then
kwdd_vm_count=$(jq -r ".vms_meta[$vm_it].vsphere_vm_count" $tf_json_config_file)
fi
done
else
kwdd_vm_count=0
fi
sed -i "s/{kwdd_vm_count}/$kwdd_vm_count/g" "${terraform_customization_file_folder}/${terraform_customization_kube_platform_script_name}"
fi
grep -q "{kw_vm_count}" "${terraform_customization_file_folder}/${terraform_customization_kube_platform_script_name}"
if [ $? -eq 0 ]; then
local kw_vm_count=7
local vm_it=0
grep -q "\"vm_type\": \"kw\"" $tf_json_config_file
if [ $? -eq 0 ]; then
for vm_it in $(jq '.vms_meta | keys | .[]' $tf_json_config_file); do
local vm_type=$(jq -r ".vms_meta[$vm_it].vm_type" $tf_json_config_file)
if [ "$vm_type" == "kw" ]; then
kw_vm_count=$(jq -r ".vms_meta[$vm_it].vsphere_vm_count" $tf_json_config_file)
fi
done
else
kw_vm_count=0
fi
sed -i "s/{kw_vm_count}/$kw_vm_count/g" "${terraform_customization_file_folder}/${terraform_customization_kube_platform_script_name}"
fi
#update web session timeout value "JWT_KEY_EXPIRY"
grep -Eq "^JWT_KEY_EXPIRY=" $custom_properties_file
if [ $? -ne 0 ]; then
JWT_KEY_EXPIRY_VALUE="30m"
else
JWT_KEY_EXPIRY_VALUE=$(cat ${custom_properties_file} | grep ^JWT_KEY_EXPIRY | awk -F "=" '{print $2}' | awk '{print $1}')
fi
grep -q "{jwt_new_expiry}" "${terraform_customization_file_folder}/${terraform_customization_kube_platform_script_name}"
if [ $? -eq 0 ]; then
sed -i "s/{jwt_new_expiry}/$JWT_KEY_EXPIRY_VALUE/g" "${terraform_customization_file_folder}/${terraform_customization_kube_platform_script_name}"
fi
# update for hadoop vms customization script
grep -q "{vm_user}" "${terraform_customization_file_folder}/${terraform_hdp_customization_script_name}"
if [ $? -eq 0 ]; then
sed -i "s/{vm_user}/$vmuser/g" "${terraform_customization_file_folder}/${terraform_hdp_customization_script_name}"
fi
for vm_it in $(jq '.vms_meta | keys | .[]' $tf_json_config_file); do
local vm_type=$(jq -r ".vms_meta[$vm_it].vm_type" $tf_json_config_file)
local vmpassword=$(jq -r ".vms_meta[$vm_it].vmpassword" $tf_json_config_file)
local vm_sl_private_ip=$(jq -r ".vms_meta[$vm_it].vm_sl_private_ip" $tf_json_config_file)
if [ "$vm_type" == "km" ]; then
#copy the secrets to km terraform files folder
cp -r $terraform_files_secrets_dir ${terraform_customization_file_folder}
#update {km_vm_password} and {km_vm_sl_private_ip}
grep -q "{km_vm_password}" "${terraform_customization_file_folder}/${terraform_hdp_customization_script_name}"
if [ $? -eq 0 ]; then
sed -i "s/{km_vm_password}/$vmpassword/g" "${terraform_customization_file_folder}/${terraform_hdp_customization_script_name}"
fi
grep -q "{km_vm_sl_private_ip}" "${terraform_customization_file_folder}/${terraform_hdp_customization_script_name}"
if [ $? -eq 0 ]; then
sed -i "s/{km_vm_sl_private_ip}/$vm_sl_private_ip/g" "${terraform_customization_file_folder}/${terraform_hdp_customization_script_name}"
fi
fi
# update kube customization script with with hdpa password and ip
if [ "$vm_type" == "hdpa" ]; then
#copy the secrets to km terraform files folder
cp -r $terraform_files_secrets_dir ${terraform_customization_file_folder}
#update {hdpa_vm_password} and {hdpa_vm_sl_private_ip}
grep -q "{hdpa_vm_password}" "${terraform_customization_file_folder}/${terraform_customization_kube_platform_script_name}"
if [ $? -eq 0 ]; then
sed -i "s/{hdpa_vm_password}/$vmpassword/g" "${terraform_customization_file_folder}/${terraform_customization_kube_platform_script_name}"
fi
grep -q "{hdpa_vm_sl_private_ip}" "${terraform_customization_file_folder}/${terraform_customization_kube_platform_script_name}"
if [ $? -eq 0 ]; then
sed -i "s/{hdpa_vm_sl_private_ip}/$vm_sl_private_ip/g" "${terraform_customization_file_folder}/${terraform_customization_kube_platform_script_name}"
fi
fi
if [ "$vm_type" == "hdpm" ]; then
grep -q "{hdpm_vm_password}" "${terraform_customization_file_folder}/${terraform_customization_kube_platform_script_name}"
if [ $? -eq 0 ]; then
sed -i "s/{hdpm_vm_password}/$vmpassword/g" "${terraform_customization_file_folder}/${terraform_customization_kube_platform_script_name}"
fi
grep -q "{hdpm_vm_sl_private_ip}" "${terraform_customization_file_folder}/${terraform_customization_kube_platform_script_name}"
if [ $? -eq 0 ]; then
sed -i "s/{hdpm_vm_sl_private_ip}/$vm_sl_private_ip/g" "${terraform_customization_file_folder}/${terraform_customization_kube_platform_script_name}"
fi
fi
done
#update data source config
grep -Eq "^DNB_USERNAME=" $custom_properties_file
if [ $? -eq 0 ]; then
DNB_USERNAME=$(cat ${custom_properties_file} | grep ^DNB_USERNAME | awk -F "DNB_USERNAME=" '{print $2}' | awk '{print $1}')
sed -i "s/{new_dnb_user}/$DNB_USERNAME/g" "${terraform_customization_file_folder}/${terraform_customization_dd_script_name}"
fi
grep -Eq "^DNB_PASSWORD=" $custom_properties_file
if [ $? -eq 0 ]; then
DNB_PASSWORD=$(cat ${custom_properties_file} | grep ^DNB_PASSWORD | awk -F "DNB_PASSWORD=" '{print $2}' | awk '{print $1}')
sed -i "s/{new_dnb_password}/$DNB_PASSWORD/g" "${terraform_customization_file_folder}/${terraform_customization_dd_script_name}"
fi
grep -Eq "^DOWJONES_USERNAME=" $custom_properties_file
if [ $? -eq 0 ]; then
DOWJONES_USERNAME=$(cat ${custom_properties_file} | grep ^DOWJONES_USERNAME | awk -F "DOWJONES_USERNAME=" '{print $2}' | awk '{print $1}')
sed -i "s/{new_dj_user}/$DOWJONES_USERNAME/g" "${terraform_customization_file_folder}/${terraform_customization_dd_script_name}"
fi
grep -Eq "^DOWJONES_PASSWORD=" $custom_properties_file
if [ $? -eq 0 ]; then
DOWJONES_PASSWORD=$(cat ${custom_properties_file} | grep ^DOWJONES_PASSWORD | awk -F "DOWJONES_PASSWORD=" '{print $2}' | awk '{print $1}')
sed -i "s/{new_dj_password}/$DOWJONES_PASSWORD/g" "${terraform_customization_file_folder}/${terraform_customization_dd_script_name}"
fi
grep -Eq "^KYCKR_USERNAME=" $custom_properties_file
if [ $? -eq 0 ]; then
KYCKR_USERNAME=$(cat ${custom_properties_file} | grep ^KYCKR_USERNAME | awk -F "KYCKR_USERNAME=" '{print $2}' | awk '{print $1}')
sed -i "s/{new_kyckr_user}/$KYCKR_USERNAME/g" "${terraform_customization_file_folder}/${terraform_customization_dd_script_name}"
fi
grep -Eq "^KYCKR_PASSWORD=" $custom_properties_file
if [ $? -eq 0 ]; then
KYCKR_PASSWORD=$(cat ${custom_properties_file} | grep ^KYCKR_PASSWORD | awk -F "KYCKR_PASSWORD=" '{print $2}' | awk '{print $1}')
sed -i "s/{new_kyckr_password}/$KYCKR_PASSWORD/g" "${terraform_customization_file_folder}/${terraform_customization_dd_script_name}"
fi
grep -Eq "^FACTIVA_EID=" $custom_properties_file
if [ $? -eq 0 ]; then
FACTIVA_EID=$(cat ${custom_properties_file} | grep ^FACTIVA_EID | awk -F "FACTIVA_EID=" '{print $2}' | awk '{print $1}')
sed -i "s/{new_factiva_eid}/$FACTIVA_EID/g" "${terraform_customization_file_folder}/${terraform_customization_dd_script_name}"
fi
grep -Eq "^FACTIVA_NEWS_ENCRYTED_TOKEN_VALUE=" $custom_properties_file
if [ $? -eq 0 ]; then
FACTIVA_NEWS_ENCRYTED_TOKEN_VALUE=$(cat ${custom_properties_file} | grep ^FACTIVA_NEWS_ENCRYTED_TOKEN_VALUE | awk -F "FACTIVA_NEWS_ENCRYTED_TOKEN_VALUE=" '{print $2}' | awk '{print $1}')
sed -i "s/{new_factiva_token}/$FACTIVA_NEWS_ENCRYTED_TOKEN_VALUE/g" "${terraform_customization_file_folder}/${terraform_customization_dd_script_name}"
fi
grep -Eq "^BING_NEWS_SUBSCRIPTION_KEY_V7=" $custom_properties_file
if [ $? -eq 0 ]; then
BING_NEWS_SUBSCRIPTION_KEY_V7=$(cat ${custom_properties_file} | grep ^BING_NEWS_SUBSCRIPTION_KEY_V7 | awk -F "BING_NEWS_SUBSCRIPTION_KEY_V7=" '{print $2}' | awk '{print $1}')
sed -i "s/{new_bing_news_key}/$BING_NEWS_SUBSCRIPTION_KEY_V7/g" "${terraform_customization_file_folder}/${terraform_customization_dd_script_name}"
fi
grep -Eq "^BING_WEB_SUBSCRIPTION_KEY_V7=" $custom_properties_file
if [ $? -eq 0 ]; then
BING_WEB_SUBSCRIPTION_KEY_V7=$(cat ${custom_properties_file} | grep ^BING_WEB_SUBSCRIPTION_KEY_V7 | awk -F "BING_WEB_SUBSCRIPTION_KEY_V7=" '{print $2}' | awk '{print $1}')
sed -i "s/{new_bing_web_key}/$BING_WEB_SUBSCRIPTION_KEY_V7/g" "${terraform_customization_file_folder}/${terraform_customization_dd_script_name}"
fi
}
#function for generating install.hosts.properties under /root/fci-install-kit/helm on km vm
function generate_install_hosts_properties() {
local tfvars_file="$1"
local vm_sl_private_ip=`grep -E "^vm_sl_private_ip" $tfvars_file | awk -F "=" '{print $2}' | awk -F '"' '{print $2}'`
local vsphere_vm_hostname=`grep -E "^vsphere_vm_hostname" $tfvars_file | awk -F "=" '{print $2}' | awk -F '"' '{print $2}'`
local vsphere_dns_domain=`grep -E "^vsphere_dns_domain" $tfvars_file | awk -F "=" '{print $2}' | awk -F '"' '{print $2}'`
local vmpassword=`grep -E "^vmpassword" $tfvars_file | awk -F "=" '{print $2}' | awk -F '"' '{print $2}'`
#for kube master
echo "$vsphere_vm_hostname" | grep -q "\-km"
if [ $? -eq 0 ]; then
echo "master.ip=$vm_sl_private_ip" >> $terraform_km_hosts_file
echo "master.fqdn=${vsphere_vm_hostname}.${vsphere_dns_domain}" >> $terraform_km_hosts_file
echo "master.root_password=$vmpassword" >> $terraform_km_hosts_file
fi
# for kube worker
echo "$vsphere_vm_hostname" | grep -Eq "\-kw[0-9]+$"
if [ $? -eq 0 ]; then
local worker_index=$(echo "$vsphere_vm_hostname" | grep -E "\-kw[0-9]+$" | awk -F "-kw" '{print $2}' | awk '{print $1}')
echo "worker.$worker_index.ip=$vm_sl_private_ip" >> $terraform_km_hosts_file
echo "worker.$worker_index.fqdn=${vsphere_vm_hostname}.${vsphere_dns_domain}" >> $terraform_km_hosts_file
echo "worker.$worker_index.root_password=$vmpassword" >> $terraform_km_hosts_file
fi
# for kube worker dd
echo "$vsphere_vm_hostname" | grep -Eq "\-kwdd[0-9]+$"
if [ $? -eq 0 ]; then
local worker_dd_index=$(echo "$vsphere_vm_hostname" | grep -E "\-kwdd[0-9]+$" | awk -F "-kwdd" '{print $2}' | awk '{print $1}')
local kw_vm_count=0
local vm_it=0
for vm_it in $(jq '.vms_meta | keys | .[]' $tf_json_config_file); do
local vm_type=$(jq -r ".vms_meta[$vm_it].vm_type" $tf_json_config_file)
if [ "$vm_type" == "kw" ]; then
kw_vm_count=$(jq -r ".vms_meta[$vm_it].vsphere_vm_count" $tf_json_config_file)
fi
done
local worker_index=0
((worker_index=worker_dd_index+kw_vm_count))
echo "worker.$worker_index.ip=$vm_sl_private_ip" >> $terraform_km_hosts_file
echo "worker.$worker_index.fqdn=${vsphere_vm_hostname}.${vsphere_dns_domain}" >> $terraform_km_hosts_file
echo "worker.$worker_index.root_password=$vmpassword" >> $terraform_km_hosts_file
fi
# for nfs2 server
echo "$vsphere_vm_hostname" | grep -Eq "\-nfs2"
if [ $? -eq 0 ]; then
echo "nfs.ip=$vm_sl_private_ip" >> $terraform_km_hosts_file
echo "nfs.fqdn=${vsphere_vm_hostname}.${vsphere_dns_domain}" >> $terraform_km_hosts_file
echo "nfs.root_password=$vmpassword" >> $terraform_km_hosts_file
fi
}
function generate_fci_hadoop_hosts_properties() {
local product_initials=$(jq -r ".product_initials" $tf_json_config_file)
local product_version=$(jq -r ".product_version" $tf_json_config_file)
local customer_env_name=$(jq -r ".customer_env_name" $tf_json_config_file)
local env_type=$(jq -r ".env_type" $tf_json_config_file)
local vsphere_dns_domain=$(jq -r ".vsphere_dns_domain" $tf_json_config_file)
local env_name="${product_initials}-${product_version}-${customer_env_name}-${env_type}"
echo "" > $terraform_hdpa_hosts_properties_file
for vm_it in $(jq '.vms_meta | keys | .[]' $tf_json_config_file); do
local vm_type=$(jq -r ".vms_meta[$vm_it].vm_type" $tf_json_config_file)
local vm_sl_private_ip=$(jq -r ".vms_meta[$vm_it].vm_sl_private_ip" $tf_json_config_file)
local vmpassword=$(jq -r ".vms_meta[$vm_it].vmpassword" $tf_json_config_file)
local hostname="${env_name}-${vm_type}.${vsphere_dns_domain}"
if [ "$vm_type" == "hdpa" ]; then
echo "ambari ${vm_sl_private_ip} ${hostname} ${vmpassword}" >> $terraform_hdpa_hosts_properties_file
fi
if [ "$vm_type" == "hdpg" ]; then
echo "hadoop.gateway ${vm_sl_private_ip} ${hostname} ${vmpassword}" >> $terraform_hdpa_hosts_properties_file
fi
if [ "$vm_type" == "hdpm" ]; then
echo "hadoop.master ${vm_sl_private_ip} ${hostname} ${vmpassword}" >> $terraform_hdpa_hosts_properties_file
fi
if [ "$vm_type" == "hdpsec" ]; then
echo "hadoop.secondary ${vm_sl_private_ip} ${hostname} ${vmpassword}" >> $terraform_hdpa_hosts_properties_file
fi
if [ "$vm_type" == "hdps1" ]; then
echo "hadoop.slave ${vm_sl_private_ip} ${hostname} ${vmpassword}" >> $terraform_hdpa_hosts_properties_file
fi
if [ "$vm_type" == "hdps2" ]; then
echo "hadoop.slave ${vm_sl_private_ip} ${hostname} ${vmpassword}" >> $terraform_hdpa_hosts_properties_file
fi
done
}
#function for generating mgmt host file based on terraform.tfvars file for each role
function generate_mgmt_host_file() {
local tfvars_file="$1"
local vm_type="$2"
local vm_sl_private_ip=`grep -E "^vm_sl_private_ip" $tfvars_file | awk -F "=" '{print $2}' | awk -F '"' '{print $2}'`
local vsphere_vm_hostname=`grep -E "^vsphere_vm_hostname" $tfvars_file | awk -F "=" '{print $2}' | awk -F '"' '{print $2}'`
local vsphere_dns_domain=`grep -E "^vsphere_dns_domain" $tfvars_file | awk -F "=" '{print $2}' | awk -F '"' '{print $2}'`
if [ "$vm_type" == "kw" ] || [ "$vm_type" == "kwdd" ]; then
grep -Eq "^vm_kube_internal_ip" $tfvars_file
if [ $? -eq 0 ]; then
local vm_kube_internal_ip=`grep -E "^vm_kube_internal_ip" $tfvars_file | awk -F "=" '{print $2}' | awk -F '"' '{print $2}'`
echo "${vsphere_vm_hostname}.${vsphere_dns_domain}. IN A ${vm_kube_internal_ip}" >> ${terraform_mgmt_host_file}
local vm_sl_hostname=`echo ${vsphere_vm_hostname}.${vsphere_dns_domain} | sed -e 's/ikw/kw/g'`
echo "${vm_sl_hostname}. IN A ${vm_sl_private_ip}" >> ${terraform_mgmt_host_file}
else
echo "${vsphere_vm_hostname}.${vsphere_dns_domain}. IN A ${vm_sl_private_ip}" >> ${terraform_mgmt_host_file}
fi
elif [ "$vm_type" == "km" ]; then
grep -Eq "^vm_kube_internal_ip" $tfvars_file
if [ $? -eq 0 ]; then
local vm_kube_internal_ip=`grep -E "^vm_kube_internal_ip" $tfvars_file | awk -F "=" '{print $2}' | awk -F '"' '{print $2}'`
echo "${vsphere_vm_hostname}.${vsphere_dns_domain}. IN A ${vm_kube_internal_ip}" >> ${terraform_mgmt_host_file}
local vm_sl_hostname=`echo ${vsphere_vm_hostname}.${vsphere_dns_domain} | sed -e 's/ikm/km/g'`
echo "${vm_sl_hostname}. IN A ${vm_sl_private_ip}" >> ${terraform_mgmt_host_file}
else
echo "${vsphere_vm_hostname}.${vsphere_dns_domain}. IN A ${vm_sl_private_ip}" >> ${terraform_mgmt_host_file}
fi
else
echo "${vsphere_vm_hostname}.${vsphere_dns_domain}. IN A ${vm_sl_private_ip}" >> ${terraform_mgmt_host_file}
if [ "$vm_type" == "mgmt" ]; then
sed -i "s/{mgmt_hostname}/${vsphere_vm_hostname}.${vsphere_dns_domain}/g" ${terraform_mgmt_host_file}
fi
#workaround to point those hardcoded tb2 hostname for wca and db vms to their new hostnames
if [ "$vm_type" == "nfs2" ]; then
echo "fci-nfs2-tb2.wfss.ibm.com. IN CNAME ${vsphere_vm_hostname}.${vsphere_dns_domain}." >> ${terraform_mgmt_host_file}
fi
if [ "$vm_type" == "db" ]; then
echo "fci-db-tb2.wfss.ibm.com. IN CNAME ${vsphere_vm_hostname}.${vsphere_dns_domain}." >> ${terraform_mgmt_host_file}
fi
if [ "$vm_type" == "wexc" ]; then
echo "fci-wexc1-tb2.wfss.ibm.com. IN CNAME ${vsphere_vm_hostname}.${vsphere_dns_domain}." >> ${terraform_mgmt_host_file}
fi
if [ "$vm_type" == "wexfp" ]; then
echo "fci-wexfp1-tb2.wfss.ibm.com. IN CNAME ${vsphere_vm_hostname}.${vsphere_dns_domain}." >> ${terraform_mgmt_host_file}
fi
if [ "$vm_type" == "wexm" ]; then
echo "fci-wexm-tb2.wfss.ibm.com. IN CNAME ${vsphere_vm_hostname}.${vsphere_dns_domain}." >> ${terraform_mgmt_host_file}
fi
fi
}
#function for generating mgmt db reverse lookup host file based on terraform.tfvars file for each role
function generate_mgmt_reverse_host_file() {
if [[ ! -d "${terraform_mgmt_files_dir}/db" ]]; then
mkdir "${terraform_mgmt_files_dir}/db"
fi
# remove any previously generate db reverse lookup file
local db_file_cnt=$(ls $terraform_mgmt_files_dir/db/db.* 2> /dev/null | wc -l)
if [ $db_file_cnt -gt 0 ]; then
for generated_db_file in `ls $terraform_mgmt_files_dir/db/db.*`; do
rm ${generated_db_file}
done
fi
db_template_file="${mgmt_reverse_hosts_file_template}"
local product_initials=$(jq -r ".product_initials" $tf_json_config_file)
local product_version=$(jq -r ".product_version" $tf_json_config_file)
local customer_env_name=$(jq -r ".customer_env_name" $tf_json_config_file)
local env_type=$(jq -r ".env_type" $tf_json_config_file)
local env_name="${product_initials}-${product_version}-${customer_env_name}-${env_type}"
for vm_it in $(jq '.vms_meta | keys | .[]' $tf_json_config_file); do
local vm_type=$(jq -r ".vms_meta[$vm_it].vm_type" $tf_json_config_file)
local vm_sl_private_ip=$(jq -r ".vms_meta[$vm_it].vm_sl_private_ip" $tf_json_config_file)
local vsphere_vm_count=$(jq -r ".vms_meta[$vm_it].vsphere_vm_count" $tf_json_config_file)
local baseaddr="$(echo $vm_sl_private_ip | cut -d. -f1-3)"
local reverse_baseaddr=`echo ${baseaddr} | awk -F. '{print $3"." $2"."$1}'`
local db_file="${terraform_mgmt_files_dir}/db/db.${baseaddr}"
if [[ ! -f ${db_file} ]]; then
cat "${db_template_file}" > ${db_file}
sed -i "s/{mgmt_hostname}/${env_name}-mgmt.wfss.ibm.com/g" ${db_file}
fi
if [[ $vsphere_vm_count != null && $vsphere_vm_count -gt 1 ]]; then
for ((i=1;i<=$vsphere_vm_count;i++)); do
local vm_function_name=$vm_type$i
local private_ip=$( increment_ip "${vm_sl_private_ip}" "${i}" )
local reverse_ip=`echo ${private_ip} | awk -F. '{print $4"."$3"." $2"."$1}'`
local entry="${reverse_ip}.in-addr.arpa. IN PTR ${env_name}-${vm_type}${i}.wfss.ibm.com."
echo "${entry}" >> ${db_file}
done
else
local reverse_ip=`echo ${vm_sl_private_ip} | awk -F. '{print $4"."$3"." $2"."$1}'`
local entry="${reverse_ip}.in-addr.arpa. IN PTR ${env_name}-${vm_type}.wfss.ibm.com."
echo "${entry}" >> ${db_file}
fi
done
}
# generate haproxy config file
function generate_haproxy_cfg_file() {
if [[ ! -f ${terraform_haproxy_cfg_file} ]]; then
touch ${terraform_haproxy_cfg_file}
fi
cat "${haproxy_cfg_file_template}" > ${terraform_haproxy_cfg_file}
for vm_it in $(jq '.vms_meta | keys | .[]' $tf_json_config_file); do
local vm_type=$(jq -r ".vms_meta[$vm_it].vm_type" $tf_json_config_file)
local vm_sl_private_ip=$(jq -r ".vms_meta[$vm_it].vm_sl_private_ip" $tf_json_config_file)
local vsphere_vm_count=$(jq -r ".vms_meta[$vm_it].vsphere_vm_count" $tf_json_config_file)
if [ "$vm_type" == "ms" ]; then
for ((i=1;i<=$vsphere_vm_count;i++)); do
local private_ip=$( increment_ip "${vm_sl_private_ip}" "${i}" )
local to_be_replaced="{master-${i}-private-ip}"
grep -q "${to_be_replaced}" "${terraform_haproxy_cfg_file}"
if [ $? -eq 0 ]; then
sed -i "s/${to_be_replaced}/${private_ip}/g" "${terraform_haproxy_cfg_file}"
fi
done
fi
done
}
# generate wdp.conf file used in WSL install
function generate_wdp_conf_file() {
local wdp_conf_file="${terraform_ms1_files_dir}/wdp.conf"
if [[ ! -f ${wdp_conf_file} ]]; then
touch ${wdp_conf_file}
fi
echo "user=root" > ${wdp_conf_file}
echo "ssh_port=22" >> ${wdp_conf_file}
echo "suppress_warning=true" >> ${wdp_conf_file}
for vm_it in $(jq '.vms_meta | keys | .[]' $tf_json_config_file); do
local vm_type=$(jq -r ".vms_meta[$vm_it].vm_type" $tf_json_config_file)
local vm_sl_private_ip=$(jq -r ".vms_meta[$vm_it].vm_sl_private_ip" $tf_json_config_file)
local vsphere_vm_count=$(jq -r ".vms_meta[$vm_it].vsphere_vm_count" $tf_json_config_file)
if [ "$vm_type" == "ms" ]; then
for ((i=1;i<=$vsphere_vm_count;i++)); do
local private_ip=$( increment_ip "${vm_sl_private_ip}" "${i}" )
echo "master_node_${i}=${private_ip}" >> ${wdp_conf_file}
echo "master_node_path_${i}=/ibm" >> ${wdp_conf_file}
done
fi
if [ "$vm_type" == "worker" ]; then
for ((i=1;i<=$vsphere_vm_count;i++)); do
local private_ip=$( increment_ip "${vm_sl_private_ip}" "${i}" )
echo "worker_node_${i}=${private_ip}" >> ${wdp_conf_file}
echo "worker_node_path_${i}=/ibm" >> ${wdp_conf_file}
done
fi
if [ "$vm_type" == "haproxy" ]; then
echo "virtual_ip_address_1=${vm_sl_private_ip}" >> ${wdp_conf_file}
echo "virtual_ip_address_2=${vm_sl_private_ip}" >> ${wdp_conf_file}
fi
if [ "$vm_type" == "nfs3" ]; then