/
elasticsearch.init.erb
193 lines (169 loc) · 5.25 KB
/
elasticsearch.init.erb
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
#!/usr/bin/env bash
#
# elasticsearch
#
<% if @platform_family == 'debian' %>
### BEGIN INIT INFO
# Provides: elasticsearch
# Required-Start: $network $remote_fs $named
# Required-Stop: $network $remote_fs $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts elasticsearch
# Description: Starts elasticsearch using start-stop-daemon
### END INIT INFO
<% else %>
# chkconfig: - 57 47
# description: @service_name
# processname: elasticsearch
# config: <%= @path_conf %>/elasticsearch.yml
<% end %>
# Source networking configuration
#
if [ -f /etc/sysconfig/network ]; then source /etc/sysconfig/network; fi
# Set limits for environments ignoring `/etc/security/limits.d`
#
ulimit -n <%= @nofile_limit %>
ulimit -l <%= @memlock_limit %>
# Exit if networking is not up
#
[ "$NETWORKING" = "no" ] && exit
PIDFILE='<%= @pid_file %>'
ES_INCLUDE='<%= @path_conf %>/elasticsearch.in.sh'
CHECK_PID_RUNNING=$(ps ax | grep 'java' | grep -e "es.pidfile=$PIDFILE" | sed 's/^\s*\([0-9]*\)\s.*/\1/')
start() {
if [ -f $PIDFILE ]; then
# PIDFILE EXISTS -- ES RUNNING?
echo -e "PID file found in $PIDFILE"
es_pid="$(cat $PIDFILE)"
pid_running="$( ps ax | grep 'java' | grep $es_pid )"
if [ ! -z "$pid_running" ] ; then
# EXIT IF ES IS ALREADY RUNNING
echo -e "\033[31;1mPID $es_pid still alive, Elasticsearch already running...\033[0m"
return 1
fi
fi
echo -en "\033[1mStarting Elasticsearch...\033[0m"
touch $PIDFILE && chown <%= @user %> $PIDFILE
<% if @platform_family == 'debian' %>
ES_INCLUDE=$ES_INCLUDE start-stop-daemon --start --background --quiet --pidfile $PIDFILE --chuid <%= @user %> --exec <%= @bindir %>/elasticsearch -- -p $PIDFILE
<% else %>
su <%= @user %> -c "ES_INCLUDE=$ES_INCLUDE <%= @bindir %>/elasticsearch <%= @args %> -p $PIDFILE"
<% end %>
if [ $? ]; then
echo -e " \e[32m[OK]\e[0m"
else
echo -e " \e[31m[FAILURE]\e[0m"
fi
return $?
}
stop() {
if [[ -f $PIDFILE ]]; then
echo -n -e "\033[1mStopping elasticsearch...\033[0m"
# REMOVE PIDFILE AND EXIT IF PROCESS NOT RUNNING
if [ ! $CHECK_PID_RUNNING ]; then
echo -e "\033[1mPID file found, but no matching process running?\033[0m"
echo "Removing PID file..."
rm $PIDFILE
exit 0
fi
# KILL PROCESS
pid=$(cat $PIDFILE)
su <%= @user %> -m -c "kill $(cat $PIDFILE)"
r=$?
# Check for process
timeout=0
while [ $(ps -p $pid | wc -l ) -gt '1' ]; do
echo -n '.'
(( timeout ++))
if [ $timeout -gt '30' ]; then return; fi
sleep 1
done
# Check for pidfile
timeout=0
while [ -f $PIDFILE ]; do
echo -n '.'
(( timeout++ ))
if [ $timeout -gt '15' ]; then return; fi
sleep 1
done
echo;
return $r
else
echo -e "\033[1mNo PID file found -- elasticsearch not running?\033[0m"
fi
}
restart() {
stop
timeout=30
while ps aux | grep 'java' | grep -e "es.pidfile"; do
echo -n '.'
(( timeout-- ))
if [ $timeout -lt '1' ]; then return; fi
sleep 1
done;
start
}
status() {
# GOT PIDFILE?
[ -f $PIDFILE ] && pid=$(cat $PIDFILE)
# RUNNING
if [[ $pid && -d "/proc/$pid" ]]; then
version=$(curl -s 'http://localhost:<%= @http_port %>' | ruby -rubygems -e 'require "json"; print JSON.parse(STDIN.read)["version"]["number"]')
echo -e "\033[1;37;46melasticsearch $version running with PID $pid\033[0m"
# VERBOSE
if [[ $pid && $1 == '-v' || $1 == '--verbose' ]]; then
curl -s 'http://localhost:<%= @http_port %>/_cluster/nodes/<%= @node_name %>?os&process&jvm&network&transport&settings&pretty' | \
ruby -rubygems -e '
begin
require "json"; h = JSON.parse(STDIN.read); id, node = h["nodes"].first;
def e(name, value); puts %Q|\e[1;36m#{(name.to_s+":").ljust(20)}\e[0m #{value || "N/A" rescue "N/A"}|; end
e "HTTP Address", node["http_address"]
e "Node Name", node["name"]
e "Cluster Name", h["cluster_name"]
e "Started", Time.at(node["jvm"]["start_time"].to_i/1000)
e "JVM", "#{node["jvm"]["vm_name"]} (#{node["jvm"]["version"]})"
e "Memory Total", node["os"]["mem"]["total"]
e "Open Files", node["process"]["max_file_descriptors"]
e "Configuration", node["settings"]["config"]
rescue
puts "Metadata cannot be retrieved."
end
'
fi
# INCORRECT PID?
if [ $pid != $CHECK_PID_RUNNING ]; then
echo -e "\033[1;31;40m[!] Incorrect PID found in $PIDFILE: $pid\033[0m"
return 1
fi
return 0
fi
# NOT RUNNING
if [[ ! $pid || ! -d "/proc/$pid" ]]; then
echo -e "\033[1;33;40melasticsearch not running\033[0m"
return 3
fi
# STALE PID FOUND
if [[ ! -d "/proc/$pid" && -f $PIDFILE ]]; then
echo -e "\033[1;31;40m[!] Stale PID found in $PIDFILE\033[0m"
return 1
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status $2
;;
*)
echo $"Usage: $0 {start|stop|restart|status [-v]|}"
exit 1
esac
exit $?