#! /bin/sh
#
# Copyright (c) 2014 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
### BEGIN INIT INFO
# Provides:          nova-compute-proxy
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: OpenStack Compute (Nova) Proxy
# Description:       OpenStack Compute (Nova) Ptoxy
### END INIT INFO

. /etc/init.d/functions

# Linux Standard Base (LSB) Error Codes
RETVAL=0
GENERIC_ERROR=1
INVALID_ARGS=2
UNSUPPORTED_FEATURE=3
NOT_INSTALLED=5
NOT_RUNNING=7

PROXY_NAME="nova-api-proxy"
DAEMON="/usr/bin/${PROXY_NAME}"
PIDFILE="/var/run/${PROXY_NAME}.pid"
CONFIGFILE="/etc/proxy/${PROXY_NAME}.conf"

if ! [ -x ${DAEMON} ] ; then
	logger "${DAEMON} is missing"
	exit ${NOT_INSTALLED}
fi

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin
export PATH

status()
{
    # Status function has a standard set of return codes to indicate daemon status
    # http://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html

    local my_processes=`pgrep -l -f "^(python|/usr/bin/python|/usr/bin/python2) ${DAEMON}([^\w-]|$)"`

    if [ -z "${my_processes}" ]; then
        echo "$PROXY_NAME is not running"
        return 1
    fi

    echo "$PROXY_NAME is running"
    return 0
}

start ()
{
    status >/dev/null
    if [ $? -eq 0 ]; then
        echo "$PROXY_NAME is already running"
        return 0
    fi

    # Delete stale pidfile, if any
    rm -f $PIDFILE

    start-stop-daemon --start -b --make-pidfile --pidfile $PIDFILE -x ${DAEMON} -- --config-file=${CONFIGFILE}
    RETVAL=$?
    if [ ${RETVAL} -eq 0 ]; then
        status >/dev/null
        if [ $? -eq 0 ]; then
            logger -t $PROXY_NAME "start OK"
            echo "OK"
            return 0
        fi
        logger -t $PROXY_NAME "start-stop-daemon returned 0, but status fails"
        rm -f $PIDFILE
    fi
    logger -t $PROXY_NAME "start failed"
    return ${GENERIC_ERROR}
}

confirm_stop()
{
    local my_processes=`pgrep -l -f "^(python|/usr/bin/python|/usr/bin/python2) ${DAEMON}([^\w-]|$)"`

    if [ -n "${my_processes}" ]
    then
        logger -t $PROXY_NAME "About to SIGKILL the following: ${my_processes}"
        pkill -KILL -f "^(python|/usr/bin/python|/usr/bin/python2) ${DAEMON}([^\w-]|$)"
    fi
}

stop ()
{
    status >/dev/null
    if [ $? -ne 0 ]; then
        echo "$PROXY_NAME is not running"
        return 0
    fi

    echo -n "Stopping ${PROXY_NAME}: "
    if [ -f $PIDFILE ]; then
        start-stop-daemon --stop --quiet --retry 3 --oknodo --pidfile $PIDFILE
    fi

    confirm_stop
    rm -f $PIDFILE

    # Confirm status
    status >/dev/null
    if [ $? -ne 0 ]; then
        echo "Stopped"
        return 0
    else
        echo "Failed"
        return ${GENERIC_ERROR}
    fi
}

rc=0

case "$1" in
	start)
		start
                rc=$?
		;;
	stop)
		stop
                rc=$?
		;;
	restart|force-reload|reload)
		stop
		start
                rc=$?
		;;
	status)
		status
                rc=$?
		;;

	*)
		echo "Usage: $0 {start|stop|force-reload|restart|reload|status}"
		exit 1
		;;
esac

exit $rc
