#!/bin/sh

# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
#  Copyright (c) 2011 Openstack, LLC.
#  All Rights Reserved.
#
#     Licensed under the Apache License, Version 2.0 (the "License"); you may
#     not use this file except in compliance with the License. You may obtain
#     a copy of the License at
#
#          http://www.apache.org/licenses/LICENSE-2.0
#
#     Unless required by applicable law or agreed to in writing, software
#     distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#     WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#     License for the specific language governing permissions and limitations
#     under the License.
#
# nova-agent	Startup script for OpenStack nova guest agent
#
# RedHat style init header:
#
# chkconfig: 2345 15 85
# description: nova-agent is an agent meant to run on unix guest instances \
#              being managed by OpenStack nova.  Currently only works with \
#              Citrix XenServer for manipulating the guest through \
#              xenstore.
# processname: nova-agent
# pidfile: /var/run/nova-agent.pid
#
# LSB style init header:
#
### BEGIN INIT INFO
# Provides: Nova-Agent
# Required-Start: $remote_fs $syslog xe-linux-distribution
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start nova-agent at boot time
# Description: nova-agent is a guest agent for OpenStack nova.
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

version="1.39.1"
exec="/usr/sbin/nova-agent"
prog="nova-agent"
config="/usr/share/nova-agent/$version/nova-agent.py"
novalib="/usr/share/nova-agent/$version/lib"
novapypath="$novalib/python2.6:$novalib/python2.6/site-packages"
novaenv="PYTHONPATH=$novapypath LD_LIBRARY_PATH=$novalib"

pidfile="/var/run/nova-agent.pid"
lockfile="/var/lock/subsys/$prog"

logfile="/var/log/nova-agent.log"
loglevel="debug"

start() {
    [ -x $exec ] || exit 5
    [ -f $config ] || exit 6
    echo -n $"Starting $prog: "
    daemon "$novaenv $exec -q -p $pidfile -o $logfile -l $loglevel $config"
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

rh_status() {
    # run checks to determine if the service is running or use generic status
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}


case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart}"
        exit 2
        ;;
esac
exit $?
