forked from apache/zeppelin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zeppelin-daemon.sh
executable file
·268 lines (232 loc) · 6.65 KB
/
zeppelin-daemon.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
#
# description: Start and stop daemon script for.
#
USAGE="-e Usage: zeppelin-daemon.sh\n\t
[--config <conf-dir>] {start|stop|upstart|restart|reload|status}\n\t
[--version | -v]"
if [[ "$1" == "--config" ]]; then
shift
conf_dir="$1"
if [[ ! -d "${conf_dir}" ]]; then
echo "ERROR : ${conf_dir} is not a directory"
echo ${USAGE}
exit 1
else
export ZEPPELIN_CONF_DIR="${conf_dir}"
fi
shift
fi
if [ -L ${BASH_SOURCE-$0} ]; then
BIN=$(dirname $(readlink "${BASH_SOURCE-$0}"))
else
BIN=$(dirname ${BASH_SOURCE-$0})
fi
BIN=$(cd "${BIN}">/dev/null; pwd)
. "${BIN}/common.sh"
. "${BIN}/functions.sh"
HOSTNAME=$(hostname)
ZEPPELIN_NAME="Zeppelin"
ZEPPELIN_LOGFILE="${ZEPPELIN_LOG_DIR}/zeppelin-${ZEPPELIN_IDENT_STRING}-${HOSTNAME}.log"
ZEPPELIN_OUTFILE="${ZEPPELIN_LOG_DIR}/zeppelin-${ZEPPELIN_IDENT_STRING}-${HOSTNAME}.out"
ZEPPELIN_PID="${ZEPPELIN_PID_DIR}/zeppelin-${ZEPPELIN_IDENT_STRING}-${HOSTNAME}.pid"
ZEPPELIN_MAIN=org.apache.zeppelin.server.ZeppelinServer
JAVA_OPTS+=" -Dzeppelin.log.file=${ZEPPELIN_LOGFILE}"
# construct classpath
if [[ -d "${ZEPPELIN_HOME}/zeppelin-interpreter/target/classes" ]]; then
ZEPPELIN_CLASSPATH+=":${ZEPPELIN_HOME}/zeppelin-interpreter/target/classes"
fi
if [[ -d "${ZEPPELIN_HOME}/zeppelin-zengine/target/classes" ]]; then
ZEPPELIN_CLASSPATH+=":${ZEPPELIN_HOME}/zeppelin-zengine/target/classes"
fi
if [[ -d "${ZEPPELIN_HOME}/zeppelin-server/target/classes" ]]; then
ZEPPELIN_CLASSPATH+=":${ZEPPELIN_HOME}/zeppelin-server/target/classes"
fi
# Add jdbc connector jar
# ZEPPELIN_CLASSPATH+=":${ZEPPELIN_HOME}/jdbc/jars/jdbc-connector-jar"
addJarInDir "${ZEPPELIN_HOME}"
addJarInDir "${ZEPPELIN_HOME}/lib"
addJarInDir "${ZEPPELIN_HOME}/zeppelin-interpreter/target/lib"
addJarInDir "${ZEPPELIN_HOME}/zeppelin-zengine/target/lib"
addJarInDir "${ZEPPELIN_HOME}/zeppelin-server/target/lib"
addJarInDir "${ZEPPELIN_HOME}/zeppelin-web/target/lib"
CLASSPATH+=":${ZEPPELIN_CLASSPATH}"
if [[ "${ZEPPELIN_NICENESS}" = "" ]]; then
export ZEPPELIN_NICENESS=0
fi
function initialize_default_directories() {
if [[ ! -d "${ZEPPELIN_LOG_DIR}" ]]; then
echo "Log dir doesn't exist, create ${ZEPPELIN_LOG_DIR}"
$(mkdir -p "${ZEPPELIN_LOG_DIR}")
fi
if [[ ! -d "${ZEPPELIN_PID_DIR}" ]]; then
echo "Pid dir doesn't exist, create ${ZEPPELIN_PID_DIR}"
$(mkdir -p "${ZEPPELIN_PID_DIR}")
fi
}
function wait_for_zeppelin_to_die() {
local pid
local count
pid=$1
timeout=$2
count=0
timeoutTime=$(date "+%s")
let "timeoutTime+=$timeout"
currentTime=$(date "+%s")
forceKill=1
while [[ $currentTime -lt $timeoutTime ]]; do
$(kill ${pid} > /dev/null 2> /dev/null)
if kill -0 ${pid} > /dev/null 2>&1; then
sleep 3
else
forceKill=0
break
fi
currentTime=$(date "+%s")
done
if [[ forceKill -ne 0 ]]; then
$(kill -9 ${pid} > /dev/null 2> /dev/null)
fi
}
function wait_zeppelin_is_up_for_ci() {
if [[ "${CI}" == "true" ]]; then
local count=0;
while [[ "${count}" -lt 30 ]]; do
curl -v localhost:8080 2>&1 | grep '200 OK'
if [[ $? -ne 0 ]]; then
sleep 1
continue
else
break
fi
let "count+=1"
done
fi
}
function print_log_for_ci() {
if [[ "${CI}" == "true" ]]; then
tail -1000 "${ZEPPELIN_LOGFILE}" | sed 's/^/ /'
fi
}
function check_if_process_is_alive() {
local pid
pid=$(cat ${ZEPPELIN_PID})
if ! kill -0 ${pid} >/dev/null 2>&1; then
action_msg "${ZEPPELIN_NAME} process died" "${SET_ERROR}"
print_log_for_ci
return 1
fi
}
function upstart() {
# upstart() allows zeppelin to be run and managed as a service
# for example, this could be called from an upstart script in /etc/init
# where the service manager starts and stops the process
initialize_default_directories
$ZEPPELIN_RUNNER $JAVA_OPTS -cp $ZEPPELIN_CLASSPATH_OVERRIDES:$CLASSPATH $ZEPPELIN_MAIN >> "${ZEPPELIN_OUTFILE}"
}
function start() {
local pid
if [[ -f "${ZEPPELIN_PID}" ]]; then
pid=$(cat ${ZEPPELIN_PID})
if kill -0 ${pid} >/dev/null 2>&1; then
echo "${ZEPPELIN_NAME} is already running"
return 0;
fi
fi
initialize_default_directories
nohup nice -n $ZEPPELIN_NICENESS $ZEPPELIN_RUNNER $JAVA_OPTS -cp $ZEPPELIN_CLASSPATH_OVERRIDES:$CLASSPATH $ZEPPELIN_MAIN >> "${ZEPPELIN_OUTFILE}" 2>&1 < /dev/null &
pid=$!
if [[ -z "${pid}" ]]; then
action_msg "${ZEPPELIN_NAME} start" "${SET_ERROR}"
return 1;
else
action_msg "${ZEPPELIN_NAME} start" "${SET_OK}"
echo ${pid} > ${ZEPPELIN_PID}
fi
wait_zeppelin_is_up_for_ci
sleep 2
check_if_process_is_alive
}
function stop() {
local pid
# zeppelin daemon kill
if [[ ! -f "${ZEPPELIN_PID}" ]]; then
echo "${ZEPPELIN_NAME} is not running"
else
pid=$(cat ${ZEPPELIN_PID})
if [[ -z "${pid}" ]]; then
echo "${ZEPPELIN_NAME} is not running"
else
wait_for_zeppelin_to_die $pid 40
$(rm -f ${ZEPPELIN_PID})
action_msg "${ZEPPELIN_NAME} stop" "${SET_OK}"
fi
fi
# list all pid that used in remote interpreter and kill them
for f in ${ZEPPELIN_PID_DIR}/*.pid; do
if [[ ! -f ${f} ]]; then
continue;
fi
pid=$(cat ${f})
wait_for_zeppelin_to_die $pid 20
$(rm -f ${f})
done
}
function find_zeppelin_process() {
local pid
if [[ -f "${ZEPPELIN_PID}" ]]; then
pid=$(cat ${ZEPPELIN_PID})
if ! kill -0 ${pid} > /dev/null 2>&1; then
action_msg "${ZEPPELIN_NAME} running but process is dead" "${SET_ERROR}"
return 1
else
action_msg "${ZEPPELIN_NAME} is running" "${SET_OK}"
fi
else
action_msg "${ZEPPELIN_NAME} is not running" "${SET_ERROR}"
return 1
fi
}
case "${1}" in
start)
start
;;
stop)
stop
;;
upstart)
upstart
;;
reload)
stop
start
;;
restart)
stop
start
;;
status)
find_zeppelin_process
;;
-v | --version)
getZeppelinVersion
;;
*)
echo ${USAGE}
esac