forked from vesoft-inc/nebula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnebula.service
executable file
·262 lines (208 loc) · 6.04 KB
/
nebula.service
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
#! /bin/bash
function usage {
echo "Usage: ${USAGE_INFO}"
}
[[ -z ${USAGE_INFO} ]] && USAGE_INFO="${0} [-v] [-c /path/to/config] <start|stop|restart|status|kill> <metad|graphd|storaged|all>"
if [[ $# == 0 ]]; then
usage
exit 1
fi
# Original directory
OLD_DIR=$(pwd)
# Path to this script
SCRIPT_PATH=$(readlink -f $0)
# Directory of this script
SCRIPT_DIR=$(dirname ${SCRIPT_PATH})
# Installation directory, i.e. parent of SCRIPT_DIR
# For now we assume that the directory hierachy of the nebula installation is:
# root/bin, root/etc/, root/scripts, etc.
INSTALL_ROOT=$(cd ${SCRIPT_DIR}/.. &>/dev/null; pwd)
UTILS_PATH=${SCRIPT_DIR}/utils.sh
cd ${INSTALL_ROOT}
[[ -f ${UTILS_PATH} ]] || ERROR_AND_EXIT "${UTILS_PATH} not exist"
source ${UTILS_PATH} || exit 1
function on_exit {
cd ${OLD_DIR} &>/dev/null
}
trap on_exit EXIT
ACTION=
CONFIG=
DAEMON=
TARGET=
TARGETS=()
VERBOSE=0
# Parsing options from arguments
while getopts ":hvc:" opt; do
case $opt in
h)
usage
exit 0
;;
c)
CONFIG=${OPTARG}
;;
v)
VERBOSE=1
;;
\?)
ERROR "Invalid option: -${OPTARG}"
usage
exit 1
;;
:)
ERROR_AND_EXIT "Option -${OPTARG} requires an argument."
;;
esac
done
# Regard the last two operands as `action' and `target'
shift $((OPTIND - 1))
if [[ $# != 2 ]]; then
usage
exit 1
fi
ACTION=${1}
TARGET=${2}
# Collect the daemons on which we perform the action on
case ${TARGET} in
metad)
TARGETS=(${TARGET})
;;
graphd)
TARGETS=(${TARGET})
;;
storaged)
TARGETS=(${TARGET})
;;
all)
TARGETS=(metad graphd storaged)
;;
*)
ERROR "Unknown daemon \`${DAEMON}'"
usage
exit 1
esac
# If the target is `all', the specified config file will be discarded
if [[ -n ${CONFIG} ]] && [[ ${TARGET} == "all" ]]; then
WARN "Config file specified but the target is \`all'"
CONFIG=
fi
# args: <daemon name> [config file]
function start_daemon {
local daemon_name=nebula-${1}
local config=${2}
local executable=${INSTALL_ROOT}/bin/${daemon_name}
[[ -e ${executable} ]] || ERROR_AND_EXIT "${executable} not found"
[[ -z ${config} ]] && config=${INSTALL_ROOT}/etc/${daemon_name}.conf
[[ -f ${config} ]] || ERROR_AND_EXIT "${config} not found"
pid_file=$(get_pid_file_from_config ${config})
if [[ -f ${pid_file} ]]; then
if is_process_running ${pid_file}; then
ERROR "${daemon_name} already running: $(cat ${pid_file})"
return 1
fi
fi
log_dir=$(get_log_dir_from_config ${config})
[[ -z ${log_dir} ]] && log_dir=logs
make_log_dir_if_absent ${log_dir} || ERROR_AND_EXIT "Failed to create ${log_dir}"
local command="${executable} --flagfile ${config}"
INFO "Starting ${daemon_name}..."
eval ${command}
INFO "Done"
}
# args: <pid file> [signal name]
function stop_by_pid_file {
local pid_file=${1}
local signame=${2}
[[ -z ${signame} ]] && signame=TERM
if is_process_running ${pid_file}; then
[[ ${VERBOSE} -ne 0 ]] && INFO "Send SIG${signame} to $(cat ${pid_file})"
kill -s ${signame} $(cat ${pid_file})
else
[[ ${VERBOSE} -ne 0 ]] && INFO "No such process"
fi
}
# args: <daemon name> [config file]
function stop_daemon {
local daemon_name=nebula-${1}
local config=${2}
local executable=${INSTALL_ROOT}/bin/${daemon_name}
[[ -e ${executable} ]] || ERROR_AND_EXIT "${executable} not found"
[[ -z ${config} ]] && config=${INSTALL_ROOT}/etc/${daemon_name}.conf
[[ -f ${config} ]] || ERROR_AND_EXIT "${config} not found"
INFO "Stopping ${daemon_name}..."
stop_by_pid_file $(get_pid_file_from_config ${config})
INFO "Done"
}
# args: <daemon name> [config file]
function wait_daemon_to_exit {
local daemon_name=nebula-${1}
local config=${2}
local executable=${INSTALL_ROOT}/bin/${daemon_name}
[[ -e ${executable} ]] || ERROR_AND_EXIT "${executable} not found"
[[ -z ${config} ]] && config=${INSTALL_ROOT}/etc/${daemon_name}.conf
[[ -f ${config} ]] || ERROR_AND_EXIT "${config} not found"
wait_for_exit $(get_pid_file_from_config ${config}) 5
}
# args: <daemon name> [config file]
function restart_daemon {
stop_daemon $@
wait_daemon_to_exit $@
start_daemon $@
}
# args: <daemon name> [config file]
function kill_daemon {
local daemon_name=nebula-${1}
local config=${2}
[[ -z ${config} ]] && config=${INSTALL_ROOT}/etc/${daemon_name}.conf
[[ -f ${config} ]] || ERROR_AND_EXIT "${config} not found"
INFO "Force killing ${daemon_name}..."
stop_by_pid_file $(get_pid_file_from_config ${config}) "KILL"
INFO "Done"
}
# args: <daemon name> [config file]
function status_daemon {
local daemon_name=nebula-${1}
local config=${2}
[[ -z ${config} ]] && config=${INSTALL_ROOT}/etc/${daemon_name}.conf
[[ -f ${config} ]] || ERROR_AND_EXIT "${config} not found"
local pid_file=$(get_pid_file_from_config ${config})
if is_process_running ${pid_file}; then
local port=$(get_port_from_config ${config})
if is_port_listened_on ${port}; then
port=${GREEN}${port}${NC}
else
port=${BLINK}${RED}${port}${NC}
fi
INFO "${daemon_name}: Running as $(cat ${pid_file}), Listening on ${port}"
else
INFO "${daemon_name}: Exited"
fi
}
# To perform some environment checking
env_check
# The main driver
for DAEMON in ${TARGETS[@]}
do
case ${ACTION} in
start)
start_daemon ${DAEMON} ${CONFIG}
;;
stop)
stop_daemon ${DAEMON} ${CONFIG}
;;
restart)
restart_daemon ${DAEMON} ${CONFIG}
;;
kill)
kill_daemon ${DAEMON} ${CONFIG}
;;
status)
status_daemon ${DAEMON} ${CONFIG}
;;
*)
usage
exit 1
;;
esac
done
exit 0