#!/bin/sh

# Copyright 2011, Hortonworks Inc.  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.

# Hadoop Single Node Services    Bring up/down HDP stack
#
# chkconfig: 345 99 10
# description: Start/Stop all HDP Services  configured to \
#              start at boot time.
#

NOW=$(date +"%m-%d-%Y-%H-%M-%S")
LOGFILE="/root/hdp-stack-$1-$NOW.log"
#exec 1>>${LOGFILE} 2>&1
exec 2>&1
{
echo
echo
echo "**************************************************"
echo "###############   ${NOW}  ########################"
echo "**************************************************"
echo
echo

HDP_STACK_RUNTIME_GSINSTALLER_DIR=/root/HDP-gsInstaller/gsInstaller
HDP_STACK_START=${HDP_STACK_RUNTIME_GSINSTALLER_DIR}/startHDP.sh
HDP_STACK_STOP=${HDP_STACK_RUNTIME_GSINSTALLER_DIR}/stopHDP.sh
HDP_STACK_SMOKE=${HDP_STACK_RUNTIME_GSINSTALLER_DIR}/gsSmokeTest.sh
HDP_STACK_IPADDRESS=${HDP_STACK_RUNTIME_GSINSTALLER_DIR}/ip_address_change.sh

RETVAL=0

case "$1" in
   start)
      echo "============================="
      echo "Starting hdp-stack..."
      echo "============================="
      $0 setipforvm
      RETVAL=$?
      echo
      [ $RETVAL -eq 0 ] && echo "IP address change task completed. Check the log file ${LOGFILE} for details and exceptions, if any." || exit 1
      [ -f ${HDP_STACK_START} ] || exit 1
      eval "${HDP_STACK_START}"
      RETVAL=$?
      echo
      [ $RETVAL -eq 0 ] && echo "Start script executed successfully. Check the log file ${LOGFILE} for details and exceptions, if any."
      #sleep 30
      #$0 syscheck
      ;;

  stop)
      echo "=================================="
      echo "Shutting down hdp-stack..."
      echo "=================================="
      [ -f ${HDP_STACK_STOP} ] || exit 1
      eval "${HDP_STACK_STOP}"
      RETVAL=$?
      echo
      [ $RETVAL -eq 0 ] && echo "Stop script executed successfully. Check the log file ${LOGFILE} for details and exceptions, if any."
      ;;
  syscheck)
      echo "=================================="
      echo "Running the System Smoke Test on installed  hdp-stack..."
      echo "=================================="
      [ -f ${HDP_STACK_SMOKE} ] || exit 1
      cd ${HDP_STACK_RUNTIME_GSINSTALLER_DIR}
      eval "${HDP_STACK_SMOKE}"
      RETVAL=$?
      echo
      [ $RETVAL -eq 0 ] && echo "Smoke Test Complete. Check the log file ${LOGFILE} for details and exceptions, if any."
      ;;
  setipforvm)
      echo "=================================="
      echo "Setting the correct dhcp IP for the System installed with  hdp-stack..."
      echo "=================================="
      [ -f ${HDP_STACK_IPADDRESS} ] || exit 1
      eval "${HDP_STACK_IPADDRESS}"
      RETVAL=$?
      echo
      #[ $RETVAL -eq 0 ] && echo "IP address change task completed" || exit 1
      ;;
  restart|reload)
   	$0 stop
   	$0 start
   	RETVAL=$?
	;;
  status)
      echo "======================================="
      echo "Checking status of hdp-gmond..."
      echo "======================================="
      [ -f ${HDP_GANLIA_GMOND_CHECKER} ] || exit 1
      eval "${HDP_GANLIA_GMOND_CHECKER}"
      RETVAL=$?
      ;;
  *)
	echo "Usage: $0 {start|syscheck|stop}"
	exit 1
esac
#echo "Return Value $RETVAL"

exit $RETVAL
} | tee -a ${LOGFILE}
