-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecs_mgr.zsh
executable file
·233 lines (199 loc) · 5.6 KB
/
ecs_mgr.zsh
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/bin/zsh
# ecs_mgr.zsh
# ECS Manager
# Author: Bret Ellis
# This script is used to manage ECS clusters/services
# The script uses the AWS CLI and jq to interact with the ECS services.
# It can:
# - list all services in a cluster
# - list all tasks for a service in a cluster
# - list all task definitions for a service in a cluster
# - list all task definitions for all services in a cluster
# - start a service in a cluster
# - stop a service in a cluster
# - start all services in a cluster
# - stop all services in a cluster
#
# Defaults:
# - region: us-east-1
# - cluster: none
# - service: none
# - debug: false
# - execute: none
# - usage: false
# - help: false
# - dependencies: aws, jq
check_command() {
if ! command -v $1 &> /dev/null; then
echo "[ERROR] Command not found and required: $1"
exit 1
fi
}
check_dependencies () {
check_command 'aws'
check_command 'jq'
}
function usage() {
echo "$0:"
echo '-r|--region <region> one of: [us-east-1 | sa-east-1 | us-west-2] (default: us-east-1)'
echo '-c|--cluster <cluster>'
echo '-s|--service <service>'
echo '-d|--debug enable debug mode'
echo '-e|--execute <subcommand>'
echo
echo ' subcommands:'
echo ' list_services list all services in a cluster'
echo ' list_tasks list all tasks for a service in a cluster'
echo ' list_task_arns list the task definitions for a service in a cluster'
echo ' list_all_task_arns list all task definitions for all services in a cluster'
echo ' start start a service in a cluster'
echo ' stop stop a service in a cluster'
echo ' start_all start all services in a cluster'
echo ' stop_all stop all services in a cluster'
echo
}
function parse_args() {
if [[ $# -eq 0 ]]; then
usage
exit 1
fi
while (( $# )); do
case $1 in
-h | --help)
usage
exit 0
;;
-r | --region)
shift
region=$1
;;
-c | --cluster)
shift
cluster=$1
;;
-s | --service)
shift
cluster_service=$1
;;
-d | --debug)
debug='true'
;;
-e | --execute)
shift
execute=$1
;;
*)
echo "Unknown argument: $1"
usage
exit 1
;;
esac
shift
done
}
function list_services () {
if [[ $debug == 'true' ]]; then
echo "[DEBUG] listing services for cluster: $cluster"
fi
aws ecs list-services --region $region --cluster $cluster \
| jq -r '.serviceArns[]' \
| awk -F/ '{print $NF}' \
| sort \
| for i in $(cat) ; do echo $i ; done
}
function list_tasks () {
if [[ $debug == 'true' ]]; then
echo "[INFO] tasks for service: $cluster_service in cluster: $cluster"
fi
aws ecs list-tasks --region $region --cluster $cluster --service-name $cluster_service \
| jq -r '.taskArns[]' \
| awk -F/ '{print $NF}' \
| sort \
| for i in $(cat) ; do echo $i ; done
}
function list_task_arns () {
if [[ $debug == 'true' ]]; then
echo "[INFO] task arns for service: $cluster_service in cluster: $cluster"
fi
aws ecs describe-services --region $region --cluster $cluster --services $cluster_service \
| jq -r '.services[].taskDefinition' \
| awk -F/ '{print $NF}' \
| sort \
| for i in $(cat) ; do echo $i ; done
}
function list_all_task_arns () {
if [[ $debug == 'true' ]]; then
echo "[INFO] all task arns for cluster: $cluster"
fi
for svc in $(list_services); do
aws ecs describe-services --region $region --cluster $cluster --services $svc \
| jq -r '.services[].taskDefinition' \
| awk -F/ '{print $NF}' \
| for i in $(cat) ; do echo $i ; done
done
}
function start () {
if [[ $debug == 'true' ]]; then
echo "[INFO] starting service: $cluster_service in cluster: $cluster"
fi
aws ecs update-service --service $cluster_service --cluster $cluster --desired-count 1
}
function stop () {
if [[ $debug == 'true' ]]; then
echo "[INFO] stopping service: $cluster_service in cluster: $cluster"
fi
aws ecs update-service --service $cluster_service --cluster $cluster --desired-count 0
}
function start_all () {
if [[ $debug == 'true' ]]; then
echo "[INFO] starting all services in cluster: $cluster"
fi
for service in $(list_services); do
aws ecs update-service --service $service --cluster $cluster --desired-count 1 2>&1 >/dev/null
done
}
function stop_all () {
if [[ $debug == 'true' ]]; then
echo "[INFO] stopping all services in cluster: $cluster"
fi
for service in $(list_services); do
aws ecs update-service --service $service --cluster $cluster --desired-count 0 2>&1 >/dev/null
done
}
check_dependencies
parse_args "$@"
valid_regions=('us-east-1' 'sa-east-1' 'us-west-2')
if [[ ! ${valid_regions[(r)$region]} ]]; then
region='us-east-1'
fi
if [[ -z $cluster || -z $execute ]]; then
usage
exit 1
fi
if [[ $debug == 'true' ]]; then
echo '[DEBUG] Arguments:'
echo "region : $region"
echo "cluster : $cluster"
echo "service : $cluster_service"
echo "execute : $execute"
fi
case $execute in
list_services|start_all|stop_all)
$execute
;;
list_tasks|list_task_arns|list_all_task_arns|start|stop)
if [[ ! $execute == 'list_all_task_arns' ]]; then
if [[ -z $cluster_service ]]; then
echo '[ERROR] no service specified as arg. need one of -s|--service <service>'
usage
exit 99
fi
fi
$execute
;;
*)
echo '[ERROR] invalid subcommand specified'
usage
exit 99
;;
esac