-
Notifications
You must be signed in to change notification settings - Fork 130
/
installer.sh
executable file
·832 lines (716 loc) · 38.8 KB
/
installer.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
#!/bin/bash
#colors for terminal
boldreduscore="\e[1;4;31m"
boldred="\e[1;31m"
cyan="\e[1;36m"
resetformatting="\e[0m"
#External helper functions
#. "$(dirname "${BASH_SOURCE[0]}")/deploy_utils.sh"
full_script_path="$(realpath "${BASH_SOURCE[0]}")"
script_directory="$(dirname "${full_script_path}")"
#call stack has full scriptname when using source
source "${script_directory}/deploy_utils.sh"
#helper files
source "${script_directory}/helpers/script_helpers.sh"
force=0
INPUT_ARGUMENTS=$(getopt -n installer -o p:t:o:d:l:s:ahif --longoptions type:,parameterfile:,storageaccountname:,deployer_tfstate_key:,landscape_tfstate_key:,state_subscription:,ado,auto-approve,force,help -- "$@")
VALID_ARGUMENTS=$?
if [ "$VALID_ARGUMENTS" != "0" ]; then
showhelp
fi
called_from_ado=0
eval set -- "$INPUT_ARGUMENTS"
while :
do
case "$1" in
-t | --type) deployment_system="$2" ; shift 2 ;;
-p | --parameterfile) parameterfile="$2" ; shift 2 ;;
-o | --storageaccountname) REMOTE_STATE_SA="$2" ; shift 2 ;;
-s | --state_subscription) STATE_SUBSCRIPTION="$2" ; shift 2 ;;
-d | --deployer_tfstate_key) deployer_tfstate_key="$2" ; shift 2 ;;
-l | --landscape_tfstate_key) landscape_tfstate_key="$2" ; shift 2 ;;
-a | --ado) called_from_ado=1 ; shift ;;
-f | --force) force=1 ; shift ;;
-i | --auto-approve) approve="--auto-approve" ; shift ;;
-h | --help) showhelp
exit 3 ; shift ;;
--) shift; break ;;
esac
done
tfstate_resource_id=""
tfstate_parameter=""
deployer_tfstate_key_parameter=""
landscape_tfstate_key_parameter=""
landscape_tfstate_key_exists=false
parameterfile_name=$(basename "${parameterfile}")
param_dirname=$(dirname "${parameterfile}")
if [ "${param_dirname}" != '.' ]; then
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $boldred Please run this command from the folder containing the parameter file $resetformatting #"
echo "# #"
echo "#########################################################################################"
exit 3
fi
if [ ! -f "${parameterfile}" ]
then
printf -v val %-35.35s "$parameterfile"
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $boldred Parameter file does not exist: ${val} $resetformatting #"
echo "# #"
echo "#########################################################################################"
exit 2 #No such file or directory
fi
if [ -z "${deployment_system}" ]
then
printf -v val %-40.40s "$deployment_system"
echo "#########################################################################################"
echo "# #"
echo -e "# $boldred Incorrect system deployment type specified: ${val}$resetformatting#"
echo "# #"
echo "# Valid options are: #"
echo "# sap_deployer #"
echo "# sap_library #"
echo "# sap_landscape #"
echo "# sap_system #"
echo "# #"
echo "#########################################################################################"
echo ""
exit 64 #script usage wrong
fi
# Check that the exports ARM_SUBSCRIPTION_ID and DEPLOYMENT_REPO_PATH are defined
validate_exports
return_code=$?
if [ 0 != $return_code ]; then
exit $return_code
fi
# Check that Terraform and Azure CLI is installed
validate_dependencies
return_code=$?
if [ 0 != $return_code ]; then
exit $return_code
fi
# Check that parameter files have environment and location defined
validate_key_parameters "$parameterfile_name"
return_code=$?
if [ 0 != $return_code ]; then
exit $return_code
fi
if valid_region_name "${region}" ; then
# Convert the region to the correct code
get_region_code ${region}
else
echo "Invalid region: $region"
exit 2
fi
key=$(echo "${parameterfile_name}" | cut -d. -f1)
#Persisting the parameters across executions
automation_config_directory=~/.sap_deployment_automation/
generic_config_information="${automation_config_directory}"config
system_config_information="${automation_config_directory}""${environment}""${region_code}"
echo "Configuration file: $system_config_information"
echo "Deployment region: $region"
echo "Deployment region code: $region_code"
if [ 1 == $called_from_ado ] ; then
this_ip=$(curl -s ipinfo.io/ip) >/dev/null 2>&1
export TF_VAR_Agent_IP=$this_ip
echo "Agent IP: $this_ip"
fi
deployer_tfstate_key_parameter=''
landscape_tfstate_key_parameter=''
parallelism=10
#Provide a way to limit the number of parallell tasks for Terraform
if [[ -n "${TF_PARALLELLISM}" ]]; then
parallelism=$TF_PARALLELLISM
fi
echo "Parallelism count $parallelism"
param_dirname=$(pwd)
init "${automation_config_directory}" "${generic_config_information}" "${system_config_information}"
var_file="${param_dirname}"/"${parameterfile}"
extra_vars=""
if [ -f terraform.tfvars ]; then
extra_vars=" -var-file=${param_dirname}/terraform.tfvars "
fi
if [ "${deployment_system}" == sap_deployer ]
then
deployer_tfstate_key=${key}.terraform.tfstate
fi
if [[ -z ${REMOTE_STATE_SA} ]];
then
echo "Loading the State file information"
load_config_vars "${system_config_information}" "REMOTE_STATE_SA"
load_config_vars "${system_config_information}" "REMOTE_STATE_RG"
load_config_vars "${system_config_information}" "tfstate_resource_id"
load_config_vars "${system_config_information}" "STATE_SUBSCRIPTION"
else
save_config_vars "${system_config_information}" REMOTE_STATE_SA
fi
echo "Terraform state file storage:" "${REMOTE_STATE_SA}"
echo "Terraform state subscription:" "${STATE_SUBSCRIPTION}"
if [[ -z $deployer_tfstate_key ]];
then
load_config_vars "${system_config_information}" "deployer_tfstate_key"
else
save_config_vars "${system_config_information}" deployer_tfstate_key
fi
if [[ -z $landscape_tfstate_key ]];
then
load_config_vars "${system_config_information}" "landscape_tfstate_key"
else
save_config_vars "${system_config_information}" landscape_tfstate_key
fi
if [[ -z $STATE_SUBSCRIPTION ]];
then
load_config_vars "${system_config_information}" "STATE_SUBSCRIPTION"
else
echo "Saving the state subscription"
if is_valid_guid "$STATE_SUBSCRIPTION" ; then
save_config_var "STATE_SUBSCRIPTION" "${system_config_information}"
else
printf -v val %-40.40s "$STATE_SUBSCRIPTION"
echo "#########################################################################################"
echo "# #"
echo -e "# The provided state_subscription is not valid:$boldred ${val}$resetformatting#"
echo "# #"
echo "#########################################################################################"
exit 65
fi
fi
account_set=0
#setting the user environment variables
set_executing_user_environment_variables "none"
if [[ -n ${subscription} ]]; then
if is_valid_guid "${subscription}" ; then
echo "Valid subscription format"
else
printf -v val %-40.40s "$subscription"
echo "#########################################################################################"
echo "# #"
echo -e "# The provided subscription is not valid:$boldred ${val} $resetformatting# "
echo "# #"
echo "#########################################################################################"
exit 65
fi
export ARM_SUBSCRIPTION_ID="${subscription}"
fi
if [[ -n $STATE_SUBSCRIPTION ]];
then
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $cyan Changing the subscription to: $STATE_SUBSCRIPTION $resetformatting #"
echo "# #"
echo "#########################################################################################"
echo ""
az account set --sub "${STATE_SUBSCRIPTION}"
fi
load_config_vars "${system_config_information}" "STATE_SUBSCRIPTION"
load_config_vars "${system_config_information}" "REMOTE_STATE_RG"
load_config_vars "${system_config_information}" "tfstate_resource_id"
if [[ -z ${REMOTE_STATE_SA} ]]; then
if [ 1 != $called_from_ado ]; then
read -p "Terraform state storage account name:" REMOTE_STATE_SA
get_and_store_sa_details "${REMOTE_STATE_SA}" "${system_config_information}"
load_config_vars "${system_config_information}" "STATE_SUBSCRIPTION"
load_config_vars "${system_config_information}" "REMOTE_STATE_RG"
load_config_vars "${system_config_information}" "tfstate_resource_id"
fi
fi
echo "Terraform state storage " "${REMOTE_STATE_SA}"
if [ -z ${REMOTE_STATE_SA} ]; then
option="REMOTE_STATE_SA"
missing
exit 1
fi
if [[ -z ${REMOTE_STATE_RG} ]]; then
get_and_store_sa_details "${REMOTE_STATE_SA}" "${system_config_information}"
load_config_vars "${system_config_information}" "STATE_SUBSCRIPTION"
load_config_vars "${system_config_information}" "REMOTE_STATE_RG"
load_config_vars "${system_config_information}" "tfstate_resource_id"
fi
if [[ -z ${tfstate_resource_id} ]]; then
get_and_store_sa_details "${REMOTE_STATE_SA}" "${system_config_information}"
load_config_vars "${system_config_information}" "STATE_SUBSCRIPTION"
load_config_vars "${system_config_information}" "REMOTE_STATE_RG"
load_config_vars "${system_config_information}" "tfstate_resource_id"
fi
tfstate_parameter=" -var tfstate_resource_id=${tfstate_resource_id}"
if [ "${deployment_system}" != sap_deployer ]
then
if [ -z ${deployer_tfstate_key} ]; then
deployer_tfstate_key_parameter=" "
else
if [ "${deployment_system}" != sap_system ] ; then
deployer_tfstate_key_parameter=" -var deployer_tfstate_key=${deployer_tfstate_key}"
else
deployer_tfstate_key_parameter=" "
fi
fi
else
tfstate_parameter=" "
save_config_vars "${system_config_information}" deployer_tfstate_key
fi
if [ "${deployment_system}" == sap_system ]
then
if [[ -n ${landscape_tfstate_key} ]]; then
landscape_tfstate_key_parameter=" -var landscape_tfstate_key=${landscape_tfstate_key}"
landscape_tfstate_key_exists=true
else
if [ 1 != $called_from_ado ]; then
read -p "Workload terraform statefile name :" landscape_tfstate_key
landscape_tfstate_key_parameter=" -var landscape_tfstate_key=${landscape_tfstate_key}"
save_config_var "landscape_tfstate_key" "${system_config_information}"
landscape_tfstate_key_exists=true
else
export last_error="Workload terraform statefile name is missing"
echo "#########################################################################################"
echo "# #"
echo -e "# $boldred Workload terraform statefile name is missing $resetformatting #"
echo "# #"
echo "#########################################################################################"
echo ""
unset TF_DATA_DIR
exit 1
fi
fi
else
landscape_tfstate_key_parameter=""
fi
terraform_module_directory="${DEPLOYMENT_REPO_PATH}"/deploy/terraform/run/"${deployment_system}"/
export TF_DATA_DIR="${param_dirname}/.terraform"
if [ ! -d "${terraform_module_directory}" ]
then
printf -v val %-40.40s "$deployment_system"
echo "#########################################################################################"
echo "# #"
echo -e "# $boldred Incorrect system deployment type specified: ${val}$resetformatting#"
echo "# #"
echo "# Valid options are: #"
echo "# sap_deployer #"
echo "# sap_library #"
echo "# sap_landscape #"
echo "# sap_system #"
echo "# #"
echo "#########################################################################################"
echo ""
exit 1
fi
ok_to_proceed=false
echo "subscription_id=${STATE_SUBSCRIPTION}"
echo "resource_group_name=${REMOTE_STATE_RG}"
echo "storage_account_name=${REMOTE_STATE_SA}"
# This is used to tell Terraform if this is a new deployment or an update
deployment_parameter=""
# This is used to tell Terraform the version information from the state file
version_parameter=""
check_output=0
if [ ! -d ./.terraform/ ];
then
deployment_parameter=" -var deployment=new "
terraform -chdir="${terraform_module_directory}" init -upgrade=true \
--backend-config "subscription_id=${STATE_SUBSCRIPTION}" \
--backend-config "resource_group_name=${REMOTE_STATE_RG}" \
--backend-config "storage_account_name=${REMOTE_STATE_SA}" \
--backend-config "container_name=tfstate" \
--backend-config "key=${key}.terraform.tfstate"
return_value=$?
else
temp=$(grep "\"type\": \"local\"" .terraform/terraform.tfstate)
if [ -n "${temp}" ]
then
terraform -chdir="${terraform_module_directory}" init -upgrade=true -force-copy \
--backend-config "subscription_id=${STATE_SUBSCRIPTION}" \
--backend-config "resource_group_name=${REMOTE_STATE_RG}" \
--backend-config "storage_account_name=${REMOTE_STATE_SA}" \
--backend-config "container_name=tfstate" \
--backend-config "key=${key}.terraform.tfstate"
return_value=$?
else
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $cyan The system has already been deployed and the statefile is in Azure $resetformatting #"
echo "# #"
echo "#########################################################################################"
echo ""
check_output=1
terraform -chdir="${terraform_module_directory}" init -upgrade=true -reconfigure \
--backend-config "subscription_id=${STATE_SUBSCRIPTION}" \
--backend-config "resource_group_name=${REMOTE_STATE_RG}" \
--backend-config "storage_account_name=${REMOTE_STATE_SA}" \
--backend-config "container_name=tfstate" \
--backend-config "key=${key}.terraform.tfstate"
return_value=$?
fi
fi
if [ 0 != $return_value ]
then
echo "#########################################################################################"
echo "# #"
echo -e "# $boldreduscore!!! Error when Initializing !!!$resetformatting #"
echo "# #"
echo "#########################################################################################"
echo ""
exit $return_value
fi
if [ 1 == $check_output ]
then
outputs=$(terraform -chdir="${terraform_module_directory}" output )
if echo "${outputs}" | grep "No outputs"; then
ok_to_proceed=true
echo "#########################################################################################"
echo "# #"
echo -e "# $cyan New deployment $resetformatting #"
echo "# #"
echo "#########################################################################################"
deployment_parameter=" -var deployment=new "
else
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $cyan Existing deployment was detected$resetformatting #"
echo "# #"
echo "#########################################################################################"
echo ""
allParams=$(printf " -var-file=%s %s %s %s %s %s %s" "${var_file}" "${extra_vars}" "${tfstate_parameter}" "${landscape_tfstate_key_parameter}" "${deployer_tfstate_key_parameter}" "${deployment_parameter}" "${version_parameter}" )
terraform -chdir="${terraform_module_directory}" refresh $allParams
deployment_parameter=" "
deployed_using_version=$(terraform -chdir="${terraform_module_directory}" output automation_version | tr -d \")
if [ -z "${deployed_using_version}" ]; then
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $boldred The environment was deployed using an older version of the Terrafrom templates$resetformatting #"
echo "# #"
echo "# !!! Risk for Data loss !!! #"
echo "# #"
echo "# Please inspect the output of Terraform plan carefully before proceeding #"
echo "# #"
echo "#########################################################################################"
if [ 1 == $called_from_ado ] ; then
unset TF_DATA_DIR
exit 1
fi
read -p "Do you want to continue Y/N?" ans
answer=${ans^^}
if [ $answer == 'Y' ]; then
ok_to_proceed=true
else
unset TF_DATA_DIR
exit 1
fi
else
version_parameter=" -var terraform_template_version=${deployed_using_version} "
printf -v val %-.20s "$deployed_using_version"
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $cyan Deployed using the Terraform templates version: $val $resetformatting #"
echo "# #"
echo "#########################################################################################"
echo ""
#Add version logic here
fi
fi
fi
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $cyan Running Terraform plan $resetformatting #"
echo "# #"
echo "#########################################################################################"
echo ""
if [ -f plan_output.log ]
then
rm plan_output.log
fi
allParams=$(printf " -var-file=%s %s %s %s %s %s %s" "${var_file}" "${extra_vars}" "${tfstate_parameter}" "${landscape_tfstate_key_parameter}" "${deployer_tfstate_key_parameter}" "${deployment_parameter}" "${version_parameter}" )
terraform -chdir="$terraform_module_directory" plan -no-color -detailed-exitcode $allParams > plan_output.log
return_value=$?
if [ 1 == $return_value ]
then
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $boldreduscore Errors during the plan phase$resetformatting #"
echo "# #"
echo "#########################################################################################"
echo ""
unset TF_DATA_DIR
exit $return_value
fi
if [ 0 == $return_value ] ; then
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $cyan Infrastructure is up to date $resetformatting #"
echo "# #"
echo "#########################################################################################"
echo ""
if [ -f plan_output.log ]
then
rm plan_output.log
fi
if [ "${deployment_system}" == sap_deployer ]
then
deployer_public_ip_address=$(terraform -chdir="${terraform_module_directory}" output deployer_public_ip_address | tr -d \")
keyvault=$(terraform -chdir="${terraform_module_directory}" output deployer_kv_user_name | tr -d \")
save_config_var "keyvault" "${system_config_information}"
save_config_var "deployer_public_ip_address" "${system_config_information}"
fi
if [ "${deployment_system}" == sap_landscape ]
then
if [ $landscape_tfstate_key_exists == false ]
then
save_config_vars "${system_config_information}" \
landscape_tfstate_key
fi
fi
if [ "${deployment_system}" == sap_library ]
then
tfstate_resource_id=$(terraform -chdir="${terraform_module_directory}" output tfstate_resource_id| tr -d \")
STATE_SUBSCRIPTION=$(echo "$tfstate_resource_id" | cut -d/ -f3 | tr -d \" | xargs)
az account set --sub "${STATE_SUBSCRIPTION}"
REMOTE_STATE_SA=$(terraform -chdir="${terraform_module_directory}" output remote_state_storage_account_name| tr -d \")
get_and_store_sa_details "${REMOTE_STATE_SA}" "${system_config_information}"
fi
unset TF_DATA_DIR
exit $return_value
fi
if [ 2 == $return_value ] ; then
fatal_errors=0
# HANA VM
test=$(grep vm_dbnode plan_output.log | grep -m1 replaced)
if [ -n "${test}" ] ; then
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $boldreduscore!!! Risk for Data loss !!!$resetformatting #"
echo "# #"
echo "# Database server(s) will be replaced #"
echo "# #"
echo "#########################################################################################"
echo ""
echo ""
fatal_errors=1
fi
# HANA VM disks
test=$(grep azurerm_managed_disk.data_disk plan_output.log | grep -m1 replaced)
if [ -n "${test}" ] ; then
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $boldreduscore!!! Risk for Data loss !!!$resetformatting #"
echo "# #"
echo "# Database server disks will be replaced #"
echo "# #"
echo "#########################################################################################"
echo ""
echo ""
fatal_errors=1
fi
# AnyDB server
test=$(grep dbserver plan_output.log | grep -m1 replaced)
if [ -n "${test}" ] ; then
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $boldreduscore!!! Risk for Data loss !!!$resetformatting #"
echo "# #"
echo "# Database server(s) will be replaced #"
echo "# #"
echo "#########################################################################################"
echo ""
echo ""
fatal_errors=1
fi
# AnyDB disks
test=$(grep azurerm_managed_disk.disks plan_output.log | grep -m1 replaced)
if [ -n "${test}" ] ; then
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $boldreduscore!!! Risk for Data loss !!!$resetformatting #"
echo "# #"
echo "# Database server disks will be replaced #"
echo "# #"
echo "#########################################################################################"
echo ""
echo ""
fatal_errors=1
fi
# App server
test=$(grep virtual_machine.app plan_output.log | grep -m1 replaced)
if [ -n "${test}" ] ; then
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $boldreduscore!!! Risk for Data loss !!!$resetformatting #"
echo "# #"
echo "# Application server will be replaced #"
echo "# #"
echo "#########################################################################################"
echo ""
echo ""
fatal_errors=1
fi
# App server disks
test=$(grep azurerm_managed_disk.app plan_output.log | grep -m1 replaced)
if [ -n "${test}" ] ; then
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $boldreduscore!!! Risk for Data loss !!!$resetformatting #"
echo "# #"
echo "# Application server disks will be replaced #"
echo "# #"
echo "#########################################################################################"
echo ""
echo ""
fatal_errors=1
fi
# SCS server
test=$(grep virtual_machine.scs plan_output.log | grep -m1 replaced)
if [ -n "${test}" ] ; then
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $boldreduscore!!! Risk for Data loss !!!$resetformatting #"
echo "# #"
echo "# SCS server(s) disks will be replaced #"
echo "# #"
echo "#########################################################################################"
echo ""
echo ""
fatal_errors=1
fi
# SCS server disks
test=$(grep azurerm_managed_disk.scs plan_output.log | grep -m1 replaced)
if [ -n "${test}" ] ; then
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $boldreduscore!!! Risk for Data loss !!!$resetformatting #"
echo "# #"
echo "# SCS server disks will be replaced #"
echo "# #"
echo "#########################################################################################"
echo ""
echo ""
fatal_errors=1
fi
# Web server
test=$(grep virtual_machine.web plan_output.log | grep -m1 replaced)
if [ -n "${test}" ] ; then
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $boldreduscore!!! Risk for Data loss !!!$resetformatting #"
echo "# #"
echo "# Web Dispatcher server(s) will be replaced #"
echo "# #"
echo "#########################################################################################"
echo ""
echo ""
fatal_errors=1
fi
# Web dispatcher server disks
test=$(grep azurerm_managed_disk.web plan_output.log | grep -m1 replaced)
if [ -n "${test}" ] ; then
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $boldreduscore!!! Risk for Data loss !!!$resetformatting #"
echo "# #"
echo "# Web Dispatcher server disks will be replaced #"
echo "# #"
echo "#########################################################################################"
echo ""
echo ""
fatal_errors=1
fi
if [ $fatal_errors == 1 ] ; then
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $boldreduscore!!! Risk for Data loss !!!$resetformatting #"
echo "# #"
echo "# Please inspect the output of Terraform plan carefully before proceeding #"
echo "# #"
echo "#########################################################################################"
echo ""
if [ 1 == "$called_from_ado" ]; then
unset TF_DATA_DIR
exit 1
fi
if [ 1 == $force ]; then
ok_to_proceed=true
else
read -p "Do you want to continue with the deployment Y/N?" ans
answer=${ans^^}
if [ $answer == 'Y' ]; then
ok_to_proceed=true
else
unset TF_DATA_DIR
exit 1
fi
fi
fi
else
ok_to_proceed=true
fi
if [ $ok_to_proceed ]; then
if [ -f error.log ]
then
rm error.log
fi
if [ -f plan_output.log ]
then
rm plan_output.log
fi
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $cyan Running Terraform apply$resetformatting #"
echo "# #"
echo "#########################################################################################"
echo ""
allParams=$(printf " -var-file=%s %s %s %s %s %s %s %s " "${var_file}" "${extra_vars}" "${tfstate_parameter}" "${landscape_tfstate_key_parameter}" "${deployer_tfstate_key_parameter}" "${deployment_parameter}" "${version_parameter} "${approve}"" )
terraform -chdir="${terraform_module_directory}" apply -parallelism="${parallelism}" $allParams 2>error.log
return_value=$?
if [ 0 != $return_value ] ; then
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $boldreduscore!Errors during the apply phase!$resetformatting #"
echo "# #"
echo "#########################################################################################"
echo ""
cat error.log
rm error.log
unset TF_DATA_DIR
exit $return_value
fi
fi
if [ "${deployment_system}" == sap_deployer ]
then
deployer_public_ip_address=$(terraform -chdir="${terraform_module_directory}" output deployer_public_ip_address | tr -d \")
keyvault=$(terraform -chdir="${terraform_module_directory}" output deployer_kv_user_name | tr -d \")
save_config_var "keyvault" "${system_config_information}"
save_config_var "deployer_public_ip_address" "${system_config_information}"
fi
if [ "${deployment_system}" == sap_landscape ]
then
save_config_vars "${system_config_information}" \
landscape_tfstate_key
fi
if [ "${deployment_system}" == sap_library ]
then
REMOTE_STATE_SA=$(terraform -chdir="${terraform_module_directory}" output remote_state_storage_account_name| tr -d \")
get_and_store_sa_details "${REMOTE_STATE_SA}" "${system_config_information}"
fi
unset TF_DATA_DIR
exit $return_value