forked from grycap/ec4docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminate-cluster
executable file
·96 lines (89 loc) · 3.25 KB
/
terminate-cluster
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
#!/bin/bash
#
# EC4Docker - Elastic Cluster for Docker
# https://github.com/grycap/ec4docker
#
# Copyright (C) GRyCAP - I3M - UPV
# Developed by Carlos A. [email protected]
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
function usage() {
echo "usage $0 [ [ --config-file | -f ] <config-file> ] [ --help | -h ]"
}
CONFIG_FILE=$(dirname $0)/ec4docker.config
DEPLOY_NODES=False
ASSUME_YES=False
while [ $# -gt 0 ]; do
case $1 in
--config-file | -f) [ $# -lt 2 ] && usage && exit 1
CONFIG_FILE=$2
shift;;
--yes | -y) ASSUME_YES=True;;
--help | -h) usage && exit 0;;
*) usage && exit 1;;
esac
shift
done
# Read the configuration if exists
if [ ! -e "$CONFIG_FILE" ]; then
echo "WARNING: file $CONFIG_FILE does not exist... ignoring and getting default variables" >&2
else
source $CONFIG_FILE
fi
# We set the default values if they are not set
EC4DOCK_SERVERNAME=${EC4DOCK_SERVERNAME:-ec4docker}
EC4DOCK_NODEBASENAME=${EC4DOCK_NODEBASENAME:-ec4docknode}
function find_nodes() {
# We'll try to find any docker container with the corresponding name whose associated server has
# the provided IP.
IPADDR=$1
POSSIBLE_NODES=$(docker ps --format {{.Names}} | grep "$EC4DOCK_NODEBASENAME")
for N in $POSSIBLE_NODES; do
VARS=$(docker inspect -f '{{.Config.Env}}' $N | tr -d '[]' | tr ' ' '\n');
for V in $VARS; do
IFS=\= read VAR VALUE <<< $V
if [ "$VAR" == "EC4DOCK_SERVERIP" -a "$VALUE" == "$IPADDR" ]; then
echo -n "$N "
fi
done
done
}
# Check if the server exists and get its IP address
IPADDR=$(docker inspect -f '{{.NetworkSettings.IPAddress}}' $EC4DOCK_SERVERNAME 2> /dev/null)
# If the front-end exists, we'll try to terminate it
if [ $? -eq 0 ]; then
echo "A container with name $EC4DOCK_SERVERNAME already exists"
WNS=$(find_nodes $IPADDR)
if [ "$WNS" != "" ]; then
echo "WARNING: Nodes '$WNS' seem to be part of the cluster"
fi
if [ "$ASSUME_YES" != "True" ]; then
read -p "Do you want me to kill container(s)? (y/N) " KILL
else
KILL=y
fi
if [ "$(echo $KILL | tr 'y' 'Y')" == "Y" ]; then
for WNNAME in $WNS; do
echo "killing container $WNNAME"
docker stop $WNNAME > /dev/null 2> /dev/null
docker rm $WNNAME > /dev/null 2> /dev/null
done
echo "killing container $EC4DOCK_SERVERNAME"
docker stop $EC4DOCK_SERVERNAME > /dev/null 2> /dev/null
docker rm $EC4DOCK_SERVERNAME > /dev/null 2> /dev/null
else
exit 1
fi
fi
exit 0