#!/bin/sh
# ganeti node daemon starter script
# based on skeleton from Debian GNU/Linux
### BEGIN INIT INFO
# Provides:          ganeti
# Required-Start:    $syslog $remote_fs
# Required-Stop:     $syslog $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Ganeti Cluster Manager
# Description:       Ganeti Cluster Manager
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
DESC="Ganeti cluster"

GANETIRUNDIR="/var/run/ganeti"

GANETI_DEFAULTS_FILE="/etc/default/ganeti"

NODED="ganeti-noded"
NODED_ARGS=""

MASTERD="ganeti-masterd"
MASTERD_ARGS=""

RAPI="ganeti-rapi"
RAPI_ARGS=""

SCRIPTNAME="/etc/init.d/ganeti"

test -f "/usr/sbin/$NODED" || exit 0

. /lib/lsb/init-functions

if [ -s $GANETI_DEFAULTS_FILE ]; then
    . $GANETI_DEFAULTS_FILE
fi

check_config() {
    for fname in \
        "/var/lib/ganeti/server.pem"
    do
        if ! [ -f "$fname" ]; then
            log_end_msg 0
            log_warning_msg "Config $fname not there, will not run."
            exit 0
        fi
    done

    for fname in \
        "/var/lib/ganeti/ssconf_hypervisor"
    do
        if [ -f "$fname" ]; then
            log_end_msg 0
            log_warning_msg "Configuration not upgraded to 2.0, will not run."
            exit 1
        fi
    done

}

check_exitcode() {
    RC=$1
    case $RC in
        0)
            log_action_end_msg 0
            ;;
        11)
            log_action_end_msg 0 "not master"
            ;;
        *)
            log_action_end_msg 1 "exit code $RC"
            ;;
    esac
}

start_action() {
    # called as start_action daemon-name
    local daemon="$1"; shift
    log_action_begin_msg "$daemon"
    start-stop-daemon --start --quiet \
        --pidfile "${GANETIRUNDIR}/${daemon}.pid" \
        --startas "/usr/sbin/$daemon" \
        --oknodo \
        -- "$@"
    check_exitcode $?
}

stop_action() {
    # called as stop_action daemon-name
    local daemon="$1"
    log_action_begin_msg "$daemon"
    start-stop-daemon --stop --quiet --oknodo \
        --retry 30 --pidfile "${GANETIRUNDIR}/${daemon}.pid"
    check_exitcode $?
}

maybe_do() {
    requested="$1"; shift
    action="$1"; shift
    target="$1"
    if [ -z "$requested" -o "$requested" = "$target" ]; then
        $action "$@"
    fi
}

if [ -n "$2" -a \
    "$2" != "$NODED" -a \
    "$2" != "$MASTERD" -a \
    "$2" != "$RAPI" ]; then
    log_failure_msg "Unknown daemon '$2' requested"
    exit 1
fi

case "$1" in
    start)
        log_daemon_msg "Starting $DESC" "$2"
        check_config
        maybe_do "$2" start_action $NODED $NODED_ARGS
        maybe_do "$2" start_action $MASTERD $MASTERD_ARGS
        maybe_do "$2" start_action $RAPI $RAPI_ARGS
        ;;
    stop)
        log_daemon_msg "Stopping $DESC" "$2"
        maybe_do "$2" stop_action $RAPI
        maybe_do "$2" stop_action $MASTERD
        maybe_do "$2" stop_action $NODED
        ;;
    restart|force-reload)
        log_daemon_msg "Reloading $DESC" "$2"
        maybe_do "$2" stop_action $RAPI
        maybe_do "$2" stop_action $MASTERD
        maybe_do "$2" stop_action $NODED
        check_config
        maybe_do "$2" start_action $NODED $NODED_ARGS
        maybe_do "$2" start_action $MASTERD $MASTERD_ARGS
        maybe_do "$2" start_action $RAPI $RAPI_ARGS
        ;;
    *)
        log_success_msg "Usage: $SCRIPTNAME {start|stop|force-reload|restart}"
        exit 1
        ;;
esac

exit 0
