#!/bin/sh
### BEGIN INIT INFO
# Provides:          kube-scheduler
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: The Kubernetes scheduler plugin
# Description:
#   The Kubernetes scheduler plugin is responsible for scheduling pods on
#   available minions.
### END INIT INFO

NAME=kube-scheduler
DAEMON=/usr/bin/$NAME
LOGFILE=/var/log/$NAME.log
PIDFILE=/var/run/$NAME.pid
DAEMON_USER=kube
DAEMON_ARGS=""

# Exit if executable is not installed
[ -x "$DAEMON" ] || exit 0

# Read common configuration file
[ -r /etc/kubernetes/config ] && . /etc/kubernetes/config

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

RETRY=TERM/30/KILL/5

# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

# Define LSB log_* functions.
. /lib/lsb/init-functions

[ -z "${KUBE_MASTER}" ] && KUBE_MASTER=" --master=127.0.0.1:8080"
DAEMON_ARGS="\
    $KUBE_LOGTOSTDERR \
    $KUBE_LOG_LEVEL \
    $KUBE_MASTER \
$DAEMON_ARGS"

_ev_ () {
  [ "$VERBOSE" = "no" ] || eval $@
}

case "$1" in
    start)
        _ev_ log_action_begin_msg \"Starting $NAME\"
        if R=$($0 status); then
            _ev_ log_action_end_msg 0 \"$R\"
        else
            R=$(start-stop-daemon --start --exec $DAEMON --pidfile $PIDFILE --make-pidfile \
             --chuid $DAEMON_USER --background --no-close -- $DAEMON_ARGS >> $LOGFILE 2>&1)
            sleep 0.2
            $0 status >>/dev/null
            _ev_ log_action_end_msg $? \"$R\"
        fi
    ;;
    stop)
        _ev_ log_action_begin_msg \"Stopping $NAME\"
        R=$(start-stop-daemon --stop --oknodo --name $NAME --pidfile $PIDFILE --remove-pidfile --retry=$RETRY 2>&1)
        _ev_ log_action_end_msg $? \"$R\"
    ;;
    status)
        ## return status 0 if process is running.
        status_of_proc -p $PIDFILE "$DAEMON" "$NAME"
    ;;
    restart|force-reload)
        $0 stop
        $0 start
    ;;
    *)
        echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload|status}" >&2
        exit 1
    ;;
esac
