#!/bin/sh
#
# gridctl:	Control grid
#
# $Id: gridctl 527 2013-05-16 10:49:40Z whitestar $
#
# Copyright 2013 the original author or authors.
#
# 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.
#


PATH=.:/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin:`dirname $0`


help() {
	echo 'Usage: gridctl SERVICE,... {start|stop|status}'
	echo ''
	echo 'Services: '
	echo '    hdfs |balancer |mapred|historyserver  (Apache Hadoop 1.x, CDH3, HDP1 or before)'
	echo '    hdfs2|balancer2|yarn  |historyserver2 (Apache Hadoop 2.x, CDH4, HDP2 or later)'
	echo '    hbase|zookeeper'
	echo 'Commands: '
	echo '    start|stop: control the grid daemons'
	echo '    status:     print the Java process(es)'
}


ARGS="$@"
SERVICES=`echo $1 | tr ',' ' '`
COMMAND=$2

# Validation
if [ $# != 2 ]; then
	help
	exit 1
fi

if [ x"$HADOOP_DISTRIBUTION" = x'' ]; then
	echo '[ERROR] HADOOP_DISTRIBUTION not set!'
	help
    exit 1
fi

# Settings
if [ x"$HADOOP_HOME" != x'' ]; then
	HADOOP_PREFIX=$HADOOP_HOME
else
	if [ x"$HADOOP_PREFIX" != x'' ]; then
		HADOOP_HOME=$HADOOP_PREFIX
	else
		if [ -d '/usr/lib/hadoop' ]; then
			HADOOP_HOME='/usr/lib/hadoop'
			HADOOP_PREFIX=$HADOOP_HOME
		elif [ -d '/grid/usr/hadoop' ]; then
			HADOOP_HOME='/grid/usr/hadoop'
			HADOOP_PREFIX=$HADOOP_HOME
		else
			echo '[ERROR] HADOOP_HOME or HADOOP_PREFIX could not be specified!'
			help
			exit 1
		fi
	fi
fi

if [ x"$HADOOP_ARTIFACT_TYPE" = x'' ]; then
	if [ $HADOOP_PREFIX = '/usr/lib/hadoop' ]; then
		HADOOP_ARTIFACT_TYPE='package'
	else
		HADOOP_ARTIFACT_TYPE='tarball'
	fi
fi

if [ x"$JAVA_HOME" != x'' ]; then
	JPS_COMMAND="${JAVA_HOME}/bin/jps"
else
	JPS_COMMAND='jps'
fi

DOMAIN=${DOMAIN:-''}
#DOMAIN='grid.example.com'

# Hadoop (HDFS, MapReduce)
HADOOP_CONF_DIR=${HADOOP_CONF_DIR:-''}
if [ x"$HADOOP_CONF_DIR" != x'' ]; then
	HADOOP_CONF_OPT="--config ${HADOOP_CONF_DIR}"
else
	HADOOP_CONF_OPT=''
fi
HDFS_USER=${HDFS_USER:-'hdfs'}
MAPRED_USER=${MAPRED_USER:-'mapred'}
DN_INIT_USER=${DN_INIT_USER:-'root'}
NAMENODE=${NAMENODE:-'localhost'}
SECONDARYNAMENODE=${SECONDARYNAMENODE:-'localhost'}
DATANODES=${DATANODES:-'localhost'}
BALANCER=${BALANCER:-'localhost'}
JOBTRACKER=${JOBTRACKER:-'localhost'}
TASKTRACKERS=${TASKTRACKERS:-"$DATANODES"}
HISTORYSERVER=${HISTORYSERVER:-'localhost'}
#NAMENODE='nn00'
#SECONDARYNAMENODE='nn02'
#DATANODES='dn00000 dn00001'
#JOBTRACKER='jt00'
# YARN
YARN_USER=${YARN_USER:-'yarn'}
RESOURCEMANAGER=${RESOURCEMANAGER:-'localhost'}
NODEMANAGERS=${NODEMANAGERS:-"$DATANODES"}
#RESOURCEMANAGER='rm00'

# HBase
HBASE_HOME=${HBASE_HOME:-'/grid/usr/hbase'}
HBASE_USER=${HBASE_USER:-'hbase'}
HMASTERS=${HMASTERS:-'localhost'}
HREGIONSERVERS=${HREGIONSERVERS:-'localhost'}
#HMASTERS='hm00 hm01'
#HREGIONSERVERS='dn00000 dn00001'

# ZooKeeper
ZOOKEEPER_PREFIX=${ZOOKEEPER_PREFIX:-'/grid/usr/zookeeper'}
ZOOCFGDIR=${ZOOCFGDIR:-"${ZOOKEEPER_PREFIX}/conf"}
ZOOKEEPER_USER=${ZOOKEEPER_USER:-'zookeeper'}
ZOOKEEPER_PEERS=${ZOOKEEPER_PEERS:-'localhost'}
#ZOOKEEPER_PEERS='zk00 zk01 zk02'


# $1: target node list (e.g. 'zk00 zk01 zk02')
get_targets() {
	TARGETS=''
	HOSTS=$1
	for HOST in $HOSTS; do
		if [ x"$DOMAIN" != x'' ]; then
			FQDN=${HOST}.${DOMAIN}
		else
			FQDN=${HOST}
		fi
		if [ x"$TARGETS" = x'' ]; then
			TARGETS=$FQDN
		else
			TARGETS="${TARGETS},${FQDN}"
		fi
	done

	echo $TARGETS
}


get_passwd() {
	echo -n '[sudo] password via SSH: '
	stty -echo
	read PASSWD
	stty echo
	echo ''
}


ctl_hdfs() {
	DAEMON_COMMAND=${DAEMON_COMMAND:-"${HADOOP_PREFIX}/bin/hadoop-daemon.sh"}
	if [ x"$SERVICE_PREFIX" = x'' ]; then
    	if [ x"$HADOOP_DISTRIBUTION" = x'cdh3' ]; then
			SERVICE_PREFIX='hadoop-0.20-'
		else
			SERVICE_PREFIX='hadoop-'
		fi
	fi
	_NAMENODE=`get_targets "$NAMENODE"`
	_SECONDARYNAMENODE=`get_targets "$SECONDARYNAMENODE"`
	_DATANODES=`get_targets "$DATANODES"`
	echo "[Grid ${COMMAND}: ${DESC}]"
	echo "HADOOP_CONF_DIR: ${HADOOP_CONF_DIR}"
	echo "Daemon command: ${DAEMON_COMMAND}"
	echo "NameNode: ${_NAMENODE}"
	echo "SecondaryNameNode: ${_SECONDARYNAMENODE}"
	echo "DataNodes: ${_DATANODES}"
	get_passwd
	case "$COMMAND" in
		start)
			echo "Starting ${DESC} ..."
			if [ $HADOOP_ARTIFACT_TYPE = 'tarball' ]; then
				dsh_ipass -P $PASSWD -m $_NAMENODE -M -- sudo -S -p \'\' -u $HDFS_USER $DAEMON_COMMAND $HADOOP_CONF_OPT start namenode
				dsh_ipass -P $PASSWD -m $_SECONDARYNAMENODE -M -- sudo -S -p \'\' -u $HDFS_USER $DAEMON_COMMAND $HADOOP_CONF_OPT start secondarynamenode
				dsh_ipass -P $PASSWD -m $_DATANODES -M -- sudo -S -p \'\' -u $DN_INIT_USER $DAEMON_COMMAND $HADOOP_CONF_OPT start datanode
			elif [ $HADOOP_ARTIFACT_TYPE = 'package' ]; then
				dsh_ipass -P $PASSWD -m $_NAMENODE -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}namenode start
				dsh_ipass -P $PASSWD -m $_SECONDARYNAMENODE -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}secondarynamenode start
				dsh_ipass -P $PASSWD -m $_DATANODES -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}datanode start
			fi
			;;
		stop)
			echo "Stopping ${DESC} ..."
			if [ $HADOOP_ARTIFACT_TYPE = 'tarball' ]; then
				dsh_ipass -P $PASSWD -m $_DATANODES -M -- sudo -S -p \'\' -u $DN_INIT_USER $DAEMON_COMMAND $HADOOP_CONF_OPT stop datanode
				dsh_ipass -P $PASSWD -m $_SECONDARYNAMENODE -M -- sudo -S -p \'\' -u $HDFS_USER $DAEMON_COMMAND $HADOOP_CONF_OPT stop secondarynamenode
				dsh_ipass -P $PASSWD -m $_NAMENODE -M -- sudo -S -p \'\' -u $HDFS_USER $DAEMON_COMMAND $HADOOP_CONF_OPT stop namenode
			elif [ $HADOOP_ARTIFACT_TYPE = 'package' ]; then
				dsh_ipass -P $PASSWD -m $_DATANODES -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}datanode stop
				dsh_ipass -P $PASSWD -m $_SECONDARYNAMENODE -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}secondarynamenode stop
				dsh_ipass -P $PASSWD -m $_NAMENODE -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}namenode stop
			fi
			;;
		status)
			echo "${DESC} Status"
			if [ $HADOOP_ARTIFACT_TYPE = 'tarball' ]; then
				dsh_ipass -P $PASSWD -m $_NAMENODE -M -- sudo -S -p \'\' -u $HDFS_USER $JPS_COMMAND -mv
				dsh_ipass -P $PASSWD -m $_SECONDARYNAMENODE -M -- sudo -S -p \'\' -u $HDFS_USER $JPS_COMMAND -mv
				dsh_ipass -P $PASSWD -m $_DATANODES -M -- sudo -S -p \'\' -u $HDFS_USER $JPS_COMMAND -mv
			elif [ $HADOOP_ARTIFACT_TYPE = 'package' ]; then
				dsh_ipass -P $PASSWD -m $_DATANODES -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}datanode status
				dsh_ipass -P $PASSWD -m $_SECONDARYNAMENODE -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}secondarynamenode status
				dsh_ipass -P $PASSWD -m $_NAMENODE -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}namenode status
			fi
			;;
	esac
	DAEMON_COMMAND=''
	SERVICE_PREFIX=''
}


ctl_hdfs2() {
	if [ $HADOOP_ARTIFACT_TYPE = 'tarball' ]; then
		DAEMON_COMMAND=${DAEMON_COMMAND:-"${HADOOP_PREFIX}/sbin/hadoop-daemon.sh"}
	else
		DAEMON_COMMAND=${DAEMON_COMMAND:-'service'}
		SERVICE_PREFIX=${SERVICE_PREFIX:-'hadoop-hdfs-'}
	fi
	ctl_hdfs
}


ctl_balancer() {
	DAEMON_COMMAND=${DAEMON_COMMAND:-"${HADOOP_PREFIX}/bin/hadoop-daemon.sh"}
	USER=$HDFS_USER
	_BALANCER=`get_targets "$BALANCER"`
	echo "[Grid ${COMMAND}: ${DESC}]"
	echo "HADOOP_CONF_DIR: ${HADOOP_CONF_DIR}"
	echo "Daemon command: ${DAEMON_COMMAND}"
	echo "Balancer: ${_BALANCER}"
	get_passwd
	case "$COMMAND" in
		start)
			echo "Starting ${DESC} ..."
			dsh_ipass -P $PASSWD -m $_BALANCER -M -- sudo -S -p \'\' -u $USER $DAEMON_COMMAND $HADOOP_CONF_OPT start balancer
			;;
		stop)
			echo "Stopping ${DESC} ..."
			dsh_ipass -P $PASSWD -m $_BALANCER -M -- sudo -S -p \'\' -u $USER $DAEMON_COMMAND $HADOOP_CONF_OPT stop balancer
			;;
		status)
			echo "${DESC} Status"
			dsh_ipass -P $PASSWD -m $_BALANCER -M -- sudo -S -p \'\' -u $USER $JPS_COMMAND -mv
			;;
	esac
	DAEMON_COMMAND=''
	SERVICE_PREFIX=''
}


ctl_balancer2() {
	DAEMON_COMMAND=${DAEMON_COMMAND:-"${HADOOP_PREFIX}/sbin/hadoop-daemon.sh"}
	ctl_balancer
}


ctl_mapred() {
	if [ $HADOOP_ARTIFACT_TYPE = 'tarball' ]; then
		DAEMON_COMMAND="${HADOOP_PREFIX}/bin/hadoop-daemon.sh"
	else
		DAEMON_COMMAND=${DAEMON_COMMAND:-'service'}
		if [ x"$SERVICE_PREFIX" = x'' ]; then
    		if [ x"$HADOOP_DISTRIBUTION" = x'cdh4' ]; then
				SERVICE_PREFIX='hadoop-0.20-mapreduce-'
    		elif [ x"$HADOOP_DISTRIBUTION" = x'cdh3' ]; then
				SERVICE_PREFIX='hadoop-0.20-'
			else
				SERVICE_PREFIX='hadoop-'
			fi
		fi
	fi
	USER=$MAPRED_USER
	_JOBTRACKER=`get_targets "$JOBTRACKER"`
	_TASKTRACKERS=`get_targets "$TASKTRACKERS"`
	echo "[Grid ${COMMAND}: ${DESC}]"
	echo "HADOOP_CONF_DIR: ${HADOOP_CONF_DIR}"
	echo "Daemon command: ${DAEMON_COMMAND}"
	echo "JobTracker: ${_JOBTRACKER}"
	echo "TaskTrackers: ${_TASKTRACKERS}"
	get_passwd
	case "$COMMAND" in
		start)
			echo "Starting ${DESC} ..."
			if [ $HADOOP_ARTIFACT_TYPE = 'tarball' ]; then
				dsh_ipass -P $PASSWD -m $_JOBTRACKER -M -- sudo -S -p \'\' -u $USER $DAEMON_COMMAND $HADOOP_CONF_OPT start jobtracker
				dsh_ipass -P $PASSWD -m $_TASKTRACKERS -M -- sudo -S -p \'\' -u $USER $DAEMON_COMMAND $HADOOP_CONF_OPT start tasktracker
			elif [ $HADOOP_ARTIFACT_TYPE = 'package' ]; then
				dsh_ipass -P $PASSWD -m $_JOBTRACKER -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}jobtracker start
				dsh_ipass -P $PASSWD -m $_TASKTRACKERS -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}tasktracker start
			fi
			;;
		stop)
			echo "Stopping ${DESC} ..."
			if [ $HADOOP_ARTIFACT_TYPE = 'tarball' ]; then
				dsh_ipass -P $PASSWD -m $_TASKTRACKERS -M -- sudo -S -p \'\' -u $USER $DAEMON_COMMAND $HADOOP_CONF_OPT stop tasktracker
				dsh_ipass -P $PASSWD -m $_JOBTRACKER -M -- sudo -S -p \'\' -u $USER $DAEMON_COMMAND $HADOOP_CONF_OPT stop jobtracker
			elif [ $HADOOP_ARTIFACT_TYPE = 'package' ]; then
				dsh_ipass -P $PASSWD -m $_TASKTRACKERS -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}tasktracker stop
				dsh_ipass -P $PASSWD -m $_JOBTRACKER -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}jobtracker stop
			fi
			;;
		status)
			echo "${DESC} Status"
			if [ $HADOOP_ARTIFACT_TYPE = 'tarball' ]; then
				dsh_ipass -P $PASSWD -m $_JOBTRACKER -M -- sudo -S -p \'\' -u $USER $JPS_COMMAND -mv
				dsh_ipass -P $PASSWD -m $_TASKTRACKERS -M -- sudo -S -p \'\' -u $USER $JPS_COMMAND -mv
			elif [ $HADOOP_ARTIFACT_TYPE = 'package' ]; then
				dsh_ipass -P $PASSWD -m $_JOBTRACKER -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}jobtracker status
				dsh_ipass -P $PASSWD -m $_TASKTRACKERS -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}tasktracker status
			fi
			;;
	esac
	DAEMON_COMMAND=''
	SERVICE_PREFIX=''
}


ctl_historyserver() {
	DAEMON_COMMAND=${DAEMON_COMMAND:-"${HADOOP_PREFIX}/bin/hadoop-daemon.sh"}
	# init script is available in the HDP1.3 and later.
	SERVICE_PREFIX=${SERVICE_PREFIX:-'hadoop-'}
    if [ x"$SERVICE_PREFIX" = x'hadoop-' ] && [ x"$HADOOP_DISTRIBUTION" = x'cdh3' -o x"$HADOOP_DISTRIBUTION" = x'cdh4' ]; then
		echo '[ERROR] Non-embeded HistoryServer is not implemented yet in the CDH3 and CDH4-MR1.'
		exit 1
	fi
	USER=$MAPRED_USER
	_HISTORYSERVER=`get_targets "$HISTORYSERVER"`
	echo "[Grid ${COMMAND}: ${DESC}]"
	echo "HADOOP_CONF_DIR: ${HADOOP_CONF_DIR}"
	echo "Daemon command: ${DAEMON_COMMAND}"
	echo "HistoryServer: ${_HISTORYSERVER}"
	get_passwd
	case "$COMMAND" in
		start)
			echo "Starting ${DESC} ..."
			if [ $HADOOP_ARTIFACT_TYPE = 'tarball' ]; then
				dsh_ipass -P $PASSWD -m $_HISTORYSERVER -M -- sudo -S -p \'\' -u $USER $DAEMON_COMMAND $HADOOP_CONF_OPT start historyserver
			elif [ $HADOOP_ARTIFACT_TYPE = 'package' ]; then
				dsh_ipass -P $PASSWD -m $_HISTORYSERVER -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}historyserver start
			fi
			;;
		stop)
			echo "Stopping ${DESC} ..."
			if [ $HADOOP_ARTIFACT_TYPE = 'tarball' ]; then
				dsh_ipass -P $PASSWD -m $_HISTORYSERVER -M -- sudo -S -p \'\' -u $USER $DAEMON_COMMAND $HADOOP_CONF_OPT stop historyserver
			elif [ $HADOOP_ARTIFACT_TYPE = 'package' ]; then
				dsh_ipass -P $PASSWD -m $_HISTORYSERVER -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}historyserver stop
			fi
			;;
		status)
			echo "${DESC} Status"
			if [ $HADOOP_ARTIFACT_TYPE = 'tarball' ]; then
				dsh_ipass -P $PASSWD -m $_HISTORYSERVER -M -- sudo -S -p \'\' -u $USER $JPS_COMMAND -mv
			elif [ $HADOOP_ARTIFACT_TYPE = 'package' ]; then
				dsh_ipass -P $PASSWD -m $_HISTORYSERVER -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}historyserver status
			fi
			;;
	esac
	DAEMON_COMMAND=''
	SERVICE_PREFIX=''
}


ctl_historyserver2() {
	if [ $HADOOP_ARTIFACT_TYPE = 'tarball' ]; then
		DAEMON_COMMAND=${DAEMON_COMMAND:-"${HADOOP_PREFIX}/sbin/mr-jobhistory-daemon.sh"}
	else
		DAEMON_COMMAND=${DAEMON_COMMAND:-'service'}
		SERVICE_PREFIX=${SERVICE_PREFIX:-'hadoop-mapreduce-'}
	fi
	ctl_historyserver
}


ctl_yarn() {
	if [ $HADOOP_ARTIFACT_TYPE = 'tarball' ]; then
		DAEMON_COMMAND="${HADOOP_PREFIX}/sbin/yarn-daemon.sh"
	else
		DAEMON_COMMAND=${DAEMON_COMMAND:-'service'}
		SERVICE_PREFIX=${SERVICE_PREFIX:-'hadoop-yarn-'}
	fi
	USER=$YARN_USER
	_RESOURCEMANAGER=`get_targets "$RESOURCEMANAGER"`
	_NODEMANAGERS=`get_targets "$NODEMANAGERS"`
	echo "[Grid ${COMMAND}: ${DESC}]"
	echo "HADOOP_CONF_DIR: ${HADOOP_CONF_DIR}"
	echo "Daemon command: ${DAEMON_COMMAND}"
	echo "ResourceManager: ${_RESOURCEMANAGER}"
	echo "NodeManagers: ${_NODEMANAGERS}"
	get_passwd
	case "$COMMAND" in
		start)
			echo "Starting ${DESC} ..."
			if [ $HADOOP_ARTIFACT_TYPE = 'tarball' ]; then
				dsh_ipass -P $PASSWD -m $_RESOURCEMANAGER -M -- sudo -S -p \'\' -u $USER $DAEMON_COMMAND $HADOOP_CONF_OPT start resourcemanager
				dsh_ipass -P $PASSWD -m $_NODEMANAGERS -M -- sudo -S -p \'\' -u $USER $DAEMON_COMMAND $HADOOP_CONF_OPT start nodemanager
			elif [ $HADOOP_ARTIFACT_TYPE = 'package' ]; then
				dsh_ipass -P $PASSWD -m $_RESOURCEMANAGER -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}resourcemanager start
				dsh_ipass -P $PASSWD -m $_NODEMANAGERS -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}nodemanager start
			fi
			;;
		stop)
			echo "Stopping ${DESC} ..."
			if [ $HADOOP_ARTIFACT_TYPE = 'tarball' ]; then
				dsh_ipass -P $PASSWD -m $_NODEMANAGERS -M -- sudo -S -p \'\' -u $USER $DAEMON_COMMAND $HADOOP_CONF_OPT stop nodemanager
				dsh_ipass -P $PASSWD -m $_RESOURCEMANAGER -M -- sudo -S -p \'\' -u $USER $DAEMON_COMMAND $HADOOP_CONF_OPT stop resourcemanager
			elif [ $HADOOP_ARTIFACT_TYPE = 'package' ]; then
				dsh_ipass -P $PASSWD -m $_NODEMANAGERS -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}nodemanager stop
				dsh_ipass -P $PASSWD -m $_RESOURCEMANAGER -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}resourcemanager stop
			fi
			;;
		status)
			echo "${DESC} Status"
			if [ $HADOOP_ARTIFACT_TYPE = 'tarball' ]; then
				dsh_ipass -P $PASSWD -m $_RESOURCEMANAGER -M -- sudo -S -p \'\' -u $USER $JPS_COMMAND -mv
				dsh_ipass -P $PASSWD -m $_NODEMANAGERS -M -- sudo -S -p \'\' -u $USER $JPS_COMMAND -mv
			elif [ $HADOOP_ARTIFACT_TYPE = 'package' ]; then
				dsh_ipass -P $PASSWD -m $_NODEMANAGERS -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}nodemanager status
				dsh_ipass -P $PASSWD -m $_RESOURCEMANAGER -M -- sudo -S -p \'\' service ${SERVICE_PREFIX}resourcemanager status
			fi
			;;
	esac
	DAEMON_COMMAND=''
	SERVICE_PREFIX=''
}


ctl_hbase() {
	DAEMON_COMMAND="${HBASE_HOME}/bin/hbase-daemon.sh"
	USER=$HBASE_USER
	_HMASTERS=`get_targets "$HMASTERS"`
	_HREGIONSERVERS=`get_targets "$HREGIONSERVERS"`
	echo "[Grid ${COMMAND}: ${DESC}]"
	echo "HBASE_HOME: ${HBASE_HOME}"
	echo "Daemon command: ${DAEMON_COMMAND}"
	echo "HMasters: ${_HMASTERS}"
	echo "HRegionServers: ${_HREGIONSERVERS}"
	get_passwd
	case "$COMMAND" in
		start)
			echo "Starting ${DESC} ..."
			dsh_ipass -P $PASSWD -m $_HMASTERS -M -- sudo -S -p \'\' -u $USER $DAEMON_COMMAND start master
			dsh_ipass -P $PASSWD -m $_HREGIONSERVERS -M -- sudo -S -p \'\' -u $USER $DAEMON_COMMAND start regionserver
			;;
		stop)
			echo "Stopping ${DESC} ..."
			dsh_ipass -P $PASSWD -m $_HREGIONSERVERS -M -- sudo -S -p \'\' -u $USER $DAEMON_COMMAND stop regionserver
			dsh_ipass -P $PASSWD -m $_HMASTERS -M -- sudo -S -p \'\' -u $USER $DAEMON_COMMAND stop master
			;;
		status)
			echo "${DESC} Status"
			dsh_ipass -P $PASSWD -m $_HMASTERS -M -- sudo -S -p \'\' -u $USER $JPS_COMMAND -mv
			dsh_ipass -P $PASSWD -m $_HREGIONSERVERS -M -- sudo -S -p \'\' -u $USER $JPS_COMMAND -mv
			;;
	esac
	DAEMON_COMMAND=''
	SERVICE_PREFIX=''
}


ctl_zookeeper() {
	DAEMON_COMMAND="${ZOOKEEPER_PREFIX}/bin/zkServer.sh"
	USER=$ZOOKEEPER_USER
	PEERS=`get_targets "$ZOOKEEPER_PEERS"`
	echo "[Grid ${COMMAND}: ${DESC}]"
	echo "ZOOKEEPER_PREFIX: ${ZOOKEEPER_PREFIX}"
	echo "ZOOCFGDIR: ${ZOOCFGDIR}"
	echo "Daemon command: ${DAEMON_COMMAND}"
	echo "ZooKeeper Peers: ${PEERS}"
	get_passwd
	case "$COMMAND" in
		start)
			echo "Starting ${DESC} by ${USER} user ..."
			DAEMON_COMMAND="export ZOOCFGDIR=${ZOOCFGDIR};${DAEMON_COMMAND} start"
			dsh_ipass -P $PASSWD -m $PEERS -M -- sudo -S -p \'\' -u $USER sh -c "'"$DAEMON_COMMAND"'"
			;;
		stop)
			echo "Stopping ${DESC} by ${USER} user ..."
			DAEMON_COMMAND="export ZOOCFGDIR=${ZOOCFGDIR};${DAEMON_COMMAND} stop"
			dsh_ipass -P $PASSWD -m $PEERS -M -- sudo -S -p \'\' -u $USER sh -c "'"$DAEMON_COMMAND"'"
			;;
		status)
			echo "${DESC} Status"
			dsh_ipass -P $PASSWD -m $PEERS -M -- sudo -S -p \'\' -u $USER $JPS_COMMAND -mv
			;;
	esac
	DAEMON_COMMAND=''
	SERVICE_PREFIX=''
}


# Main
echo "DOMAIN: ${DOMAIN}"
echo "HADOOP_ARTIFACT_TYPE: ${HADOOP_ARTIFACT_TYPE}"
echo "HADOOP_DISTRIBUTION: ${HADOOP_DISTRIBUTION}"
echo "HADOOP_HOME: ${HADOOP_HOME}"
echo "HADOOP_PREFIX: ${HADOOP_PREFIX}"

for SERVICE in $SERVICES; do
	case "$SERVICE" in
		hdfs)
			DESC='HDFS Cluster'
			ctl_hdfs
			;;
		hdfs2)
			DESC='HDFS-2 Cluster'
			ctl_hdfs2
			;;
		balancer)
			DESC='HDFS Balancer'
			ctl_balancer
			;;
		balancer2)
			DESC='HDFS-2 Balancer'
			ctl_balancer2
			;;
		yarn)
			DESC='YARN Cluster'
			ctl_yarn
			;;
		mapred)
			DESC='MapReduce Cluster'
			ctl_mapred
			;;
		historyserver)
			DESC='MapReduce JobHistoryServer'
			ctl_historyserver
			;;
		historyserver2)
			DESC='MapReduce JobHistoryServer2'
			ctl_historyserver2
			;;
		hbase)
			DESC='HBase Cluster'
			ctl_hbase
			;;
		zookeeper)
			DESC='ZooKeeper Ensemble'
			ctl_zookeeper
			;;
		*)
			help
			exit 1
			;;
	esac
done

exit 0
