Statistics
| Branch: | Tag: | Revision:

root / daemons / daemon-util.in @ 59881a0b

History | View | Annotate | Download (7.7 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-masterd
299
  start ganeti-rapi
300
  start ganeti-luxid
301
}
302

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

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

    
315
    # Try to start daemon
316
    start $i || rc=$?
317

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

    
324
  return 0
325
}
326

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

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

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

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

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

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

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

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

    
378
orig_action=$1; shift
379

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

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

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

    
394
# Call handler function
395
$action "$@"