#!/bin/bash
### BEGIN INIT INFO
# Provides:          eucalyptus-cc
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Eucalyptus cluster controller
# Description:       Start the Eucalyptus cluster controller
### END INIT INFO
#
# Init script for the Eucalyptus cluster controller.
#
# chkconfig: 2345 99 05
# description: script for starting and stopping eucalyptus
#
# $Id: eucalyptus.in,v 1.6 2009-01-06 00:14:41 graziano Exp $
#

# Do NOT "set -e"

# if we are on rocks we need the rocks binaries 
[ -e /etc/profile.d/rocks-binaries.sh ] && . /etc/profile.d/rocks-binaries.sh

# I just wanted to set PATH to be the system PATH, but if a user install
# eucalyptus manually, it may have binaries in a non-standard position:
# hence we need to keep the PATH we receive.
PATH=/sbin:/usr/sbin:/bin:/usr/bin:$PATH
DESC="Eucalyptus Cluster Controller"
NAME=eucalyptus
BRCTL="`which brctl 2> /dev/null`"
ANT="`which ant 2> /dev/null`"
JAVA="`which java 2> /dev/null`"
HTTPD="`which apache2 2> /dev/null`"
MODPROBE="`which modprobe 2> /dev/null`"

EUCALYPTUS="/"
export EUCALYPTUS
AXIS2C_HOME="/usr/lib/axis2"
export AXIS2C_HOME

. /lib/lsb/init-functions

create_httpd_config() {
    IPS="all"
    
    # let's configure the common parts for both CC and NC
    cat /etc/eucalyptus/httpd.conf |sed "s|EUCALYPTUS|$EUCALYPTUS|"|sed "s|AXIS2C_HOME|$AXIS2C_HOME|"|sed "s|\(ServerRoot\).*|\1 "$HTTPD_HOME"|" |sed "s|EUCA_USER|$EUCA_USER|" >  /etc/eucalyptus/httpd-tmp.conf

    echo "LoadModule authz_host_module /usr/lib/apache2/modules/mod_authz_host.so" >> $EUCALYPTUS/etc/eucalyptus/httpd-tmp.conf

    # let's configure the CC 
    cat $EUCALYPTUS/etc/eucalyptus/httpd-tmp.conf |sed "s|\(Listen\).*|\1 $CC_PORT|"|sed "s|\(PidFile\).*|\1 $EUCALYPTUS/var/run/eucalyptus/eucalyptus-cc.pid|"|sed "s|\(Allow from\).*|\1 $IPS|"|sed "s|\(ErrorLog\).*|\1 $EUCALYPTUS/var/log/eucalyptus/httpd-cc_error_log|"|sed "s|\(StartServers\).*|\1 5|"|sed "s|\(MinSpareServers\).*|\1 5|"|sed "s|\(MaxSpareServers\).*|\1 10|"|sed "s|\(MaxClients\).*|\1 25|" |sed "s|\(MinSpareThreads\).*|\1 5|" | sed "s|\(MaxSpareThreads\).*|\1 10|" | sed "s|\(ThreadsPerChild\).*|\1 25|">  $EUCALYPTUS/etc/eucalyptus/httpd-cc.conf
}

# crude way to start the axis2c services
do_start() {
    mkdir -p /var/run/eucalyptus
    chown eucalyptus:eucalyptus /var/run/eucalyptus
    if [ "$VNET_MODE" = "MANAGED" ]; then
        if [ ! -w /proc/sys/net/ipv4/ip_forward ]; then
            echo "You need to enable IP forwarding for eucalyptus to use MANAGED mode!"
            exit 1
        fi
        echo -n 1 > /proc/sys/net/ipv4/ip_forward
    fi

    create_httpd_config

    # let's do some basic checkings depending on what we want to run
    if [ -z "$NODES" ]; then
        echo "NODES is empty in configuration file!" 
    fi

    # if in MANAGED or STATIC mode, we need to use
    # dhcpd, so better check we know how to use it
    if [ "$VNET_MODE" = "MANAGED" -o "$VNET_MODE" = "STATIC" ]; then
        if [ -z "$VNET_DHCPDAEMON" -o ! -x $VNET_DHCPDAEMON ]; then
            echo "Cannot find dhcp server!"
            exit 1
        fi
        if ! $VNET_DHCPDAEMON --help 2>&1 |grep -- -tf > /dev/null ; then
            echo "dhcp server doesn't accept -tf option!"
        fi
    fi

    if ! $HTTPD -f /etc/eucalyptus/httpd-cc.conf ; then
        echo "Failed to start the CC!"
        exit 1
    fi
}

do_stop() {
    pidfile=/var/run/eucalyptus/eucalyptus-cc.pid
    if test -s $pidfile
    then
        pid=`cat $pidfile 2> /dev/null`
        kill $pid > /dev/null 2>&1 
    fi
    timeout=5
    while [ $timeout -ge 0 ]
    do
        if ps $pid > /dev/null 2>&1
        then
            sleep 1
            timeout=$(($timeout - 1))
        else
            break
        fi
    done
    if [ $timeout -eq 0 ]
    then
        kill -9 $pid > /dev/null 2>&1
    fi
    rm -f $pidfile
	rm -f /dev/shm/eucalyptusCC*
}

# Read configuration variable file if it is present
if [ -r $EUCALYPTUS/etc/eucalyptus/$NAME.conf ]; then
    . $EUCALYPTUS/etc/eucalyptus/$NAME.conf
else
    echo "Cannot find eucalyptus configuration file!"
    exit 1
fi
if [ "$EUCALYPTUS" = "not_configured" ]; then
    echo "EUCALYPTUS not configured!" 
    exit 1
fi
if [ -n "$TEMPLATE_PATH" ]; then
    echo "TEMPLATE_PATH is defined: do you have an old configuration?"
    exit 1
fi

START_CLOUD="`echo $START_CLOUD | tr 'a-z' 'A-Z'`"
START_CC="`echo $START_CC | tr 'a-z' 'A-Z'`"
START_NC="`echo $START_NC | tr 'a-z' 'A-Z'`"
if [ -z "$EUCA_USER" ] ; then
    EUCA_USER="root"
fi

# let's see if the user exists
ID="`which id 2> /dev/null`"
if [ -n "$ID" ]; then
    if ! $ID $EUCA_USER > /dev/null 2> /dev/null ; then
        echo "User $EUCA_USER doesn't exists!"
        exit 1
    fi
fi

# let's try to pick the system apache2
HTTPD="`which apache2 2> /dev/null`"        # .deb based machines
if [ -z  "$HTTPD" ]; then
    HTTPD="`which httpd 2> /dev/null`"    # .rpm based machines
fi
HTTPD_HOME="/tmp/"

# set up eucalyptus var locations to be owned by EUCA_USER
for y in $EUCALYPTUS/var/lib/eucalyptus/ $EUCALYPTUS/var/run/eucalyptus/ $EUCALYPTUS/var/log/eucalyptus/ $INSTANCE_PATH; do
    if [ $y != "/" -a -d $y ]
    then
        chown -R $EUCA_USER:$EUCA_USER $y
    fi
done

# set the library path correctly
export LD_LIBRARY_PATH="$AXIS2C_HOME/lib:$LIBVIRT_HOME/lib:$LIBVIRT_HOME/usr/lib:$EUCALYPTUS/lib:$LD_LIBRARY_PATH"

case "$1" in
    start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
        do_start
           case "$?" in
            0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
            2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac

    ;;
  stop)
        log_begin_msg "Stopping $DESC"
        do_stop
        log_end_msg 0
    ;;
    restart|reload)
        log_begin_msg "Restarting $DESC"
        do_stop
        do_start
      ;;
    *)
        echo "Usage: $NAME {start|stop|restart}" >&2
        exit 3
    ;;
esac

:
