forked from GorillaStack/auto-tag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy_autotag.sh
executable file
·802 lines (661 loc) · 24.6 KB
/
deploy_autotag.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
#!/usr/bin/env bash
set -e
BoldText=$(tput bold)
NormalText=$(tput sgr0)
function build-package () {
local BUILD_TYPE="$1"
if [ -n "$DEV_MODE" ] ; then
npm run test
npm run lint
fi
npm run compile
cp package.json lib/
npm install --prefix lib/ --production
rm -rf lib/node_modules/aws-sdk
(
cd lib
zip -x\*.zip -qr9 "autotag-${BUILD_TYPE}.zip" -- *
)
}
function upload-package () {
local BUILD_TYPE="$1"
echo "Uploading '${TEMP_DIR}/lib/autotag-${BUILD_TYPE}.zip' to 's3://${S3_BUCKET}/${S3_PATH}'"
aws s3 cp $S3_AWS_CREDENTIALS -- "lib/autotag-${BUILD_TYPE}.zip" "s3://${S3_BUCKET}/${S3_PATH}"
}
function manage-bucket () {
local ACTION="$1"
# local CAPITALIZED_ACTION="${ACTION^}"
BUCKET_OWNER=$(aws s3api list-buckets $S3_AWS_CREDENTIALS | jq -r '.Buckets[] | select(.Name == "'$S3_BUCKET'")')
if [[ -n $BUCKET_OWNER ]] ; then # S3 Bucket exists
if [ "$ACTION" == 'create' ] ; then
echo "The ${S3_BUCKET} S3 Bucket already exists, skipping bucket create"
elif [ "$ACTION" == 'delete' ] ; then
echo -e "\nThe ${S3_BUCKET} S3 Bucket still exists, delete this bucket manually if it won't be used again."
# elif [ "$ACTION" == 'delete' ] ; then
#
# echo "The ${S3_BUCKET} S3 Bucket exists, deleting bucket"
#
# while true ; do
# read -rp "Are you user you want to delete the ${S3_BUCKET} S3 Bucket (yes/no)? " DELETE_BUCKET_ANSWER
# if [[ $DELETE_BUCKET_ANSWER =~ ^(Yes|yes)$ ]] ; then
# break
# else
# echo "Retaining the ${S3_BUCKET} S3 Bucket"
# return
# fi
# done
#
# echo "Deleting the ${S3_BUCKET} S3 Bucket..."
#
# aws s3api delete-bucket $S3_AWS_CREDENTIALS --bucket "$S3_BUCKET" --region "$MAIN_STACK_AWS_REGION"
fi
else
if [ "$ACTION" == 'create' ] ; then
echo "The ${S3_BUCKET} S3 Bucket does not exist for this profile, creating bucket"
echo "Creating the ${S3_BUCKET} secure S3 Bucket with all public access blocked..."
if [ "$MAIN_STACK_AWS_REGION" != 'us-east-1' ] ; then
local LOCATION_CONSTRAINT="--create-bucket-configuration LocationConstraint=$MAIN_STACK_AWS_REGION"
fi
aws s3api create-bucket $S3_AWS_CREDENTIALS --bucket "$S3_BUCKET" --region "$MAIN_STACK_AWS_REGION" $LOCATION_CONSTRAINT
# block all public access to the new S3 bucket
aws s3api put-public-access-block $S3_AWS_CREDENTIALS --bucket "$S3_BUCKET" --region "$MAIN_STACK_AWS_REGION" \
--public-access-block-configuration "BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true"
fi
fi
if [ "$ACTION" == 'create' ] ; then
# check if the configuration requires a S3 bucket policy to allow cross-account access
if [ "$AWS_CREDENTIALS" != "$S3_AWS_CREDENTIALS" ]; then
AWS_ACCOUNT_ID=$(aws sts get-caller-identity $AWS_CREDENTIALS | jq -r '.Account')
S3_ACCOUNT_ID=$(aws sts get-caller-identity $S3_AWS_CREDENTIALS | jq -r '.Account')
if [ "$AWS_ACCOUNT_ID" != "$S3_ACCOUNT_ID" ] ; then
set +e
S3_BUCKET_POLICY=$(aws s3api get-bucket-policy $S3_AWS_CREDENTIALS --bucket "$S3_BUCKET" --output text 2> /dev/null | jq )
if [ -n "$S3_BUCKET_POLICY" ] ; then
S3_BUCKET_POLICY_ACCESS_EXISTS=$(echo "$S3_BUCKET_POLICY" | jq -r '.Statement[] | select(.Principal.AWS | contains("'$AWS_ACCOUNT_ID'"))' 2> /dev/null)
fi
set -e
if [ -n "$S3_BUCKET_POLICY" ] && [ -z "$S3_BUCKET_POLICY_ACCESS_EXISTS" ] ; then
S3_BUCKET_POLICY_STATEMENT=$(cat <<EOF
{
"Sid": "AllowAccountId${AWS_ACCOUNT_ID}",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::${AWS_ACCOUNT_ID}:root"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::${S3_BUCKET}/*"
}
EOF
)
# append the new account to the existing bucket policy
S3_BUCKET_POLICY_UPDATED=$(echo "$S3_BUCKET_POLICY" | jq -r '.Statement += ['"$S3_BUCKET_POLICY_STATEMENT"']')
elif [ -z "$S3_BUCKET_POLICY" ] ; then # this is a brand new bucket policy
S3_BUCKET_POLICY_UPDATED=$(cat <<EOF
{
"Version": "2012-10-17",
"Id": "Policy1475613436714",
"Statement": [
{
"Sid": "AllowAccountId${AWS_ACCOUNT_ID}",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::${AWS_ACCOUNT_ID}:root"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::${S3_BUCKET}/*"
}
]
}
EOF
)
fi
if [ -n "$S3_BUCKET_POLICY_UPDATED" ] ; then
echo "Adding AWS account ID ${AWS_ACCOUNT_ID} to the ${S3_BUCKET} S3 Bucket policy to allow cross-account access"
echo -e "Applying UPDATED Bucket Policy: \n$S3_BUCKET_POLICY_UPDATED\n"
aws s3api put-bucket-policy $S3_AWS_CREDENTIALS --bucket "$S3_BUCKET" --policy "$S3_BUCKET_POLICY_UPDATED"
fi
fi
fi
fi
}
function select-release-version () {
if [ -z "$RELEASE_VERSION" ] ; then
local GITHUB_RELEASES
local LATEST_VERSION
GITHUB_RELEASES=$(curl -sS "$GITHUB_API_RELEASES_URL")
LATEST_VERSION=$(curl -sS "$GITHUB_API_LATEST_URL" | jq -r '.name')
while true ; do
read -rp "Which release version would you like to deploy? [$LATEST_VERSION]: " RELEASE_VERSION
if [[ $(echo "$GITHUB_RELEASES" | jq -er ".[] | select(.name==\"$RELEASE_VERSION\")") ]] ; then
break
elif [[ -z "$RELEASE_VERSION" ]] ; then
RELEASE_VERSION="$LATEST_VERSION"
break
else
echo -e "\nCouldn't find release version '$RELEASE_VERSION' on GitHub"
echo "Here is the list of release versions:"
echo "$GITHUB_RELEASES" | jq -r '.[].name'
echo
continue
fi
done
elif [ "$RELEASE_VERSION" == 'latest' ]; then
RELEASE_VERSION=$(curl -sS "$GITHUB_API_LATEST_URL" | jq -r '.name')
fi
}
function cf-parameters () {
# If we rename or delete parameters from the CF template, do not remove them from this list,
# logic below keeps this list backward-compatible for older CF templates
CF_PARAMETERS=$(cat <<EOF
[
{
"ParameterKey": "CodeS3Bucket",
"ParameterValue": "$S3_BUCKET"
},
{
"ParameterKey": "CodeS3Path",
"ParameterValue": "$S3_PATH"
},
{
"ParameterKey": "AutoTagDebugLogging",
"ParameterValue": "$LOG_LEVEL_DEBUG"
},
{
"ParameterKey": "AutoTagTagsCreateTime",
"ParameterValue": "$CREATE_TIME"
},
{
"ParameterKey": "AutoTagTagsInvokedBy",
"ParameterValue": "$INVOKED_BY"
},
{
"ParameterKey": "LogRetentionInDays",
"ParameterValue": "$LOG_RETENTION_DAYS"
},
{
"ParameterKey": "CustomTags",
"ParameterValue": "${CUSTOM_TAGS//\"/\\\"}"
}
]
EOF
)
TEMPLATE_PARAMETER_KEYS=$(echo "$MAIN_TEMPLATE" | jq -r '.Parameters | [keys[]]')
# For backward-compatibility remove any CF_PARAMETERS that don't exist in the MAIN_TEMPLATE that has been selected
CF_PARAMETERS=$(echo "$CF_PARAMETERS" | jq --argjson tpk "$TEMPLATE_PARAMETER_KEYS" -r '[.[] | select( .[] as $in | $tpk | index($in))]')
}
function wait-for-stack () {
local STACK_PREFIX="$1"
local STACK_NAME="$2"
local STACK_AWS_REGION="$3"
echo "${STACK_PREFIX} CloudFormation Stack in $STACK_AWS_REGION has been created, waiting for completion..."
STACK_STATUS_COMMAND=(aws cloudformation describe-stacks $AWS_CREDENTIALS --stack-name "$STACK_NAME" --region "$STACK_AWS_REGION")
STACK_STATUS=$(${STACK_STATUS_COMMAND[@]} | jq -r '.Stacks[].StackStatus')
until [[ $STACK_STATUS =~ ^(CREATE|UPDATE)_COMPLETE$ ]] || [[ -z "$STACK_STATUS" ]] ; do
echo "${STACK_PREFIX} CloudFormation Stack Status: $STACK_STATUS"
sleep 30
STACK_STATUS=$(${STACK_STATUS_COMMAND[@]} | jq -r '.Stacks[].StackStatus')
done
echo "${STACK_PREFIX} CloudFormation Stack completed with Status: $STACK_STATUS"
}
function manage-stacks () {
local ACTION="$1"
local MANAGE_RELEASE_VERSION="$2"
local TEMP_DIR
# Deploy Main CloudFormation Template
if [ "$ACTION" == "create" ] ; then
TEMP_DIR=$(mktemp -d)
RELEASES=$(curl -sS "$GITHUB_API_RELEASES_URL")
RELEASE_COMMIT=$(curl -sS "${GITHUB_API_TAG_URL}/${MANAGE_RELEASE_VERSION}" | jq -r '.object.sha')
RELEASE_URL_QUERY=$(cat <<EOF
.[] | select(.name = "$MANAGE_RELEASE_VERSION") |
.assets[] | select( .name == "autotag-$MANAGE_RELEASE_VERSION.zip") |
.browser_download_url
EOF
)
RELEASE_ZIP_FILE_URL=$(echo "$RELEASES" | jq -r "$RELEASE_URL_QUERY")
echo "Initializing AutoTag release version $MANAGE_RELEASE_VERSION"
(
cd "$TEMP_DIR"
if ! [[ $(aws s3 ls $S3_AWS_CREDENTIALS "s3://$S3_BUCKET/releases/autotag-$MANAGE_RELEASE_VERSION.zip") ]] ; then
curl -sS -LO "$RELEASE_ZIP_FILE_URL" # Download the release ZIP
aws s3 cp $S3_AWS_CREDENTIALS "autotag-$MANAGE_RELEASE_VERSION.zip" "s3://$S3_BUCKET/releases/autotag-$MANAGE_RELEASE_VERSION.zip"
fi
)
rm -rf "$TEMP_DIR"
ZIP_FILE="autotag-$MANAGE_RELEASE_VERSION.zip"
S3_PATH="releases/$ZIP_FILE"
# TODO: this doesn't work before v0.5.1 because the JSON template wasn't in the repo
MAIN_TEMPLATE=$(curl -sS ${GITHUB_URL}/${RELEASE_COMMIT}/cloud_formation/event_multi_region_template/autotag_event_main-template.json)
echo "Creating the Main CloudFormation Stack..."
cf-parameters
aws cloudformation create-stack \
$AWS_CREDENTIALS \
--template-body "$MAIN_TEMPLATE" \
--stack-name "$MAIN_STACK_NAME" \
--region "$MAIN_STACK_AWS_REGION" \
--capabilities CAPABILITY_NAMED_IAM \
--parameters "$CF_PARAMETERS"
wait-for-stack 'Main' "$MAIN_STACK_NAME" "$MAIN_STACK_AWS_REGION"
elif [ "$ACTION" == "delete" ] ; then
echo "Deleting the Main CloudFormation Stack"
aws cloudformation delete-stack \
$AWS_CREDENTIALS \
--stack-name "$MAIN_STACK_NAME" \
--region "$MAIN_STACK_AWS_REGION"
fi
# Deploy Collector CloudFormation Template
ACTIVE_REGIONS=$(aws ec2 describe-regions $AWS_CREDENTIALS --region "$MAIN_STACK_AWS_REGION" | jq -r '.Regions | sort_by(.RegionName) | .[].RegionName')
for REGION in $ACTIVE_REGIONS ; do
if [ "$ACTION" == "create" ] ; then
echo "Creating the Collector CloudFormation Stack in $REGION..."
COLLECTOR_TEMPLATE=$(curl -sS ${GITHUB_URL}/${RELEASE_COMMIT}/cloud_formation/event_multi_region_template/autotag_event_collector-template.json)
aws cloudformation create-stack \
$AWS_CREDENTIALS \
--template-body "$COLLECTOR_TEMPLATE" \
--stack-name "$COLLECTOR_STACK_NAME" \
--region "$REGION" \
--capabilities CAPABILITY_IAM \
--parameters \
ParameterKey=MainAwsRegion,ParameterValue=$MAIN_STACK_AWS_REGION
elif [ "$ACTION" == "delete" ] ; then
echo "Deleting the Collector CloudFormation Stack in $REGION"
aws cloudformation delete-stack \
$AWS_CREDENTIALS \
--stack-name "$COLLECTOR_STACK_NAME" \
--region "$REGION"
fi
done
if [ "$ACTION" == "delete" ] ; then
wait-for-stack 'Main' "$MAIN_STACK_NAME" "$MAIN_STACK_AWS_REGION"
fi
for REGION in $ACTIVE_REGIONS ; do
wait-for-stack 'Collector' "$COLLECTOR_STACK_NAME" "$REGION"
done
}
# create a way to deploy a release to existing stacks
function update-stacks () {
local UPDATE_RELEASE_VERSION="$1"
local TEMP_DIR
TEMP_DIR=$(mktemp -d)
if [ "$UPDATE_RELEASE_VERSION" == 'master' ] ; then
ZIP_FILE="autotag-master-$(date +%s).zip"
S3_PATH="master-build/$ZIP_FILE"
(
cd "$TEMP_DIR"
git clone --depth 1 "https://github.com/$REPO_NAME.git"
echo
cd auto-tag
npm install
build-package 'master'
upload-package 'master'
)
elif [ "$UPDATE_RELEASE_VERSION" == 'local' ] ; then
ZIP_FILE="autotag-local-$(date +%s).zip"
S3_PATH="local-build/$ZIP_FILE"
build-package 'local'
upload-package 'local'
else # this should be a release version
RELEASES=$(curl -sS "$GITHUB_API_RELEASES_URL")
RELEASE_COMMIT=$(curl -sS "${GITHUB_API_TAG_URL}/${UPDATE_RELEASE_VERSION}" | jq -r '.object.sha')
RELEASE_URL_QUERY=$(cat <<EOF
.[] | select(.name = "$UPDATE_RELEASE_VERSION") |
.assets[] | select( .name == "autotag-$UPDATE_RELEASE_VERSION.zip") |
.browser_download_url
EOF
)
RELEASE_ZIP_FILE_URL=$(echo "$RELEASES" | jq -r "$RELEASE_URL_QUERY")
echo "Initializing AutoTag release version $UPDATE_RELEASE_VERSION"
(
cd "$TEMP_DIR"
if ! [[ $(aws s3 ls $S3_AWS_CREDENTIALS "s3://$S3_BUCKET/releases/autotag-$UPDATE_RELEASE_VERSION.zip") ]] ; then
curl -sS -LO "$RELEASE_ZIP_FILE_URL" # Download the release ZIP
aws s3 cp $S3_AWS_CREDENTIALS "autotag-$UPDATE_RELEASE_VERSION.zip" "s3://$S3_BUCKET/releases/autotag-$UPDATE_RELEASE_VERSION.zip"
fi
)
ZIP_FILE="autotag-$UPDATE_RELEASE_VERSION.zip"
S3_PATH="releases/$ZIP_FILE"
fi
rm -rf "$TEMP_DIR"
if [ "$UPDATE_RELEASE_VERSION" == 'master' ] ; then
MAIN_TEMPLATE=$(curl -sS "${GITHUB_URL}/master/cloud_formation/event_multi_region_template/autotag_event_main-template.json")
COLLECTOR_TEMPLATE=$(curl -sS "${GITHUB_URL}/master/cloud_formation/event_multi_region_template/autotag_event_collector-template.json")
elif [ "$UPDATE_RELEASE_VERSION" == 'local' ] ; then
MAIN_TEMPLATE=$(<cloud_formation/event_multi_region_template/autotag_event_main-template.json)
COLLECTOR_TEMPLATE=$(<cloud_formation/event_multi_region_template/autotag_event_collector-template.json)
else # this should be a release version
MAIN_TEMPLATE=$(curl -sS "${GITHUB_URL}/${RELEASE_COMMIT}/cloud_formation/event_multi_region_template/autotag_event_main-template.json")
COLLECTOR_TEMPLATE=$(curl -sS "${GITHUB_URL}/${RELEASE_COMMIT}/cloud_formation/event_multi_region_template/autotag_event_collector-template.json")
fi
echo "Updating the Main CloudFormation Stack..."
cf-parameters
MAIN_STACK_FAIL_FILE=$(mktemp)
set +e
2> "$MAIN_STACK_FAIL_FILE" \
aws cloudformation update-stack \
$AWS_CREDENTIALS \
--template-body "$MAIN_TEMPLATE" \
--stack-name "$MAIN_STACK_NAME" \
--region "$MAIN_STACK_AWS_REGION" \
--capabilities CAPABILITY_NAMED_IAM \
--parameters "$CF_PARAMETERS"
set -e
MAIN_STACK_FAILURE=$(<$MAIN_STACK_FAIL_FILE)
rm -f "$MAIN_STACK_FAIL_FILE"
MAIN_STACK_FAILURE_PATTERN="^.*ValidationError.*No updates are to be performed\.$"
if [[ -n $MAIN_STACK_FAILURE ]] && [[ ! "$MAIN_STACK_FAILURE" =~ $MAIN_STACK_FAILURE_PATTERN ]] ; then
echo "$MAIN_STACK_FAILURE"
exit 1
fi
wait-for-stack 'Main' "$MAIN_STACK_NAME" "$MAIN_STACK_AWS_REGION"
ACTIVE_REGIONS=$(aws ec2 describe-regions $AWS_CREDENTIALS --region "$MAIN_STACK_AWS_REGION" | jq -r '.Regions[].RegionName')
for REGION in $ACTIVE_REGIONS ; do
echo "Updating the Collector CloudFormation Stack in $REGION..."
COLLECTOR_STACK_FAIL_FILE=$(mktemp)
set +e
2> "$COLLECTOR_STACK_FAIL_FILE" \
aws cloudformation update-stack \
$AWS_CREDENTIALS \
--template-body "$COLLECTOR_TEMPLATE" \
--stack-name "$COLLECTOR_STACK_NAME" \
--region "$REGION" \
--capabilities CAPABILITY_IAM \
--parameters \
ParameterKey=MainAwsRegion,ParameterValue=$MAIN_STACK_AWS_REGION
set -e
COLLECTOR_STACK_FAILURE=$(<$COLLECTOR_STACK_FAIL_FILE)
rm -f "$COLLECTOR_STACK_FAIL_FILE"
COLLECTOR_STACK_FAILURE_PATTERN="^.*ValidationError.*No updates are to be performed\.$"
if [[ -n $COLLECTOR_STACK_FAILURE ]] && [[ ! "$COLLECTOR_STACK_FAILURE" =~ $COLLECTOR_STACK_FAILURE_PATTERN ]] ; then
echo "$COLLECTOR_STACK_FAILURE"
exit 1
fi
done
for REGION in $ACTIVE_REGIONS ; do
wait-for-stack 'Collector' "$COLLECTOR_STACK_NAME" "$REGION"
done
}
function print-header () {
echo -e "******\n****** ${BoldText}${1}${NormalText}\n******\n"
}
function command_exists () {
type "$1" &> /dev/null ;
}
function check-min-dependencies () {
if [ "${BASH_VERSINFO[0]}" -lt "$MIN_BASH_VERSION" ] ; then
echo "Bash version $BASH_VERSION is unsupported, Bash must be at version $MIN_BASH_VERSION or greater."
exit 1
fi
# detect platform
if [[ $(uname | cut -d_ -f1) == 'Darwin' ]] ; then
PLATFORM='macos'
elif command_exists 'yum' ; then
PLATFORM='redhat'
elif command_exists 'apt' ; then
PLATFORM='debian'
else
echo "Supported platform not detected: macOS, redhat, debian/ubuntu"
echo -e "Install the dependencies manually: jq, aws-cli, git, npm, and zip\n"
return 0
fi
# checking for jq
if ! command_exists 'jq' ; then
echo "Command 'jq' is missing, installing it now..."
if [ "$PLATFORM" == 'macos' ] ; then
if command_exists 'brew' ; then
brew install jq
else
echo "Error: Install Homebrew (https://brew.sh) and try again."
exit 1
fi
elif [ "$PLATFORM" == 'redhat' ] ; then
sudo yum install -y jq
elif [ "$PLATFORM" == 'debian' ] ; then
sudo apt install -y jq
fi
fi
# checking for the aws cli
if ! command_exists 'aws' ; then
echo "Command 'aws' is missing, installing it now..."
if command_exists 'pip3' ; then
if [ "$PLATFORM" == 'macos' ] ; then
pip3 install awscli --user
elif [ "$PLATFORM" == 'debian' ] ; then
sudo apt install -y awscli
elif [ "$PLATFORM" == 'redhat' ] ; then
sudo pip3 install awscli
fi
else
if command_exists 'python3' ; then
if [ "$PLATFORM" == 'macos' ] ; then
curl -sS https://bootstrap.pypa.io/get-pip.py -o /tmp/get-pip.py
python3 /tmp/get-pip.py
rm -f /tmp/get-pip.py
pip3 install awscli --user
elif [ "$PLATFORM" == 'debian' ] ; then
sudo apt install -y awscli
elif [ "$PLATFORM" == 'redhat' ] ; then
curl -sS https://bootstrap.pypa.io/get-pip.py -o /tmp/get-pip.py
sudo python3 /tmp/get-pip.py
rm -f /tmp/get-pip.py
sudo pip3 install awscli
fi
else
echo "Error: Install python and try again."
exit 1
fi
fi
fi
echo
}
function check-build-dependencies () {
# checking for git
if ! command_exists 'git' ; then
echo "Command 'git' is missing, installing it now..."
if [ "$PLATFORM" == 'macos' ] ; then
if command_exists 'brew' ; then
brew install git
else
echo "Error: Install Homebrew (https://brew.sh) and try again."
exit 1
fi
elif [ "$PLATFORM" == 'redhat' ] ; then
sudo yum install -y git
elif [ "$PLATFORM" == 'debian' ] ; then
sudo apt install -y git
fi
fi
# checking for npm
if ! command_exists 'npm' ; then
echo "Command 'npm' is missing, installing it now..."
if [ "$PLATFORM" == 'macos' ] ; then
if command_exists 'brew' ; then
brew install npm
else
echo "Error: Install Homebrew (https://brew.sh) and try again."
exit 1
fi
elif [ "$PLATFORM" == 'redhat' ] ; then
if command_exists 'amazon-linux-extras' ; then
sudo amazon-linux-extras install -y epel
sudo yum install -y npm
else
sudo yum install -y npm
fi
elif [ "$PLATFORM" == 'debian' ] ; then
sudo apt install -y npm
fi
fi
# checking for zip
if ! command_exists 'zip' ; then
echo "Command 'zip' is missing, installing it now..."
if [ "$PLATFORM" == 'macos' ] ; then
if command_exists 'brew' ; then
brew install zip
else
echo "Error: Install Homebrew (https://brew.sh) and try again."
exit 1
fi
elif [ "$PLATFORM" == 'redhat' ] ; then
sudo yum install -y zip
elif [ "$PLATFORM" == 'debian' ] ; then
sudo apt install -y zip
fi
fi
}
function show-help {
cat <<- EOF
Usage: $(basename "$0") [options] <command>
Commands:
create Create the AutoTag infrastructure
delete Delete the AutoTag infrastructure
update-release Update the AutoTag infrastructure with a specific release version
update-master Update the AutoTag infrastructure with the latest from the master branch
update-local Update the AutoTag infrastructure with the local source code
Options:
-h --help Show this screen
-r --region The primary AWS region where the main CloudFormation stack will be deployed
-p --profile The main AWS credential profile
-s3bu --s3-bucket The S3 bucket where the code package will be uploaded
-s3pr --s3-profile A separate AWS credential profile to upload code packages to the S3 Bucket
-rv --release-version The release version to deploy, e.g. '0.5.2' or 'latest'
-lr --log-retention-days The number of days to retain the Lambda Function's logs (default: 90)
-ld --log-level-debug Enable the debug logging for the Lambda Function
-dct --disable-create-time Disable the 'CreateTime' tagging for all AWS resources
-dib --disable-invoked-by Disable the 'InvokedBy' tagging for all AWS resources
-ct --custom-tags Define custom tags in a JSON document
EOF
exit 0
}
PARAMS=''
while (( "$#" )); do
case "$1" in
-h|--help)
show-help
;;
-p|--profile)
export PROFILE=$2
shift 2
;;
-r|--region)
export MAIN_STACK_AWS_REGION=$2
shift 2
;;
-s3pr|--s3-profile)
export S3_PROFILE=$2
shift 2
;;
-s3bu|--s3-bucket)
export S3_BUCKET=$2
shift 2
;;
-rv|--release-version)
export RELEASE_VERSION=$2
shift 2
;;
-lr|--log-retention-days)
export LOG_RETENTION_DAYS=$2
shift 2
;;
-ld|--log-level-debug)
export LOG_LEVEL_DEBUG=Enabled
shift 1
;;
-dct|--disable-create-time)
export CREATE_TIME=Disabled
shift 1
;;
-dib|--disable-invoked-by)
export INVOKED_BY=Disabled
shift 1
;;
-ct|--custom-tags)
export CUSTOM_TAGS="$2"
shift 2
;;
-d|--dev)
export DEV_MODE=true
shift 1
;;
-b|--no-banner)
export NO_BANNER=true
shift 1
;;
--) # end argument parsing
shift
break
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*) # preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
# set positional arguments in their proper place
eval set -- "$PARAMS"
PARAMS=($PARAMS)
COMMAND=${PARAMS[0]}
# no command params were sent or help
if [[ -z $PARAMS ]] || [ "$COMMAND" == 'help' ] ; then
show-help
fi
if [[ -z "$NO_BANNER" ]] ; then
cat << "EOF"
_______ __ _______
| _ .--.--| |_.-----| .---.-.-----.
|. 1 | | | _| _ |.| | | _ | _ |
|. _ |_____|____|_____`-|. |-|___._|___ |
|: | | |: | |_____|
|::.|:. | |::.|
`--- ---' `---'
EOF
fi
MIN_BASH_VERSION='4'
REPO_NAME='GorillaStack/auto-tag'
MAIN_STACK_NAME='AutoTag'
COLLECTOR_STACK_NAME="${MAIN_STACK_NAME}-Collector"
GITHUB_URL="https://raw.githubusercontent.com/$REPO_NAME"
GITHUB_API_RELEASES_URL="https://api.github.com/repos/$REPO_NAME/releases"
GITHUB_API_LATEST_URL="https://api.github.com/repos/$REPO_NAME/releases/latest"
GITHUB_API_TAG_URL="https://api.github.com/repos/$REPO_NAME/git/ref/tags"
# primary credentials
[ -n "$PROFILE" ] && AWS_CREDENTIALS="--profile $PROFILE"
# separate S3 credentials
if [ -n "$S3_PROFILE" ] ; then
S3_AWS_CREDENTIALS="--profile $S3_PROFILE"
else
S3_AWS_CREDENTIALS="$AWS_CREDENTIALS"
fi
# CF template defaults
[ -z "$LOG_RETENTION_DAYS" ] && export LOG_RETENTION_DAYS=90
[ -z "$LOG_LEVEL_DEBUG" ] && export LOG_LEVEL_DEBUG=Disabled
[ -z "$CREATE_TIME" ] && export CREATE_TIME=Enabled
[ -z "$INVOKED_BY" ] && export INVOKED_BY=Enabled
[ -z "$CUSTOM_TAGS" ] && export CUSTOM_TAGS=''
check-min-dependencies
if [ "$COMMAND" == 'create' ] ; then
print-header 'Create Stacks'
select-release-version
manage-bucket 'create'
manage-stacks 'create' "$RELEASE_VERSION"
elif [ "$COMMAND" == 'delete' ] ; then
print-header 'Delete Stacks'
manage-stacks 'delete'
manage-bucket 'delete'
elif [ "$COMMAND" == 'update-release' ] ; then
print-header 'Update Stacks with a git release'
select-release-version
update-stacks "$RELEASE_VERSION"
elif [ "$COMMAND" == 'update-master' ] ; then
check-build-dependencies
print-header 'Update Stacks from master'
update-stacks 'master'
elif [ "$COMMAND" == 'update-local' ] ; then
check-build-dependencies
print-header 'Update Stacks from the local repo'
update-stacks 'local'
fi
echo "The AutoTag deployment is complete"