daemon-util.in 10 KB
Newer Older
1 2 3
#!/bin/bash
#

Michael Hanselmann's avatar
Michael Hanselmann committed
4
# Copyright (C) 2009, 2011, 2012 Google Inc.
Klaus Aehlig's avatar
Klaus Aehlig committed
5
# All rights reserved.
6
#
Klaus Aehlig's avatar
Klaus Aehlig committed
7 8 9
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
10
#
Klaus Aehlig's avatar
Klaus Aehlig committed
11 12
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
13
#
Klaus Aehlig's avatar
Klaus Aehlig committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 30 31

set -e

32 33 34
@SHELL_ENV_INIT@

readonly defaults_file="$SYSCONFDIR/default/ganeti"
35

36 37 38 39 40
# This is a list of all daemons and the order in which they're started. The
# order is important as there are dependencies between them. On shutdown,
# they're stopped in reverse order.
DAEMONS=(
  ganeti-noded
Niklas Hambuechen's avatar
Niklas Hambuechen committed
41
  ganeti-confd
Klaus Aehlig's avatar
Klaus Aehlig committed
42
  ganeti-wconfd
43
  ganeti-rapi
44
  ganeti-luxid
45
  ganeti-kvmd
46 47
  )

48 49 50 51 52 53
# This is the list of daemons that are loaded on demand; they should only be
# stopped, not started.
ON_DEMAND_DAEMONS=(
  ganeti-metad
  )

54 55 56 57 58
_mond_enabled() {
  [[ "@CUSTOM_ENABLE_MOND@" == True ]]
}

if _mond_enabled; then
59
  DAEMONS+=( ganeti-mond ganeti-maintd)
60 61
fi

62 63 64
# The full list of all daemons we know about
ALL_DAEMONS=( ${DAEMONS[@]} ${ON_DEMAND_DAEMONS[@]} )

65 66
NODED_ARGS=
CONFD_ARGS=
Petr Pudlak's avatar
Petr Pudlak committed
67
WCONFD_ARGS=
Thomas Thrainer's avatar
Thomas Thrainer committed
68
LUXID_ARGS=
69
RAPI_ARGS=
70
MOND_ARGS=
71 72 73 74 75 76

# Read defaults file if it exists
if [[ -s $defaults_file ]]; then
  . $defaults_file
fi

77 78 79 80 81 82
# Meant to facilitate use utilities in /etc/rc.d/init.d/functions in case
# start-stop-daemon is not available.
_ignore_error() {
  eval "$@" || :
}

83
_daemon_pidfile() {
84
  echo "$RUN_DIR/$1.pid"
85 86
}

87 88 89 90
_daemon_executable() {
  echo "@PREFIX@/sbin/$1"
}

91 92 93
_daemon_usergroup() {
  case "$1" in
    confd)
94
      echo "@GNTCONFDUSER@:@GNTCONFDGROUP@"
95
      ;;
Petr Pudlak's avatar
Petr Pudlak committed
96 97 98
    wconfd)
      echo "@GNTWCONFDUSER@:@GNTWCONFDGROUP@"
      ;;
Thomas Thrainer's avatar
Thomas Thrainer committed
99 100
    luxid)
      echo "@GNTLUXIDUSER@:@GNTLUXIDGROUP@"
101
      ;;
102 103 104 105
    rapi)
      echo "@GNTRAPIUSER@:@GNTRAPIGROUP@"
      ;;
    noded)
Michele Tartara's avatar
Michele Tartara committed
106
      echo "@GNTNODEDUSER@:@GNTNODEDGROUP@"
107
      ;;
108 109 110
    mond)
      echo "@GNTMONDUSER@:@GNTMONDGROUP@"
      ;;
111 112 113
    metad)
      echo "@GNTMETADUSER@:@GNTMETADGROUP@"
      ;;
114 115 116 117 118 119
    *)
      echo "root:@GNTDAEMONSGROUP@"
      ;;
  esac
}

120 121 122 123 124 125 126 127 128 129 130 131
# Specifies the additional capabilities needed by individual daemons
_daemon_caps() {
  case "$1" in
    metad)
      echo "cap_net_bind_service=+ep"
      ;;
    *)
      echo ""
      ;;
  esac
}

132 133
# Checks whether the local machine is part of a cluster
check_config() {
134
  local server_pem=$DATA_DIR/server.pem
135 136 137 138 139 140 141 142 143 144 145 146
  local fname

  for fname in $server_pem; do
    if [[ ! -f $fname ]]; then
      echo "Missing configuration file $fname" >&2
      return 1
    fi
  done

  return 0
}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
# Checks the exit code of a daemon
check_exitcode() {
  if [[ "$#" -lt 1 ]]; then
    echo 'Missing exit code.' >&2
    return 1
  fi

  local rc="$1"; shift

  case "$rc" in
    0) ;;
    11)
      echo "not master"
    ;;
    *)
      echo "exit code $rc"
      return 1
    ;;
  esac

  return 0
}

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
# Checks if we should use systemctl to start/stop daemons
use_systemctl() {
  # Is systemd running as PID 1?
  [ -d /run/systemd/system ] || return 1

  type -p systemctl >/dev/null || return 1

  # Does systemd know about Ganeti at all?
  loadstate="$(systemctl show -pLoadState ganeti.target)"
  if [ "$loadstate" = "LoadState=loaded" ]; then
    return 0
  fi

  return 1
}

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
# Prints path to PID file for a daemon.
daemon_pidfile() {
  if [[ "$#" -lt 1 ]]; then
    echo 'Missing daemon name.' >&2
    return 1
  fi

  local name="$1"; shift

  _daemon_pidfile $name
}

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

  local name="$1"; shift

  _daemon_executable $name
}

210 211 212 213 214
# Prints a list of all daemons in the order in which they should be started
list_start_daemons() {
  local name

  for name in "${DAEMONS[@]}"; do
215
    echo "$name"
216 217 218 219 220
  done
}

# Prints a list of all daemons in the order in which they should be stopped
list_stop_daemons() {
221
  for name in "${ALL_DAEMONS[@]}"; do
222 223
    echo "$name"
  done | tac
224 225 226 227 228 229 230 231 232 233 234
}

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

  local name="$1"; shift

235
  for i in "${ALL_DAEMONS[@]}"; do
236 237 238 239 240 241 242 243 244
    if [[ "$i" == "$name" ]]; then
      return 0
    fi
  done

  echo "Unknown daemon name '$name'" >&2
  return 1
}

245 246 247 248
# Checks whether daemon is running
check() {
  if [[ "$#" -lt 1 ]]; then
    echo 'Missing daemon name.' >&2
249
    return 1
250 251 252
  fi

  local name="$1"; shift
253 254 255
  local pidfile=$(_daemon_pidfile $name)
  local daemonexec=$(_daemon_executable $name)

256 257 258 259 260 261 262 263
  if use_systemctl; then
    activestate="$(systemctl show -pActiveState "${name}.service")"
    if [ "$activestate" = "ActiveState=active" ]; then
      return 0
    else
      return 1
    fi
  elif type -p start-stop-daemon >/dev/null; then
264 265 266 267 268 269 270
    start-stop-daemon --stop --signal 0 --quiet \
      --pidfile $pidfile
  else
    _ignore_error status \
      -p $pidfile \
      $daemonexec
  fi
271 272 273 274 275 276
}

# Starts a daemon
start() {
  if [[ "$#" -lt 1 ]]; then
    echo 'Missing daemon name.' >&2
277
    return 1
278 279 280
  fi

  local name="$1"; shift
281 282 283
  # Convert daemon name to uppercase after removing "ganeti-" prefix
  local plain_name=${name#ganeti-}
  local ucname=$(tr a-z A-Z <<<$plain_name)
284 285 286
  local pidfile=$(_daemon_pidfile $name)
  local usergroup=$(_daemon_usergroup $plain_name)
  local daemonexec=$(_daemon_executable $name)
287

288 289 290 291 292
  if use_systemctl; then
    systemctl start "${name}.service"
    return $?
  fi

293
  # Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS
294
  eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\""
295

296 297
  @PKGLIBDIR@/ensure-dirs

298 299 300
  # Grant capabilities to daemons that need them
  local daemoncaps=$(_daemon_caps $plain_name)
  if [[ "$daemoncaps" != "" ]]; then
301 302 303 304 305 306
    if type -p setcap >/dev/null; then
      setcap $daemoncaps $(readlink -f $daemonexec)
    else
      echo "setcap missing, could not set capabilities for $name." >&2
      return 1
    fi
307 308
  fi

309 310 311 312 313 314 315 316 317 318 319 320 321 322
  if type -p start-stop-daemon >/dev/null; then
    start-stop-daemon --start --quiet --oknodo \
      --pidfile $pidfile \
      --startas $daemonexec \
      --chuid $usergroup \
      -- $args "$@"
  else
    # TODO: Find a way to start daemon with a group, until then the group must
    # be removed
    _ignore_error daemon \
      --pidfile $pidfile \
      --user ${usergroup%:*} \
      $daemonexec $args "$@"
  fi
323

324 325 326 327 328 329
}

# Stops a daemon
stop() {
  if [[ "$#" -lt 1 ]]; then
    echo 'Missing daemon name.' >&2
330
    return 1
331 332 333
  fi

  local name="$1"; shift
334
  local pidfile=$(_daemon_pidfile $name)
335

336 337 338
  if use_systemctl; then
    systemctl stop "${name}.service"
  elif type -p start-stop-daemon >/dev/null; then
339 340 341 342 343
    start-stop-daemon --stop --quiet --oknodo --retry 30 \
      --pidfile $pidfile
  else
    _ignore_error killproc -p $pidfile $name
  fi
344 345 346 347 348 349 350
}

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

  if ! check $name; then
351 352 353 354 355
    if use_systemctl; then
      echo "${name} supervised by systemd but not running, will not restart."
      return 1
    fi

356 357 358 359 360 361
    start $name
  fi
}

# Starts the master role
start_master() {
362 363 364 365 366 367 368
  if use_systemctl; then
    systemctl start ganeti-master.target
  else
    start ganeti-wconfd
    start ganeti-rapi
    start ganeti-luxid
  fi
369 370 371 372
}

# Stops the master role
stop_master() {
373 374 375 376 377 378 379
  if use_systemctl; then
    systemctl stop ganeti-master.target
  else
    stop ganeti-luxid
    stop ganeti-rapi
    stop ganeti-wconfd
  fi
380 381
}

382 383
# Start all daemons
start_all() {
384 385 386
  use_systemctl && systemctl start ganeti.target
  # Fall through so that we detect any errors.

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
  for i in $(list_start_daemons); do
    local rc=0

    # Try to start daemon
    start $i || rc=$?

    if ! errmsg=$(check_exitcode $rc); then
      echo "$errmsg" >&2
      return 1
    fi
  done

  return 0
}

# Stop all daemons
stop_all() {
404 405 406 407 408 409 410
  if use_systemctl; then
    systemctl stop ganeti.target
  else
    for i in $(list_stop_daemons); do
      stop $i
    done
  fi
411 412
}

413 414 415 416 417 418 419 420 421 422 423 424 425
# SIGHUP a process to force re-opening its logfiles
rotate_logs() {
  if [[ "$#" -lt 1 ]]; then
    echo 'Missing daemon name.' >&2
    return 1
  fi

  local name="$1"; shift
  local pidfile=$(_daemon_pidfile $name)
  local daemonexec=$(_daemon_executable $name)

  if type -p start-stop-daemon >/dev/null; then
    start-stop-daemon --stop --signal HUP --quiet \
426
      --oknodo --pidfile $pidfile
427 428 429 430 431 432 433 434 435 436 437 438 439 440
  else
    _ignore_error killproc \
      -p $pidfile \
      $daemonexec -HUP
  fi
}

# SIGHUP all processes
rotate_all_logs() {
  for i in $(list_stop_daemons); do
    rotate_logs $i
  done
}

441 442 443 444 445
# Reloads the SSH keys
reload_ssh_keys() {
  @RPL_SSH_INITD_SCRIPT@ restart
}

446 447 448 449 450 451
# Read @SYSCONFDIR@/rc.d/init.d/functions if start-stop-daemon not available
if ! type -p start-stop-daemon >/dev/null && \
   [[ -f @SYSCONFDIR@/rc.d/init.d/functions ]]; then
  _ignore_error . @SYSCONFDIR@/rc.d/init.d/functions
fi

452 453 454 455 456 457 458
if [[ "$#" -lt 1 ]]; then
  echo "Usage: $0 <action>" >&2
  exit 1
fi

orig_action=$1; shift

459 460 461 462 463
if [[ "$orig_action" == *_* ]]; then
  echo "Command must not contain underscores" >&2
  exit 1
fi

464 465 466 467 468 469 470 471 472 473 474
# Replace all dashes (-) with underlines (_)
action=${orig_action//-/_}

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

# Call handler function
$action "$@"