#!/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.
if [ -e "/etc/rc.d/init.d/functions" ]
then
  . /etc/rc.d/init.d/functions
fi

prefix="/usr"
exec_prefix="${prefix}"
sbindir="/usr/bin"
datadir="${prefix}/share/nova-agent"
reallibdir="/usr/share/nova-agent/1.39.1/lib"

nova_agent="${sbindir}/nova-agent"
agent_config="/usr/share/nova-agent/nova-agent.py"
pidfile="/var/run/nova-agent.pid"
logfile="/var/log/nova-agent.log"
loglevel="info"

#####
# Setting PYTHONPATH Environment Variable:
#   Use NOVA-AGENT packaged Python libraries in precedence, if required then
#     depend on System Python library packages.
#   This helps with Bintar made on earlier minor version say v2.6 to be used
#     with system having v2.7 default python setup.
#####
NOVA_PYTHONPATH=`ls -l $reallibdir | grep '^d' | awk '{print \$NF}'`
NOVA_PYTHONPATH=`echo $NOVA_PYTHONPATH | grep 'python[0-9]\.[0-9]'`
NOVA_PYTHONPATH="${reallibdir}/${NOVA_PYTHONPATH}"
NOVA_PYTHONPATH="${NOVA_PYTHONPATH}:${NOVA_PYTHONPATH}/site-packages"

if [ `which python > /dev/null 2>&1 ; echo $?` -eq 0 ]; then
  PYTHONPATH="$(python -c 'import sys; print ":".join(sys.path)')"
fi
export PYTHONPATH="$NOVA_PYTHONPATH:$PYTHONPATH"


do_start() {
  LD_LIBRARY_PATH="${reallibdir}"
  export LD_LIBRARY_PATH
  ${nova_agent} -q -p ${pidfile} -o ${logfile} -l ${loglevel} ${agent_config}
}

do_stop() {
  num_tries=0
  while [ $num_tries -lt 10 ] ; do
    if [ ! -f ${pidfile} ] ; then
        break
    fi
    if [ $num_tries -eq 0 ] ; then
      pid=`cat ${pidfile}`
      if [ $? -eq 0 ] ; then
        kill $pid
      fi
    fi
    num_tries=`expr $num_tries + 1`
    sleep 1
  done
  rm -f ${pidfile}
}

SCRIPTNAME=$0

case "$1" in
  start)
    do_start
    ;;
  stop)
    do_stop
    ;;
  restart)
    do_stop
    do_start
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
    exit 3
    ;;
esac
