-
Notifications
You must be signed in to change notification settings - Fork 3
/
deploy
executable file
·164 lines (137 loc) · 4.84 KB
/
deploy
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
#!/bin/bash
set -euo pipefail
AWS_ACCOUNT=314751168214
AWS_REGION=eu-west-1
AWS_ECR_URL="$AWS_ACCOUNT.dkr.ecr.$AWS_REGION.amazonaws.com"
AWS_ECS_SERVICE=demo-starlink
AWS_WEB_ECR=memgraph/demo-starlink-web
AWS_WEB_DOCKERFILE=web_app
AWS_SIMULATOR_ECR=memgraph/demo-starlink-simulator
AWS_SIMULATOR_DOCKERFILE=simulator
AWS_CACHE_HANDLER_ECR=memgraph/demo-starlink-cache-handler
AWS_CACHE_HANDLER_DOCKERFILE=cache_handler
AWS_ECS_SERVICE_TAG_PRODUCTION=master
AWS_ECS_CLUSTER_PRODUCTION=production
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
function print_help () {
echo "Usage: $0 ENVIRONMENT"
echo -e "Build and Deploy Starlink demo application.\n"
echo "Required argument ENVIRONMENT:"
echo -e " production\tDeploy on production"
echo -e " staging \tDeploy on staging"
echo "Optional arguments:"
echo -e " -h\t\tDisplay this help and exit"
echo ""
}
function build () {
IMAGE_NAME=$1
DOCKERFILE_PATH=$2
echo "[Docker]: Building project $IMAGE_NAME..."
docker build -t "${IMAGE_NAME}" "${DOCKERFILE_PATH}"
}
function aws_login () {
if [ -z ${AWS_PROFILE:-} ]; then
AWS_PROFILE=default
echo "[Config]: AWS_PROFILE not set. Setting to value 'default'"
fi
echo "[Config]: Set AWS_REGION=$AWS_REGION"
aws configure set region $AWS_REGION
echo "[AWS]: Logging in to ECR Repository with profile $AWS_PROFILE..."
$(aws ecr get-login --no-include-email --profile $AWS_PROFILE)
}
function aws_push () {
IMAGE_NAME=$1
AWS_ECR_IMAGE_BRANCH="$AWS_ECR_URL/$IMAGE_NAME:$GIT_BRANCH"
AWS_ECR_IMAGE_COMMIT="$AWS_ECR_URL/$IMAGE_NAME:$GIT_COMMIT"
echo "[Docker]: Tagging image $IMAGE_NAME with remote tag $AWS_ECR_IMAGE_BRANCH"
docker tag $IMAGE_NAME $AWS_ECR_IMAGE_BRANCH
echo "[AWS]: Pushing image $AWS_ECR_IMAGE_BRANCH to ECR Repository"
docker push $AWS_ECR_IMAGE_BRANCH
echo "[Docker]: Tagging image $IMAGE_NAME with remote tag $AWS_ECR_IMAGE_COMMIT"
docker tag $IMAGE_NAME $AWS_ECR_IMAGE_COMMIT
echo "[AWS]: Pushing image $AWS_ECR_IMAGE_COMMIT to ECR Repository"
docker push $AWS_ECR_IMAGE_COMMIT
while true; do
echo "[AWS]: Waiting for docker image confirmation from ECR..."
sleep 2
JSON_RES=$(aws ecr describe-images \
--repository-name ${IMAGE_NAME} \
--image-ids imageTag=${GIT_BRANCH} || echo '{}')
if [[ $JSON_RES == *${GIT_BRANCH}* ]]; then
echo "[AWS]: Docker image is available on ECR!"
break
fi
done
}
function git_check () {
EXPECTED_GIT_BRANCH=$1
if [ -z ${GIT_BRANCH:-} ]; then
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
echo "[Config]: GIT_BRANCH not set. Setting to value '${GIT_BRANCH}'"
else
echo "[Config]: GIT_BRANCH already set to value '${GIT_BRANCH}'"
fi
GIT_COMMIT=$(git rev-parse HEAD)
echo "[Config]: GIT_BRANCH=$GIT_BRANCH"
echo "[Config]: GIT_COMMIT=$GIT_COMMIT"
if [ ${GIT_BRANCH} != ${EXPECTED_GIT_BRANCH} ]; then
echo -e "\n[AWS] Warning: Deploy is skipped because branch '${GIT_BRANCH}' is not equal to branch '${EXPECTED_GIT_BRANCH}'"
echo " Run deploy in two ways:"
echo " 1) Apply changes to branch '${EXPECTED_GIT_BRANCH}' and deploy from it"
echo " 1) Set env var GIT_BRANCH=${EXPECTED_GIT_BRANCH} and run deploy script from custom branch"
echo ""
exit 1
fi
}
function aws_deploy () {
AWS_ECS_CLUSTER=$1
echo "[AWS]: Deploying service $AWS_ECS_SERVICE in cluster $AWS_ECS_CLUSTER..."
JSON_RES=$(aws ecs update-service \
--cluster $AWS_ECS_CLUSTER \
--service $AWS_ECS_SERVICE \
--force-new-deployment)
CONT_STABLE_EVENT_COUNT=0
for _ in {1..150}; do
sleep 2
LAST_LIVE_EVENT=$(aws ecs describe-services --services $AWS_ECS_SERVICE --cluster $AWS_ECS_CLUSTER | jq .services[].events[0].message -r)
echo "[AWS]: ECS event: $LAST_LIVE_EVENT"
if [[ $LAST_LIVE_EVENT == *'has reached a steady state'* ]]; then
echo "[AWS]: Service ${AWS_ECS_SERVICE} is stable and running"
CONT_STABLE_EVENT_COUNT=$(( $CONT_STABLE_EVENT_COUNT + 1 ))
if [[ $CONT_STABLE_EVENT_COUNT -gt 4 ]]; then
break
fi
else
CONT_STABLE_EVENT_COUNT=0
fi
done
}
if test "$#" -ne 1; then
# Illegal number of parameters
print_help
exit 1
else
case "$1" in
-h)
print_help
exit 0
;;
production)
git_check "${AWS_ECS_SERVICE_TAG_PRODUCTION}"
build "${AWS_WEB_ECR}" "${AWS_WEB_DOCKERFILE}"
build "${AWS_SIMULATOR_ECR}" "${AWS_SIMULATOR_DOCKERFILE}"
build "${AWS_CACHE_HANDLER_ECR}" "${AWS_CACHE_HANDLER_DOCKERFILE}"
aws_login
aws_push "${AWS_WEB_ECR}"
aws_push "${AWS_SIMULATOR_ECR}"
aws_push "${AWS_CACHE_HANDLER_ECR}"
aws_deploy "${AWS_ECS_CLUSTER_PRODUCTION}"
exit 0
;;
*)
# Unknown option
print_help
exit 1
;;
esac
fi