-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenable-data-node.sh
72 lines (58 loc) · 2.29 KB
/
enable-data-node.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
#!/bin/bash
ES=${1:-$ES}
ES=${ES:-localhost:9200}
NODE=${2:-$NODE}
: ${PREFIX=data-}
: ${TYPE:=persistent}
perl -MJSON -e 1 2>/dev/null || {
echo "ERROR: perl-JSON isn't installed."
exit 1
}
[ "$NODE" ] || {
echo "Usage: $0 ES NODE" >&2
exit 1
}
[ "$TYPE" = persistent -o "$TYPE" = transient ] || {
echo "ERROR: \$TYPE must be 'persistent' or 'transient'"
exit 1
}
[ "$NODE" != "${NODE#$PREFIX}" ] || NODE="${PREFIX}$NODE"
read -p "Confirm to ${TYPE}ly enable $NODE for $ES? [y/N]" c
[ "$c" = y -o "$c" = Y ] || exit 1
echo "requesting $ES/_cluster/settings?pretty ..."
response=`curl -m 60 -s $ES/_cluster/settings?pretty`
[ $? = 0 -a "$response" ] && echo "$response" || {
echo "ERROR: failed to query $ES/_cluster/settings"
exit 1
}
nodes=`perl -MJSON -e '
$h = decode_json($ARGV[0]);
exit 1 unless $h && exists $h->{$ARGV[1]};
print $h->{$ARGV[1]}{"cluster"}{"routing"}{"allocation"}{"exclude"}{"_name"}' "$response" $TYPE 2>/dev/null`
[ $? = 0 ] || {
echo "ERROR: failed to query $ES/_cluster/settings"
exit 1
}
echo
echo "old excluded nodes: $nodes"
if perl -e 'exit(scalar(grep $_ eq $ARGV[1], split(/\s*,\s*/, $ARGV[0])) > 0 ? 0 : 1)' "$nodes" "$NODE"; then
nodes=`perl -e '%h = map { $_, 1 } split(/\s*,\s*/, $ARGV[0]); delete $h{$ARGV[1]}; print join(",", sort keys %h)' "$nodes" "$NODE"`
echo "new excluded nodes: $nodes"
curl -m 60 -s -XPUT $ES/_cluster/settings?pretty --data-binary \
"{\"$TYPE\": {\"cluster.routing.allocation.exclude._name\": \"$nodes\"}}" |
grep -q ' "acknowledged"\s*:\s*true' || {
echo "ERROR: failed to put cluster.routing.allocation.exclude._name=$nodes to $ES/_cluster/settings"
exit 1
}
else
echo "$NODE isn't in excluding list."
fi
echo
echo "Issue this command to enable rebalancing if it's not enabled:"
echo " curl -m 60 -s -XPUT $ES/_cluster/settings --data-binary '{\"transient\": {\"cluster.routing.rebalance.enable\": \"all\"}}'"
echo "then periodically use these commands to check if $NODE is really joined:"
echo " curl -s $ES/_cat/health?v"
echo " curl -s $ES/_cat/shards | fgrep $NODE"
echo " curl -s $ES/_cat/recovery | fgrep -v done"
echo "After it's done, restore \"cluster.routing.rebalance.enable\" to its original value, eg. none."
echo