Statistics
| Branch: | Tag: | Revision:

root / daemons / daemon-util.in @ c0eb47b9

History | View | Annotate | Download (7.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
@SHELL_ENV_INIT@
24

    
25
readonly defaults_file="$SYSCONFDIR/default/ganeti"
26

    
27
# This is a list of all daemons and the order in which they're started. The
28
# order is important as there are dependencies between them. On shutdown,
29
# they're stopped in reverse order.
30
DAEMONS=(
31
  ganeti-wconfd
32
  ganeti-noded
33
  ganeti-masterd
34
  ganeti-rapi
35
  ganeti-luxid
36
  )
37

    
38
_confd_enabled() {
39
  [[ "@CUSTOM_ENABLE_CONFD@" == True ]]
40
}
41

    
42
if _confd_enabled; then
43
  DAEMONS+=( ganeti-confd )
44
fi
45

    
46
_mond_enabled() {
47
  [[ "@CUSTOM_ENABLE_MOND@" == True ]]
48
}
49

    
50
if _mond_enabled; then
51
  DAEMONS+=( ganeti-mond )
52
fi
53

    
54
NODED_ARGS=
55
MASTERD_ARGS=
56
CONFD_ARGS=
57
WCONFD_ARGS=
58
LUXID_ARGS=
59
RAPI_ARGS=
60
MOND_ARGS=
61

    
62
# Read defaults file if it exists
63
if [[ -s $defaults_file ]]; then
64
  . $defaults_file
65
fi
66

    
67
# Meant to facilitate use utilities in /etc/rc.d/init.d/functions in case
68
# start-stop-daemon is not available.
69
_ignore_error() {
70
  eval "$@" || :
71
}
72

    
73
_daemon_pidfile() {
74
  echo "$RUN_DIR/$1.pid"
75
}
76

    
77
_daemon_executable() {
78
  echo "@PREFIX@/sbin/$1"
79
}
80

    
81
_daemon_usergroup() {
82
  case "$1" in
83
    masterd)
84
      echo "@GNTMASTERUSER@:@GNTMASTERDGROUP@"
85
      ;;
86
    confd)
87
      echo "@GNTCONFDUSER@:@GNTCONFDGROUP@"
88
      ;;
89
    wconfd)
90
      echo "@GNTWCONFDUSER@:@GNTWCONFDGROUP@"
91
      ;;
92
    luxid)
93
      echo "@GNTLUXIDUSER@:@GNTLUXIDGROUP@"
94
      ;;
95
    rapi)
96
      echo "@GNTRAPIUSER@:@GNTRAPIGROUP@"
97
      ;;
98
    noded)
99
      echo "@GNTNODEDUSER@:@GNTNODEDGROUP@"
100
      ;;
101
    mond)
102
      echo "@GNTMONDUSER@:@GNTMONDGROUP@"
103
      ;;
104
    *)
105
      echo "root:@GNTDAEMONSGROUP@"
106
      ;;
107
  esac
108
}
109

    
110
# Checks whether the local machine is part of a cluster
111
check_config() {
112
  local server_pem=$DATA_DIR/server.pem
113
  local fname
114

    
115
  for fname in $server_pem; do
116
    if [[ ! -f $fname ]]; then
117
      echo "Missing configuration file $fname" >&2
118
      return 1
119
    fi
120
  done
121

    
122
  return 0
123
}
124

    
125
# Checks the exit code of a daemon
126
check_exitcode() {
127
  if [[ "$#" -lt 1 ]]; then
128
    echo 'Missing exit code.' >&2
129
    return 1
130
  fi
131

    
132
  local rc="$1"; shift
133

    
134
  case "$rc" in
135
    0) ;;
136
    11)
137
      echo "not master"
138
    ;;
139
    *)
140
      echo "exit code $rc"
141
      return 1
142
    ;;
143
  esac
144

    
145
  return 0
146
}
147

    
148
# Prints path to PID file for a daemon.
149
daemon_pidfile() {
150
  if [[ "$#" -lt 1 ]]; then
151
    echo 'Missing daemon name.' >&2
152
    return 1
153
  fi
154

    
155
  local name="$1"; shift
156

    
157
  _daemon_pidfile $name
158
}
159

    
160
# Prints path to daemon executable.
161
daemon_executable() {
162
  if [[ "$#" -lt 1 ]]; then
163
    echo 'Missing daemon name.' >&2
164
    return 1
165
  fi
166

    
167
  local name="$1"; shift
168

    
169
  _daemon_executable $name
170
}
171

    
172
# Prints a list of all daemons in the order in which they should be started
173
list_start_daemons() {
174
  local name
175

    
176
  for name in "${DAEMONS[@]}"; do
177
    echo "$name"
178
  done
179
}
180

    
181
# Prints a list of all daemons in the order in which they should be stopped
182
list_stop_daemons() {
183
  list_start_daemons | tac
184
}
185

    
186
# Checks whether a daemon name is known
187
is_daemon_name() {
188
  if [[ "$#" -lt 1 ]]; then
189
    echo 'Missing daemon name.' >&2
190
    return 1
191
  fi
192

    
193
  local name="$1"; shift
194

    
195
  for i in "${DAEMONS[@]}"; do
196
    if [[ "$i" == "$name" ]]; then
197
      return 0
198
    fi
199
  done
200

    
201
  echo "Unknown daemon name '$name'" >&2
202
  return 1
203
}
204

    
205
# Checks whether daemon is running
206
check() {
207
  if [[ "$#" -lt 1 ]]; then
208
    echo 'Missing daemon name.' >&2
209
    return 1
210
  fi
211

    
212
  local name="$1"; shift
213
  local pidfile=$(_daemon_pidfile $name)
214
  local daemonexec=$(_daemon_executable $name)
215

    
216
  if type -p start-stop-daemon >/dev/null; then
217
    start-stop-daemon --stop --signal 0 --quiet \
218
      --pidfile $pidfile
219
  else
220
    _ignore_error status \
221
      -p $pidfile \
222
      $daemonexec
223
  fi
224
}
225

    
226
# Starts a daemon
227
start() {
228
  if [[ "$#" -lt 1 ]]; then
229
    echo 'Missing daemon name.' >&2
230
    return 1
231
  fi
232

    
233
  local name="$1"; shift
234
  # Convert daemon name to uppercase after removing "ganeti-" prefix
235
  local plain_name=${name#ganeti-}
236
  local ucname=$(tr a-z A-Z <<<$plain_name)
237
  local pidfile=$(_daemon_pidfile $name)
238
  local usergroup=$(_daemon_usergroup $plain_name)
239
  local daemonexec=$(_daemon_executable $name)
240

    
241
  if [[ "$name" == ganeti-confd ]] \
242
      && ! _confd_enabled; then
243
    echo 'ganeti-confd disabled at build time' >&2
244
    return 1
245
  fi
246

    
247
  # Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS
248
  eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\""
249

    
250
  @PKGLIBDIR@/ensure-dirs
251

    
252
  if type -p start-stop-daemon >/dev/null; then
253
    start-stop-daemon --start --quiet --oknodo \
254
      --pidfile $pidfile \
255
      --startas $daemonexec \
256
      --chuid $usergroup \
257
      -- $args "$@"
258
  else
259
    # TODO: Find a way to start daemon with a group, until then the group must
260
    # be removed
261
    _ignore_error daemon \
262
      --pidfile $pidfile \
263
      --user ${usergroup%:*} \
264
      $daemonexec $args "$@"
265
  fi
266

    
267
}
268

    
269
# Stops a daemon
270
stop() {
271
  if [[ "$#" -lt 1 ]]; then
272
    echo 'Missing daemon name.' >&2
273
    return 1
274
  fi
275

    
276
  local name="$1"; shift
277
  local pidfile=$(_daemon_pidfile $name)
278

    
279
  if type -p start-stop-daemon >/dev/null; then
280
    start-stop-daemon --stop --quiet --oknodo --retry 30 \
281
      --pidfile $pidfile
282
  else
283
    _ignore_error killproc -p $pidfile $name
284
  fi
285
}
286

    
287
# Starts a daemon if it's not yet running
288
check_and_start() {
289
  local name="$1"
290

    
291
  if ! check $name; then
292
    start $name
293
  fi
294
}
295

    
296
# Starts the master role
297
start_master() {
298
  start ganeti-wconfd
299
  start ganeti-masterd
300
  start ganeti-rapi
301
  start ganeti-luxid
302
}
303

    
304
# Stops the master role
305
stop_master() {
306
  stop ganeti-luxid
307
  stop ganeti-rapi
308
  stop ganeti-masterd
309
  stop ganeti-wconfd
310
}
311

    
312
# Start all daemons
313
start_all() {
314
  for i in $(list_start_daemons); do
315
    local rc=0
316

    
317
    # Try to start daemon
318
    start $i || rc=$?
319

    
320
    if ! errmsg=$(check_exitcode $rc); then
321
      echo "$errmsg" >&2
322
      return 1
323
    fi
324
  done
325

    
326
  return 0
327
}
328

    
329
# Stop all daemons
330
stop_all() {
331
  for i in $(list_stop_daemons); do
332
    stop $i
333
  done
334
}
335

    
336
# SIGHUP a process to force re-opening its logfiles
337
rotate_logs() {
338
  if [[ "$#" -lt 1 ]]; then
339
    echo 'Missing daemon name.' >&2
340
    return 1
341
  fi
342

    
343
  local name="$1"; shift
344
  local pidfile=$(_daemon_pidfile $name)
345
  local daemonexec=$(_daemon_executable $name)
346

    
347
  if type -p start-stop-daemon >/dev/null; then
348
    start-stop-daemon --stop --signal HUP --quiet \
349
      --oknodo --pidfile $pidfile
350
  else
351
    _ignore_error killproc \
352
      -p $pidfile \
353
      $daemonexec -HUP
354
  fi
355
}
356

    
357
# SIGHUP all processes
358
rotate_all_logs() {
359
  for i in $(list_stop_daemons); do
360
    rotate_logs $i
361
  done
362
}
363

    
364
# Reloads the SSH keys
365
reload_ssh_keys() {
366
  @RPL_SSH_INITD_SCRIPT@ restart
367
}
368

    
369
# Read @SYSCONFDIR@/rc.d/init.d/functions if start-stop-daemon not available
370
if ! type -p start-stop-daemon >/dev/null && \
371
   [[ -f @SYSCONFDIR@/rc.d/init.d/functions ]]; then
372
  _ignore_error . @SYSCONFDIR@/rc.d/init.d/functions
373
fi
374

    
375
if [[ "$#" -lt 1 ]]; then
376
  echo "Usage: $0 <action>" >&2
377
  exit 1
378
fi
379

    
380
orig_action=$1; shift
381

    
382
if [[ "$orig_action" == *_* ]]; then
383
  echo "Command must not contain underscores" >&2
384
  exit 1
385
fi
386

    
387
# Replace all dashes (-) with underlines (_)
388
action=${orig_action//-/_}
389

    
390
# Is it a known function?
391
if ! declare -F "$action" >/dev/null 2>&1; then
392
  echo "Unknown command: $orig_action" >&2
393
  exit 1
394
fi
395

    
396
# Call handler function
397
$action "$@"