forked from skahack/ecs-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy
executable file
·135 lines (116 loc) · 3.64 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
#!/bin/bash
set -o errexit
set -o pipefail
set -u
function usage {
set -e
cat <<EOM
deploy -c cluster-name -n service-name --asg ASG name
EOM
exit 2
}
if [ $# == 0 ]; then usage; fi
# Check requirements
function require {
command -v "$1" > /dev/null 2>&1 || {
echo "Some of the required software is not installed:"
echo " please install $1" >&2;
exit 1;
}
}
function getRunningTaskInstance {
local cluster=$1
local instances=$( aws ecs list-container-instances --cluster $cluster )
local list=$(echo $instances | jq -r '.containerInstanceArns|join(" ")')
local containerInstances=$( aws ecs describe-container-instances \
--cluster $CLUSTER \
--container-instances ${list} | jq '.containerInstances' )
local runningTaskInstances=$( echo $containerInstances | jq -r '[ .[]|select(.runningTasksCount > 0)|.ec2InstanceId ] | join(" ")' )
echo $runningTaskInstances
}
function scaleIn {
local cluster=$1
local asgName=$2
local asgDesiredCapacity=$3
local newAsgDesiredCapacity=$4
runningTaskInstances=$( getRunningTaskInstance $cluster )
aws autoscaling set-instance-protection \
--auto-scaling-group-name $asgName \
--instance-ids ${runningTaskInstances} \
--protected-from-scale-in
echo "[INFO] cluster scale in: $newAsgDesiredCapacity -> $asgDesiredCapacity"
aws autoscaling set-desired-capacity \
--auto-scaling-group-name $asgName \
--desired-capacity $asgDesiredCapacity > /dev/null
local i=0
local every=10
while [ $i -lt $TIMEOUT ]
do
size=$( aws ecs list-container-instances --cluster $cluster | jq '.containerInstanceArns|length' )
if [ $size = $asgDesiredCapacity ]; then
aws autoscaling set-instance-protection \
--auto-scaling-group-name $asgName \
--instance-ids ${runningTaskInstances} \
--no-protected-from-scale-in
echo "[INFO] scale in successfully."
exit 0
fi
sleep $every
i=$(( $i + $every ))
done
echo "[ERROR] cluster scale-in not finishing within $TIMEOUT seconds"
}
# Check for AWS, AWS Command Line Interface
require aws
# Check for jq, Command-line JSON processor
require jq
# args
CLUSTER=false
SERVICE=false
ASG_NAME=false
TIMEOUT=900
# Loop through arguments, two at a time for key and value
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--asg)
ASG_NAME="$2"
shift
;;
-c|--cluster)
CLUSTER="$2"
shift
;;
-n|--service-name)
SERVICE="$2"
shift # past argument
;;
-t|--timeout)
TIMEOUT="$2"
shift
;;
*)
usage
exit 2
;;
esac
shift # past argument or value
done
asg=$( aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name $ASG_NAME )
asgInstances=$( echo $asg \
| jq '.AutoScalingGroups[] | [.Instances[]|select(.LifecycleState == "InService")]' )
asgDesiredCapacity=$( echo $asg | jq '.AutoScalingGroups[0].DesiredCapacity' )
newAsgDesiredCapacity=$(( $asgDesiredCapacity * 2 ))
echo "[INFO] cluster scale out: $asgDesiredCapacity -> $newAsgDesiredCapacity"
aws autoscaling set-desired-capacity \
--auto-scaling-group-name $ASG_NAME \
--desired-capacity $newAsgDesiredCapacity > /dev/null
ecsDeployExitCode=0
./ecs-deploy -c $CLUSTER -n $SERVICE || ecsDeployExitCode=$?
if [ $ecsDeployExitCode != 0 ]; then
echo "[INFO] rollback cluster size: $newAsgDesiredCapacity -> $asgDesiredCapacity"
scaleIn $CLUSTER $ASG_NAME $asgDesiredCapacity $newAsgDesiredCapacity
exit 1
fi
scaleIn $CLUSTER $ASG_NAME $asgDesiredCapacity $newAsgDesiredCapacity