#!/bin/bash
################################################################################
# Filename: ./src/base/etc/init.d/qrm-start
# 
# The contents of this file are subject to the Qlusters Public License
# Version 1.1 (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.openqrm.org/content/view/33/69/.
# 
# Software distributed under the License is distributed on an AS IS basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
# 
# The Original Code is openQRM.
# 
# The Initial Developer of the Original Code is Qlusters Corporation. Portions
# created by Qlusters Corporation are Copyright (C) 2003-2005. All Rights
# Reserved.
# 
# Contributor(s):
# 
# Description:
# 
# Comments:
################################################################################
# qrm-start	This shell script takes care of starting the qrm services
#
# chkconfig: 345 98 19
# description: Wrapper script for Qrm

PATH=/usr/bin:/sbin:/bin:/usr/sbin
export PATH
# source paramters + functions
. /var/qrm/conf/node-parameter.conf
. $QRM_NODES_BASE_DIR/qrm/include/qrm-functions
. $QRM_NODES_BASE_DIR/qrm/include/qrm-nodes-functions

# for qrm-execd + qrm-infod
export QRM_SERVER_BASE_DIR
export QRM_NODES_LOG_DIR
export QRM_NODES_BASE_DIR
export q_qrm_ip=$QRM_SERVER_IP_ADDRESS
export q_statistics_servlet="http://$q_qrm_ip/monitor/submit.do"
export q_peer=$QRM_SERVER_IP_ADDRESS
export q_res_ip=$CS_QRM_IP
export q_res_id=$CS_ID
export q_ve_id=$VE_ID
export q_gather_interval=1
export q_cpu_util_changedelta=0.1
export q_cpu_load_changedelta=0.1
export q_mem_used_changedelta=20
export q_swap_used_changedelta=20
export q_node_heartbeat=$CS_HEARTBEAT
export q_min_stats_interval=60
export q_agent_mode=$CS_TYPE
export QRM_EASYMIGRATION_CONF=/etc/qrm/easymigration.conf
if [ "$QRM_NODES_LOG_DIR" != "syslog" ]; then
	export LOG_SYSLOG=false
	export LOG_DIR=$QRM_NODES_LOG_DIR
	export LOCKFILE=$QRM_NODES_LOG_DIR/qrm.lock
	mkdir -p $QRM_NODES_LOG_DIR
else
	export LOG_SYSLOG=true
	export LOG_DIR=none
	export LOCKFILE=$QRM_NODES_BASE_DIR/qrm/logs/qrm.lock
	mkdir -p $QRM_NODES_BASE_DIR/qrm/logs/
fi

# wait until /proc is mounted and /tmp is writeable
while [ ! -f /proc/cpuinfo ]; do
        sleep 1
done
while ! touch /tmp/iswriteable 1>/dev/null 2>&1; do
        sleep 1
done
/bin/rm -f /tmp/iswriteable


echo "Starting qrm-services"
# start qrm-execd
if [ -f $QRM_EASYMIGRATION_CONF ]; then
	echo "Node in easymigration phase 1 detected. Bringing up Qrm-interface"
	. $QRM_EASYMIGRATION_CONF
	if [ "$QRM_CS_NIC" == "" ] || [ "$QRM_CS_IP" == "" ] || [ "$QRM_CS_BROADCAST" == "" ]; then
		echo "ERROR: Missing parameter in $QRM_EASYMIGRATION_CONF"
		exit 1
	fi
	/sbin/ifconfig $QRM_CS_NIC:qrm $QRM_CS_IP netmask $QRM_CS_SUBNET broadcast $QRM_CS_BROADCAST up 1>/dev/null 2>&1
fi
if [ "$QRM_NODES_LOG_DIR" != "syslog" ]; then
	$QRM_NODES_BASE_DIR/qrm/sbin/qrm-tcp-monitor $CS_QRM_IP $QRM_REXECD_PORT 2>$QRM_NODES_LOG_DIR/tcpmon.log | $QRM_NODES_BASE_DIR/qrm/sbin/qrm-execd &
else
	$QRM_NODES_BASE_DIR/qrm/sbin/qrm-tcp-monitor $CS_QRM_IP $QRM_REXECD_PORT 2>>/var/log/messages| $QRM_NODES_BASE_DIR/qrm/sbin/qrm-execd &
fi
$QRM_NODES_BASE_DIR/qrm/sbin/qrm-infod id=$CS_ID &
# be sure to umount /old_root and /old_root/mnt2 (for shared images)
/bin/umount /old_root/mnt2 1>/dev/null 2>&1
/bin/umount /old_root 1>/dev/null 2>&1
touch $LOCKFILE



