-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoscaling-update-ami.sh
7150 lines (7132 loc) · 316 KB
/
autoscaling-update-ami.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
#
#
# ------------------------------------------------------------------------------------
#
# MIT License
#
# Copyright (c) 2017 Enterprise Group, Ltd.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# ------------------------------------------------------------------------------------
#
# File: autoscaling-update-ami.sh
#
script_version=1.2.25
#
# Dependencies:
# - bash shell
# - jq - JSON wrangler https://stedolan.github.io/jq/
# - AWS CLI tools (pre-installed on AWS AMIs)
# - AWS CLI profile with IAM permissions for the following AWS CLI commands:
# - aws autoscaling create-launch-configuration
# - aws autoscaling delete-launch-configuration
# - aws autoscaling describe-auto-scaling-groups
# - aws autoscaling describe-launch-configurations
# - aws autoscaling update-auto-scaling-group
# - aws ec2 describe-images (used to test AMI)
# - aws iam list-account-aliases (used to pull AWS account alias)
# - aws sts get-caller-identity (used to pull AWS acount)
#
# Tested on:
# Windows Subsystem for Linux (WSL)
# OS Build: 15063.540
# bash.exe version: 10.0.15063.0
# Ubuntu 16.04
# GNU bash, version 4.3.48(1)
# jq 1.5-1-a5b5cbe
# aws-cli/1.11.134 Python/2.7.12 Linux/4.4.0-43-Microsoft botocore/1.6.1
#
# AWS EC2
# Amazon Linux AMI release 2017.03
# Linux 4.9.43-17.38.amzn1.x86_64
# GNU bash, version 4.2.46(2)
# jq-1.5
# aws-cli/1.11.133 Python/2.7.12 Linux/4.9.43-17.38.amzn1.x86_64 botocore/1.6.0
#
#
# By: Douglas Hackney
# https://github.com/dhackney
#
# Type: AWS utility
# Description:
# This shell script is used to update autoscaling Launch Configurations (LC) and associated autoscaling groups with a new AMI
# For every launch config using a target AMI, a new clone launch config is created with all existing parameters
# All clone launch configs are updated to use the new AMI
# All Autoscaling Groups (AG) using an updated launch config are updated to use the new launch config with the new AMI
# For every launch config using the new AMI, the X most recent launch config versions are retained
#
#
# Roadmap:
# - report: LCs deleted
# - delete run after ASG update to support 0 retained versions
# - -r region
# - -r all regions
# - delete LC name text -x parameter
# - info level logging
#
#
###############################################################################
#
#
#
#
###############################################################################
#
#
# Set the language to prevent time lost to unicode processing
#
export LC_ALL=C
#
###############################################################################
#
# set the environmental variables
#
set -o pipefail
#
###############################################################################
#
# initialize the script variables
#
ami_new_id=""
ami_new_id_report=""
ami_new_name=""
ami_new_name_report=""
ami_new_string=""
ami_new_trim=""
ami_new_type=""
ami_old_id=""
ami_old_id_report=""
ami_old_name=""
ami_old_name_report=""
ami_old_string=""
ami_old_trim=""
ami_old_type=""
choices=""
cli_profile=""
count_ag=""
count_ag_report=0
count_ami_new_id=0
count_ami_old_id=0
count_ami_old_id_owned=0
count_ami_old_id_shared=0
count_aws_error_lc_limit_exceeded=0
count_cli_profile=0
count_data_lines=0
count_error_lines=0
count_lc=0
count_lc_ag_name=0
count_lc_append_name_index=0
count_lc_arn=0
count_lc_create_name=0
count_lc_duplicate_names=0
count_lc_index=0
count_lc_name_dup_line=0
count_lc_name_list_duplicated_names_sorted=0
count_lc_name_list_raw=0
count_lc_name_list_raw_duplicated=0
count_lc_name_list_raw_unique=0
count_lc_names=0
count_lc_report=0
count_lc_report_index=0
count_lc_target=0
count_lc_target_appended_text_arn_sorted=0
count_lc_target_appended_text_names_sorted=0
count_lc_version_deleted_lines=0
count_lc_versions_all=0
count_lc_versions_post_delete=0
count_name_line=0
count_post_lc_name_update_lines=0
count_script_version_length=0
count_text_header_length=0
count_text_block_length=0
count_text_width_menu=0
count_text_width_header=0
count_text_side_length_menu=0
count_text_side_length_header=0
count_text_bar_menu=0
count_text_bar_header=0
count_this_file_tasks=0
count_ver=0
count_ver_line=0
count_write_mapping_ag=0
count_write_mapping_lc=0
count_write_mapping_name_new_arn=0
count_write_mapping_name_old_arn=0
count_year=0
count_year_line=0
counter_ag_name_json_populate=0
counter_arn=0
counter_arn_json_populate=0
counter_count_lc=0
counter_count_lc_append_name=0
counter_file=0
counter_lc_create=0
counter_lc_name_file=0
counter_lcname_new_arn_json_populate=0
counter_lc_name_new_file=0
counter_lc_old_name_json_populate=0
counter_lc_report=0
counter_lc_target_appended_text_names_sorted=0
counter_name_append_loop=0
counter_name_unique_append=0
counter_name_unique_loop=0
counter_source_file=0
counter_target_file=0
counter_this_file_tasks=0
counter_update_ag_lc_name_new=0
counter_version_deleted=0
counter_write_mapping_ag=0
counter_write_mapping_lc=0
counter_write_mapping_name_new_arn=0
counter_write_mapping_name_old_arn=0
date_file="$(date +"%Y-%m-%d-%H%M%S")"
date_now="$(date +"%Y-%m-%d-%H%M%S")"
detailed_monitoring_state=""
_empty=""
_empty_task=""
error_aws_flag=""
exportLC_ALL=""
feed_write_log=""
_fill=""
_fill_task=""
full_path=""
function_LCNameNewWrite_parameter_1_stripped=""
integer_re_pattern=""
lc_array=""
lc_create_json=""
lc_create_json_aws=""
lc_create_json_edited_01=""
lc_create_json_edited_02=""
lc_create_json_edited_03=""
lc_create_json_edited_04=""
lc_create_json_edited_05=""
lc_duplicate_name_write=""
lc_name_append_string=""
lc_name_append_string_clean=""
lc_name_list_duplicated_names=""
lc_name_list_raw=""
lc_name_new=""
lc_name_new_mapping=""
lc_name_new_report=""
lc_name_new_stripped=""
lc_name_new_unique=""
lc_name_new_unique_stripped=""
lc_name_old=""
lc_name_old_arn=""
lc_name_old_mapping=""
lc_name_old_report=""
lc_name_old_stripped=""
lc_name_unique_text_clean=""
lc_name_version_number=""
lc_name_version_number_check=""
lc_name_version_prefix=""
lc_new_names_raw=""
lc_new_names_sorted=""
lc_old_names_raw=""
lc_old_names_sorted=""
lc_target=""
lc_target_appended_text=""
lc_target_appended_text_names_sorted_line=""
lc_target_monitoring_set=""
lc_target_new_ami_id=""
lc_target_security_group_add=""
lc_versions_all=""
lc_versions_delete=""
lc_versions_post_delete=""
let_done=""
let_done_task=""
let_left=""
let_left_task=""
let_progress=""
let_progress_task=""
logging=""
parameter1=""
paramter2=""
security_group_add_id=""
text_header=""
text_bar_menu_build=""
text_bar_header_build=""
text_side_menu=""
text_side_header=""
text_menu=""
text_menu_bar=""
text_header=""
text_header_bar=""
this_aws_account=""
this_aws_account_alias=""
this_file=""
this_file_tasks=""
this_log=""
thislogdate=""
this_log_file=""
this_log_file_errors=""
this_log_file_errors_full_path=""
this_log_file_full_path=""
this_log_temp_file_full_path=""
this_path=""
this_summary_report=""
this_summary_report_full_path=""
this_user=""
user_data_base64_decode=""
verbose=""
#
###############################################################################
#
#
# Initialize the baseline variables
#
this_utility_acronym="lc"
this_utility_filename_plug="autoscaling-update-ami"
this_path="$(pwd)"
this_file="$(basename "$0")"
full_path="${this_path}"/"$this_file"
this_log_temp_file_full_path="$this_path"/"$this_utility_filename_plug"-log-temp.log
this_user="$(whoami)"
date_file="$(date +"%Y-%m-%d-%H%M%S")"
logging="n"
#
#
#
#
###############################################################################
#
#
# Initialize the temp log file
#
echo "" > "$this_log_temp_file_full_path"
#
#
#
#
##############################################################################################################33
# Function definition begin
##############################################################################################################33
#
#
# Functions definitions
#
#######################################################################
#
#######################################################################
#
#
# function to display the Usage
#
#
function fnUsage()
{
echo ""
echo " --------------------------------------- Autoscaling update AMI utility usage ----------------------------------------"
echo ""
echo " This utility updates targeted AWS Autoscale EC2 Launch Configurations and Autoscaling Groups with a new AMI "
echo ""
echo " This script will: "
echo " * Update AWS Autoscaling Launch Configurations to use a new AMI "
echo " * Update Launch Configuration names to reflect the new version "
echo " * Create new Launch Configurations using the updated configurations "
echo " * Delete prior Launch Configuration versions "
echo " * Update all related Autoscaling Groups to use the new Launch Configurations "
echo ""
echo "----------------------------------------------------------------------------------------------------------------------"
echo ""
echo " Usage:"
echo " autoscaling-update-ami.sh -p AWS_CLI_profile -n new_AMI_ID_or_name -t old_target_AMI_ID_or_name -a append-text "
echo ""
echo " Optional parameters: -d 3 -m f -v y "
echo ""
echo " Where: "
echo " -p - Name of the AWS CLI cli_profile (i.e. what you would pass to the --profile parameter in an AWS CLI command)"
echo " Example: -p myAWSCLIprofile "
echo ""
echo " -n - New AMI ID or the name unique string. For the name: A string that uniquely identifies the new AMI name. "
echo " This can be the entire new AMI name or any subset of the name that uniquely identifies it. "
echo ""
echo " -t - Old, target AMI ID or the name unique string. For the name: A string that uniquely identifies the old, target "
echo " AMI name that you need to update. This can be the entire old, target AMI name or any subset of the name "
echo " that uniquely identifies it."
echo ""
echo " Note: The new and old AMI inputs must both be AMI names or AMI IDs "
echo ""
echo " Examples using AMI names:"
echo " Example: -n act123456789999_worker_v432_2017_11_23_1302_utc "
echo " Example: -t act123456789999_worker_v321_2016_04_30_1845_utc "
echo ""
echo " Examples using AMI IDs:"
echo " Example: -n ami-ab7874d1 "
echo " Example: -t ami-c5636fde "
echo ""
echo " -a - Text to append to the new launch config name. The append text will begin after the first occurance of "
echo " the dash character '-' when it is followed by a v and a number or when it is followed by a year number. "
echo " The append text will begin at the v in this example: -vNNNN (where NNNN is any number). "
echo " The append text will begin at the Y in this example -YYYY (where YYYY is a year, e.g. 2017) "
echo " The utility will default to append the current date as: -YYYY-MM-DD-HHMM-utc"
echo -e " >> Note: The append text cannot contain the characters: \` ~ ' ! # ^ \* \" () {} [] <> $ % @ & = + \\ / <<"
echo " Example: -a v32-2017-08-21-1845-utc produces: my-launch-config-existing-text-v32-2017-08-21-1845-utc "
echo " Example: -a 2017-08-21-1845-utc produces: my-launch-config-existing-text-2017-08-21-1845-utc "
echo ""
echo " -s - Security Group ID to add to all launch configurations."
echo " Example: -s sg-9612f4e3 "
echo ""
echo " -d - Delete versions older than X. Set to the number of versions to retain. Setting to 3 will retain three prior versions."
echo " Note: this flag assumes the following Launch Configuration Name syntax: My-Launch-Configuration-Name-vXX-Anything-Else"
echo " Where 'XX' is equal to any number of decimals, e.g. 45, 456, 4567890. Decimal places are not supported, e.g. 10.00.23"
echo " Example: -d 3 "
echo ""
echo " -m - Detailed monitoring enabled true/false. Set this to either 't' or 'f' to enable or disable EC2 detailed monitoring."
echo " Example: -m f "
echo ""
echo " -b - Verbose console output. Set to 'y' for verbose console output. Note: this mode is very slow."
echo " Example: -b y "
echo ""
echo " -g - Logging on / off. Default is off. Set to 'y' to create a debug log. Note: logging mode is slower. "
echo " Example: -g y "
echo ""
echo " -h - Display this message"
echo " Example: -h "
echo ""
echo " ---version - Display the script version"
echo " Example: --version "
echo ""
echo ""
exit 1
}
#
#######################################################################
#
#
# function to echo the progress bar to the console
#
# source: https://stackoverflow.com/questions/238073/how-to-add-a-progress-bar-to-a-shell-script
#
# 1. Create ProgressBar function
# 1.1 Input is currentState($1) and totalState($2)
function fnProgressBar()
{
# Process data
let _progress=(${1}*100/"${2}"*100)/100
let _done=(${_progress}*4)/10
let _left=40-"$_done"
# Build progressbar string lengths
_fill="$(printf "%${_done}s")"
_empty="$(printf "%${_left}s")"
#
# 1.2 Build progressbar strings and print the ProgressBar line
# 1.2.1 Output example:
# 1.2.1.1 Progress : [########################################] 100%
printf "\r Overall Progress : [${_fill// /#}${_empty// /-}] ${_progress}%%"
}
#
#######################################################################
#
#
# function to update the task progress bar
#
# source: https://stackoverflow.com/questions/238073/how-to-add-a-progress-bar-to-a-shell-script
#
# 1. Create ProgressBar function
# 1.1 Input is currentState($1) and totalState($2)
function fnProgressBarTask()
{
# Process data
let _progress_task=(${1}*100/"${2}"*100)/100
let _done_task=(${_progress_task}*4)/10
let _left_task=40-"$_done_task"
# Build progressbar string lengths
_fill_task="$(printf "%${_done_task}s")"
_empty_task="$(printf "%${_left_task}s")"
#
# 1.2 Build progressbar strings and print the ProgressBar line
# 1.2.1 Output example:
# 1.2.1.1 Progress : [########################################] 100%
printf "\r Task Progress : [${_fill_task// /#}${_empty_task// /-}] ${_progress_task}%%"
}
#
#######################################################################
#
#
# function to display the task progress bar on the console
#
# parameter 1 = counter
# paramter 2 = count
#
function fnProgressBarTaskDisplay()
{
fnWriteLog ${LINENO} level_0 " ---------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
fnProgressBarTask "$1" "$2"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " ---------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
}
#
#######################################################################
#
#
# function to echo the header to the console
#
function fnHeader()
{
clear
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} "--------------------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} "--------------------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "$text_header"
fnWriteLog ${LINENO} level_0 ""
fnProgressBar ${counter_this_file_tasks} ${count_this_file_tasks}
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "$text_header_bar"
fnWriteLog ${LINENO} level_0 ""
}
#
#######################################################################
#
#
# function to echo to the console and write to the log file
#
function fnWriteLog()
{
# clear IFS parser
IFS=
# write the output to the console
fnOutputConsole "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
# if logging is enabled, then write to the log
if [[ ("$logging" = "y") || ("$logging" = "z") ]] ;
then
# write the output to the log
fnOutputLog "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
fi
# reset IFS parser to default values
unset IFS
}
#
#######################################################################
#
#
# function to echo to the console
#
function fnOutputConsole()
{
#
# console output section
#
# test for verbose
if [ "$verbose" = "y" ] ;
then
# if verbose console output then
# echo everything to the console
#
# strip the leading 'level_0'
if [ "$2" = "level_0" ] ;
then
# if the line is tagged for display in non-verbose mode
# then echo the line to the console without the leading 'level_0'
echo " Line: "$1" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "
else
# if a normal line echo all to the console
echo " Line: "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "
fi
else
# test for minimum console output
if [ "$2" = "level_0" ] ;
then
#
# "console output no -v: the logic test for level_0 was true"
#
# if the line is tagged for display in non-verbose mode
# then echo the line to the console without the leading 'level_0'
echo " "$3" "$4" "$5" "$6" "$7" "$8" "$9" "
fi
fi
#
#
}
#
#######################################################################
#
#
# function to write to the log file
#
function fnOutputLog()
{
# log output section
#
# load the timestamp
thislogdate="$(date +"%Y-%m-%d-%H:%M:%S")"
#
# ----------------------------------------------------------
#
# normal logging
#
# append the line to the log variable
# the variable is written to the log file on exit by function fnWriteLogFile
#
# if the script is crashing then comment out this section and enable the
# section below "use this logging for debug"
#
if [ "$2" = "level_0" ] ;
then
# if the line is tagged for logging in non-verbose mode
# then write the line to the log without the leading 'level_0'
this_log+="$(echo "${thislogdate} Line: "$1" "$3" "$4" "$5" "$6" "$7" "$8" "$9" " 2>&1)"
else
# if a normal line write the entire set to the log
this_log+="$(echo "${thislogdate} Line: "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" " 2>&1)"
fi
#
# append the new line
# do not quote the following variable: $'\n'
this_log+=$'\n'
#
#
# ---------------------------------------------------------
#
# 'use this for debugging' - debug logging
#
# if the script is crashing then enable this logging section and
# comment out the prior logging into the 'this_log' variable
#
# note that this form of logging is VERY slow
#
# write to the log file with a prefix timestamp
# echo ${thislogdate} Line: "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" 2>&1 >> "$this_log_file_full_path"
#
#
}
#
#######################################################################
#
#
# function to append the log variable to the temp log file
#
function fnWriteLogTempFile()
{
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "Appending the log variable to the temp log file"
fnWriteLog ${LINENO} ""
echo "$this_log" >> "$this_log_temp_file_full_path"
# empty the temp log variable
this_log=""
}
#
#######################################################################
#
#
# function to write log variable to the log file
#
function fnWriteLogFile()
{
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} "Writing temp log to log file"
fnWriteLog ${LINENO} level_0 ""
# write the contents of the variable to the temp file
fnWriteLogTempFile
# append the temp file onto the log file
cat "$this_log_temp_file_full_path" >> "$this_log_file_full_path"
echo "" >> "$this_log_file_full_path"
echo "Log end" >> "$this_log_file_full_path"
# delete the temp log file
rm -f "$this_log_temp_file_full_path"
}
#
##########################################################################
#
#
# function to delete the work files
#
function fnDeleteWorkFiles()
{
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "Creating timestamp version of file: 'launch-configs-mapping.json' "
fnWriteLog ${LINENO} level_0 ""
feed_write_log="$(mv -f launch-configs-mapping.json "$this_launch_configs_mapping_json_full_path" 2>&1)"
fnWriteLog ${LINENO} "$feed_write_log"
fnWriteLog ${LINENO} level_0 ""
if [ "$verbose" != "y" ] ;
then
# if not verbose console output then delete the work files
fnWriteLog ${LINENO} level_0 "Deleting work files"
fnWriteLog ${LINENO} level_0 ""
feed_write_log="$(rm -f "$this_utility_acronym"-* 2>&1)"
fnWriteLog ${LINENO} "$feed_write_log"
feed_write_log="$(rm -f "$this_utility_acronym"_* 2>&1)"
fnWriteLog ${LINENO} "$feed_write_log"
feed_write_log="$(rm -f launch-configs-mapping-temp-ami.json 2>&1)"
fnWriteLog ${LINENO} "$feed_write_log"
# if no errors, then delete the error log file
count_error_lines="$(cat "$this_log_file_errors_full_path" | wc -l)"
if (( "$count_error_lines" < 3 ))
then
rm -f "$this_log_file_errors_full_path"
fi
else
# in verbose mode so preserve the work files
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "In verbose mode, preserving work files "
fnWriteLog ${LINENO} level_0 ""
fi
}
#
##########################################################################
#
#
# function to increment the task counter
#
function fnCounterIncrementTask()
{
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "increment the task counter"
counter_this_file_tasks="$((counter_this_file_tasks+1))"
fnWriteLog ${LINENO} "value of variable 'counter_this_file_tasks': "$counter_this_file_tasks" "
fnWriteLog ${LINENO} "value of variable 'count_this_file_tasks': "$count_this_file_tasks" "
fnWriteLog ${LINENO} ""
}
#
##########################################################################
#
#
# function to log non-fatal errors
#
function fnErrorLog()
{
fnWriteLog ${LINENO} level_0 "-----------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Error message: "
fnWriteLog ${LINENO} level_0 " "$feed_write_log" "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "-----------------------------------------------------------------------------------------------------"
echo "-----------------------------------------------------------------------------------------------------" >> "$this_log_file_errors_full_path"
echo "" >> "$this_log_file_errors_full_path"
echo " Error message: " >> "$this_log_file_errors_full_path"
echo " "$feed_write_log"" >> "$this_log_file_errors_full_path"
echo "" >> "$this_log_file_errors_full_path"
echo "-----------------------------------------------------------------------------------------------------" >> "$this_log_file_errors_full_path"
}
#
##########################################################################
#
#
# function to handle command or pipeline errors
#
function fnErrorPipeline()
{
fnWriteLog ${LINENO} level_0 "-----------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Command or Command Pipeline Error "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "-----------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " System Error while running the previous command or pipeline "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Please check the error message above "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Error at script line number: "$error_line_pipeline" "
fnWriteLog ${LINENO} level_0 ""
if [[ "$logging" == "y" ]] ;
then
fnWriteLog ${LINENO} level_0 " The log will also show the error message and other environment, variable and diagnostic information "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " The log is located here: "
fnWriteLog ${LINENO} level_0 " "$this_log_file_full_path" "
fi
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Exiting the script"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "-----------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
# append the temp log onto the log file
fnWriteLogTempFile
# write the log variable to the log file
fnWriteLogFile
exit 1
}
#
##########################################################################
#
#
# function for AWS CLI errors
#
function fnErrorAws()
{
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " AWS Error while executing AWS CLI command"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Please check the AWS error message above "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Error at script line number: "$error_line_aws" "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " The log will also show the AWS error message and other diagnostic information "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " The log is located here: "
fnWriteLog ${LINENO} level_0 " "$this_log_file_full_path" "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Exiting the script"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "--------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
# append the temp log onto the log file
fnWriteLogTempFile
# write the log variable to the log file
fnWriteLogFile
exit 1
}
#
##########################################################################
#
#
# function to handle fatal errors
#
function fnError()
{
if [ "$?" -ne 0 ]
then
fnWriteLog ${LINENO} level_0 "-----------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Error message: "
fnWriteLog ${LINENO} level_0 " "$feed_write_log""
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "-----------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " System Error while running the previous command "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Please check the error message above "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " The log will also show the error message and other environment, variable and diagnostic information "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " The log is located here: "
fnWriteLog ${LINENO} level_0 " "$this_log_file_full_path""
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Exiting the script"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "-----------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
# delete the work files
fnDeleteWorkFiles
# write the log variable to the log file
fnWriteLogFile
exit 1
fi
}
#
#######################################################################
#
#
# function to update the LC name
#
function fnUpdateLcName()
{
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in fnUpdateLcName"
fnWriteLog ${LINENO} "value of variable 'lc_name_old_line': "$lc_name_old_line" "
fnWriteLog ${LINENO} "value of variable 'lc_name_new_stripped': "$lc_name_new_stripped" "
fnWriteLog ${LINENO} ""
#
# update the LC name in the target variable and append the object to the file: 'lc_target_appended_text.json'
feed_write_log="$(echo "$lc_array" | jq -r --arg lc_name_old_line_jq "$lc_name_old_line" --arg lc_name_new_stripped_jq "$lc_name_new_stripped" ' .[] | select(.LaunchConfigurationName==$lc_name_old_line_jq) | .LaunchConfigurationName=$lc_name_new_stripped_jq ' >> lc_target_appended_text.json 2>&1)"
#
# check for command / pipeline error(s)
if [ "$?" -ne 0 ]
then
#
# set the command/pipeline error line number
error_line_pipeline="$((${LINENO}-7))"
#
# call the command / pipeline error function
fnErrorPipeline
#
#
fi
#
fnWriteLog ${LINENO} "$feed_write_log"
#
# check to see if a comma is needed to separate the appended objects
if (( "$counter_name_append_loop" < "$count_lc_names" ))
then
feed_write_log="$(echo "," >> lc_target_appended_text.json 2>&1)"
fnWriteLog ${LINENO} "$feed_write_log"
fi
#
}
#
#######################################################################
#
#
# function to dedupe the LC name
#
function fnDedupeLcName()
{
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in fnDedupeLcName"
fnWriteLog ${LINENO} "value of function parameters: "
fnWriteLog ${LINENO} "parameter 1 carries the deduped LC name"
fnWriteLog ${LINENO} "value of function parameter 1:"$1" "
fnWriteLog ${LINENO} "parameter 2 carries the old name arn"
fnWriteLog ${LINENO} "value of function parameter 2: "$2" "
fnWriteLog ${LINENO} ""
#
# update the LC name in the target variable and append the object to the file: 'lc_name_unique_text.json'
feed_write_log="$(echo "$lc_array" | jq -r --arg lc_arn_line_jq "$2" --arg lc_name_new_unique_stripped_jq "$1" ' .[] | select(.LaunchConfigurationARN==$lc_arn_line_jq) | .LaunchConfigurationName=$lc_name_new_unique_stripped_jq ' >> lc_name_unique_text.json 2>&1)"
#
# check for command / pipeline error(s)
if [ "$?" -ne 0 ]
then
#
# set the command/pipeline error line number
error_line_pipeline="$((${LINENO}-7))"
#
# call the command / pipeline error function
fnErrorPipeline
#
#
fi
#
fnWriteLog ${LINENO} "$feed_write_log"
#
# check to see if a comma is needed to separate the appended objects
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "value of variable 'counter_arn': "$counter_arn" "
fnWriteLog ${LINENO} "'count_lc_target_appended_text_arn_sorted': "$count_lc_target_appended_text_arn_sorted" "
fnWriteLog ${LINENO} "testing for counter_arn < count_lc_target_appended_text_arn_sorted "
fnWriteLog ${LINENO} ""
if [[ "$counter_arn" -lt "$count_lc_target_appended_text_arn_sorted" ]]
then
fnWriteLog ${LINENO} "counter arn is tested as < count_lc_target_appended_text_arn_sorted"
fnWriteLog ${LINENO} "adding comma between JSON LC objects "
feed_write_log="$(echo "," >> lc_name_unique_text.json 2>&1)"
fnWriteLog ${LINENO} "$feed_write_log"
fi
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "contents of file: 'lc_name_unique_text.json' "
feed_write_log="$(cat lc_name_unique_text.json 2>&1)"
fnWriteLog ${LINENO} "$feed_write_log"
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} ""
}
#
#######################################################################
#
#
# function to write the new LC name into the mapping json file
#
function fnLCNameNewWrite()
{
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "----------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "------------ begin function fnLCNameNewWrite : write new LC name to file: 'lc-mapping.json' -------------"
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "----------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "Writing the new Launch Configuration name to the file: 'lc-mapping.json'... "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} "initializing the counters"
counter_source_file="$counter_file"
counter_target_file="$((counter_source_file+1))"
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} "value of variable 'counter_source_file': "$counter_source_file" "
fnWriteLog ${LINENO} "value of variable 'counter_target_file': "$counter_target_file" "
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} "value of function parameters: "
fnWriteLog ${LINENO} "value of function parameter 1: "$1" "
fnWriteLog ${LINENO} "value of function parameter 2: "$2" "
fnWriteLog ${LINENO} "value of function parameter 3: "$3" "
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} "initializing the variables"
fnWriteLog ${LINENO} "loading the variable: 'lc_name_old' "
# test if this is a dedupe run and is called with the ARN in parameter 2
if [[ "$2" = "" ]] ;
then
lc_name_old="$lc_name_old"
else
lc_name_old="$3"
fi
fnWriteLog ${LINENO} "value of variable 'lc_name_old': "$lc_name_old" "
fnWriteLog ${LINENO} ""
#
lc_name_old_stripped="$(echo "$lc_name_old" | tr -d '\",' )"
fnWriteLog ${LINENO} "value of variable 'lc_name_old_stripped': "$lc_name_old_stripped" "
fnWriteLog ${LINENO} "loading into lc_name_old variable for the run"
fnWriteLog ${LINENO} "command: lc_name_old_mapping="$lc_name_old_stripped" "
lc_name_old_mapping="$lc_name_old_stripped"
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} "Loading the new LC name from the function parameter"
fnWriteLog ${LINENO} "value of function parameter 1:"
fnWriteLog ${LINENO} ""$1" "
function_LCNameNewWrite_parameter_1_stripped="$(echo "$1" | tr -d '\",' )"
fnWriteLog ${LINENO} "value of variable 'function_LCNameNewWrite_parameter_1_stripped':"
fnWriteLog ${LINENO} ""$function_LCNameNewWrite_parameter_1_stripped" "
fnWriteLog ${LINENO} "loading into variable: 'lc_name_new' "
fnWriteLog ${LINENO} "command: lc_name_new_mapping= $ function_LCNameNewWrite_parameter_1_stripped "
lc_name_new_mapping="$function_LCNameNewWrite_parameter_1_stripped"
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} "Loading the old LC name ARN from the function parameter"
fnWriteLog ${LINENO} "value of function parameter 2:"
fnWriteLog ${LINENO} ""$2" "