-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstop.sh
executable file
·184 lines (175 loc) · 7.36 KB
/
stop.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
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
#!/bin/bash
######################################################################
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. #
# SPDX-License-Identifier: MIT-0 #
######################################################################
source .env
if [ "${DEBUG}" == "true" ]; then
set -x
fi
echo ""
echo "Stopping container ${CONTAINER} on ${TO} ..."
case "${TO}" in
"compose")
CMD="${DOCKER_COMPOSE} -f ${COMPOSE_FILE} down"
;;
"swarm")
CMD="docker stack rm ${SWARM_STACK_NAME}"
;;
"ecs")
COMPOSE_FILE=${ECS_COMPOSE_FILE}
TASK_ARNS=$(aws ecs list-tasks --cluster ${ECS_CLUSTER} --query 'taskArns' --output text)
for task in $TASK_ARNS; do
echo "Stopping task $task ..."
CMD="aws ecs stop-task --cluster ${ECS_CLUSTER} --task $task"
if [ "${VERBOSE}" == "true" ]; then
echo "${CMD}"
fi
if [ "${DRY_RUN}" == "false" ]; then
RESULT=$(eval "${CMD}")
fi
TASK_DEFINITION=$(aws ecs describe-tasks --cluster ${ECS_CLUSTER} --tasks $task --query 'tasks[].taskDefinitionArn' --output text)
CMD="aws ecs deregister-task-definition --task-definition ${TASK_DEFINITION}"
if [ "${VERBOSE}" == "true" ]; then
echo "${CMD}"
fi
if [ "${DRY_RUN}" == "false" ]; then
RESULT=$(eval "${CMD}")
fi
done
if [ "${ECS_MANAGE_CLUSTER}" == "true" ]; then
CMD="${ECS_CLI} down --force"
else
COMPOSE_FILE=${ECS_COMPOSE_FILE}
CMD="${ECS_CLI} compose --file ${COMPOSE_FILE} --cluster ${ECS_CLUSTER} down"
fi
;;
"kubernetes")
CMD="${KUBECTL} delete -f ${KUBERNETES_APP_PATH}"
;;
"lambda")
CMD="aws lambda delete-function --function-name ${LAMBDA_FUNCTION_NAME}"
;;
"batch")
echo ""
echo "Stopping job ${BATCH_JOB_NAME} ..."
# Cancel SUBMITTED, PENDING or RUNNABLE jobs
for STATUS in "SUBMITTED" "PENDING" "RUNNABLE"; do
JOB_IDS=$(aws batch list-jobs --job-queue ${BATCH_JOB_QUEUE_NAME} --job-status ${STATUS} --query 'jobSummaryList[*].jobId' --output text)
if [ ! "${JOB_IDS}" == "" ]; then
for JOB_ID in "${JOB_IDS}"; do
echo "Cancelling job ${JOB_ID} ..."
CMD="aws batch cancel-job --job-id ${JOB_ID} --reason 'Stopped by user'"
if [ "${VERBOSE}" == "true" ]; then
echo "${CMD}"
fi
if [ "${DRY_RUN}" == "false" ]; then
RESULT=$(eval "${CMD}")
fi
done
fi
done
# Terminate STARTING or RUNNING jobs
for STATUS in "STARTING" "RUNNING"; do
JOB_IDS=$(aws batch list-jobs --job-queue ${BATCH_JOB_QUEUE_NAME} --job-status ${STATUS} --query 'jobSummaryList[*].jobId' --output text)
if [ ! "${JOB_IDS}" == "" ]; then
for JOB_ID in "${JOB_IDS}"; do
echo "Terminating job ${JOB_ID} ..."
CMD="aws batch terminate-job --job-id ${JOB_ID} --reason 'Stopped by user'"
if [ "${VERBOSE}" == "true" ]; then
echo "${CMD}"
fi
if [ "${DRY_RUN}" == "false" ]; then
RESULT=$(eval "${CMD}")
fi
done
fi
done
# Deregister job definitions
REVISIONS=$(aws batch describe-job-definitions --job-definition-name ${BATCH_JOB_DEFINITION_NAME} --query 'jobDefinitions[?status==`ACTIVE`].revision' --output text)
for REVISION in ${REVISIONS}; do
echo "Deregistering job definition ${BATCH_JOB_DEFINITION_NAME}:${REVISION} ..."
CMD="aws batch deregister-job-definition --job-definition ${BATCH_JOB_DEFINITION_NAME}:${REVISION}"
if [ "${VERBOSE}" == "true" ]; then
echo "${CMD}"
fi
if [ "${DRY_RUN}" == "false" ]; then
RESULT=$(eval "${CMD}")
fi
done
if [ "${BATCH_MANAGE_COMPUTE_ENVIRONMENT}" == "true" ]; then
JOB_QUEUE=$(aws batch describe-job-queues --job-queues ${BATCH_JOB_QUEUE_NAME} --query 'jobQueues[*].jobQueueName' --output text)
if [ "${JOB_QUEUE}" == "${BATCH_JOB_QUEUE_NAME}" ]; then
echo "Deleting job queue ${BATCH_JOB_QUEUE_NAME} ..."
CMD="aws batch update-job-queue --job-queue ${BATCH_JOB_QUEUE_NAME} --state DISABLED"
if [ "${VERBOSE}" == "true" ]; then
echo "${CMD}"
fi
if [ "${DRY_RUN}" == "false" ]; then
RESULT=$(eval "${CMD}")
fi
sleep 2
STATE=$(aws batch describe-job-queues --job-queues ${BATCH_JOB_QUEUE_NAME} --query 'jobQueues[*].state' --output text)
while [ ! "${STATE}" == "DISABLED" ]; do
echo "Waiting for ${BATCH_JOB_QUEUE_NAME} state to change to DISABLED ..."
sleep 2
STATE=$(aws batch describe-job-queues --job-queues ${BATCH_JOB_QUEUE_NAME} --query 'jobQueues[*].state' --output text)
done
CMD="aws batch delete-job-queue --job-queue ${BATCH_JOB_QUEUE_NAME}"
if [ "${VERBOSE}" == "true" ]; then
echo "${CMD}"
fi
if [ "${DRY_RUN}" == "false" ]; then
RESULT=$(eval "${CMD}")
fi
# Wait for job queue to be deleted
JOB_QUEUE=$(aws batch describe-job-queues --job-queues ${BATCH_JOB_QUEUE_NAME} --query 'jobQueues[*].jobQueueName' --output text)
while [ ! "${JOB_QUEUE}" == "" ]; do
echo "Waiting for job queue ${JOB_QUEUE} to be deleted ..."
sleep 2
JOB_QUEUE=$(aws batch describe-job-queues --job-queues ${BATCH_JOB_QUEUE_NAME} --query 'jobQueues[*].jobQueueName' --output text)
done
fi
COMPUTE_ENVIRONMENT=$(aws batch describe-compute-environments --compute-environments ${BATCH_COMPUTE_ENVIRONMENT_NAME} --query 'computeEnvironments[*].computeEnvironmentName' --output text)
if [ "${COMPUTE_ENVIRONMENT}" == "${BATCH_COMPUTE_ENVIRONMENT_NAME}" ]; then
echo "Deleting compute environment ${BATCH_COMPUTE_ENVIRONMENT_NAME} ..."
CMD="aws batch update-compute-environment --compute-environment ${BATCH_COMPUTE_ENVIRONMENT_NAME} --state DISABLED"
if [ "${VERBOSE}" == "true" ]; then
echo "${CMD}"
fi
if [ "${DRY_RUN}" == "false" ]; then
RESULT=$(eval "${CMD}")
fi
sleep 2
STATE=$(aws batch describe-compute-environments --compute-environments ${BATCH_COMPUTE_ENVIRONMENT_NAME} --query 'computeEnvironments[*].state' --output text)
while [ ! "${STATE}" == "DISABLED" ]; do
echo "Waiting for ${BATCH_COMPUTE_ENVIRONMENT_NAME} state to change to DISABLED ..."
sleep 2
STATE=$(aws batch describe-compute-environment --compute-environments ${BATCH_COMPUTE_ENVIRONMENT_NAME} --query 'computeEnvironments[*].state' --output text)
done
STATUS=$(aws batch describe-compute-environments --compute-environments ${BATCH_COMPUTE_ENVIRONMENT_NAME} --query 'computeEnvironments[*].status' --output text)
while [ ! "${STATUS}" == "VALID" ]; do
echo "Waiting for ${BATCH_COMPUTE_ENVIRONMENT_NAME} status to change to VALID ..."
sleep 2
STATUS=$(aws batch describe-compute-environments --compute-environments ${BATCH_COMPUTE_ENVIRONMENT_NAME} --query 'computeEnvironments[*].status' --output text)
done
CMD="aws batch delete-compute-environment --compute-environment ${BATCH_COMPUTE_ENVIRONMENT_NAME}"
fi
else
CMD=""
fi
;;
*)
checkTO "${TO}"
CMD="docker container rm -f ${CONTAINER}"
;;
esac
if [ "${VERBOSE}" == "true" ]; then
echo "${CMD}"
fi
if [ "${DRY_RUN}" == "false" ]; then
eval "${CMD}"
fi
if [ "${DEBUG}" == "true" ]; then
set +x
fi