-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·197 lines (176 loc) · 5.19 KB
/
deploy.sh
File metadata and controls
executable file
·197 lines (176 loc) · 5.19 KB
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
#!/usr/bin/env bash
set -eo pipefail
# more bash-friendly output for jq
JQ="jq --raw-output --exit-status"
ENV=$1
TAG=$2
PROVIDER=$3
COUNTER_LIMIT=12
# Counter limit will be caluculaed based on sleep seconds
if [[ -z "$ENV" ]] ; then
echo "Environment should be set on startup with one of the below values"
echo "ENV must be one of - DEV, QA, PROD or LOCAL"
exit
fi
if [[ -z "$TAG" ]] ; then
echo "TAG must be specificed for image"
exit
fi
if [[ -z "$PROVIDER" ]] ; then
PROVIDER=$ENV
fi
AWS_REGION=$(eval "echo \$${ENV}_AWS_REGION")
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_REPOSITORY=$(eval "echo \$${ENV}_AWS_REPOSITORY")
AWS_ECS_CLUSTER=$(eval "echo \$${ENV}_AWS_ECS_CLUSTER")
AWS_ECS_SERVICE=$(eval "echo \$${ENV}_AWS_ECS_SERVICE")
family=$(eval "echo \$${ENV}_AWS_ECS_TASK_FAMILY")
AWS_ECS_CONTAINER_NAME=$(eval "echo \$${ENV}_AWS_ECS_CONTAINER_NAME")
echo $APP_NAME
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
echo "Configured AWS CLI."
}
push_ecr_image() {
echo "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:$TAG
echo "Docker Image published."
}
update_cluster_taskdef() {
make_task_def
register_definition
}
deploy_cluster() {
AWS_ECS_SERVICE=$1
update_result=$(aws ecs update-service --cluster $AWS_ECS_CLUSTER --service $AWS_ECS_SERVICE --task-definition $revision )
#echo $update_result
result=$(echo $update_result | $JQ '.service.taskDefinition' )
echo $result
if [[ $result != $revision ]]; then
echo "Error updating service."
exit 1
fi
echo "Update service intialised successfully for deployment"
return 0
}
make_task_def(){
task_template='[
{
"name": "%s",
"image": "%s.dkr.ecr.%s.amazonaws.com/%s:%s",
"essential": true,
"memory": 1000,
"cpu": 100,
"environment": [
{
"name": "ENV",
"value": "%s"
},
{
"name": "PROVIDER",
"value": "%s"
}
],
"portMappings": [
{
"hostPort": 0,
"containerPort": 8000,
"protocol": "tcp"
},
{
"hostPort": 0,
"containerPort": 8001,
"protocol": "tcp"
}
],
"mountPoints": [
{
"containerPath": "/nfs_shares",
"sourceVolume": "nfs_share"
},
{
"containerPath": "/data/nginx",
"sourceVolume": "nginxdata"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/aws/ecs/%s",
"awslogs-region": "%s",
"awslogs-stream-prefix": "%s_%s"
}
}
}
]'
volume_def='[
{
"host": {
"sourcePath": "/nfs_shares"
},
"name": "nfs_share"
},
{
"host": {
"sourcePath": "/mnt/nginx"
},
"name": "nginxdata"
}
]'
task_def=$(printf "$task_template" $AWS_ECS_CONTAINER_NAME $AWS_ACCOUNT_ID $AWS_REGION $AWS_REPOSITORY $TAG $ENV $PROVIDER $AWS_ECS_CLUSTER $AWS_REGION $AWS_ECS_CLUSTER $ENV)
}
register_definition() {
if revision=$(aws ecs register-task-definition --container-definitions "$task_def" --volumes "$volume_def" --family $family | $JQ '.taskDefinition.taskDefinitionArn'); then
echo "Revision: $revision"
else
echo "Failed to register task definition"
return 1
fi
}
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 does not reach steady state with in 180 seconds. Please check"
exit 1
fi
done
echo "$servicestatus"
}
configure_aws_cli
push_ecr_image
#deploy_cluster
#check_service_status
update_cluster_taskdef
#Service name will be provided with comma seperated
AWS_ECS_SERVICE_NAMES=`echo ${AWS_ECS_SERVICE} | sed 's/,/ /g' | sed 'N;s/\n//' `
IFS=' ' read -a AWS_ECS_SERVICES <<< $AWS_ECS_SERVICE_NAMES
if [ ${#AWS_ECS_SERVICES[@]} -gt 0 ]; then
echo "${#AWS_ECS_SERVICES[@]} service are going to be updated"
for AWS_ECS_SERVICE_NAME in "${AWS_ECS_SERVICES[@]}"
do
echo "updating ECS Cluster Service - $AWS_ECS_SERVICE_NAME"
deploy_cluster "$AWS_ECS_SERVICE_NAME"
check_service_status "$AWS_ECS_SERVICE_NAME"
done
else
echo "Kindly check the service name in Parameter"
usage
exit 1
fi