Statistics
| Branch: | Tag: | Revision:

root / daemons / daemon-util.in @ 34c9ee7b

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