root / daemons / daemon-util.in @ a34d8cc0
History | View | Annotate | Download (4.6 kB)
1 |
#!/bin/bash |
---|---|
2 |
# |
3 |
|
4 |
# Copyright (C) 2009 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 |
defaults_file=@SYSCONFDIR@/default/ganeti |
24 |
|
25 |
# This is a list of all daemons and the order in which they're started. The |
26 |
# order is important as there are dependencies between them. On shutdown, |
27 |
# they're stopped in reverse order. |
28 |
DAEMONS=( |
29 |
ganeti-noded |
30 |
ganeti-masterd |
31 |
ganeti-rapi |
32 |
ganeti-confd |
33 |
) |
34 |
|
35 |
NODED_ARGS= |
36 |
MASTERD_ARGS= |
37 |
CONFD_ARGS= |
38 |
RAPI_ARGS= |
39 |
|
40 |
# Read defaults file if it exists |
41 |
if [[ -s $defaults_file ]]; then |
42 |
. $defaults_file |
43 |
fi |
44 |
|
45 |
_daemon_pidfile() { |
46 |
echo "@LOCALSTATEDIR@/run/ganeti/$1.pid" |
47 |
} |
48 |
|
49 |
_daemon_executable() { |
50 |
echo "@PREFIX@/sbin/$1" |
51 |
} |
52 |
|
53 |
# Checks whether the local machine is part of a cluster |
54 |
check_config() { |
55 |
local server_pem=@LOCALSTATEDIR@/lib/ganeti/server.pem |
56 |
local fname |
57 |
|
58 |
for fname in $server_pem; do |
59 |
if [[ ! -f $fname ]]; then |
60 |
echo "Missing configuration file $fname" >&2 |
61 |
return 1 |
62 |
fi |
63 |
done |
64 |
|
65 |
return 0 |
66 |
} |
67 |
|
68 |
# Checks the exit code of a daemon |
69 |
check_exitcode() { |
70 |
if [[ "$#" -lt 1 ]]; then |
71 |
echo 'Missing exit code.' >&2 |
72 |
return 1 |
73 |
fi |
74 |
|
75 |
local rc="$1"; shift |
76 |
|
77 |
case "$rc" in |
78 |
0) ;; |
79 |
11) |
80 |
echo "not master" |
81 |
;; |
82 |
*) |
83 |
echo "exit code $rc" |
84 |
return 1 |
85 |
;; |
86 |
esac |
87 |
|
88 |
return 0 |
89 |
} |
90 |
|
91 |
# Prints a list of all daemons in the order in which they should be started |
92 |
list_start_daemons() { |
93 |
local name |
94 |
|
95 |
for name in "${DAEMONS[@]}"; do |
96 |
echo "$name" |
97 |
done |
98 |
} |
99 |
|
100 |
# Prints a list of all daemons in the order in which they should be stopped |
101 |
list_stop_daemons() { |
102 |
list_start_daemons | tac |
103 |
} |
104 |
|
105 |
# Checks whether a daemon name is known |
106 |
is_daemon_name() { |
107 |
if [[ "$#" -lt 1 ]]; then |
108 |
echo 'Missing daemon name.' >&2 |
109 |
return 1 |
110 |
fi |
111 |
|
112 |
local name="$1"; shift |
113 |
|
114 |
for i in "${DAEMONS[@]}"; do |
115 |
if [[ "$i" == "$name" ]]; then |
116 |
return 0 |
117 |
fi |
118 |
done |
119 |
|
120 |
echo "Unknown daemon name '$name'" >&2 |
121 |
return 1 |
122 |
} |
123 |
|
124 |
# Checks whether daemon is running |
125 |
check() { |
126 |
if [[ "$#" -lt 1 ]]; then |
127 |
echo 'Missing daemon name.' >&2 |
128 |
return 1 |
129 |
fi |
130 |
|
131 |
local name="$1"; shift |
132 |
|
133 |
start-stop-daemon --stop --signal 0 --quiet \ |
134 |
--pidfile $(_daemon_pidfile $name) |
135 |
} |
136 |
|
137 |
# Starts a daemon |
138 |
start() { |
139 |
if [[ "$#" -lt 1 ]]; then |
140 |
echo 'Missing daemon name.' >&2 |
141 |
return 1 |
142 |
fi |
143 |
|
144 |
local name="$1"; shift |
145 |
|
146 |
# Convert daemon name to uppercase after removing "ganeti-" prefix |
147 |
local ucname=$(echo ${name#ganeti-} | tr a-z A-Z) |
148 |
|
149 |
# Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS |
150 |
eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\"" |
151 |
|
152 |
start-stop-daemon --start --quiet --oknodo \ |
153 |
--pidfile $(_daemon_pidfile $name) \ |
154 |
--startas $(_daemon_executable $name) \ |
155 |
-- $args "$@" |
156 |
} |
157 |
|
158 |
# Stops a daemon |
159 |
stop() { |
160 |
if [[ "$#" -lt 1 ]]; then |
161 |
echo 'Missing daemon name.' >&2 |
162 |
return 1 |
163 |
fi |
164 |
|
165 |
local name="$1"; shift |
166 |
|
167 |
start-stop-daemon --stop --quiet --oknodo --retry 30 \ |
168 |
--pidfile $(_daemon_pidfile $name) |
169 |
} |
170 |
|
171 |
# Starts a daemon if it's not yet running |
172 |
check_and_start() { |
173 |
local name="$1" |
174 |
|
175 |
if ! check $name; then |
176 |
start $name |
177 |
fi |
178 |
} |
179 |
|
180 |
# Starts the master role |
181 |
start_master() { |
182 |
start ganeti-masterd |
183 |
start ganeti-rapi |
184 |
} |
185 |
|
186 |
# Stops the master role |
187 |
stop_master() { |
188 |
stop ganeti-rapi |
189 |
stop ganeti-masterd |
190 |
} |
191 |
|
192 |
# Start all daemons |
193 |
start_all() { |
194 |
for i in $(list_start_daemons); do |
195 |
local rc=0 |
196 |
|
197 |
# Try to start daemon |
198 |
start $i || rc=$? |
199 |
|
200 |
if ! errmsg=$(check_exitcode $rc); then |
201 |
echo "$errmsg" >&2 |
202 |
return 1 |
203 |
fi |
204 |
done |
205 |
|
206 |
return 0 |
207 |
} |
208 |
|
209 |
# Stop all daemons |
210 |
stop_all() { |
211 |
for i in $(list_stop_daemons); do |
212 |
stop $i |
213 |
done |
214 |
} |
215 |
|
216 |
# Reloads the SSH keys |
217 |
reload_ssh_keys() { |
218 |
@RPL_SSH_INITD_SCRIPT@ restart |
219 |
} |
220 |
|
221 |
if [[ "$#" -lt 1 ]]; then |
222 |
echo "Usage: $0 <action>" >&2 |
223 |
exit 1 |
224 |
fi |
225 |
|
226 |
orig_action=$1; shift |
227 |
|
228 |
if [[ "$orig_action" == *_* ]]; then |
229 |
echo "Command must not contain underscores" >&2 |
230 |
exit 1 |
231 |
fi |
232 |
|
233 |
# Replace all dashes (-) with underlines (_) |
234 |
action=${orig_action//-/_} |
235 |
|
236 |
# Is it a known function? |
237 |
if ! declare -F "$action" >/dev/null 2>&1; then |
238 |
echo "Unknown command: $orig_action" >&2 |
239 |
exit 1 |
240 |
fi |
241 |
|
242 |
# Call handler function |
243 |
$action "$@" |