-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaster_deploy.sh
executable file
·1238 lines (1108 loc) · 45.4 KB
/
master_deploy.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
#Variable Declaration
JQ="jq --raw-output --exit-status"
DEPLOYMENT_TYPE=""
ENV=""
BUILD_VARIABLE_FILE_NAME="./buildvar.conf"
SECRET_FILE_NAME="./buildsecvar.conf"
SHARED_PROPERTY_FILENAME=""
# Common variables
#echo $AWS_ACCESS_KEY_ID
# AWS_ACCESS_KEY_ID=""
# AWS_SECRET_ACCESS_KEY=""
# AWS_ACCOUNT_ID=""
# AWS_REGION=""
TAG=""
SEC_LIST=""
SECPS_LIST=""
ARG_SECPS_LIST=""
#COUNTER_LIMIT=12
if [ -z "$COUNTER_LIMIT" ]; then
COUNTER_LIMIT=12
fi
# Variables specific to ECS
#AWS_REPOSITORY=""
#AWS_ECS_CLUSTER=""
#AWS_ECS_SERVICE=""
#AWS_ECS_TASK_FAMILY=""
#AWS_ECS_CONTAINER_NAME=""
ECS_TAG=""
REVISION=""
ECS_TEMPLATE_TYPE="EC2"
task_def=""
CONTAINER_LOG_DRIVER="awslogs"
portcount=0
envcount=0
psenvcount=0
volcount=0
template=""
TEMPLATE_SKELETON_FILE="base_template_v2.json"
APP_IMAGE_NAME=""
DEPLOYCATEGORY=""
ECSCLI_ENVFILE="api.env"
# Variables specific to EBS
DOCKERRUN="Dockerrun.aws.json"
#EBS_EB_EXTENSTION_LOCATION=""
IMG_WITH_EBS_TAG=""
EBS_TEMPLATE_SKELETON_FILE="ebs_base_template_v3.json.template"
EBS_APPLICATION_NAME=""
EBS_APPVER=""
EBS_TAG=""
IMAGE=""
AWS_EBS_APPVER=""
#AWS_S3_BUCKET=""
AWS_S3_KEY=""
AWS_EB_ENV=""
EBS_TEMPLATE_FILE_NAME=""
#AWS_EBS_EB_DOCKERRUN_TEMPLATE_LOCATION=$(eval "echo \$${ENV}_AWS_EBS_EB_DOCKERRUN_TEMPLATE_LOCATION")
#AWS_EBS_DOCKERRUN_TEMPLATE=$(eval "echo \$${ENV}_AWS_EBS_DOCKERRUN_TEMPLATE")
#AWS_S3_KEY_LOCATION=""
ebsportcount=0
ebstemplate=""
#variable for cloud front
#AWS_S3_BUCKET=""
#AWS_S3_SOURCE_SYNC_PATH=""
CFCACHE="false"
# AWS_CLOUD_FRONT_ID=""
# Variables for Lambda
#AWS_LAMBDA_DEPLOY_TYPE=""
#AWS_LAMBDA_STAGE=""
# FUNCTIONS
# usage Function - provides information about how to execute the script
usage()
{
cat << EOF
usage: $0 options
This script need to be executed with below option.
OPTIONS:
-h Show this message
-d Deployment Type [ECS|EBS|CFRONT]
-e Environment [DEV|QA|PROD]
-t ECS Tag Name [mandatory if ECS ]
-v EBS version [mandatory if EBS deployment]
-i ECS Image name
-c cache option true [optional : value = true| false]i
-s Security file location GIT|AWS
-p ECS template type
-g Common property file which is uploaded to shared-properties folder
EOF
}
# log Function - Used to provide information of execution information with date and time
log()
{
echo "`date +'%D %T'` : $1"
}
# track_error function - validates whether the application execute without any error
track_error()
{
if [ $1 != "0" ]; then
log "$2 exited with error code $1"
log "completed execution IN ERROR at `date`"
exit $1
fi
}
# Function for AWS login
configure_aws_cli() {
aws --version
aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
aws configure set default.region $AWS_REGION
aws configure set default.output json
log "Configured AWS CLI."
}
# Function for private dcoker login
configure_docker_private_login() {
aws s3 cp "s3://appirio-platform-$ENV_CONFIG/services/common/dockercfg" ~/.dockercfg
}
# ECS Deployment Functions
ECS_push_ecr_image() {
echo "\n\n"
if [ -z "$APP_IMAGE_NAME" ];
then
log "ECS image follows the standard format"
else
log "ECS Image does not follow the standard format. Modifying the image and updating the ECS_TAG"
docker tag $APP_IMAGE_NAME:$ECS_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$AWS_REPOSITORY:$CIRCLE_BUILD_NUM
ECS_TAG=$CIRCLE_BUILD_NUM
fi
CHECK_ECR_EXIST=""
CHECK_ECR_EXIST=$(aws ecr describe-repositories --repository-names ${AWS_REPOSITORY} 2>&1)
if [ $? -ne 0 ]; then
if echo ${CHECK_ECR_EXIST} | grep -q RepositoryNotFoundException; then
echo "ECR repo does not exist -- creating repo"
aws ecr create-repository --repository-name $AWS_REPOSITORY
track_error $? "ECS ECR repo creation"
log "ECR repo created successfully."
else
echo ${CHECK_ECR_EXIST}
fi
else
echo "$AWS_REPOSITORY ECR repository already exists"
fi
log "Pushing Docker Image..."
eval $(aws ecr get-login --region $AWS_REGION --no-include-email)
docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$AWS_REPOSITORY:$ECS_TAG
track_error $? "ECS ECR image push"
log "Docker Image published\n\n"
}
ECSCLI_push_ecr_image() {
ECS_REPONAME=$1
IMAGE_NAME=$2
if [ -z "$IMAGE_NAME" ];
then
log "ECS image follows the standard format"
else
log "ECS image does not follow the standard format. Modifying the image and updating the ECS_TAG"
docker tag $IMAGE_NAME:$ECS_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECS_REPONAME:$CIRCLE_BUILD_NUM
ECS_TAG=$CIRCLE_BUILD_NUM
fi
log "Pushing Docker Image..."
eval $(aws ecr get-login --region $AWS_REGION --no-include-email)
docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECS_REPONAME:$ECS_TAG
track_error $? "ECS ECR image push"
log "Docker ECR Image published\n\n"
}
ECSCLI_update_env()
{
Buffer_seclist=$(echo $SEC_LIST | sed 's/,/ /g')
for listname in $Buffer_seclist;
do
local o=$IFS
IFS=$(echo -en "\n\b")
envvars=$( cat $listname.json | jq -r ' . ' | jq ' . | to_entries[] | { "name": .key , "value": .value } ' | jq -s . )
log "ECS env vars are fetched"
for s in $(echo $envvars | jq -c ".[]" ); do
#echo $envvars
varname=$(echo $s| jq -r ".name")
varvalue=$(echo $s| jq -r ".value")
envaddition "$varname" "$varvalue"
echo "$varname"="\"$varvalue\"" >>$ECSCLI_ENVFILE
done
IFS=$o
done
}
portmapping() {
hostport=$1
containerport=$2
containerprotocol=$3
template=$(echo $template | jq --argjson hostPort $hostport --argjson containerPort $containerport --arg protocol $containerprotocol --arg portcount $portcount '.containerDefinitions[0].portMappings[$portcount |tonumber] |= .+ { hostPort: $hostPort, containerPort: $containerPort, protocol: $protocol }')
let portcount=portcount+1
}
envaddition() {
#echo "envcount before " $envcount
envname=$1
envvalue=$2
#echo "env value before" $envvalue
set -f
template=$(echo $template | jq --arg name "$envname" --arg value "$envvalue" --arg envcount $envcount '.containerDefinitions[0].environment[$envcount |tonumber] |= .+ { name: $name, value: $value }')
set +f
let envcount=envcount+1
#echo "envcount after ---------" $envcount
#echo "envvalue after ---------" $envvalue
}
psenvaddition() {
#echo "psenvcount before " $psenvcount
envname=$1
envvalue=$2
#echo "env value before" $envvalue
set -f
template=$(echo $template | jq --arg name "$envname" --arg value "$envvalue" --arg psenvcount $psenvcount '.containerDefinitions[0].secrets[$psenvcount |tonumber] |= .+ { name: $name, valueFrom: $value }')
set +f
let psenvcount=psenvcount+1
#echo "psenvcount after ---------" $psenvcount
#echo "envvalue after ---------" $envvalue
}
logconfiguration() {
template=$(echo $template | jq --arg logDriver $CONTAINER_LOG_DRIVER '.containerDefinitions[0].logConfiguration.logDriver=$logDriver')
template=$(echo $template | jq --arg awslogsgroup "/aws/ecs/$AWS_ECS_CLUSTER" '.containerDefinitions[0].logConfiguration.options."awslogs-group"=$awslogsgroup')
template=$(echo $template | jq --arg awslogsregion $AWS_REGION '.containerDefinitions[0].logConfiguration.options."awslogs-region"=$awslogsregion')
template=$(echo $template | jq --arg awslogsstreamprefix $ENV '.containerDefinitions[0].logConfiguration.options."awslogs-stream-prefix"=$awslogsstreamprefix')
template=$(echo $template | jq 'del(.containerDefinitions[0].logConfiguration.options.KeyName)')
}
volumeupdate() {
volname=$1
sourcepath=$2
mountpath=$3
#mntpermission=$4
#echo $volname $sourcepath $mountpath $mntpermission
#volumes update
template=$(echo $template | jq --arg volname $volname --arg sourcepath $sourcepath --arg volcount $volcount '.volumes[$volcount |tonumber] |= .+ { name: $volname, host: { sourcePath: $sourcepath } }')
#mount point update
template=$(echo $template | jq --arg volname $volname --arg mountpath $mountpath --arg volcount $volcount '.containerDefinitions[0].mountPoints[$volcount |tonumber] |= .+ { sourceVolume: $volname, containerPath: $mountpath }')
let volcount=volcount+1
}
ECS_Container_HealthCheck_integ() {
HealthCheckCmd="$1"
template=$(echo $template | jq '.containerDefinitions[0].healthCheck.retries=3')
template=$(echo $template | jq '.containerDefinitions[0].healthCheck.timeout=15')
template=$(echo $template | jq '.containerDefinitions[0].healthCheck.interval=60')
template=$(echo $template | jq '.containerDefinitions[0].healthCheck.startPeriod=120')
template=$(echo $template | jq --arg HealthCheckCmd "$HealthCheckCmd" '.containerDefinitions[0].healthCheck.command=["CMD-SHELL",$HealthCheckCmd]')
}
ECS_Container_cmd_integ() {
ContainerCmd="$1"
template=$(echo $template | jq --arg ContainerCmd "$ContainerCmd" '.containerDefinitions[0].command=[$ContainerCmd]')
}
ECS_template_create_register() {
#Getting Template skeleton
#template=`aws ecs register-task-definition --generate-cli-skeleton`
template=$(cat $TEMPLATE_SKELETON_FILE)
#Updating ECS task def file
template=$(echo $template | jq --arg family $AWS_ECS_TASK_FAMILY '.family=$family')
log "ECS Task Family updated"
#taskrole and excution role has updated
if [ -z $AWS_ECS_TASK_ROLE_ARN ];
then
log "No ECS Task Role defined"
else
template=$(echo $template | jq --arg taskRoleArn arn:aws:iam::$AWS_ACCOUNT_ID:role/$AWS_ECS_TASK_ROLE_ARN '.taskRoleArn=$taskRoleArn')
fi
if [ -z $AWS_ECS_TASK_EXECUTION_ROLE_ARN ];
then
log "No ECS Task Execution Role defined"
else
template=$(echo $template | jq --arg executionRoleArn arn:aws:iam::$AWS_ACCOUNT_ID:role/$AWS_ECS_TASK_EXECUTION_ROLE_ARN '.executionRoleArn=$executionRoleArn')
fi
#Container Name update
template=$(echo $template | jq --arg name $AWS_ECS_CONTAINER_NAME '.containerDefinitions[0].name=$name')
log "ECS Container Name updated"
#Container Image Name update
template=$(echo $template | jq --arg image $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$AWS_REPOSITORY:$ECS_TAG '.containerDefinitions[0].image=$image')
log "ECR Image name updated"
#Container readonlyRootFilesystem
if [ -z $AWS_ECS_READONLY_ROOTFILESYSTEM ];
then
log "No ECS readonlyRootFilesystem defined. Going with default value as true"
AWS_ECS_READONLY_ROOTFILESYSTEM=true
template=$(echo $template | jq --argjson readonlyRootFilesystem $AWS_ECS_READONLY_ROOTFILESYSTEM '.containerDefinitions[0].readonlyRootFilesystem=$readonlyRootFilesystem')
else
template=$(echo $template | jq --argjson readonlyRootFilesystem $AWS_ECS_READONLY_ROOTFILESYSTEM '.containerDefinitions[0].readonlyRootFilesystem=$readonlyRootFilesystem')
fi
log "ECS readonlyRootFilesystem updated."
#Container Memory reservation
if [ -z $AWS_ECS_CONTAINER_MEMORY_RESERVATION ];
then
log "No ECS reserved memory defined. Going with default value 500 MB"
AWS_ECS_CONTAINER_MEMORY_RESERVATION="1000"
template=$(echo $template | jq --argjson memoryReservation $AWS_ECS_CONTAINER_MEMORY_RESERVATION '.containerDefinitions[0].memoryReservation=$memoryReservation')
else
template=$(echo $template | jq --argjson memoryReservation $AWS_ECS_CONTAINER_MEMORY_RESERVATION '.containerDefinitions[0].memoryReservation=$memoryReservation')
fi
log "ECS memory reservation updated."
#Container CPU reservation
if [ -z $AWS_ECS_CONTAINER_CPU ];
then
echo "No ECS container CPU defined. Going with default value 100"
AWS_ECS_CONTAINER_CPU=100
template=$(echo $template | jq --argjson cpu $AWS_ECS_CONTAINER_CPU '.containerDefinitions[0].cpu=$cpu')
else
template=$(echo $template | jq --argjson cpu $AWS_ECS_CONTAINER_CPU '.containerDefinitions[0].cpu=$cpu')
fi
log "ECS container CPU updated."
#Port Mapping
Buffer_portmap=$(echo $AWS_ECS_PORTS | sed 's/,/ /g')
for b1 in $Buffer_portmap;
do
hostport=$( echo $b1 | cut -d ':' -f 1 )
log "ECS host port: $hostport"
containerport=$( echo $b1 | cut -d ':' -f 2 )
log "ECS container port: $containerport"
protocolmapped=$( echo $b1 | cut -d ':' -f 3 )
log "ECS mapped protocol: $protocolmapped"
portmapping $hostport $containerport $protocolmapped
done
log "ECS container port mapping updated"
# Environment addition
Buffer_seclist=$(echo $SEC_LIST | sed 's/,/ /g')
for listname in $Buffer_seclist;
do
local o=$IFS
IFS=$(echo -en "\n\b")
envvars=$( cat $listname.json | jq -r ' . ' | jq ' . | to_entries[] | { "name": .key , "value": .value } ' | jq -s . )
log "vars are fetched"
for s in $(echo $envvars | jq -c ".[]" ); do
#echo $envvars
varname=$(echo $s| jq -r ".name")
varvalue=$(echo $s| jq -r ".value")
envaddition "$varname" "$varvalue"
done
IFS=$o
done
if [ -z $SECPS_LIST ];
then
log "No ps file provided"
else
Buffer_seclist=$(echo $SECPS_LIST | sed 's/,/ /g')
for listname in $Buffer_seclist;
do
local o=$IFS
IFS=$(echo -en "\n\b")
varpath=$( cat $listname.json | jq -r ' .ParmeterPathList[] ' )
#log "vars are fetched"
for k in $varpath;
do
echo $k
aws ssm get-parameters-by-path --path $k --query "Parameters[*].{Name:Name}" > paramnames.json
###paramnames=$(cat paramnames.json | jq -r .[].Name | rev | cut -d / -f 1 | rev)
for s in $(cat paramnames.json | jq -r .[].Name )
do
varname=$(echo $s | rev | cut -d / -f 1 | rev)
varvalue="arn:aws:ssm:$AWS_REGION:$AWS_ACCOUNT_ID:parameter$s"
psenvaddition "$varname" "$varvalue"
#echo "$varname" "$varvalue"
done
done
IFS=$o
done
fi
if [ -z $ARG_SECPS_LIST ];
then
log "No ps file provided"
else
Buffer_seclist=$(echo $ARG_SECPS_LIST | sed 's/,/ /g')
for listname in $Buffer_seclist;
do
local o=$IFS
IFS=$(echo -en "\n\b")
k=$listname
echo $k
aws ssm get-parameters-by-path --path $k --query "Parameters[*].{Name:Name}" > paramnames.json
###paramnames=$(cat paramnames.json | jq -r .[].Name | rev | cut -d / -f 1 | rev)
for s in $(cat paramnames.json | jq -r .[].Name )
do
varname=$(echo $s | rev | cut -d / -f 1 | rev)
varvalue="arn:aws:ssm:$AWS_REGION:$AWS_ACCOUNT_ID:parameter$s"
psenvaddition "$varname" "$varvalue"
#echo "$varname" "$varvalue"
done
IFS=$o
done
fi
log "Environment has updated"
# Log Configuration
logconfiguration
log "Log configuration has updated"
#volume update
if [ -z $AWS_ECS_VOLUMES ];
then
echo "No ECS volume mapping defined"
else
Buffer_volumes=$(echo $AWS_ECS_VOLUMES | sed 's/,/ /g')
for v1 in $Buffer_volumes;
do
volname=$( echo $v1 | cut -d ':' -f 1 )
sourcepath=$( echo $v1 | cut -d ':' -f 2 )
mountpath=$( echo $v1 | cut -d ':' -f 3 )
#mntpermission=$( echo $v1 | cut -d ':' -f 4 )
#volumeupdate $volname $sourcepath $mountpath $mntpermission
volumeupdate $volname $sourcepath $mountpath
done
log "ECS volumes are mapped"
fi
#Container health check update
if [ -z "$AWS_ECS_CONTAINER_HEALTH_CMD" ];
then
echo "No ECS container health check command defined"
else
ECS_Container_HealthCheck_integ "$AWS_ECS_CONTAINER_HEALTH_CMD"
fi
#Container command integration
if [ -z "$AWS_ECS_CONTAINER_CMD" ];
then
echo "No ECS container start-up command defined"
else
ECS_Container_cmd_integ "$AWS_ECS_CONTAINER_CMD"
fi
#updating data based on ECS deploy type
if [ "$ECS_TEMPLATE_TYPE" == "FARGATE" ]
then
#updating Network
ECS_NETWORKTYPE="awsvpc"
template=$(echo $template | jq --arg executionRoleArn arn:aws:iam::$AWS_ACCOUNT_ID:role/ecsTaskExecutionRole '.executionRoleArn=$executionRoleArn')
template=$(echo $template | jq --arg networkMode $ECS_NETWORKTYPE '.networkMode=$networkMode')
# Updating the compatibiltiy
#template=$(echo $template | jq --arg requiresCompatibilities EC2 '.requiresCompatibilities[0] |= .+ $requiresCompatibilities')
template=$(echo $template | jq --arg requiresCompatibilities FARGATE '.requiresCompatibilities[.requiresCompatibilities| length] |= .+ $requiresCompatibilities')
# Updating Fargate CPU
if [ -z $AWS_ECS_FARGATE_CPU ];
then
echo "No FARGATE CPU defined. Going with default value 1024"
AWS_ECS_FARGATE_CPU="1024"
template=$(echo $template | jq --arg cpu $AWS_ECS_FARGATE_CPU '.cpu=$cpu')
else
template=$(echo $template | jq --arg cpu $AWS_ECS_FARGATE_CPU '.cpu=$cpu')
fi
# Updating Fargate Memory
if [ -z $AWS_ECS_FARGATE_MEMORY ];
then
echo "No FARGATE memory defined. Going with default value 2048"
AWS_ECS_FARGATE_MEMORY="2048"
template=$(echo $template | jq --arg memory $AWS_ECS_FARGATE_MEMORY '.memory=$memory')
else
template=$(echo $template | jq --arg memory $AWS_ECS_FARGATE_MEMORY '.memory=$memory')
fi
else
#CONTAINER_CPU
ECS_NETWORKTYPE="bridge"
template=$(echo $template | jq --arg networkMode $ECS_NETWORKTYPE '.networkMode=$networkMode')
# Updating the compatibiltiy
template=$(echo $template | jq --arg requiresCompatibilities EC2 '.requiresCompatibilities[0] = $requiresCompatibilities')
fi
if [ -z "$template" ];
then
track_error 1 "Task Definition was not set by template variable"
exit 1
else
# echo "template values ------:" $template
if REVISION=$(aws ecs register-task-definition --cli-input-json "$template" | $JQ '.taskDefinition.taskDefinitionArn'); then
log "Revision: $REVISION"
else
track_error 1 "Task Def registration"
log "Failed to register task definition"
return 1
fi
fi
}
ECS_deploy_cluster() {
AWS_ECS_SERVICE=$1
#checking if cluster exists
CHECK_CLUSTER_EXIST=""
CHECK_CLUSTER_EXIST=$(aws ecs describe-clusters --cluster $AWS_ECS_CLUSTER | jq --raw-output 'select(.clusters[].clusterName != null ) | .clusters[].clusterName')
if [ -z $CHECK_CLUSTER_EXIST ];
then
echo "$AWS_ECS_CLUSTER cluster does not exist. Kindly check with DevOps team"
exit 1
else
echo "$AWS_ECS_CLUSTER cluster exists"
fi
#checking if service exists
CHECK_SERVICE_EXIST=""
CHECK_SERVICE_EXIST=$(aws ecs describe-services --service $AWS_ECS_SERVICE --cluster $AWS_ECS_CLUSTER | jq --raw-output 'select(.services[].status != null ) | .services[].status')
if [ -z $CHECK_SERVICE_EXIST ];
then
if [ "$ECS_TEMPLATE_TYPE" == "FARGATE" ];
then
echo "Fargate Service does not exist. Kindly check with DevOps team"
exit 1
else
echo "Service does not exist. Creating service"
aws ecs create-service --cluster $AWS_ECS_CLUSTER --service-name $AWS_ECS_SERVICE --task-definition $REVISION --desired-count 1
echo "Kindly work with DevOps team for routing"
fi
else
echo "ECS Service exists. Updating the service..."
update_result=$(aws ecs update-service --cluster $AWS_ECS_CLUSTER --service $AWS_ECS_SERVICE --task-definition $REVISION )
result=$(echo $update_result | $JQ '.service.taskDefinition' )
log $result
if [[ $result != $REVISION ]]; then
#echo "Error updating service."
track_error 1 "ECS updating service."
return 1
fi
echo "Updated service intialised successfully for deployment\n\n"
fi
return 0
}
check_service_status() {
AWS_ECS_SERVICE=$1
counter=0
sleep 60
servicestatus=`aws ecs describe-services --service $AWS_ECS_SERVICE --cluster $AWS_ECS_CLUSTER | $JQ '.services[].events[0].message'`
while [[ $servicestatus != *"steady state"* ]]
do
echo "Current event message : $servicestatus"
echo "Waiting for 15 sec to check the service status..."
sleep 15
servicestatus=`aws ecs describe-services --service $AWS_ECS_SERVICE --cluster $AWS_ECS_CLUSTER | $JQ '.services[].events[0].message'`
counter=`expr $counter + 1`
if [[ $counter -gt $COUNTER_LIMIT ]] ; then
echo "Service did not reach steady state with in 180 seconds. Please check the logs."
exit 1
fi
done
echo "$servicestatus"
}
validate_update_loggroup()
{
log_group_fetch=$(aws logs describe-log-groups --log-group-name-prefix /aws/ecs/$AWS_ECS_CLUSTER | jq -r .logGroups[].logGroupName | grep "^/aws/ecs/$AWS_ECS_CLUSTER$")
#echo $log_group_fetch
if [ -z $log_group_fetch ];
then
echo "\nLog group does not exist\n"
aws logs create-log-group --log-group-name /aws/ecs/$AWS_ECS_CLUSTER
track_error $? "aws log group"
else
echo "\nLog group exists\n"
fi
}
# EBS integration
ebsportmapping() {
echo "Port map called\n"
containerport=$1
hostport=$2
if [ -z $hostport ]
then
ebstemplate=$(echo $ebstemplate | jq --arg containerPort $containerport --arg ebsportcount $ebsportcount '.Ports[$ebsportcount |tonumber] |= .+ { ContainerPort: $containerPort }')
else
ebstemplate=$(echo $ebstemplate | jq --arg hostPort $hostport --arg containerPort $containerport --arg ebsportcount $ebsportcount '.Ports[$ebsportcount |tonumber] |= .+ { HostPort: $hostPort, ContainerPort: $containerPort }')
fi
let ebsportcount=ebsportcount+1
}
EBS_push_docker_image() {
echo "Pushing Docker image: ${IMAGE}"
IMAGE="${DOCKER_REGISTRY_NAME}/${IMG_WITH_EBS_TAG}"
docker push $IMAGE
track_error $? "Docker push failed."
}
creating_updating_ebs_docker_json() {
echo "Updating S3 auth bucket name"
sed -i.bak -e "s/@AWSS3AUTHBUCKET@/appirio-platform-$ENV_CONFIG/g" $EBS_TEMPLATE_SKELETON_FILE
rm ${EBS_TEMPLATE_SKELETON_FILE}.bak
#EBS Port Mapping
ebstemplate=$(cat $EBS_TEMPLATE_SKELETON_FILE)
if [ -z $AWS_EBS_PORTS ];
then
echo "No container port is defined. Configuring default 8080 port"
ebsportmapping 8080
else
Buffer_portmap=$(echo $AWS_EBS_PORTS | sed 's/,/ /g')
for ebsportbuf in $Buffer_portmap;
do
containerport=$( echo $ebsportbuf | cut -d ':' -f 1 )
if [[ $ebsportbuf = *:* ]]; then
hostport=$( echo $ebsportbuf | cut -d ':' -f 2 )
fi
ebsportmapping $containerport $hostport
done
fi
echo "$ebstemplate" > $EBS_TEMPLATE_SKELETON_FILE
log "port mapping updated"
if [ -z "$EBS_EB_EXTENSTION_LOCATION" ];
then
cat $EBS_TEMPLATE_SKELETON_FILE | sed -e "s/@IMAGE@/${IMG_WITH_EBS_TAG}/g" > $DOCKERRUN
echo "Pushing $DOCKERRUN as ${IMG_WITH_EBS_TAG} to S3: ${AWS_S3_BUCKET}/${AWS_S3_KEY}"
aws s3api put-object --bucket "${AWS_S3_BUCKET}" --key "${AWS_S3_KEY}" --body $DOCKERRUN
track_error $? "aws s3api put-object failed."
else
cat $EBS_TEMPLATE_SKELETON_FILE | sed -e "s/@IMAGE@/${IMG_WITH_EBS_TAG}/g" > $DOCKERRUN
cp -rvf $EBS_EB_EXTENSTION_LOCATION/.ebextensions .
jar cMf ${IMG_WITH_EBS_TAG}.zip $DOCKERRUN .ebextensions
echo "Pushing ${IMG_WITH_EBS_TAG}.zip to S3: ${AWS_S3_BUCKET}/${AWS_S3_KEY}"
aws s3api put-object --bucket "${AWS_S3_BUCKET}" --key "${AWS_S3_KEY}" --body ${IMG_WITH_EBS_TAG}.zip
track_error $? "aws s3api put-object failed."
fi
}
creating_updating_EBS_appversion() {
echo "Creating new application version $AWS_EBS_APPVER in ${AWS_EBS_APPLICATION_NAME} from s3:${AWS_S3_BUCKET}/${AWS_S3_KEY}"
aws elasticbeanstalk create-application-version --application-name $AWS_EBS_APPLICATION_NAME --version-label $AWS_EBS_APPVER --source-bundle S3Bucket="$AWS_S3_BUCKET",S3Key="$AWS_S3_KEY"
track_error $? "aws elasticbeanstalk create-application-version failed."
echo "Updating elastic beanstalk environment ${AWS_EB_ENV} with the version ${AWS_EBS_APPVER}."
# assumes beanstalk app for this service has already been created and configured
aws elasticbeanstalk update-environment --environment-name $AWS_EBS_ENV_NAME --version-label $AWS_EBS_APPVER
track_error $? "aws elasticbeanstalk update-environment failed."
}
#CloudFront deployment
deploy_s3bucket() {
echo -e "application/font-woff\t\t\t\twoff2" >> /etc/mime.types
echo -e "application/font-sfnt\t\t\t\tttf" >> /etc/mime.types
echo -e "application/json\t\t\t\tmap" >> /etc/mime.types
cat /etc/mime.types | grep -i woff
cat /etc/mime.types | grep -i ico
cat /etc/mime.types | grep -i map
cat /etc/mime.types | grep -i ttf
if [ "$CFCACHE" = "true" ]; then
# caching is enabled, so set the cache control's max age
S3_CACHE_OPTIONS="--cache-control max-age=0,s-maxage=86400"
echo "*** Deploying with Cloudfront Cache enabled ***"
else
# caching is disabled, so set the cache control to never cache
S3_CACHE_OPTIONS="--cache-control private,no-store,no-cache,must-revalidate,max-age=0"
echo "*** Deploying with Cloudfront Cache disabled ***"
fi
S3_OPTIONS="--exclude '*.txt' --exclude '*.js' --exclude '*.css'"
echo aws s3 sync $AWS_S3_SOURCE_SYNC_PATH s3://${AWS_S3_BUCKET} ${S3_CACHE_OPTIONS} ${S3_OPTIONS}
eval "aws s3 sync --dryrun $AWS_S3_SOURCE_SYNC_PATH s3://${AWS_S3_BUCKET} ${S3_CACHE_OPTIONS} ${S3_OPTIONS}"
result=`eval "aws s3 sync $AWS_S3_SOURCE_SYNC_PATH s3://${AWS_S3_BUCKET} ${S3_CACHE_OPTIONS} ${S3_OPTIONS}"`
if [ $? -eq 0 ]; then
echo "All html, font, image, map and media files are Deployed without gzip encoding!"
else
echo "Deployment Failed - $result"
exit 1
fi
# S3_OPTIONS="--exclude '*' --include '*.txt' --include '*.js' --include '*.css' --content-encoding gzip"
searchpath=${AWS_S3_SOURCE_SYNC_PATH}
lengthofsearchpath=$(echo ${#searchpath})
lengthofsearchpath=$((lengthofsearchpath+1))
for syncfilepath in $(find ${searchpath} -name '*.js' -o -name '*.txt' -o -name '*.css');
do
echo "$syncfilepath"
uploadpath=$(echo $syncfilepath | cut -b ${lengthofsearchpath}-)
echo $uploadpath
getformatdetails=$(file ${syncfilepath})
if [[ $getformatdetails == *"ASCII"* ]] || [[ $getformatdetails == *"UTF"* ]] || [[ $getformatdetails == *"empty"* ]];
then
echo "file format is ASCII and skipping gzip option"
S3_OPTIONS=""
else
echo $getformatdetails
S3_OPTIONS="--content-encoding gzip"
fi
echo aws s3 cp --dryrun $syncfilepath s3://${AWS_S3_BUCKET}${uploadpath} ${S3_CACHE_OPTIONS} ${S3_OPTIONS}
eval "aws s3 cp --dryrun $syncfilepath s3://${AWS_S3_BUCKET}${uploadpath} ${S3_CACHE_OPTIONS} ${S3_OPTIONS}"
result=`eval "aws s3 cp $syncfilepath s3://${AWS_S3_BUCKET}${uploadpath} ${S3_CACHE_OPTIONS} ${S3_OPTIONS}"`
if [ $? -eq 0 ]; then
echo "File Deployed!"
else
echo "Deployment Failed - $result"
exit 1
fi
done;
}
check_invalidation_status() {
INVALIDATE_ID=$1
counter=0
echo "invalidating cache with ID $INVALIDATE_ID"
sleep 60
invalidatestatus=`aws cloudfront get-invalidation --distribution-id $AWS_CLOUD_FRONT_ID --id $INVALIDATE_ID | $JQ '.Invalidation.Status'`
while [[ $invalidatestatus != *"Completed"* ]]
do
echo $invalidatestatus
echo "Waiting for 15 sec and try to check the invalidation status..."
sleep 15
invalidatestatus=`aws cloudfront get-invalidation --distribution-id $AWS_CLOUD_FRONT_ID --id $INVALIDATE_ID | $JQ '.Invalidation.Status'`
counter=`expr $counter + 1`
if [[ $counter -gt $COUNTER_LIMIT ]] ; then
echo "Invalidation does not complete with in 180 seconds. Please check the GUI mode."
exit 1
fi
done
echo "Invalidation completed"
}
invalidate_cf_cache()
{
#if [ "$CFCACHE" = "true" ]; then
if [ -z $AWS_CLOUD_FRONT_ID ]; then
echo "Based on header applicaiton has invalidated"
echo "Skipped which is based on AWS cloudfront ID.Kindly raise request to configure cloud front ID in deployment configuration"
else
#aws cloudfront create-invalidation --distribution-id $AWS_CLOUD_FRONT_ID --paths '/*'
INVALIDATE_ID=`aws cloudfront create-invalidation --distribution-id $AWS_CLOUD_FRONT_ID --paths '/*' | $JQ '.Invalidation.Id'`
check_invalidation_status "$INVALIDATE_ID"
fi
#fi
}
download_envfile()
{
Buffer_seclist=$(echo $SEC_LIST | sed 's/,/ /g' )
for listname in $Buffer_seclist;
do
aws s3 cp s3://tc-platform-${ENV_CONFIG}/securitymanager/$listname.json .
track_error $? "$listname.json download"
jq 'keys[]' $listname.json
track_error $? "$listname.json"
#cp $HOME/buildscript/securitymanager/$listname.json.enc .
#SECPASSWD=$(eval "echo \$${listname}")
#openssl enc -aes-256-cbc -d -md MD5 -in $listname.json.enc -out $listname.json -k $SECPASSWD
done
}
download_psfile()
{
Buffer_seclist=$(echo $SECPS_LIST | sed 's/,/ /g' )
for listname in $Buffer_seclist;
do
aws s3 cp s3://tc-platform-${ENV_CONFIG}/securitymanager/$listname.json .
track_error $? "$listname.json download"
jq 'keys[]' $listname.json
track_error $? "$listname.json"
done
}
decrypt_fileenc()
{
Buffer_seclist=$(echo $SEC_LIST | sed 's/,/ /g' )
for listname in $Buffer_seclist;
do
#aws s3 cp s3://tc-platform-dev/securitymanager/$listname.json .
#cp $HOME/buildscript/securitymanager/$listname.json.enc .
SECPASSWD=$(eval "echo \$${listname}")
openssl enc -aes-256-cbc -d -md MD5 -in $listname.json.enc -out $listname.json -k $SECPASSWD
done
}
uploading_envvar()
{
Buffer_seclist=$(echo $SEC_LIST | sed 's/,/ /g')
for listname in $Buffer_seclist;
do
# for envappvar in $( cat $listname.json | jq -r ' . ' | jq ' . | to_entries | map(select(.key | test("AWS.") ) ) | from_entries' | jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]" ); do
# export $envappvar
# done
o=$IFS
IFS=$(echo -en "\n\b")
envvars=$( cat $listname.json | jq -r ' .awsdeployvar ' | jq ' . | to_entries[] | { "name": .key , "value": .value } ' | jq -s . )
for s in $(echo $envvars | jq -c ".[]" ); do
#echo $envvars
varname=$(echo $s| jq -r ".name")
varvalue=$(echo $s| jq -r ".value")
export "$varname"="$varvalue"
done
IFS=$o
done
}
configure_Lambda_template()
{
if [ "$AWS_LAMBDA_DEPLOY_TYPE" == "SLS" ]
then
mkdir -p /home/circleci/project/config
if [ -z $SEC_LIST ];
then
log "No ps path provided"
else
Buffer_seclist=$(echo $SEC_LIST | sed 's/,/ /g')
#envvars=$( cat $listname.json | jq -c ' .app_var ')
for listname in $Buffer_seclist;
do
local o=$IFS
IFS=$(echo -en "\n\b")
envvars=$( cat $listname.json | jq -c ' . ')
echo "$envvars" > /home/circleci/project/config/$AWS_LAMBDA_STAGE.json
sed -i 's/\\n/\\\\n/g' /home/circleci/project/config/$AWS_LAMBDA_STAGE.json
IFS=$o
done
fi
if [ -z $ARG_SECPS_LIST ];
then
log "No ps path provided"
else
Buffer_seclist=$(echo $ARG_SECPS_LIST | sed 's/,/ /g')
for listname in $Buffer_seclist;
do
local o=$IFS
IFS=$(echo -en "\n\b")
k=$listname
echo $k
aws ssm get-parameters-by-path --with-decryption --path $k --query "Parameters[*].{Name:Name, Value:Value}" >fetched_parameters.json
cat fetched_parameters.json | jq -r ' . |= (map({ (.Name): .Value }) | add)' | sed -e "s~$k/~~" >paramwithvalue.json
envvars=$( cat paramwithvalue.json | jq -c ' . ')
echo "$envvars" > /home/circleci/project/config/$AWS_LAMBDA_STAGE.json
sed -i 's/\\n/\\\\n/g' /home/circleci/project/config/$AWS_LAMBDA_STAGE.json
###paramnames=$(cat paramnames.json | jq -r .[].Name | rev | cut -d / -f 1 | rev)
IFS=$o
done
fi
fi
}
deploy_lambda_package()
{
# sls deploy
if [ "$AWS_LAMBDA_DEPLOY_TYPE" == "SLS" ]
then
echo "Welcome to lambda SLS deploy"
sls deploy --stage $AWS_LAMBDA_STAGE
fi
}
# decrypt_aws_sys_parameter()
# {
# for future implmentation.
# }
# Input Collection and validation
input_parsing_validation()
{
while getopts .d:h:i:e:l:j:t:v:s:p:g:c:m:. OPTION
do
case $OPTION in
d)
DEPLOYMENT_TYPE=$OPTARG
;;
h)
usage
exit 1
;;
i)
APP_IMAGE_NAME=$OPTARG
;;
e)
ENV=$OPTARG
;;
l)
SECPS_LIST=$OPTARG
;;
j)
ARG_SECPS_LIST=$OPTARG
;;
t)
TAG=$OPTARG
;;
c)
CFCACHE=$OPTARG
;;
v)
EBS_APPVER=$OPTARG
;;
s)
SEC_LIST=$OPTARG
;;
p)
ECS_TEMPLATE_TYPE=$OPTARG
;;
g)
SHARED_PROPERTY_FILENAME=$OPTARG
;;
m)
DEPLOYCATEGORY=$OPTARG
;;
?)
log "additional param required"
usage
exit
;;
esac
done
if [ -z $DEPLOYMENT_TYPE ] || [ -z $ENV ] ;
then
log "Param validation error"
usage
exit 1
fi
log "ENV : $ENV"
log "DEPLOYMENT_TYPE : $DEPLOYMENT_TYPE"
log "app variable list : $SEC_LIST"
ENV_CONFIG=`echo "$ENV" | tr '[:upper:]' '[:lower:]'`
#Validating AWS configuration
#Getting Deployment varaible only
# AWS_ACCESS_KEY_ID=$(eval "echo \$${ENV}_AWS_ACCESS_KEY_ID")
# AWS_SECRET_ACCESS_KEY=$(eval "echo \$${ENV}_AWS_SECRET_ACCESS_KEY")
# AWS_ACCOUNT_ID=$(eval "echo \$${ENV}_AWS_ACCOUNT_ID")
# AWS_REGION=$(eval "echo \$${ENV}_AWS_REGION")
# if [ -z $AWS_ACCESS_KEY_ID ] || [ -z $AWS_SECRET_ACCESS_KEY ] || [ -z $AWS_ACCOUNT_ID ] || [ -z $AWS_REGION ];
# then
# log "AWS Secret Parameters are not configured in circleci/environment"
# usage
# exit 1
# else
# configure_aws_cli
# #aws configure list
# fi
download_envfile
if [ -z $SECPS_LIST ];
then
log "No secret parameter file list provided"
else
download_psfile
fi
#decrypt_fileenc
#uploading_envvar
#Validating parameter based on Deployment type
#ECS parameter validation
if [ "$DEPLOYMENT_TYPE" == "ECS" ]
then