-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathinstall_workloadzone.sh
executable file
·778 lines (670 loc) · 33.6 KB
/
install_workloadzone.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
#!/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
called_from_ado=0
INPUT_ARGUMENTS=$(getopt -n install_workloadzone -o p:d:e:k:o:s:c:n:t:v:aifh --longoptions parameterfile:,deployer_tfstate_key:,deployer_environment:,subscription:,spn_id:,spn_secret:,tenant_id:,state_subscription:,keyvault:,storageaccountname:,ado,auto-approve,force,help -- "$@")
VALID_ARGUMENTS=$?
if [ "$VALID_ARGUMENTS" != "0" ]; then
showhelp
fi
eval set -- "$INPUT_ARGUMENTS"
while :
do
case "$1" in
-p | --parameterfile) parameterfile="$2" ; shift 2 ;;
-d | --deployer_tfstate_key) deployer_tfstate_key="$2" ; shift 2 ;;
-e | --deployer_environment) deployer_environment="$2" ; shift 2 ;;
-k | --state_subscription) STATE_SUBSCRIPTION="$2" ; shift 2 ;;
-o | --storageaccountname) REMOTE_STATE_SA="$2" ; shift 2 ;;
-s | --subscription) subscription="$2" ; shift 2 ;;
-c | --spn_id) client_id="$2" ; shift 2 ;;
-v | --keyvault) keyvault="$2" ; shift 2 ;;
-n | --spn_secret) spn_secret="$2" ; shift 2 ;;
-a | --ado) called_from_ado=1 ; shift ;;
-t | --tenant_id) tenant_id="$2" ; shift 2 ;;
-f | --force) force=1 ; shift ;;
-i | --auto-approve) approve="--auto-approve" ; shift ;;
-h | --help) workload_zone_showhelp
exit 3 ; shift ;;
--) shift; break ;;
esac
done
tfstate_resource_id=""
tfstate_parameter=""
deployer_tfstate_key_parameter=""
landscape_tfstate_key=""
landscape_tfstate_key_parameter=""
deployment_system="sap_landscape"
echo "Deployer environment: $deployer_environment"
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
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
workload_file_parametername=$(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 "${workload_file_parametername}" ]
then
printf -v val %-40.40s "$workload_file_parametername"
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $boldreduscore Parameter file does not exist: ${val}$resetformatting #"
echo "# #"
echo "#########################################################################################"
exit 3
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 "$workload_file_parametername"
if [ 0 != $return_code ]; then
exit $return_code
fi
# Convert the region to the correct code
get_region_code "$region"
private_link_used=$(grep "use_private_endpoint=" "${param_dirname}"/"${parameterfile}" | cut -d'=' -f2 | tr -d '"')
key=$(echo "${workload_file_parametername}" | cut -d. -f1)
landscape_tfstate_key=${key}.terraform.tfstate
#Persisting the parameters across executions
automation_config_directory=~/.sap_deployment_automation
generic_config_information="${automation_config_directory}"/config
workload_config_information="${automation_config_directory}"/"${environment}""${region_code}"
if [ "${force}" == 1 ]
then
if [ -f "${workload_config_information}" ]
then
rm "${workload_config_information}"
fi
rm -Rf .terraform terraform.tfstate*
fi
echo "Workload configuration file: $workload_config_information"
if [ "${force}" == 1 ]
then
if [ -f "${workload_config_information}" ]
then
rm "${workload_config_information}"
fi
rm -Rf .terraform terraform.tfstate*
fi
if [ ! -f "${workload_config_information}" ]
then
# Ask for deployer environment name and try to read the deployer state file and resource group details from the configuration file
if [ -z "$deployer_environment" ]
then
read -p "Deployer environment name: " deployer_environment
fi
deployer_config_information="${automation_config_directory}"/"${deployer_environment}""${region_code}"
if [ -f "$deployer_config_information" ]
then
load_config_vars "${deployer_config_information}" "keyvault"
load_config_vars "${deployer_config_information}" "REMOTE_STATE_RG"
load_config_vars "${deployer_config_information}" "REMOTE_STATE_SA"
load_config_vars "${deployer_config_information}" "tfstate_resource_id"
load_config_vars "${deployer_config_information}" "deployer_tfstate_key"
load_config_vars "${deployer_config_information}" "subscription"
save_config_vars "${workload_config_information}" \
keyvault \
subscription \
deployer_tfstate_key \
tfstate_resource_id \
REMOTE_STATE_SA \
REMOTE_STATE_RG
fi
fi
if [ -z "$tfstate_resource_id" ]
then
echo "No tfstate_resource_id"
if [ -n "$deployer_environment" ]
then
deployer_config_information="${automation_config_directory}"/"${deployer_environment}""${region_code}"
echo "Deployer config file $deployer_config_information"
if [ -f "$deployer_config_information" ]
then
load_config_vars "${deployer_config_information}" "keyvault"
load_config_vars "${deployer_config_information}" "REMOTE_STATE_RG"
load_config_vars "${deployer_config_information}" "REMOTE_STATE_SA"
load_config_vars "${deployer_config_information}" "tfstate_resource_id"
load_config_vars "${deployer_config_information}" "deployer_tfstate_key"
load_config_vars "${deployer_config_information}" "subscription"
echo "tfstate_resource_id $tfstate_resource_id"
save_config_vars "${workload_config_information}" \
tfstate_resource_id
save_config_vars "${workload_config_information}" \
keyvault \
subscription \
deployer_tfstate_key \
REMOTE_STATE_SA \
REMOTE_STATE_RG
fi
fi
else
echo "tfstate_resource_id $tfstate_resource_id"
save_config_vars "${workload_config_information}" \
tfstate_resource_id
fi
init "${automation_config_directory}" "${generic_config_information}" "${workload_config_information}"
param_dirname=$(pwd)
var_file="${param_dirname}"/"${parameterfile}"
export TF_DATA_DIR="${param_dirname}/.terraform"
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
fi
if [ -n "$STATE_SUBSCRIPTION" ]
then
echo "Saving the state subscription"
if is_valid_guid "$STATE_SUBSCRIPTION" ; then
echo "Valid subscription format"
save_config_vars "${workload_config_information}" \
STATE_SUBSCRIPTION
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
if [ -n "$client_id" ]
then
if is_valid_guid "$client_id" ; then
echo "Valid spn id format"
else
printf -v val %-40.40s "$client_id"
echo "#########################################################################################"
echo "# #"
echo -e "# The provided spn_id is not valid:$boldred ${val} $resetformatting #"
echo "# #"
echo "#########################################################################################"
exit 65
fi
fi
if [ -n "$tenant_id" ]
then
if is_valid_guid "$tenant_id" ; then
echo "Valid spn id format"
else
printf -v val %-40.40s "$tenant_id"
echo "#########################################################################################"
echo "# #"
echo -e "# The provided tenant_id is not valid:$boldred ${val} $resetformatting #"
echo "# #"
echo "#########################################################################################"
exit 65
fi
fi
if [ -n "$keyvault" ]
then
if valid_kv_name "$keyvault" ; then
save_config_var "keyvault" "${workload_config_information}"
else
printf -v val %-40.40s "$keyvault"
echo "#########################################################################################"
echo "# #"
echo -e "# The provided keyvault is not valid:$boldred ${val} $resetformatting #"
echo "# #"
echo "#########################################################################################"
exit 65
fi
fi
#setting the user environment variables
set_executing_user_environment_variables "none"
load_config_vars "${workload_config_information}" "REMOTE_STATE_SA"
load_config_vars "${workload_config_information}" "REMOTE_STATE_RG"
load_config_vars "${workload_config_information}" "tfstate_resource_id"
load_config_vars "${workload_config_information}" "STATE_SUBSCRIPTION"
load_config_vars "${workload_config_information}" "subscription"
load_config_vars "${workload_config_information}" "keyvault"
load_config_vars "${workload_config_information}" "deployer_tfstate_key"
if [ -n "$tfstate_resource_id" ]
then
REMOTE_STATE_RG=$(echo "$tfstate_resource_id" | cut -d / -f5)
REMOTE_STATE_SA=$(echo "$tfstate_resource_id" | cut -d / -f9)
STATE_SUBSCRIPTION=$(echo "$tfstate_resource_id" | cut -d / -f3)
save_config_vars "${workload_config_information}" \
REMOTE_STATE_SA \
REMOTE_STATE_RG \
STATE_SUBSCRIPTION
else
get_and_store_sa_details ${REMOTE_STATE_SA} ${workload_config_information}
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}"
account_set=1
fi
if [ -z "$REMOTE_STATE_SA" ]
then
if [ -z "$REMOTE_STATE_RG" ]
then
load_config_vars "${workload_config_information}" "tfstate_resource_id"
if [ -n "${tfstate_resource_id}" ]
then
REMOTE_STATE_RG=$(echo "$tfstate_resource_id" | cut -d / -f5)
REMOTE_STATE_SA=$(echo "$tfstate_resource_id" | cut -d / -f9)
STATE_SUBSCRIPTION=$(echo "$tfstate_resource_id" | cut -d / -f3)
fi
fi
tfstate_parameter=" -var tfstate_resource_id=${tfstate_resource_id}"
else
if [ -z "$REMOTE_STATE_RG" ]
then
get_and_store_sa_details "${REMOTE_STATE_SA}" "${workload_config_information}"
load_config_vars "${workload_config_information}" "STATE_SUBSCRIPTION"
load_config_vars "${workload_config_information}" "REMOTE_STATE_RG"
load_config_vars "${workload_config_information}" "tfstate_resource_id"
fi
fi
if [ -n "$keyvault" ]
then
secretname="${environment}"-client-id
az keyvault secret show --name "$secretname" --vault "$keyvault" --only-show-errors 2>error.log
if [ -s error.log ]
then
save_config_var "client_id" "${workload_config_information}"
save_config_var "tenant_id" "${workload_config_information}"
if [ -n "$spn_secret" ]
then
allParams=$(printf " --workload --environment %s --region %s --vault %s --spn_secret %s --subscription %s --spn_id %s " "${environment}" "${region_code}" "${keyvault}" "${spn_secret}" "${subscription}" "${client_id}" )
"${DEPLOYMENT_REPO_PATH}"/deploy/scripts/set_secrets.sh $allParams
if [ $? -eq 255 ]
then
exit $?
fi
else
read -p "Do you want to specify the Workload SPN Details Y/N?" ans
answer=${ans^^}
if [ $answer == 'Y' ]; then
allParams=$(printf " --workload --environment %s --region %s --vault %s --subscription %s --spn_id %s " "${environment}" "${region_code}" "${keyvault}" "${subscription}" "${client_id}" )
"${DEPLOYMENT_REPO_PATH}"/deploy/scripts/set_secrets.sh ${allParams}
if [ $? -eq 255 ]
then
exit $?
fi
fi
fi
fi
if [ -f error.log ]
then
rm error.log
fi
if [ -f kv.log ]
then
rm kv.log
fi
fi
if [ -z "${deployer_tfstate_key}" ]
then
load_config_vars "${workload_config_information}" "deployer_tfstate_key"
if [ -n "${deployer_tfstate_key}" ]
then
# Deployer state was specified in ~/.sap_deployment_automation library config
deployer_tfstate_key_parameter=" -var deployer_tfstate_key=${deployer_tfstate_key}"
fi
else
deployer_tfstate_key_parameter=" -var deployer_tfstate_key=${deployer_tfstate_key}"
save_config_vars "${workload_config_information}" deployer_tfstate_key
fi
if [ -z "${REMOTE_STATE_SA}" ]; then
read -p "Terraform state storage account name:" REMOTE_STATE_SA
get_and_store_sa_details "${REMOTE_STATE_SA}" "${workload_config_information}"
load_config_vars "${workload_config_information}" "STATE_SUBSCRIPTION"
load_config_vars "${workload_config_information}" "REMOTE_STATE_RG"
load_config_vars "${workload_config_information}" "tfstate_resource_id"
tfstate_parameter=" -var tfstate_resource_id=${tfstate_resource_id}"
if [ -n "${STATE_SUBSCRIPTION}" ]
then
if [ $account_set == 0 ]
then
az account set --sub "${STATE_SUBSCRIPTION}"
account_set=1
fi
fi
fi
if [ -z "${REMOTE_STATE_RG}" ]; then
if [ -n "${REMOTE_STATE_SA}" ]; then
get_and_store_sa_details ${REMOTE_STATE_SA} "${workload_config_information}"
load_config_vars "${workload_config_information}" "STATE_SUBSCRIPTION"
load_config_vars "${workload_config_information}" "REMOTE_STATE_RG"
load_config_vars "${workload_config_information}" "tfstate_resource_id"
tfstate_parameter=" -var tfstate_resource_id=${tfstate_resource_id}"
else
option="REMOTE_STATE_RG"
read -p "Remote state resource group name:" REMOTE_STATE_RG
save_config_vars "${workload_config_information}" REMOTE_STATE_RG
fi
fi
if [ -n "${tfstate_resource_id}" ]
then
tfstate_parameter=" -var tfstate_resource_id=${tfstate_resource_id}"
else
get_and_store_sa_details ${REMOTE_STATE_SA} "${workload_config_information}"
load_config_vars "${workload_config_information}" "tfstate_resource_id"
tfstate_parameter=" -var tfstate_resource_id=${tfstate_resource_id}"
fi
terraform_module_directory="$(realpath "${DEPLOYMENT_REPO_PATH}"/deploy/terraform/run/"${deployment_system}" )"
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_landscape #"
echo "# #"
echo "#########################################################################################"
echo ""
exit 1
fi
ok_to_proceed=false
new_deployment=false
#Plugins
if [ ! -d "$HOME/.terraform.d/plugin-cache" ]
then
mkdir -p "$HOME/.terraform.d/plugin-cache"
fi
export TF_PLUGIN_CACHE_DIR="$HOME/.terraform.d/plugin-cache"
root_dirname=$(pwd)
check_output=0
if [ $account_set == 0 ]
then
az account set --sub "${STATE_SUBSCRIPTION}"
account_set=1
fi
if [ ! -d ./.terraform/ ];
then
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
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
save_config_var "REMOTE_STATE_SA" "${workload_config_information}"
save_config_var "subscription" "${workload_config_information}"
save_config_var "STATE_SUBSCRIPTION" "${workload_config_information}"
save_config_var "tfstate_resource_id" "${workload_config_information}"
if [ 1 == $check_output ]
then
outputs=$(terraform -chdir="${terraform_module_directory}" output)
if echo "${outputs}" | grep "No outputs"; then
ok_to_proceed=true
new_deployment=true
echo "#########################################################################################"
echo "# #"
echo -e "# $cyan New deployment $resetformatting #"
echo "# #"
echo "#########################################################################################"
else
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $cyan Existing deployment was detected $resetformatting #"
echo "# #"
echo "#########################################################################################"
echo ""
deployed_using_version=$(terraform -chdir="${terraform_module_directory}" output automation_version)
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
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 ""
terraform -chdir="${terraform_module_directory}" plan -no-color -detailed-exitcode -var-file=${var_file} $tfstate_parameter $deployer_tfstate_key_parameter > plan_output.log
return_value=$?
if [ 1 == $return_value ]
then
echo "#########################################################################################"
echo "# #"
echo -e "# $boldreduscore Errors running plan $resetformatting #"
echo "# #"
echo "#########################################################################################"
echo ""
if [ -f plan_output.log ] ; then
cat plan_output.log
rm plan_output.log
fi
unset TF_DATA_DIR
exit $return_value
fi
ok_to_proceed=0
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 [ "$private_link_used" == "true" ]; then
echo "#########################################################################################"
echo "# #"
echo -e "# $cyan Configuring Private Link $resetformatting #"
echo "# #"
echo "#########################################################################################"
echo ""
app_subnet_id=$(terraform -chdir="${terraform_module_directory}" output app_subnet_id| tr -d \")
az storage account network-rule add -g $REMOTE_STATE_RG --account-name $REMOTE_STATE_SA --subnet $app_subnet_id --only-show-errors --output none
fi
workloadkeyvault=$(terraform -chdir="${terraform_module_directory}" output workloadzone_kv_name | tr -d \")
save_config_var "workloadkeyvault" "${workload_config_information}"
save_config_vars "landscape_tfstate_key" "${workload_config_information}"
ok_to_proceed=0
fi
if [ 2 == $return_value ] ; then
test=$(grep kv_user plan_output.log | grep -m1 replaced)
if [ -n "${test}" ] ; then
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $boldred !!! 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
read -n 1 -r -s -p $'Press enter to continue...\n'
cat plan_output.log
read -p "Do you want to continue with the deployment Y/N?" ans
answer=${ans^^}
if [ $answer == 'Y' ]; then
ok_to_proceed=1
else
unset TF_DATA_DIR
exit 0
fi
else
ok_to_proceed=1
fi
fi
if [ 1 == $ok_to_proceed ]; then
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# $cyan Running Terraform apply $resetformatting #"
echo "# #"
echo "#########################################################################################"
echo ""
terraform -chdir="${terraform_module_directory}" apply ${approve} -var-file=${var_file} $tfstate_parameter $landscape_tfstate_key_parameter $deployer_tfstate_key_parameter
return_value=$?
fi
save_config_var "landscape_tfstate_key" "${workload_config_information}"
if [ 0 == $return_value ] ; then
save_config_vars "landscape_tfstate_key" "${workload_config_information}"
workloadkeyvault=$(terraform -chdir="${terraform_module_directory}" output workloadzone_kv_name | tr -d \")
temp=$(echo "${workloadkeyvault}" | grep "Warning")
if [ -z "${temp}" ]
then
temp=$(echo "${workloadkeyvault}" | grep "Backend reinitialization required")
if [ -z "${temp}" ]
then
printf -v val %-.20s "$workloadkeyvault"
echo ""
echo "#########################################################################################"
echo "# #"
echo -e "# Keyvault to use for System details:$cyan $val $resetformatting #"
echo "# #"
echo "#########################################################################################"
echo ""
save_config_var "workloadkeyvault" "${workload_config_information}"
fi
fi
unset TF_DATA_DIR
if [ "$private_link_used" == "true" ]; then
echo "#########################################################################################"
echo "# #"
echo -e "# $cyan Configuring Private Link $resetformatting #"
echo "# #"
echo "#########################################################################################"
echo ""
app_subnet_id=$(terraform -chdir="${terraform_module_directory}" output app_subnet_id| tr -d \")
az storage account network-rule add -g $REMOTE_STATE_RG --account-name $REMOTE_STATE_SA --subnet $app_subnet_id --only-show-errors --output none
fi
fi
now=$(date)
cat <<EOF > "${workload_config_information}".md
# Workload Zone Deployment #
Date : "${now}"
## Configuration details ##
| Item | Name |
| ----------------------- | -------------------- |
| Environment | $environment |
| Location | $region |
| Keyvault Name | ${workloadkeyvault} |
EOF
unset TF_DATA_DIR
exit $return_value