Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a39928e

Browse files
NkumarNkumar
Nkumar
authored and
Nkumar
committedJul 3, 2018
base template, changes to ms
1 parent efd7015 commit a39928e

File tree

2 files changed

+672
-0
lines changed

2 files changed

+672
-0
lines changed
 

‎base_template_v2.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"family": "",
3+
"taskRoleArn": "arn:aws:iam::811668436784:role/ecsTaskExecutionRole",
4+
"executionRoleArn": "arn:aws:iam::811668436784:role/ecsTaskExecutionRole",
5+
"networkMode": "host",
6+
"containerDefinitions": [
7+
{
8+
"name": "",
9+
"image": "",
10+
"cpu": 100,
11+
"memoryReservation": 500,
12+
"portMappings": [],
13+
"essential": true,
14+
"environment": [],
15+
"mountPoints": [],
16+
"volumesFrom": [],
17+
"logConfiguration": {
18+
"logDriver": "gelf",
19+
"options": {
20+
"KeyName": ""
21+
}
22+
}
23+
}
24+
],
25+
"volumes": [],
26+
"placementConstraints": [],
27+
"requiresCompatibilities": [
28+
"EC2"
29+
]
30+
}

‎master_deply_v4.sh

Lines changed: 642 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,642 @@
1+
#!/bin/bash
2+
3+
4+
#Variable Declaration
5+
JQ="jq --raw-output --exit-status"
6+
DEPLOYMENT_TYPE=""
7+
ENV=""
8+
BUILD_VARIABLE_FILE_NAME="./buildvar.conf"
9+
SECRET_FILE_NAME="./buildsecvar.conf"
10+
SHARED_PROPERTY_FILENAME=""
11+
12+
#Common Varibles
13+
AWS_ACCESS_KEY_ID=""
14+
AWS_SECRET_ACCESS_KEY=""
15+
AWS_ACCOUNT_ID=""
16+
AWS_REGION=""
17+
TAG=""
18+
SEC_LIST=""
19+
COUNTER_LIMIT=12
20+
21+
#Varibles specific to ECS
22+
AWS_REPOSITORY=""
23+
AWS_ECS_CLUSTER=""
24+
AWS_ECS_SERVICE=""
25+
AWS_ECS_TASK_FAMILY=""
26+
AWS_ECS_CONTAINER_NAME=""
27+
ECS_TAG=""
28+
REVISION=""
29+
ECS_TEMPLATE_TYPE="EC2"
30+
task_def=""
31+
CONTAINER_LOG_DRIVER="awslogs"
32+
portcount=0
33+
envcount=0
34+
volcount=0
35+
template=""
36+
TEMPLATE_SKELETON_FILE="base_template_v2.json"
37+
38+
#variable specific to EBS
39+
EBS_APPLICATION_NAME=""
40+
EBS_APPVER=""
41+
EBS_TAG=""
42+
IMAGE=""
43+
AWS_EBS_APPVER=""
44+
AWS_S3_BUCKET=""
45+
AWS_S3_KEY=""
46+
AWS_EB_ENV=""
47+
EBS_TEMPLATE_FILE_NAME=""
48+
AWS_EBS_EB_DOCKERRUN_TEMPLATE_LOCATION=$(eval "echo \$${ENV}_AWS_EBS_EB_DOCKERRUN_TEMPLATE_LOCATION")
49+
AWS_EBS_DOCKERRUN_TEMPLATE=$(eval "echo \$${ENV}_AWS_EBS_DOCKERRUN_TEMPLATE")
50+
AWS_S3_KEY_LOCATION=""
51+
52+
#variable for cloud front
53+
AWS_S3_BUCKET=""
54+
AWS_S3_SOURCE_SYNC_PATH=""
55+
CFCACHE="true"
56+
57+
#FUNCTIONS
58+
#usage Function - provides information like how to execute the script
59+
usage()
60+
{
61+
cat << EOF
62+
usage: $0 options
63+
64+
This script need to be executed with below option.
65+
66+
OPTIONS:
67+
-h Show this message
68+
-d Deployment Type [ECS|EBS|CFRONT]
69+
-e Environment [DEV|QA|PROD]
70+
-t ECS Tag Name [mandatatory if ECS ]
71+
-v EBS version [mandatatory if EBS deployment]
72+
-c cache option true [optional : value = true| false]i
73+
-s Security file location GIT|AWS
74+
-p ECS template type
75+
-g Enter common property file which has uploaded in shared-properties folder
76+
EOF
77+
}
78+
#log Function - Used to provide information of execution information with date and time
79+
log()
80+
{
81+
echo "`date +'%D %T'` : $1"
82+
}
83+
#track_error function validates whether the application execute without any error
84+
85+
track_error()
86+
{
87+
if [ $1 != "0" ]; then
88+
log "$2 exited with error code $1"
89+
log "completed execution IN ERROR at `date`"
90+
exit $1
91+
fi
92+
93+
}
94+
95+
96+
#Function for aws login
97+
98+
configure_aws_cli() {
99+
aws --version
100+
aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
101+
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
102+
aws configure set default.region $AWS_REGION
103+
aws configure set default.output json
104+
log "Configured AWS CLI."
105+
}
106+
#Function for private dcoker login
107+
configure_docker_private_login() {
108+
aws s3 cp "s3://appirio-platform-$ENV_CONFIG/services/common/dockercfg" ~/.dockercfg
109+
}
110+
111+
#ECS Deployment Functions
112+
113+
ECS_push_ecr_image() {
114+
log "Pushing Docker Image..."
115+
eval $(aws ecr get-login --region $AWS_REGION --no-include-email)
116+
docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$AWS_REPOSITORY:$ECS_TAG
117+
track_error $? "ECS ECR image push"
118+
log "Docker Image published."
119+
}
120+
121+
#================
122+
portmapping() {
123+
hostport=$1
124+
containerport=$2
125+
containerprotocol=$3
126+
127+
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 }')
128+
let portcount=portcount+1
129+
130+
}
131+
#=============================
132+
133+
134+
envaddition() {
135+
#echo "envcount before " $envcount
136+
137+
envname=$1
138+
envvalue=$2
139+
#echo "env value before" $envvalue
140+
template=$(echo $template | jq --arg name "$envname" --arg value "$envvalue" --arg envcount $envcount '.containerDefinitions[0].environment[$envcount |tonumber] |= .+ { name: $name, value: $value }')
141+
142+
let envcount=envcount+1
143+
#echo "envcount after ---------" $envcount
144+
#echo "envvalue after ---------" $envvalue
145+
}
146+
#=========================
147+
logconfiguration() {
148+
template=$(echo $template | jq --arg logDriver $CONTAINER_LOG_DRIVER '.containerDefinitions[0].logConfiguration.logDriver=$logDriver')
149+
template=$(echo $template | jq --arg awslogsgroup "/aws/ecs/$AWS_ECS_CLUSTER" '.containerDefinitions[0].logConfiguration.options."awslogs-group"=$awslogsgroup')
150+
template=$(echo $template | jq --arg awslogsregion $AWS_REGION '.containerDefinitions[0].logConfiguration.options."awslogs-region"=$awslogsregion')
151+
template=$(echo $template | jq --arg awslogsstreamprefix $ENV '.containerDefinitions[0].logConfiguration.options."awslogs-stream-prefix"=$awslogsstreamprefix')
152+
template=$(echo $template | jq 'del(.containerDefinitions[0].logConfiguration.options.KeyName)')
153+
}
154+
#=============================================
155+
volumeupdate() {
156+
volname=$1
157+
sourcepath=$2
158+
mountpath=$3
159+
#mntpermission=$4
160+
#echo $volname $sourcepath $mountpath $mntpermission
161+
#volumes update
162+
template=$(echo $template | jq --arg volname $volname --arg sourcepath $sourcepath --arg volcount $volcount '.volumes[$volcount |tonumber] |= .+ { name: $volname, host: { sourcePath: $sourcepath } }')
163+
#mount point update
164+
template=$(echo $template | jq --arg volname $volname --arg mountpath $mountpath --arg volcount $volcount '.containerDefinitions[0].mountPoints[$volcount |tonumber] |= .+ { sourceVolume: $volname, containerPath: $mountpath }')
165+
166+
let volcount=volcount+1
167+
}
168+
#============================================
169+
170+
171+
ECS_template_create_register() {
172+
173+
#Getting Template skeleton
174+
#template=`aws ecs register-task-definition --generate-cli-skeleton`
175+
template=$(cat $TEMPLATE_SKELETON_FILE)
176+
177+
#Updating ECS task def file
178+
template=$(echo $template | jq --arg family $AWS_ECS_TASK_FAMILY '.family=$family')
179+
log "Family updated"
180+
181+
#taskrole and excution role has updated
182+
template=$(echo $template | jq --arg taskRoleArn arn:aws:iam::$AWS_ACCOUNT_ID:role/ecsTaskExecutionRole '.taskRoleArn=$taskRoleArn')
183+
template=$(echo $template | jq --arg executionRoleArn arn:aws:iam::$AWS_ACCOUNT_ID:role/ecsTaskExecutionRole '.executionRoleArn=$executionRoleArn')
184+
185+
#Container Name update
186+
template=$(echo $template | jq --arg name $AWS_ECS_CONTAINER_NAME '.containerDefinitions[0].name=$name')
187+
log "Container Name updated"
188+
189+
#Container Image Name update
190+
template=$(echo $template | jq --arg image $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$AWS_REPOSITORY:$ECS_TAG '.containerDefinitions[0].image=$image')
191+
log "Image name updated"
192+
193+
#Container Memory reservation
194+
if [ -z $AWS_ECS_CONTAINER_MEMORY_RESERVATION ];
195+
then
196+
log "No reseveed memory defined . Going with default value 500 MB"
197+
AWS_ECS_CONTAINER_MEMORY_RESERVATION="1000"
198+
template=$(echo $template | jq --argjson memoryReservation $AWS_ECS_CONTAINER_MEMORY_RESERVATION '.containerDefinitions[0].memoryReservation=$memoryReservation')
199+
else
200+
template=$(echo $template | jq --argjson memoryReservation $AWS_ECS_CONTAINER_MEMORY_RESERVATION '.containerDefinitions[0].memoryReservation=$memoryReservation')
201+
fi
202+
log "Memory reservation updated"
203+
204+
#Port Mapping
205+
Buffer_portmap=$(echo $AWS_ECS_PORTS | sed 's/,/ /g')
206+
for b1 in $Buffer_portmap;
207+
do
208+
hostport=$( echo $b1 | cut -d ':' -f 1 )
209+
containerport=$( echo $b1 | cut -d ':' -f 2 )
210+
protocolmapped=$( echo $b1 | cut -d ':' -f 3 )
211+
portmapping $hostport $containerport $protocolmapped
212+
done
213+
log "port mapping updated"
214+
# Environment addition
215+
Buffer_seclist=$(echo $SEC_LIST | sed 's/,/ /g')
216+
for listname in $Buffer_seclist;
217+
do
218+
local o=$IFS
219+
IFS=$(echo -en "\n\b")
220+
envvars=$( cat $listname.json | jq -r ' . ' | jq ' . | to_entries[] | { "name": .key , "value": .value } ' | jq ' select(.name | test("AWS.") | not ) ' | jq -s . )
221+
log "vars are fetched"
222+
for s in $(echo $envvars | jq -c ".[]" ); do
223+
#echo $envvars
224+
varname=$(echo $s| jq -r ".name")
225+
varvalue=$(echo $s| jq -r ".value")
226+
envaddition "$varname" "$varvalue"
227+
done
228+
IFS=$o
229+
done
230+
231+
log "environment has updated"
232+
# Log Configuration
233+
logconfiguration
234+
log "log configuration has updated"
235+
236+
#volume update
237+
if [ -z $AWS_ECS_VOLUMES ];
238+
then
239+
echo "No volume mapping defined"
240+
else
241+
Buffer_volumes=$(echo $AWS_ECS_VOLUMES | sed 's/,/ /g')
242+
for v1 in $Buffer_volumes;
243+
do
244+
volname=$( echo $v1 | cut -d ':' -f 1 )
245+
sourcepath=$( echo $v1 | cut -d ':' -f 2 )
246+
mountpath=$( echo $v1 | cut -d ':' -f 3 )
247+
#mntpermission=$( echo $v1 | cut -d ':' -f 4 )
248+
#volumeupdate $volname $sourcepath $mountpath $mntpermission
249+
volumeupdate $volname $sourcepath $mountpath
250+
done
251+
log "volumes are mapped"
252+
fi
253+
254+
#updating data based on ECS deploy type
255+
if [ "$ECS_TEMPLATE_TYPE" == "FARGATE" ]
256+
then
257+
#updating Network
258+
ECS_NETWORKTYPE="awsvpc"
259+
template=$(echo $template | jq --arg networkMode $ECS_NETWORKTYPE '.networkMode=$networkMode')
260+
# Updating the compatibiltiy
261+
template=$(echo $template | jq --arg requiresCompatibilities EC2 '.requiresCompatibilities[0] |= .+ $requiresCompatibilities')
262+
template=$(echo $template | jq --arg requiresCompatibilities FARGATE '.requiresCompatibilities[.requiresCompatibilities| length] |= .+ $requiresCompatibilities')
263+
# Updating Fargate CPU
264+
if [ -z $AWS_ECS_FARGATE_CPU ];
265+
then
266+
echo "No FARGATE cpu defined . Going with default value 1024"
267+
AWS_ECS_FARGATE_CPU="1024"
268+
template=$(echo $template | jq --argjson cpu $AWS_ECS_FARGATE_CPU '.cpu=$cpu')
269+
else
270+
template=$(echo $template | jq --argjson cpu $AWS_ECS_FARGATE_CPU '.cpu=$cpu')
271+
fi
272+
# Updating Fargate Memory
273+
if [ -z $AWS_ECS_FARGATE_MEMORY ];
274+
then
275+
echo "No FARGATE memory defined . Going with default value 2048"
276+
AWS_ECS_FARGATE_MEMORY="2048"
277+
template=$(echo $template | jq --argjson memory $FARGATE_MEMORY '.memory=$memory')
278+
else
279+
template=$(echo $template | jq --argjson memory $FARGATE_MEMORY '.memory=$memory')
280+
fi
281+
else
282+
#CONTAINER_CPU
283+
ECS_NETWORKTYPE="bridge"
284+
template=$(echo $template | jq --arg networkMode $ECS_NETWORKTYPE '.networkMode=$networkMode')
285+
#Container Memory reservation
286+
if [ -z $AWS_ECS_CONTAINER_CPU ];
287+
then
288+
echo "No cpu defined . Going with default value 100"
289+
AWS_ECS_CONTAINER_CPU=100
290+
template=$(echo $template | jq --argjson cpu $AWS_ECS_CONTAINER_CPU '.containerDefinitions[0].cpu=$cpu')
291+
else
292+
template=$(echo $template | jq --argjson cpu $AWS_ECS_CONTAINER_CPU '.containerDefinitions[0].cpu=$cpu')
293+
fi
294+
295+
# Updating the compatibiltiy
296+
template=$(echo $template | jq --arg requiresCompatibilities EC2 '.requiresCompatibilities[0] = $requiresCompatibilities')
297+
fi
298+
if [ -z "$template" ];
299+
then
300+
track_error 1 "Task Def has not set by template variable"
301+
else
302+
echo "template values ------:" $template
303+
if REVISION=$(aws ecs register-task-definition --cli-input-json "$template" | $JQ '.taskDefinition.taskDefinitionArn'); then
304+
log "Revision: $REVISION"
305+
else
306+
track_error 1 "Task Def registration"
307+
log "Failed to register task definition"
308+
return 1
309+
fi
310+
fi
311+
}
312+
313+
ECS_deploy_cluster() {
314+
315+
AWS_ECS_SERVICE=$1
316+
update_result=$(aws ecs update-service --cluster $AWS_ECS_CLUSTER --service $AWS_ECS_SERVICE --task-definition $REVISION )
317+
result=$(echo $update_result | $JQ '.service.taskDefinition' )
318+
log $result
319+
if [[ $result != $REVISION ]]; then
320+
#echo "Error updating service."
321+
track_error 1 "ECS updating service."
322+
return 1
323+
fi
324+
325+
echo "Update service intialised successfully for deployment"
326+
return 0
327+
}
328+
329+
check_service_status() {
330+
AWS_ECS_SERVICE=$1
331+
counter=0
332+
sleep 60
333+
servicestatus=`aws ecs describe-services --service $AWS_ECS_SERVICE --cluster $AWS_ECS_CLUSTER | $JQ '.services[].events[0].message'`
334+
while [[ $servicestatus != *"steady state"* ]]
335+
do
336+
echo "Current event message : $servicestatus"
337+
echo "Waiting for 15 sec to check the service status...."
338+
sleep 15
339+
servicestatus=`aws ecs describe-services --service $AWS_ECS_SERVICE --cluster $AWS_ECS_CLUSTER | $JQ '.services[].events[0].message'`
340+
counter=`expr $counter + 1`
341+
if [[ $counter -gt $COUNTER_LIMIT ]] ; then
342+
echo "Service does not reach steady state with in 180 seconds. Please check"
343+
exit 1
344+
fi
345+
done
346+
echo "$servicestatus"
347+
}
348+
349+
# EBS integration
350+
351+
352+
EBS_push_docker_image() {
353+
354+
echo "pushing docker image: ${IMAGE}"
355+
docker push $IMAGE
356+
track_error $? "docker push failed."
357+
358+
}
359+
360+
creating_updating_ebs_docker_json() {
361+
cd $AWS_EBS_EB_DOCKERRUN_TEMPLATE_LOCATION
362+
cat $AWS_EBS_DOCKERRUN_TEMPLATE | sed -e "s/@IMAGE@/${EBS_TAG}/g" > $DOCKERRUN
363+
jar cMf ${EBS_TAG}.zip $DOCKERRUN .ebextensions
364+
echo "pushing ${EBS_TAG}.zip to S3: ${AWS_S3_BUCKET}/${AWS_S3_KEY}"
365+
aws s3api put-object --bucket "${AWS_S3_BUCKET}" --key "${AWS_S3_KEY}" --body ${EBS_TAG}.zip
366+
track_error $? "aws s3api put-object failed."
367+
}
368+
369+
creating_updating_EBS_appversion() {
370+
371+
echo "creating new application version $AWS_EBS_APPVER in ${EBS_APPLICATION_NAME} from s3:${AWS_S3_BUCKET}/${AWS_S3_KEY}"
372+
aws elasticbeanstalk create-application-version --application-name $EBS_APPLICATION_NAME --version-label $AWS_EBS_APPVER --source-bundle S3Bucket="$AWS_S3_BUCKET",S3Key="$AWS_S3_KEY"
373+
track_error $? "aws elasticbeanstalk create-application-version failed."
374+
375+
echo "updating elastic beanstalk environment ${AWS_EB_ENV} with the version ${AWS_EBS_APPVER}."
376+
# assumes beanstalk app for this service has already been created and configured
377+
aws elasticbeanstalk update-environment --environment-name $AWS_EBS_ENV_NAME --version-label $AWS_EBS_APPVER
378+
track_error $? "aws elasticbeanstalk update-environment failed."
379+
380+
}
381+
382+
#Cloud Front DEPLOYMENT
383+
384+
deploy_s3bucket() {
385+
echo -e "application/font-woff\t\t\t\twoff2" >> /etc/mime.types
386+
echo -e "application/font-sfnt\t\t\t\tttf" >> /etc/mime.types
387+
echo -e "application/json\t\t\t\tmap" >> /etc/mime.types
388+
389+
cat /etc/mime.types | grep -i woff
390+
cat /etc/mime.types | grep -i ico
391+
cat /etc/mime.types | grep -i map
392+
cat /etc/mime.types | grep -i ttf
393+
if [ "$CFCACHE" = "true" ]; then
394+
S3_CACHE_OPTIONS="--cache-control max-age=0,s-maxage=86400"
395+
else
396+
S3_CACHE_OPTIONS="--cache-control private,no-store,no-cache,must-revalidate,max-age=0"
397+
echo "*** Deploying with Cloudfront Cache disabled ***"
398+
fi
399+
400+
S3_OPTIONS="--exclude '*.txt' --exclude '*.js' --exclude '*.css'"
401+
echo aws s3 sync $AWS_S3_SOURCE_SYNC_PATH s3://${AWS_S3_BUCKET} ${S3_CACHE_OPTIONS} ${S3_OPTIONS}
402+
eval "aws s3 sync --dryrun $AWS_S3_SOURCE_SYNC_PATH s3://${AWS_S3_BUCKET} ${S3_CACHE_OPTIONS} ${S3_OPTIONS}"
403+
result=`eval "aws s3 sync $AWS_S3_SOURCE_SYNC_PATH s3://${AWS_S3_BUCKET} ${S3_CACHE_OPTIONS} ${S3_OPTIONS}"`
404+
if [ $? -eq 0 ]; then
405+
echo "All html, font, image, map and media files are Deployed without gzip encoding!"
406+
else
407+
echo "Deployment Failed - $result"
408+
exit 1
409+
fi
410+
411+
S3_OPTIONS="--exclude '*' --include '*.txt' --include '*.js' --include '*.css' --content-encoding gzip"
412+
echo aws s3 sync --dryrun $AWS_S3_SOURCE_SYNC_PATH s3://${AWS_S3_BUCKET} ${S3_CACHE_OPTIONS} ${S3_OPTIONS}
413+
eval "aws s3 sync --dryrun $AWS_S3_SOURCE_SYNC_PATH s3://${AWS_S3_BUCKET} ${S3_CACHE_OPTIONS} ${S3_OPTIONS}"
414+
result=`eval "aws s3 sync $AWS_S3_SOURCE_SYNC_PATH s3://${AWS_S3_BUCKET} ${S3_CACHE_OPTIONS} ${S3_OPTIONS}"`
415+
if [ $? -eq 0 ]; then
416+
echo "All txt, css, and js files are Deployed! with gzip"
417+
else
418+
echo "Deployment Failed - $result"
419+
exit 1
420+
fi
421+
}
422+
423+
decrypt_fileenc()
424+
{
425+
Buffer_seclist=$(echo $SEC_LIST | sed 's/,/ /g' )
426+
for listname in $Buffer_seclist;
427+
do
428+
cp $HOME/buildscript/securitymanager/$listname.json.enc .
429+
SECPASSWD=$(eval "echo \$${listname}")
430+
openssl enc -aes-256-cbc -d -md MD5 -in $listname.json.enc -out $listname.json -k $SECPASSWD
431+
done
432+
}
433+
434+
# decrypt_aws_sys_parameter()
435+
# {
436+
437+
# for future implmentation.
438+
# }
439+
440+
# Input Collection and validation
441+
input_parsing_validation()
442+
{
443+
while getopts .d:h:e:t:v:s:p:g:c:. OPTION
444+
do
445+
case $OPTION in
446+
d)
447+
DEPLOYMENT_TYPE=$OPTARG
448+
;;
449+
h)
450+
usage
451+
exit 1
452+
;;
453+
e)
454+
ENV=$OPTARG
455+
;;
456+
t)
457+
TAG=$OPTARG
458+
;;
459+
c)
460+
CFCACHE=$OPTARG
461+
;;
462+
v)
463+
EBS_APPVER=$OPTARG
464+
;;
465+
s)
466+
SEC_LIST=$OPTARG
467+
;;
468+
p)
469+
ECS_TEMPLATE_TYPE=$OPTARG
470+
;;
471+
g)
472+
SHARED_PROPERTY_FILENAME=$OPTARG
473+
;;
474+
475+
?)
476+
log "additional param required"
477+
usage
478+
exit
479+
;;
480+
esac
481+
done
482+
483+
if [ -z $DEPLOYMENT_TYPE ] || [ -z $ENV ] ;
484+
then
485+
log "Param validation error"
486+
usage
487+
exit 1
488+
fi
489+
490+
log "ENV : $ENV"
491+
log "DEPLOYMENT_TYPE : $DEPLOYMENT_TYPE"
492+
ENV_CONFIG=`echo "$ENV" | tr '[:upper:]' '[:lower:]'`
493+
494+
#Validating AWS configuration
495+
496+
AWS_ACCESS_KEY_ID=$(eval "echo \$${ENV}_AWS_ACCESS_KEY_ID")
497+
AWS_SECRET_ACCESS_KEY=$(eval "echo \$${ENV}_AWS_SECRET_ACCESS_KEY")
498+
AWS_ACCOUNT_ID=$(eval "echo \$${ENV}_AWS_ACCOUNT_ID")
499+
AWS_REGION=$(eval "echo \$${ENV}_AWS_REGION")
500+
if [ -z $AWS_ACCESS_KEY_ID ] || [ -z $AWS_SECRET_ACCESS_KEY ] || [ -z $AWS_ACCOUNT_ID ] || [ -z $AWS_REGION ];
501+
then
502+
log "AWS Secret Parameters are not configured in circleci/environment"
503+
usage
504+
exit 1
505+
else
506+
configure_aws_cli
507+
#aws configure list
508+
fi
509+
510+
#Getting Deployment varaible only
511+
512+
513+
decrypt_fileenc
514+
515+
516+
Buffer_seclist=$(echo $SEC_LIST | sed 's/,/ /g')
517+
for listname in $Buffer_seclist;
518+
do
519+
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
520+
export $envappvar
521+
done
522+
done
523+
524+
#Validating parameter based on Deployment type
525+
#ECS parameter validation
526+
if [ "$DEPLOYMENT_TYPE" == "ECS" ]
527+
then
528+
ECS_TAG=$TAG
529+
cp $HOME/buildscript/$TEMPLATE_SKELETON_FILE .
530+
531+
if [ -z $AWS_REPOSITORY ] || [ -z $AWS_ECS_CLUSTER ] || [ -z $AWS_ECS_SERVICE ] || [ -z $AWS_ECS_TASK_FAMILY ] || [ -z $AWS_ECS_CONTAINER_NAME ] || [ -z $AWS_ECS_PORTS ] || [ -z $ECS_TAG ];
532+
then
533+
log "Deployment varibale are not updated. Please check tag option has provided. also ensure AWS_REPOSITORY, AWS_ECS_TASK_FAMILY,AWS_ECS_CONTAINER_NAME,AWS_ECS_PORTS,AWS_ECS_CLUSTER and AWS_ECS_SERVICE ariables are configured on secret manager"
534+
usage
535+
exit 1
536+
fi
537+
log "AWS_REPOSITORY : $AWS_REPOSITORY"
538+
log "AWS_ECS_CLUSTER : $AWS_ECS_CLUSTER"
539+
log "AWS_ECS_SERVICE_NAMES : $AWS_ECS_SERVICE"
540+
log "AWS_ECS_TASK_FAMILY : $AWS_ECS_TASK_FAMILY"
541+
log "AWS_ECS_CONTAINER_NAME : $AWS_ECS_CONTAINER_NAME"
542+
log "AWS_ECS_PORTS : $AWS_ECS_PORTS"
543+
log "ECS_TAG : $ECS_TAG"
544+
fi
545+
#EBS parameter validation
546+
if [ "$DEPLOYMENT_TYPE" == "EBS" ]
547+
then
548+
EBS_APPLICATION_NAME=$(eval "echo \$${ENV}_EBS_APPLICATION_NAME")
549+
AWS_EBS_ENV_NAME=$(eval "echo \$${ENV}_AWS_EBS_ENV_NAME")
550+
AWS_EBS_APPVER="${AWS_EBS_ENV_NAME}-${EBS_APPVER}"
551+
EBS_TAG="${IMAGE_NAME}:${ENV_CONFIG}.${EBS_APPVER}"
552+
IMAGE="${DOCKER_REGISTRY_NAME}/${EBS_TAG}"
553+
AWS_S3_BUCKET=$(eval "echo \$${ENV}_AWS_S3_BUCKET")
554+
AWS_S3_KEY_LOCATION=$(eval "echo \$${ENV}_AWS_S3_KEY_LOCATION")
555+
if [ "$AWS_S3_KEY_LOCATION" = "" ] ;
556+
then
557+
AWS_S3_KEY="${EBS_TAG}"
558+
else
559+
AWS_S3_KEY="$AWS_S3_KEY_LOCATION/${EBS_TAG}"
560+
fi
561+
AWS_EBS_EB_DOCKERRUN_TEMPLATE_LOCATION=$(eval "echo \$${ENV}_AWS_EBS_EB_DOCKERRUN_TEMPLATE_LOCATION")
562+
AWS_EBS_DOCKERRUN_TEMPLATE=$(eval "echo \$${ENV}_AWS_EBS_DOCKERRUN_TEMPLATE")
563+
if [ -z $EBS_APPLICATION_NAME ] || [ -z $AWS_EBS_ENV_NAME ] || [ -z $EBS_APPVER ] || [ -z $AWS_EBS_APPVER ] || [ -z $EBS_TAG ] || [ -z $IMAGE ] || [ -z $AWS_S3_BUCKET ] || [ -z $AWS_EBS_EB_DOCKERRUN_TEMPLATE_LOCATION ] || [ -z $AWS_EBS_DOCKERRUN_TEMPLATE ];
564+
then
565+
log "Build varibale are not updated. Please update the Build variable file"
566+
usage
567+
exit 1
568+
fi
569+
log "EBS_APPLICATION_NAME : $EBS_APPLICATION_NAME"
570+
log "EBS_APPVER : $EBS_APPVER"
571+
log "AWS_EBS_APPVER : $AWS_EBS_APPVER"
572+
log "EBS_TAG : $EBS_TAG"
573+
log "IMAGE : $IMAGE"
574+
log "AWS_S3_BUCKET : $AWS_S3_BUCKET"
575+
log "AWS_S3_KEY : $AWS_S3_KEY"
576+
log "AWS_EB_ENV : $AWS_EBS_ENV_NAME"
577+
fi
578+
#CFRONT parameter validation
579+
if [ "$DEPLOYMENT_TYPE" == "CFRONT" ]
580+
then
581+
582+
if [ -z $AWS_S3_BUCKET ] || [ -z $AWS_S3_SOURCE_SYNC_PATH ];
583+
then
584+
log "Build varibale are not updated. Please update the Build variable file"
585+
usage
586+
exit 1
587+
fi
588+
log "AWS_S3_BUCKET : $AWS_S3_BUCKET"
589+
log "AWS_S3_SOURCE_SYNC_PATH : $AWS_S3_SOURCE_SYNC_PATH"
590+
fi
591+
}
592+
593+
# Main
594+
595+
main()
596+
{
597+
598+
input_parsing_validation $@
599+
600+
if [ "$DEPLOYMENT_TYPE" == "ECS" ]
601+
then
602+
#ECS_push_ecr_image
603+
ECS_template_create_register
604+
echo "value of AWS_ECS_SERVICE " $AWS_ECS_SERVICE
605+
AWS_ECS_SERVICE_NAMES=$(echo ${AWS_ECS_SERVICE} | sed 's/,/ /g')
606+
#AWS_ECS_SERVICE_NAMES=$(echo ${AWS_ECS_SERVICE} | sed 's/,/ /g' | sed 'N;s/\n//')
607+
echo "value of AWS_ECS_SERVICE_NAMES " $AWS_ECS_SERVICE_NAMES
608+
IFS=' ' read -a AWS_ECS_SERVICES <<< $AWS_ECS_SERVICE_NAMES
609+
if [ ${#AWS_ECS_SERVICES[@]} -gt 0 ]; then
610+
echo "${#AWS_ECS_SERVICES[@]} service are going to be updated"
611+
for AWS_ECS_SERVICE_NAME in "${AWS_ECS_SERVICES[@]}"
612+
do
613+
echo "updating ECS Cluster Service - $AWS_ECS_SERVICE_NAME"
614+
ECS_deploy_cluster "$AWS_ECS_SERVICE_NAME"
615+
check_service_status "$AWS_ECS_SERVICE_NAME"
616+
#echo $REVISION
617+
done
618+
else
619+
echo "Kindly check the service name in Parameter"
620+
usage
621+
exit 1
622+
fi
623+
624+
fi
625+
626+
627+
if [ "$DEPLOYMENT_TYPE" == "EBS" ]
628+
then
629+
configure_aws_cli
630+
configure_docker_private_login
631+
EBS_push_docker_image
632+
creating_updating_ebs_docker_json
633+
creating_updating_EBS_appversion
634+
fi
635+
636+
if [ "$DEPLOYMENT_TYPE" == "CFRONT" ]
637+
then
638+
deploy_s3bucket
639+
fi
640+
}
641+
main $@
642+

0 commit comments

Comments
 (0)
Please sign in to comment.