#!/bin/sh
#
# start/stop the cloud monitor daemon.
#
### BEGIN INIT INFO
# Provides:          cloud_monitor
# Required-Start:    $syslog $time
# Required-Stop:     $syslog $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts/stops the cloud monitor daemon.
# Description:       Starts and stops the cloud monitor daemon
#                    to manage VM instances deployed on clouds
### END INIT INFO

# Source function library.
if [ -f /etc/init.d/functions ]; then
   . /etc/init.d/functions
fi

if ! [ -x /opt/submit/monitorCloud.py ]; then
   exit 1
fi

case "$1" in
   start)
      export PYTHONPATH=/opt/submit/Wispy/boto/boto-2.0rc1/lib/python2.5/site-packages
      if [ -f /sbin/start-stop-daemon ]; then
         echo -n "Starting cloud monitor:"
         echo -n " cloudmon" ; pgrep -fl '/opt/submit/monitorCloud.py' > /dev/null 2>&1 || /sbin/start-stop-daemon --start --quiet --chdir /opt/submit --chuid gridman --exec /opt/submit/monitorCloud.py
      else
         echo -n " cloudmon" ; daemon --check monitorCloud --user=gridman /opt/submit/monitorCloud.py
      fi
      echo "."
      ;;
    stop)
      if [ -f /sbin/start-stop-daemon ]; then
         echo -n "Stopping cloud monitor:"
      fi
      echo -n " cloudmon" ; pkill -TERM -u gridman -f '/opt/submit/monitorCloud.py'
      echo "."
      ;;
    restart|force-reload)
      if [ -f /sbin/start-stop-daemon ]; then
         echo -n "Restarting cloud monitor:"
      fi
      echo -n " cloudmon"
      pkill -TERM -u gridman -f '/opt/submit/monitorCloud.py'
      if [ -f /sbin/start-stop-daemon ]; then
         /sbin/start-stop-daemon --start --quiet --chdir /opt/submit --chuid gridman --exec /opt/submit/monitorCloud.py
      else
         daemon --check monitorCloud --user=gridman /opt/submit/monitorCloud.py
      fi
      echo "."
      ;;
       *)
      echo "Usage: /etc/init.d/cloudmon {start|stop|restart|force-reload}"
      exit 1
      ;;
esac

exit 0
