root / daemons / daemon-util.in @ 8c37d618
History | View | Annotate | Download (6.8 kB)
1 |
#!/bin/bash |
---|---|
2 |
# |
3 |
|
4 |
# Copyright (C) 2009, 2011, 2012 Google Inc. |
5 |
# |
6 |
# This program is free software; you can redistribute it and/or modify |
7 |
# it under the terms of the GNU General Public License as published by |
8 |
# the Free Software Foundation; either version 2 of the License, or |
9 |
# (at your option) any later version. |
10 |
# |
11 |
# This program is distributed in the hope that it will be useful, but |
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
# General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License |
17 |
# along with this program; if not, write to the Free Software |
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 |
# 02110-1301, USA. |
20 |
|
21 |
set -e |
22 |
|
23 |
readonly defaults_file="${GANETI_ROOTDIR}@SYSCONFDIR@/default/ganeti" |
24 |
readonly localstatedir="${GANETI_ROOTDIR}@LOCALSTATEDIR@" |
25 |
|
26 |
# This is a list of all daemons and the order in which they're started. The |
27 |
# order is important as there are dependencies between them. On shutdown, |
28 |
# they're stopped in reverse order. |
29 |
DAEMONS=( |
30 |
ganeti-noded |
31 |
ganeti-masterd |
32 |
ganeti-rapi |
33 |
) |
34 |
|
35 |
if [[ "@CUSTOM_ENABLE_CONFD@" == True ]]; then |
36 |
DAEMONS+=( ganeti-confd ) |
37 |
fi |
38 |
|
39 |
NODED_ARGS= |
40 |
MASTERD_ARGS= |
41 |
CONFD_ARGS= |
42 |
RAPI_ARGS= |
43 |
|
44 |
# Read defaults file if it exists |
45 |
if [[ -s $defaults_file ]]; then |
46 |
. $defaults_file |
47 |
fi |
48 |
|
49 |
# Meant to facilitate use utilities in /etc/rc.d/init.d/functions in case |
50 |
# start-stop-daemon is not available. |
51 |
_ignore_error() { |
52 |
eval "$@" || : |
53 |
} |
54 |
|
55 |
_daemon_pidfile() { |
56 |
echo "$localstatedir/run/ganeti/$1.pid" |
57 |
} |
58 |
|
59 |
_daemon_executable() { |
60 |
echo "@PREFIX@/sbin/$1" |
61 |
} |
62 |
|
63 |
_daemon_usergroup() { |
64 |
case "$1" in |
65 |
masterd) |
66 |
echo "@GNTMASTERUSER@:@GNTMASTERDGROUP@" |
67 |
;; |
68 |
confd) |
69 |
echo "@GNTCONFDUSER@:@GNTCONFDGROUP@" |
70 |
;; |
71 |
rapi) |
72 |
echo "@GNTRAPIUSER@:@GNTRAPIGROUP@" |
73 |
;; |
74 |
noded) |
75 |
echo "@GNTNODEDUSER@:@GNTDAEMONSGROUP@" |
76 |
;; |
77 |
*) |
78 |
echo "root:@GNTDAEMONSGROUP@" |
79 |
;; |
80 |
esac |
81 |
} |
82 |
|
83 |
# Checks whether the local machine is part of a cluster |
84 |
check_config() { |
85 |
local server_pem=$localstatedir/lib/ganeti/server.pem |
86 |
local fname |
87 |
|
88 |
for fname in $server_pem; do |
89 |
if [[ ! -f $fname ]]; then |
90 |
echo "Missing configuration file $fname" >&2 |
91 |
return 1 |
92 |
fi |
93 |
done |
94 |
|
95 |
return 0 |
96 |
} |
97 |
|
98 |
# Checks the exit code of a daemon |
99 |
check_exitcode() { |
100 |
if [[ "$#" -lt 1 ]]; then |
101 |
echo 'Missing exit code.' >&2 |
102 |
return 1 |
103 |
fi |
104 |
|
105 |
local rc="$1"; shift |
106 |
|
107 |
case "$rc" in |
108 |
0) ;; |
109 |
11) |
110 |
echo "not master" |
111 |
;; |
112 |
*) |
113 |
echo "exit code $rc" |
114 |
return 1 |
115 |
;; |
116 |
esac |
117 |
|
118 |
return 0 |
119 |
} |
120 |
|
121 |
# Prints path to PID file for a daemon. |
122 |
daemon_pidfile() { |
123 |
if [[ "$#" -lt 1 ]]; then |
124 |
echo 'Missing daemon name.' >&2 |
125 |
return 1 |
126 |
fi |
127 |
|
128 |
local name="$1"; shift |
129 |
|
130 |
_daemon_pidfile $name |
131 |
} |
132 |
|
133 |
# Prints path to daemon executable. |
134 |
daemon_executable() { |
135 |
if [[ "$#" -lt 1 ]]; then |
136 |
echo 'Missing daemon name.' >&2 |
137 |
return 1 |
138 |
fi |
139 |
|
140 |
local name="$1"; shift |
141 |
|
142 |
_daemon_executable $name |
143 |
} |
144 |
|
145 |
# Prints a list of all daemons in the order in which they should be started |
146 |
list_start_daemons() { |
147 |
local name |
148 |
|
149 |
for name in "${DAEMONS[@]}"; do |
150 |
echo "$name" |
151 |
done |
152 |
} |
153 |
|
154 |
# Prints a list of all daemons in the order in which they should be stopped |
155 |
list_stop_daemons() { |
156 |
list_start_daemons | tac |
157 |
} |
158 |
|
159 |
# Checks whether a daemon name is known |
160 |
is_daemon_name() { |
161 |
if [[ "$#" -lt 1 ]]; then |
162 |
echo 'Missing daemon name.' >&2 |
163 |
return 1 |
164 |
fi |
165 |
|
166 |
local name="$1"; shift |
167 |
|
168 |
for i in "${DAEMONS[@]}"; do |
169 |
if [[ "$i" == "$name" ]]; then |
170 |
return 0 |
171 |
fi |
172 |
done |
173 |
|
174 |
echo "Unknown daemon name '$name'" >&2 |
175 |
return 1 |
176 |
} |
177 |
|
178 |
# Checks whether daemon is running |
179 |
check() { |
180 |
if [[ "$#" -lt 1 ]]; then |
181 |
echo 'Missing daemon name.' >&2 |
182 |
return 1 |
183 |
fi |
184 |
|
185 |
local name="$1"; shift |
186 |
local pidfile=$(_daemon_pidfile $name) |
187 |
local daemonexec=$(_daemon_executable $name) |
188 |
|
189 |
if type -p start-stop-daemon >/dev/null; then |
190 |
start-stop-daemon --stop --signal 0 --quiet \ |
191 |
--pidfile $pidfile |
192 |
else |
193 |
_ignore_error status \ |
194 |
-p $pidfile \ |
195 |
$daemonexec |
196 |
fi |
197 |
} |
198 |
|
199 |
# Starts a daemon |
200 |
start() { |
201 |
if [[ "$#" -lt 1 ]]; then |
202 |
echo 'Missing daemon name.' >&2 |
203 |
return 1 |
204 |
fi |
205 |
|
206 |
local name="$1"; shift |
207 |
# Convert daemon name to uppercase after removing "ganeti-" prefix |
208 |
local plain_name=${name#ganeti-} |
209 |
local ucname=$(tr a-z A-Z <<<$plain_name) |
210 |
local pidfile=$(_daemon_pidfile $name) |
211 |
local usergroup=$(_daemon_usergroup $plain_name) |
212 |
local daemonexec=$(_daemon_executable $name) |
213 |
|
214 |
if [[ "$name" == ganeti-confd && |
215 |
"@CUSTOM_ENABLE_CONFD@" == False ]]; then |
216 |
echo 'ganeti-confd disabled at build time' >&2 |
217 |
return 1 |
218 |
fi |
219 |
|
220 |
# Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS |
221 |
eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\"" |
222 |
|
223 |
@PKGLIBDIR@/ensure-dirs |
224 |
|
225 |
if type -p start-stop-daemon >/dev/null; then |
226 |
start-stop-daemon --start --quiet --oknodo \ |
227 |
--pidfile $pidfile \ |
228 |
--startas $daemonexec \ |
229 |
--chuid $usergroup \ |
230 |
-- $args "$@" |
231 |
else |
232 |
# TODO: Find a way to start daemon with a group, until then the group must |
233 |
# be removed |
234 |
_ignore_error daemon \ |
235 |
--pidfile $pidfile \ |
236 |
--user ${usergroup%:*} \ |
237 |
$daemonexec $args "$@" |
238 |
fi |
239 |
} |
240 |
|
241 |
# Stops a daemon |
242 |
stop() { |
243 |
if [[ "$#" -lt 1 ]]; then |
244 |
echo 'Missing daemon name.' >&2 |
245 |
return 1 |
246 |
fi |
247 |
|
248 |
local name="$1"; shift |
249 |
local pidfile=$(_daemon_pidfile $name) |
250 |
|
251 |
if type -p start-stop-daemon >/dev/null; then |
252 |
start-stop-daemon --stop --quiet --oknodo --retry 30 \ |
253 |
--pidfile $pidfile |
254 |
else |
255 |
_ignore_error killproc -p $pidfile $name |
256 |
fi |
257 |
} |
258 |
|
259 |
# Starts a daemon if it's not yet running |
260 |
check_and_start() { |
261 |
local name="$1" |
262 |
|
263 |
if ! check $name; then |
264 |
start $name |
265 |
fi |
266 |
} |
267 |
|
268 |
# Starts the master role |
269 |
start_master() { |
270 |
start ganeti-masterd |
271 |
start ganeti-rapi |
272 |
} |
273 |
|
274 |
# Stops the master role |
275 |
stop_master() { |
276 |
stop ganeti-rapi |
277 |
stop ganeti-masterd |
278 |
} |
279 |
|
280 |
# Start all daemons |
281 |
start_all() { |
282 |
for i in $(list_start_daemons); do |
283 |
local rc=0 |
284 |
|
285 |
# Try to start daemon |
286 |
start $i || rc=$? |
287 |
|
288 |
if ! errmsg=$(check_exitcode $rc); then |
289 |
echo "$errmsg" >&2 |
290 |
return 1 |
291 |
fi |
292 |
done |
293 |
|
294 |
return 0 |
295 |
} |
296 |
|
297 |
# Stop all daemons |
298 |
stop_all() { |
299 |
for i in $(list_stop_daemons); do |
300 |
stop $i |
301 |
done |
302 |
} |
303 |
|
304 |
# Reloads the SSH keys |
305 |
reload_ssh_keys() { |
306 |
@RPL_SSH_INITD_SCRIPT@ restart |
307 |
} |
308 |
|
309 |
# Read @SYSCONFDIR@/rc.d/init.d/functions if start-stop-daemon not available |
310 |
if ! type -p start-stop-daemon >/dev/null && \ |
311 |
[[ -f @SYSCONFDIR@/rc.d/init.d/functions ]]; then |
312 |
_ignore_error . @SYSCONFDIR@/rc.d/init.d/functions |
313 |
fi |
314 |
|
315 |
if [[ "$#" -lt 1 ]]; then |
316 |
echo "Usage: $0 <action>" >&2 |
317 |
exit 1 |
318 |
fi |
319 |
|
320 |
orig_action=$1; shift |
321 |
|
322 |
if [[ "$orig_action" == *_* ]]; then |
323 |
echo "Command must not contain underscores" >&2 |
324 |
exit 1 |
325 |
fi |
326 |
|
327 |
# Replace all dashes (-) with underlines (_) |
328 |
action=${orig_action//-/_} |
329 |
|
330 |
# Is it a known function? |
331 |
if ! declare -F "$action" >/dev/null 2>&1; then |
332 |
echo "Unknown command: $orig_action" >&2 |
333 |
exit 1 |
334 |
fi |
335 |
|
336 |
# Call handler function |
337 |
$action "$@" |