-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupd_service.sh
executable file
·81 lines (62 loc) · 2.05 KB
/
upd_service.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
#!/bin/bash
set -euo pipefail
# Uncomment for debug
# set -x
export AWS_PROFILE=${AWS_PROFILE:-cdl-pad-dev}
export FUNC=${FUNC:-pub-dspace-dev}
show_usage() {
echo "Usage: $0 <service_component> e.g. backend, frontend, solr"
echo "Example: $0 backend"
echo
exit 1
}
get_service_message() {
AWS_PROFILE=$1 aws ecs describe-services \
--cluster $2 --service $3 \
--query services[0].events[0].message
}
get_service_info() {
AWS_PROFILE=$1 aws ecs describe-services \
--cluster $2 --service $3 \
--query 'services[0].[deployments, runningCount, desiredCount]' \
--output json | \
jq -r '.[0] | if type == "array" then length else 0 end'
}
# Check if '-h' or '--help' is provided as an argument
if [[ "$1" == '-h' || "$1" == '--help' ]]; then
show_usage
fi
# Check if no service name is provided or too many parameters are given
if [ $# -ne 1 ]; then
show_usage
fi
# Get the service short name from the command line
SERVICE_COMPONENT=$1
# Build the SERVICE name
SERVICE="${FUNC}-${SERVICE_COMPONENT}-service"
# Build the CLUSTER name
CLUSTER="${FUNC}-cluster"
last_msg=$(get_service_message $AWS_PROFILE $CLUSTER $SERVICE)
echo "Telling $SERVICE to update..."
if ! AWS_PROFILE=$AWS_PROFILE aws ecs update-service --cluster $CLUSTER --service $SERVICE --force-new-deployment > /dev/null; then
echo "Error: Failed to update $SERVICE."
exit 1
fi
echo "Waiting for $SERVICE to stabilize."
while true; do
cur_msg=$(get_service_message $AWS_PROFILE $CLUSTER $SERVICE)
if [ "$last_msg" != "$cur_msg" ]; then
echo " ${cur_msg//\"/}"
last_msg="$cur_msg"
fi
if ! read -r ndep running desired < <(get_service_info $AWS_PROFILE $CLUSTER $SERVICE); then
echo "Error: Failed to retrieve service information for $SERVICE."
exit 1
fi
if [ "$ndep" == 1 ] && [ "$running" == "$desired" ]; then
timestamp=$(date +"%Y-%m-%d %I:%M:%S %p")
echo "✅ Success: $SERVICE is stable. Timestamp: $timestamp"
exit 0
fi
sleep 5
done