root / daemons / daemon-util.in @ 5d8acbdd
History | View | Annotate | Download (4.2 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 |
# Checks whether the local machine is part of a cluster |
50 |
check_config() { |
51 |
local server_pem=@LOCALSTATEDIR@/lib/ganeti/server.pem |
52 |
local fname |
53 |
|
54 |
for fname in $server_pem; do |
55 |
if [[ ! -f $fname ]]; then |
56 |
echo "Missing configuration file $fname" >&2 |
57 |
return 1 |
58 |
fi |
59 |
done |
60 |
|
61 |
return 0 |
62 |
} |
63 |
|
64 |
# Checks the exit code of a daemon |
65 |
check_exitcode() { |
66 |
if [[ "$#" -lt 1 ]]; then |
67 |
echo 'Missing exit code.' >&2 |
68 |
return 1 |
69 |
fi |
70 |
|
71 |
local rc="$1"; shift |
72 |
|
73 |
case "$rc" in |
74 |
0) ;; |
75 |
11) |
76 |
echo "not master" |
77 |
;; |
78 |
*) |
79 |
echo "exit code $rc" |
80 |
return 1 |
81 |
;; |
82 |
esac |
83 |
|
84 |
return 0 |
85 |
} |
86 |
|
87 |
# Prints a list of all daemons in the order in which they should be started |
88 |
list_start_daemons() { |
89 |
local name |
90 |
|
91 |
for name in "${DAEMONS[@]}"; do |
92 |
echo "$name" |
93 |
done |
94 |
} |
95 |
|
96 |
# Prints a list of all daemons in the order in which they should be stopped |
97 |
list_stop_daemons() { |
98 |
list_start_daemons | tac |
99 |
} |
100 |
|
101 |
# Checks whether a daemon name is known |
102 |
is_daemon_name() { |
103 |
if [[ "$#" -lt 1 ]]; then |
104 |
echo 'Missing daemon name.' >&2 |
105 |
return 1 |
106 |
fi |
107 |
|
108 |
local name="$1"; shift |
109 |
|
110 |
for i in "${DAEMONS[@]}"; do |
111 |
if [[ "$i" == "$name" ]]; then |
112 |
return 0 |
113 |
fi |
114 |
done |
115 |
|
116 |
echo "Unknown daemon name '$name'" >&2 |
117 |
return 1 |
118 |
} |
119 |
|
120 |
# Checks whether daemon is running |
121 |
check() { |
122 |
if [[ "$#" -lt 1 ]]; then |
123 |
echo 'Missing daemon name.' >&2 |
124 |
exit 1 |
125 |
fi |
126 |
|
127 |
local name="$1"; shift |
128 |
|
129 |
start-stop-daemon --stop --signal 0 --quiet \ |
130 |
--pidfile $(_daemon_pidfile $name) |
131 |
} |
132 |
|
133 |
# Starts a daemon |
134 |
start() { |
135 |
if [[ "$#" -lt 1 ]]; then |
136 |
echo 'Missing daemon name.' >&2 |
137 |
exit 1 |
138 |
fi |
139 |
|
140 |
local name="$1"; shift |
141 |
|
142 |
# Convert daemon name to uppercase after removing "ganeti-" prefix |
143 |
local ucname=$(echo ${name#ganeti-} | tr a-z A-Z) |
144 |
|
145 |
# Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS |
146 |
eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\"" |
147 |
|
148 |
start-stop-daemon --start --quiet --oknodo \ |
149 |
--pidfile $(_daemon_pidfile $name) \ |
150 |
--startas "@PREFIX@/sbin/$name" \ |
151 |
-- $args "$@" |
152 |
} |
153 |
|
154 |
# Stops a daemon |
155 |
stop() { |
156 |
if [[ "$#" -lt 1 ]]; then |
157 |
echo 'Missing daemon name.' >&2 |
158 |
exit 1 |
159 |
fi |
160 |
|
161 |
local name="$1"; shift |
162 |
|
163 |
start-stop-daemon --stop --quiet --oknodo --retry 30 \ |
164 |
--pidfile $(_daemon_pidfile $name) |
165 |
} |
166 |
|
167 |
# Starts a daemon if it's not yet running |
168 |
check_and_start() { |
169 |
local name="$1" |
170 |
|
171 |
if ! check $name; then |
172 |
start $name |
173 |
fi |
174 |
} |
175 |
|
176 |
# Starts the master role |
177 |
start_master() { |
178 |
start ganeti-masterd |
179 |
start ganeti-rapi |
180 |
} |
181 |
|
182 |
# Stops the master role |
183 |
stop_master() { |
184 |
stop ganeti-rapi |
185 |
stop ganeti-masterd |
186 |
} |
187 |
|
188 |
# Reloads the SSH keys |
189 |
reload_ssh_keys() { |
190 |
@RPL_SSH_INITD_SCRIPT@ restart |
191 |
} |
192 |
|
193 |
if [[ "$#" -lt 1 ]]; then |
194 |
echo "Usage: $0 <action>" >&2 |
195 |
exit 1 |
196 |
fi |
197 |
|
198 |
orig_action=$1; shift |
199 |
|
200 |
if [[ "$orig_action" == *_* ]]; then |
201 |
echo "Command must not contain underscores" >&2 |
202 |
exit 1 |
203 |
fi |
204 |
|
205 |
# Replace all dashes (-) with underlines (_) |
206 |
action=${orig_action//-/_} |
207 |
|
208 |
# Is it a known function? |
209 |
if ! declare -F "$action" >/dev/null 2>&1; then |
210 |
echo "Unknown command: $orig_action" >&2 |
211 |
exit 1 |
212 |
fi |
213 |
|
214 |
# Call handler function |
215 |
$action "$@" |