Skip to content

Commit a45faa5

Browse files
authored
Merge pull request #14 from topcoder-platform/dev
parameter store integration
2 parents c7f4c48 + f86f26e commit a45faa5

File tree

1 file changed

+126
-14
lines changed

1 file changed

+126
-14
lines changed

master_deploy.sh

Lines changed: 126 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ SHARED_PROPERTY_FILENAME=""
1717
# AWS_REGION=""
1818
TAG=""
1919
SEC_LIST=""
20+
SECPS_LIST=""
2021
#COUNTER_LIMIT=12
2122

2223
if [ -z "$COUNTER_LIMIT" ]; then
@@ -36,6 +37,7 @@ task_def=""
3637
CONTAINER_LOG_DRIVER="awslogs"
3738
portcount=0
3839
envcount=0
40+
psenvcount=0
3941
volcount=0
4042
template=""
4143
TEMPLATE_SKELETON_FILE="base_template_v2.json"
@@ -136,6 +138,22 @@ ECS_push_ecr_image() {
136138
docker tag $APP_IMAGE_NAME:$ECS_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$AWS_REPOSITORY:$CIRCLE_BUILD_NUM
137139
ECS_TAG=$CIRCLE_BUILD_NUM
138140
fi
141+
142+
CHECK_ECR_EXIST=""
143+
CHECK_ECR_EXIST=$(aws ecr describe-repositories --repository-names ${AWS_REPOSITORY} 2>&1)
144+
if [ $? -ne 0 ]; then
145+
if echo ${CHECK_ECR_EXIST} | grep -q RepositoryNotFoundException; then
146+
echo "repo does not exist and creating repo"
147+
aws ecr create-repository --repository-name $AWS_REPOSITORY
148+
track_error $? "ECS ECR repo creation"
149+
log "Repo created successfully."
150+
else
151+
echo ${CHECK_ECR_EXIST}
152+
fi
153+
else
154+
echo "$AWS_REPOSITORY repository already exist"
155+
fi
156+
139157
log "Pushing Docker Image..."
140158
eval $(aws ecr get-login --region $AWS_REGION --no-include-email)
141159
docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$AWS_REPOSITORY:$ECS_TAG
@@ -207,6 +225,21 @@ let envcount=envcount+1
207225
#echo "envcount after ---------" $envcount
208226
#echo "envvalue after ---------" $envvalue
209227
}
228+
#=========================
229+
psenvaddition() {
230+
#echo "psenvcount before " $psenvcount
231+
232+
envname=$1
233+
envvalue=$2
234+
#echo "env value before" $envvalue
235+
set -f
236+
template=$(echo $template | jq --arg name "$envname" --arg value "$envvalue" --arg psenvcount $psenvcount '.containerDefinitions[0].secrets[$psenvcount |tonumber] |= .+ { name: $name, valueFrom: $value }')
237+
set +f
238+
let psenvcount=psenvcount+1
239+
#echo "psenvcount after ---------" $psenvcount
240+
#echo "envvalue after ---------" $envvalue
241+
}
242+
210243
#=========================
211244
logconfiguration() {
212245
template=$(echo $template | jq --arg logDriver $CONTAINER_LOG_DRIVER '.containerDefinitions[0].logConfiguration.logDriver=$logDriver')
@@ -259,12 +292,16 @@ log "Family updated"
259292
#taskrole and excution role has updated
260293
if [ -z $AWS_ECS_TASK_ROLE_ARN ];
261294
then
262-
log "No Execution Role defined"
295+
log "No Task Role defined"
263296
else
264297
template=$(echo $template | jq --arg taskRoleArn arn:aws:iam::$AWS_ACCOUNT_ID:role/$AWS_ECS_TASK_ROLE_ARN '.taskRoleArn=$taskRoleArn')
265298
fi
266-
#template=$(echo $template | jq --arg executionRoleArn arn:aws:iam::$AWS_ACCOUNT_ID:role/ecsTaskExecutionRole '.executionRoleArn=$executionRoleArn')
267-
299+
if [ -z $AWS_ECS_TASK_EXECUTION_ROLE_ARN ];
300+
then
301+
log "No Task Execution Role defined"
302+
else
303+
template=$(echo $template | jq --arg executionRoleArn arn:aws:iam::$AWS_ACCOUNT_ID:role/$AWS_ECS_TASK_EXECUTION_ROLE_ARN '.executionRoleArn=$executionRoleArn')
304+
fi
268305
#Container Name update
269306
template=$(echo $template | jq --arg name $AWS_ECS_CONTAINER_NAME '.containerDefinitions[0].name=$name')
270307
log "Container Name updated"
@@ -321,7 +358,33 @@ do
321358
done
322359
IFS=$o
323360
done
324-
361+
if [ -z $SECPS_LIST ];
362+
then
363+
log "No ps file provided"
364+
else
365+
Buffer_seclist=$(echo $SECPS_LIST | sed 's/,/ /g')
366+
for listname in $Buffer_seclist;
367+
do
368+
local o=$IFS
369+
IFS=$(echo -en "\n\b")
370+
varpath=$( cat $listname.json | jq -r ' .ParmeterPathList[] ' )
371+
#log "vars are fetched"
372+
for k in $varpath;
373+
do
374+
echo $k
375+
aws ssm get-parameters-by-path --path $k --query "Parameters[*].{Name:Name}" > paramnames.json
376+
###paramnames=$(cat paramnames.json | jq -r .[].Name | rev | cut -d / -f 1 | rev)
377+
for s in $(cat paramnames.json | jq -r .[].Name )
378+
do
379+
varname=$(echo $s | rev | cut -d / -f 1 | rev)
380+
varvalue="arn:aws:ssm:$AWS_REGION:$AWS_ACCOUNT_ID:parameter$s"
381+
psenvaddition "$varname" "$varvalue"
382+
#echo "$varname" "$varvalue"
383+
done
384+
done
385+
IFS=$o
386+
done
387+
fi
325388
log "environment has updated"
326389
# Log Configuration
327390
logconfiguration
@@ -413,16 +476,44 @@ fi
413476
ECS_deploy_cluster() {
414477

415478
AWS_ECS_SERVICE=$1
416-
update_result=$(aws ecs update-service --cluster $AWS_ECS_CLUSTER --service $AWS_ECS_SERVICE --task-definition $REVISION )
417-
result=$(echo $update_result | $JQ '.service.taskDefinition' )
418-
log $result
419-
if [[ $result != $REVISION ]]; then
420-
#echo "Error updating service."
421-
track_error 1 "ECS updating service."
422-
return 1
479+
#checking cluster exist
480+
CHECK_CLUSTER_EXIST=""
481+
CHECK_CLUSTER_EXIST=$(aws ecs describe-clusters --cluster $AWS_ECS_CLUSTER | jq --raw-output 'select(.clusters[].clusterName != null ) | .clusters[].clusterName')
482+
if [ -z $CHECK_CLUSTER_EXIST ];
483+
then
484+
echo "$AWS_ECS_CLUSTER cluster does not exist. Kindly check with admin team"
485+
exit 1
486+
else
487+
echo "$AWS_ECS_CLUSTER Cluster exist"
488+
fi
489+
#checking service exist
490+
CHECK_SERVICE_EXIST=""
491+
CHECK_SERVICE_EXIST=$(aws ecs describe-services --service $AWS_ECS_SERVICE --cluster $AWS_ECS_CLUSTER | jq --raw-output 'select(.services[].status != null ) | .services[].status')
492+
if [ -z $CHECK_SERVICE_EXIST ];
493+
then
494+
if [ "$ECS_TEMPLATE_TYPE" == "FARGATE" ];
495+
then
496+
echo "Fargate Service does not exist. Kindly check with admin team"
497+
exit 1
498+
else
499+
echo "service does not exist. Creating service"
500+
aws ecs create-service --cluster $AWS_ECS_CLUSTER --service-name $AWS_ECS_SERVICE --task-definition $REVISION --desired-count 1
501+
echo "Kindly work with admin team for routing"
502+
fi
503+
else
504+
echo "service exist.Application updates the service"
505+
update_result=$(aws ecs update-service --cluster $AWS_ECS_CLUSTER --service $AWS_ECS_SERVICE --task-definition $REVISION )
506+
result=$(echo $update_result | $JQ '.service.taskDefinition' )
507+
log $result
508+
if [[ $result != $REVISION ]]; then
509+
#echo "Error updating service."
510+
track_error 1 "ECS updating service."
511+
return 1
512+
fi
513+
514+
echo "Update service intialised successfully for deployment"
423515
fi
424516

425-
echo "Update service intialised successfully for deployment"
426517
return 0
427518
}
428519

@@ -613,6 +704,17 @@ download_envfile()
613704
#openssl enc -aes-256-cbc -d -md MD5 -in $listname.json.enc -out $listname.json -k $SECPASSWD
614705
done
615706
}
707+
download_psfile()
708+
{
709+
Buffer_seclist=$(echo $SECPS_LIST | sed 's/,/ /g' )
710+
for listname in $Buffer_seclist;
711+
do
712+
aws s3 cp s3://tc-platform-${ENV_CONFIG}/securitymanager/$listname.json .
713+
track_error $? "$listname.json download"
714+
jq 'keys[]' $listname.json
715+
track_error $? "$listname.json"
716+
done
717+
}
616718
decrypt_fileenc()
617719
{
618720
Buffer_seclist=$(echo $SEC_LIST | sed 's/,/ /g' )
@@ -696,7 +798,7 @@ deploy_lambda_package()
696798
# Input Collection and validation
697799
input_parsing_validation()
698800
{
699-
while getopts .d:h:i:e:t:v:s:p:g:c:m:. OPTION
801+
while getopts .d:h:i:e:l:t:v:s:p:g:c:m:. OPTION
700802
do
701803
case $OPTION in
702804
d)
@@ -712,6 +814,9 @@ do
712814
e)
713815
ENV=$OPTARG
714816
;;
817+
l)
818+
SECPS_LIST=$OPTARG
819+
;;
715820
t)
716821
TAG=$OPTARG
717822
;;
@@ -773,6 +878,13 @@ ENV_CONFIG=`echo "$ENV" | tr '[:upper:]' '[:lower:]'`
773878
# fi
774879

775880
download_envfile
881+
if [ -z $SECPS_LIST ];
882+
then
883+
log "No secret parameter file list provided"
884+
885+
else
886+
download_psfile
887+
fi
776888
#decrypt_fileenc
777889
#uploading_envvar
778890

@@ -974,7 +1086,7 @@ then
9741086
echo "${#AWS_ECS_SERVICES[@]} service are going to be updated"
9751087
for AWS_ECS_SERVICE_NAME in "${AWS_ECS_SERVICES[@]}"
9761088
do
977-
echo "updating ECS Cluster Service - $AWS_ECS_SERVICE_NAME"
1089+
echo "creating/updating ECS Cluster Service - $AWS_ECS_SERVICE_NAME"
9781090
ECS_deploy_cluster "$AWS_ECS_SERVICE_NAME"
9791091
check_service_status "$AWS_ECS_SERVICE_NAME"
9801092
#echo $REVISION

0 commit comments

Comments
 (0)