Statistics
| Branch: | Tag: | Revision:

root / daemons / daemon-util.in @ d9c82a4e

History | View | Annotate | Download (5.1 kB)

1 f154a7a3 Michael Hanselmann
#!/bin/bash
2 f154a7a3 Michael Hanselmann
#
3 f154a7a3 Michael Hanselmann
4 f154a7a3 Michael Hanselmann
# Copyright (C) 2009 Google Inc.
5 f154a7a3 Michael Hanselmann
#
6 f154a7a3 Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 f154a7a3 Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 f154a7a3 Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 f154a7a3 Michael Hanselmann
# (at your option) any later version.
10 f154a7a3 Michael Hanselmann
#
11 f154a7a3 Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 f154a7a3 Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 f154a7a3 Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 f154a7a3 Michael Hanselmann
# General Public License for more details.
15 f154a7a3 Michael Hanselmann
#
16 f154a7a3 Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 f154a7a3 Michael Hanselmann
# along with this program; if not, write to the Free Software
18 f154a7a3 Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 f154a7a3 Michael Hanselmann
# 02110-1301, USA.
20 f154a7a3 Michael Hanselmann
21 f154a7a3 Michael Hanselmann
set -e
22 f154a7a3 Michael Hanselmann
23 f154a7a3 Michael Hanselmann
defaults_file=@SYSCONFDIR@/default/ganeti
24 f154a7a3 Michael Hanselmann
25 5d8acbdd Michael Hanselmann
# This is a list of all daemons and the order in which they're started. The
26 5d8acbdd Michael Hanselmann
# order is important as there are dependencies between them. On shutdown,
27 5d8acbdd Michael Hanselmann
# they're stopped in reverse order.
28 5d8acbdd Michael Hanselmann
DAEMONS=(
29 5d8acbdd Michael Hanselmann
  ganeti-noded
30 5d8acbdd Michael Hanselmann
  ganeti-masterd
31 5d8acbdd Michael Hanselmann
  ganeti-rapi
32 5d8acbdd Michael Hanselmann
  ganeti-confd
33 5d8acbdd Michael Hanselmann
  )
34 5d8acbdd Michael Hanselmann
35 f154a7a3 Michael Hanselmann
NODED_ARGS=
36 f154a7a3 Michael Hanselmann
MASTERD_ARGS=
37 f154a7a3 Michael Hanselmann
CONFD_ARGS=
38 f154a7a3 Michael Hanselmann
RAPI_ARGS=
39 f154a7a3 Michael Hanselmann
40 f154a7a3 Michael Hanselmann
# Read defaults file if it exists
41 f154a7a3 Michael Hanselmann
if [[ -s $defaults_file ]]; then
42 f154a7a3 Michael Hanselmann
  . $defaults_file
43 f154a7a3 Michael Hanselmann
fi
44 f154a7a3 Michael Hanselmann
45 f154a7a3 Michael Hanselmann
_daemon_pidfile() {
46 f154a7a3 Michael Hanselmann
  echo "@LOCALSTATEDIR@/run/ganeti/$1.pid"
47 f154a7a3 Michael Hanselmann
}
48 f154a7a3 Michael Hanselmann
49 a34d8cc0 Michael Hanselmann
_daemon_executable() {
50 a34d8cc0 Michael Hanselmann
  echo "@PREFIX@/sbin/$1"
51 a34d8cc0 Michael Hanselmann
}
52 a34d8cc0 Michael Hanselmann
53 cbccd9ca René Nussbaumer
_daemon_usergroup() {
54 cbccd9ca René Nussbaumer
  case "$1" in
55 cbccd9ca René Nussbaumer
    masterd)
56 cbccd9ca René Nussbaumer
      echo "@GNTMASTERUSER@:@GNTMASTERDGROUP@"
57 cbccd9ca René Nussbaumer
      ;;
58 cbccd9ca René Nussbaumer
    confd)
59 cbccd9ca René Nussbaumer
      echo "@GNTCONFDUSER@:@GNTCONFDGROUP@"
60 cbccd9ca René Nussbaumer
      ;;
61 cbccd9ca René Nussbaumer
    rapi)
62 cbccd9ca René Nussbaumer
      echo "@GNTRAPIUSER@:@GNTRAPIGROUP@"
63 cbccd9ca René Nussbaumer
      ;;
64 cbccd9ca René Nussbaumer
    noded)
65 cbccd9ca René Nussbaumer
      echo "@GNTNODEDUSER@:@GNTDAEMONSGROUP@"
66 cbccd9ca René Nussbaumer
      ;;
67 cbccd9ca René Nussbaumer
    *)
68 cbccd9ca René Nussbaumer
      echo "root:@GNTDAEMONSGROUP@"
69 cbccd9ca René Nussbaumer
      ;;
70 cbccd9ca René Nussbaumer
  esac
71 cbccd9ca René Nussbaumer
}
72 cbccd9ca René Nussbaumer
73 d2baa21d Michael Hanselmann
# Checks whether the local machine is part of a cluster
74 d2baa21d Michael Hanselmann
check_config() {
75 d2baa21d Michael Hanselmann
  local server_pem=@LOCALSTATEDIR@/lib/ganeti/server.pem
76 d2baa21d Michael Hanselmann
  local fname
77 d2baa21d Michael Hanselmann
78 d2baa21d Michael Hanselmann
  for fname in $server_pem; do
79 d2baa21d Michael Hanselmann
    if [[ ! -f $fname ]]; then
80 d2baa21d Michael Hanselmann
      echo "Missing configuration file $fname" >&2
81 d2baa21d Michael Hanselmann
      return 1
82 d2baa21d Michael Hanselmann
    fi
83 d2baa21d Michael Hanselmann
  done
84 d2baa21d Michael Hanselmann
85 d2baa21d Michael Hanselmann
  return 0
86 d2baa21d Michael Hanselmann
}
87 d2baa21d Michael Hanselmann
88 10c5c1c3 Michael Hanselmann
# Checks the exit code of a daemon
89 10c5c1c3 Michael Hanselmann
check_exitcode() {
90 10c5c1c3 Michael Hanselmann
  if [[ "$#" -lt 1 ]]; then
91 10c5c1c3 Michael Hanselmann
    echo 'Missing exit code.' >&2
92 10c5c1c3 Michael Hanselmann
    return 1
93 10c5c1c3 Michael Hanselmann
  fi
94 10c5c1c3 Michael Hanselmann
95 10c5c1c3 Michael Hanselmann
  local rc="$1"; shift
96 10c5c1c3 Michael Hanselmann
97 10c5c1c3 Michael Hanselmann
  case "$rc" in
98 10c5c1c3 Michael Hanselmann
    0) ;;
99 10c5c1c3 Michael Hanselmann
    11)
100 10c5c1c3 Michael Hanselmann
      echo "not master"
101 10c5c1c3 Michael Hanselmann
    ;;
102 10c5c1c3 Michael Hanselmann
    *)
103 10c5c1c3 Michael Hanselmann
      echo "exit code $rc"
104 10c5c1c3 Michael Hanselmann
      return 1
105 10c5c1c3 Michael Hanselmann
    ;;
106 10c5c1c3 Michael Hanselmann
  esac
107 10c5c1c3 Michael Hanselmann
108 10c5c1c3 Michael Hanselmann
  return 0
109 10c5c1c3 Michael Hanselmann
}
110 10c5c1c3 Michael Hanselmann
111 5d8acbdd Michael Hanselmann
# Prints a list of all daemons in the order in which they should be started
112 5d8acbdd Michael Hanselmann
list_start_daemons() {
113 5d8acbdd Michael Hanselmann
  local name
114 5d8acbdd Michael Hanselmann
115 5d8acbdd Michael Hanselmann
  for name in "${DAEMONS[@]}"; do
116 5d8acbdd Michael Hanselmann
    echo "$name"
117 5d8acbdd Michael Hanselmann
  done
118 5d8acbdd Michael Hanselmann
}
119 5d8acbdd Michael Hanselmann
120 5d8acbdd Michael Hanselmann
# Prints a list of all daemons in the order in which they should be stopped
121 5d8acbdd Michael Hanselmann
list_stop_daemons() {
122 5d8acbdd Michael Hanselmann
  list_start_daemons | tac
123 5d8acbdd Michael Hanselmann
}
124 5d8acbdd Michael Hanselmann
125 5d8acbdd Michael Hanselmann
# Checks whether a daemon name is known
126 5d8acbdd Michael Hanselmann
is_daemon_name() {
127 5d8acbdd Michael Hanselmann
  if [[ "$#" -lt 1 ]]; then
128 5d8acbdd Michael Hanselmann
    echo 'Missing daemon name.' >&2
129 5d8acbdd Michael Hanselmann
    return 1
130 5d8acbdd Michael Hanselmann
  fi
131 5d8acbdd Michael Hanselmann
132 5d8acbdd Michael Hanselmann
  local name="$1"; shift
133 5d8acbdd Michael Hanselmann
134 5d8acbdd Michael Hanselmann
  for i in "${DAEMONS[@]}"; do
135 5d8acbdd Michael Hanselmann
    if [[ "$i" == "$name" ]]; then
136 5d8acbdd Michael Hanselmann
      return 0
137 5d8acbdd Michael Hanselmann
    fi
138 5d8acbdd Michael Hanselmann
  done
139 5d8acbdd Michael Hanselmann
140 5d8acbdd Michael Hanselmann
  echo "Unknown daemon name '$name'" >&2
141 5d8acbdd Michael Hanselmann
  return 1
142 5d8acbdd Michael Hanselmann
}
143 5d8acbdd Michael Hanselmann
144 f154a7a3 Michael Hanselmann
# Checks whether daemon is running
145 f154a7a3 Michael Hanselmann
check() {
146 f154a7a3 Michael Hanselmann
  if [[ "$#" -lt 1 ]]; then
147 f154a7a3 Michael Hanselmann
    echo 'Missing daemon name.' >&2
148 49d43244 Michael Hanselmann
    return 1
149 f154a7a3 Michael Hanselmann
  fi
150 f154a7a3 Michael Hanselmann
151 f154a7a3 Michael Hanselmann
  local name="$1"; shift
152 f154a7a3 Michael Hanselmann
153 f154a7a3 Michael Hanselmann
  start-stop-daemon --stop --signal 0 --quiet \
154 f154a7a3 Michael Hanselmann
    --pidfile $(_daemon_pidfile $name)
155 f154a7a3 Michael Hanselmann
}
156 f154a7a3 Michael Hanselmann
157 f154a7a3 Michael Hanselmann
# Starts a daemon
158 f154a7a3 Michael Hanselmann
start() {
159 f154a7a3 Michael Hanselmann
  if [[ "$#" -lt 1 ]]; then
160 f154a7a3 Michael Hanselmann
    echo 'Missing daemon name.' >&2
161 49d43244 Michael Hanselmann
    return 1
162 f154a7a3 Michael Hanselmann
  fi
163 f154a7a3 Michael Hanselmann
164 f154a7a3 Michael Hanselmann
  local name="$1"; shift
165 f154a7a3 Michael Hanselmann
166 f154a7a3 Michael Hanselmann
  # Convert daemon name to uppercase after removing "ganeti-" prefix
167 cbccd9ca René Nussbaumer
  local plain_name=${name#ganeti-}
168 cbccd9ca René Nussbaumer
  local ucname=$(tr a-z A-Z <<<$plain_name)
169 f154a7a3 Michael Hanselmann
170 f154a7a3 Michael Hanselmann
  # Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS
171 03956d39 Michael Hanselmann
  eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\""
172 f154a7a3 Michael Hanselmann
173 46a8da3b René Nussbaumer
  @PKGLIBDIR@/ensure-dirs
174 46a8da3b René Nussbaumer
175 f154a7a3 Michael Hanselmann
  start-stop-daemon --start --quiet --oknodo \
176 f154a7a3 Michael Hanselmann
    --pidfile $(_daemon_pidfile $name) \
177 a34d8cc0 Michael Hanselmann
    --startas $(_daemon_executable $name) \
178 cbccd9ca René Nussbaumer
    --chuid $(_daemon_usergroup $plain_name) \
179 f154a7a3 Michael Hanselmann
    -- $args "$@"
180 f154a7a3 Michael Hanselmann
}
181 f154a7a3 Michael Hanselmann
182 f154a7a3 Michael Hanselmann
# Stops a daemon
183 f154a7a3 Michael Hanselmann
stop() {
184 f154a7a3 Michael Hanselmann
  if [[ "$#" -lt 1 ]]; then
185 f154a7a3 Michael Hanselmann
    echo 'Missing daemon name.' >&2
186 49d43244 Michael Hanselmann
    return 1
187 f154a7a3 Michael Hanselmann
  fi
188 f154a7a3 Michael Hanselmann
189 f154a7a3 Michael Hanselmann
  local name="$1"; shift
190 f154a7a3 Michael Hanselmann
191 f154a7a3 Michael Hanselmann
  start-stop-daemon --stop --quiet --oknodo --retry 30 \
192 f154a7a3 Michael Hanselmann
    --pidfile $(_daemon_pidfile $name)
193 f154a7a3 Michael Hanselmann
}
194 f154a7a3 Michael Hanselmann
195 f154a7a3 Michael Hanselmann
# Starts a daemon if it's not yet running
196 f154a7a3 Michael Hanselmann
check_and_start() {
197 f154a7a3 Michael Hanselmann
  local name="$1"
198 f154a7a3 Michael Hanselmann
199 f154a7a3 Michael Hanselmann
  if ! check $name; then
200 f154a7a3 Michael Hanselmann
    start $name
201 f154a7a3 Michael Hanselmann
  fi
202 f154a7a3 Michael Hanselmann
}
203 f154a7a3 Michael Hanselmann
204 f154a7a3 Michael Hanselmann
# Starts the master role
205 f154a7a3 Michael Hanselmann
start_master() {
206 f154a7a3 Michael Hanselmann
  start ganeti-masterd
207 f154a7a3 Michael Hanselmann
  start ganeti-rapi
208 f154a7a3 Michael Hanselmann
}
209 f154a7a3 Michael Hanselmann
210 f154a7a3 Michael Hanselmann
# Stops the master role
211 f154a7a3 Michael Hanselmann
stop_master() {
212 f154a7a3 Michael Hanselmann
  stop ganeti-rapi
213 f154a7a3 Michael Hanselmann
  stop ganeti-masterd
214 f154a7a3 Michael Hanselmann
}
215 f154a7a3 Michael Hanselmann
216 2250afb3 Michael Hanselmann
# Start all daemons
217 2250afb3 Michael Hanselmann
start_all() {
218 2250afb3 Michael Hanselmann
  for i in $(list_start_daemons); do
219 2250afb3 Michael Hanselmann
    local rc=0
220 2250afb3 Michael Hanselmann
221 2250afb3 Michael Hanselmann
    # Try to start daemon
222 2250afb3 Michael Hanselmann
    start $i || rc=$?
223 2250afb3 Michael Hanselmann
224 2250afb3 Michael Hanselmann
    if ! errmsg=$(check_exitcode $rc); then
225 2250afb3 Michael Hanselmann
      echo "$errmsg" >&2
226 2250afb3 Michael Hanselmann
      return 1
227 2250afb3 Michael Hanselmann
    fi
228 2250afb3 Michael Hanselmann
  done
229 2250afb3 Michael Hanselmann
230 2250afb3 Michael Hanselmann
  return 0
231 2250afb3 Michael Hanselmann
}
232 2250afb3 Michael Hanselmann
233 2250afb3 Michael Hanselmann
# Stop all daemons
234 2250afb3 Michael Hanselmann
stop_all() {
235 2250afb3 Michael Hanselmann
  for i in $(list_stop_daemons); do
236 2250afb3 Michael Hanselmann
    stop $i
237 2250afb3 Michael Hanselmann
  done
238 2250afb3 Michael Hanselmann
}
239 2250afb3 Michael Hanselmann
240 7e1fac25 Michael Hanselmann
# Reloads the SSH keys
241 7e1fac25 Michael Hanselmann
reload_ssh_keys() {
242 7e1fac25 Michael Hanselmann
  @RPL_SSH_INITD_SCRIPT@ restart
243 7e1fac25 Michael Hanselmann
}
244 7e1fac25 Michael Hanselmann
245 f154a7a3 Michael Hanselmann
if [[ "$#" -lt 1 ]]; then
246 f154a7a3 Michael Hanselmann
  echo "Usage: $0 <action>" >&2
247 f154a7a3 Michael Hanselmann
  exit 1
248 f154a7a3 Michael Hanselmann
fi
249 f154a7a3 Michael Hanselmann
250 f154a7a3 Michael Hanselmann
orig_action=$1; shift
251 f154a7a3 Michael Hanselmann
252 7289c16e Michael Hanselmann
if [[ "$orig_action" == *_* ]]; then
253 7289c16e Michael Hanselmann
  echo "Command must not contain underscores" >&2
254 7289c16e Michael Hanselmann
  exit 1
255 7289c16e Michael Hanselmann
fi
256 7289c16e Michael Hanselmann
257 f154a7a3 Michael Hanselmann
# Replace all dashes (-) with underlines (_)
258 f154a7a3 Michael Hanselmann
action=${orig_action//-/_}
259 f154a7a3 Michael Hanselmann
260 f154a7a3 Michael Hanselmann
# Is it a known function?
261 f154a7a3 Michael Hanselmann
if ! declare -F "$action" >/dev/null 2>&1; then
262 f154a7a3 Michael Hanselmann
  echo "Unknown command: $orig_action" >&2
263 f154a7a3 Michael Hanselmann
  exit 1
264 f154a7a3 Michael Hanselmann
fi
265 f154a7a3 Michael Hanselmann
266 f154a7a3 Michael Hanselmann
# Call handler function
267 f154a7a3 Michael Hanselmann
$action "$@"