root / daemons / daemon-util.in @ bb514a22
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 |
_confd_enabled() { |
36 |
[[ "@CUSTOM_ENABLE_CONFD@" == True ]] |
37 |
} |
38 |
|
39 |
if _confd_enabled; then |
40 |
DAEMONS+=( ganeti-confd ) |
41 |
fi |
42 |
|
43 |
NODED_ARGS= |
44 |
MASTERD_ARGS= |
45 |
CONFD_ARGS= |
46 |
RAPI_ARGS= |
47 |
|
48 |
# Read defaults file if it exists |
49 |
if [[ -s $defaults_file ]]; then |
50 |
. $defaults_file |
51 |
fi |
52 |
|
53 |
# Meant to facilitate use utilities in /etc/rc.d/init.d/functions in case |
54 |
# start-stop-daemon is not available. |
55 |
_ignore_error() { |
56 |
eval "$@" || : |
57 |
} |
58 |
|
59 |
_daemon_pidfile() { |
60 |
echo "$localstatedir/run/ganeti/$1.pid" |
61 |
} |
62 |
|
63 |
_daemon_executable() { |
64 |
echo "@PREFIX@/sbin/$1" |
65 |
} |
66 |
|
67 |
_daemon_usergroup() { |
68 |
case "$1" in |
69 |
masterd) |
70 |
echo "@GNTMASTERUSER@:@GNTMASTERDGROUP@" |
71 |
;; |
72 |
confd) |
73 |
echo "@GNTCONFDUSER@:@GNTCONFDGROUP@" |
74 |
;; |
75 |
rapi) |
76 |
echo "@GNTRAPIUSER@:@GNTRAPIGROUP@" |
77 |
;; |
78 |
noded) |
79 |
echo "@GNTNODEDUSER@:@GNTDAEMONSGROUP@" |
80 |
;; |
81 |
*) |
82 |
echo "root:@GNTDAEMONSGROUP@" |
83 |
;; |
84 |
esac |
85 |
} |
86 |
|
87 |
# Checks whether the local machine is part of a cluster |
88 |
check_config() { |
89 |
local server_pem=$localstatedir/lib/ganeti/server.pem |
90 |
local fname |
91 |
|
92 |
for fname in $server_pem; do |
93 |
if [[ ! -f $fname ]]; then |
94 |
echo "Missing configuration file $fname" >&2 |
95 |
return 1 |
96 |
fi |
97 |
done |
98 |
|
99 |
return 0 |
100 |
} |
101 |
|
102 |
# Checks the exit code of a daemon |
103 |
check_exitcode() { |
104 |
if [[ "$#" -lt 1 ]]; then |
105 |
echo 'Missing exit code.' >&2 |
106 |
return 1 |
107 |
fi |
108 |
|
109 |
local rc="$1"; shift |
110 |
|
111 |
case "$rc" in |
112 |
0) ;; |
113 |
11) |
114 |
echo "not master" |
115 |
;; |
116 |
*) |
117 |
echo "exit code $rc" |
118 |
return 1 |
119 |
;; |
120 |
esac |
121 |
|
122 |
return 0 |
123 |
} |
124 |
|
125 |
# Prints path to PID file for a daemon. |
126 |
daemon_pidfile() { |
127 |
if [[ "$#" -lt 1 ]]; then |
128 |
echo 'Missing daemon name.' >&2 |
129 |
return 1 |
130 |
fi |
131 |
|
132 |
local name="$1"; shift |
133 |
|
134 |
_daemon_pidfile $name |
135 |
} |
136 |
|
137 |
# Prints path to daemon executable. |
138 |
daemon_executable() { |
139 |
if [[ "$#" -lt 1 ]]; then |
140 |
echo 'Missing daemon name.' >&2 |
141 |
return 1 |
142 |
fi |
143 |
|
144 |
local name="$1"; shift |
145 |
|
146 |
_daemon_executable $name |
147 |
} |
148 |
|
149 |
# Prints a list of all daemons in the order in which they should be started |
150 |
list_start_daemons() { |
151 |
local name |
152 |
|
153 |
for name in "${DAEMONS[@]}"; do |
154 |
echo "$name" |
155 |
done |
156 |
} |
157 |
|
158 |
# Prints a list of all daemons in the order in which they should be stopped |
159 |
list_stop_daemons() { |
160 |
list_start_daemons | tac |
161 |
} |
162 |
|
163 |
# Checks whether a daemon name is known |
164 |
is_daemon_name() { |
165 |
if [[ "$#" -lt 1 ]]; then |
166 |
echo 'Missing daemon name.' >&2 |
167 |
return 1 |
168 |
fi |
169 |
|
170 |
local name="$1"; shift |
171 |
|
172 |
for i in "${DAEMONS[@]}"; do |
173 |
if [[ "$i" == "$name" ]]; then |
174 |
return 0 |
175 |
fi |
176 |
done |
177 |
|
178 |
echo "Unknown daemon name '$name'" >&2 |
179 |
return 1 |
180 |
} |
181 |
|
182 |
# Checks whether daemon is running |
183 |
check() { |
184 |
if [[ "$#" -lt 1 ]]; then |
185 |
echo 'Missing daemon name.' >&2 |
186 |
return 1 |
187 |
fi |
188 |
|
189 |
local name="$1"; shift |
190 |
local pidfile=$(_daemon_pidfile $name) |
191 |
local daemonexec=$(_daemon_executable $name) |
192 |
|
193 |
if type -p start-stop-daemon >/dev/null; then |
194 |
start-stop-daemon --stop --signal 0 --quiet \ |
195 |
--pidfile $pidfile |
196 |
else |
197 |
_ignore_error status \ |
198 |
-p $pidfile \ |
199 |
$daemonexec |
200 |
fi |
201 |
} |
202 |
|
203 |
# Starts a daemon |
204 |
start() { |
205 |
if [[ "$#" -lt 1 ]]; then |
206 |
echo 'Missing daemon name.' >&2 |
207 |
return 1 |
208 |
fi |
209 |
|
210 |
local name="$1"; shift |
211 |
# Convert daemon name to uppercase after removing "ganeti-" prefix |
212 |
local plain_name=${name#ganeti-} |
213 |
local ucname=$(tr a-z A-Z <<<$plain_name) |
214 |
local pidfile=$(_daemon_pidfile $name) |
215 |
local usergroup=$(_daemon_usergroup $plain_name) |
216 |
local daemonexec=$(_daemon_executable $name) |
217 |
|
218 |
if [[ "$name" == ganeti-confd ]] && ! _confd_enabled; then |
219 |
echo 'ganeti-confd disabled at build time' >&2 |
220 |
return 1 |
221 |
fi |
222 |
|
223 |
# Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS |
224 |
eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\"" |
225 |
|
226 |
@PKGLIBDIR@/ensure-dirs |
227 |
|
228 |
if type -p start-stop-daemon >/dev/null; then |
229 |
start-stop-daemon --start --quiet --oknodo \ |
230 |
--pidfile $pidfile \ |
231 |
--startas $daemonexec \ |
232 |
--chuid $usergroup \ |
233 |
-- $args "$@" |
234 |
else |
235 |
# TODO: Find a way to start daemon with a group, until then the group must |
236 |
# be removed |
237 |
_ignore_error daemon \ |
238 |
--pidfile $pidfile \ |
239 |
--user ${usergroup%:*} \ |
240 |
$daemonexec $args "$@" |
241 |
fi |
242 |
} |
243 |
|
244 |
# Stops a daemon |
245 |
stop() { |
246 |
if [[ "$#" -lt 1 ]]; then |
247 |
echo 'Missing daemon name.' >&2 |
248 |
return 1 |
249 |
fi |
250 |
|
251 |
local name="$1"; shift |
252 |
local pidfile=$(_daemon_pidfile $name) |
253 |
|
254 |
if type -p start-stop-daemon >/dev/null; then |
255 |
start-stop-daemon --stop --quiet --oknodo --retry 30 \ |
256 |
--pidfile $pidfile |
257 |
else |
258 |
_ignore_error killproc -p $pidfile $name |
259 |
fi |
260 |
} |
261 |
|
262 |
# Starts a daemon if it's not yet running |
263 |
check_and_start() { |
264 |
local name="$1" |
265 |
|
266 |
if ! check $name; then |
267 |
start $name |
268 |
fi |
269 |
} |
270 |
|
271 |
# Starts the master role |
272 |
start_master() { |
273 |
start ganeti-masterd |
274 |
start ganeti-rapi |
275 |
} |
276 |
|
277 |
# Stops the master role |
278 |
stop_master() { |
279 |
stop ganeti-rapi |
280 |
stop ganeti-masterd |
281 |
} |
282 |
|
283 |
# Start all daemons |
284 |
start_all() { |
285 |
for i in $(list_start_daemons); do |
286 |
local rc=0 |
287 |
|
288 |
# Try to start daemon |
289 |
start $i || rc=$? |
290 |
|
291 |
if ! errmsg=$(check_exitcode $rc); then |
292 |
echo "$errmsg" >&2 |
293 |
return 1 |
294 |
fi |
295 |
done |
296 |
|
297 |
return 0 |
298 |
} |
299 |
|
300 |
# Stop all daemons |
301 |
stop_all() { |
302 |
for i in $(list_stop_daemons); do |
303 |
stop $i |
304 |
done |
305 |
} |
306 |
|
307 |
# Reloads the SSH keys |
308 |
reload_ssh_keys() { |
309 |
@RPL_SSH_INITD_SCRIPT@ restart |
310 |
} |
311 |
|
312 |
# Read @SYSCONFDIR@/rc.d/init.d/functions if start-stop-daemon not available |
313 |
if ! type -p start-stop-daemon >/dev/null && \ |
314 |
[[ -f @SYSCONFDIR@/rc.d/init.d/functions ]]; then |
315 |
_ignore_error . @SYSCONFDIR@/rc.d/init.d/functions |
316 |
fi |
317 |
|
318 |
if [[ "$#" -lt 1 ]]; then |
319 |
echo "Usage: $0 <action>" >&2 |
320 |
exit 1 |
321 |
fi |
322 |
|
323 |
orig_action=$1; shift |
324 |
|
325 |
if [[ "$orig_action" == *_* ]]; then |
326 |
echo "Command must not contain underscores" >&2 |
327 |
exit 1 |
328 |
fi |
329 |
|
330 |
# Replace all dashes (-) with underlines (_) |
331 |
action=${orig_action//-/_} |
332 |
|
333 |
# Is it a known function? |
334 |
if ! declare -F "$action" >/dev/null 2>&1; then |
335 |
echo "Unknown command: $orig_action" >&2 |
336 |
exit 1 |
337 |
fi |
338 |
|
339 |
# Call handler function |
340 |
$action "$@" |