hbase/contrib/ec2/bin/launch-hbase-master

108 lines
3.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
# Launch an EC2 HBase master.
if [ -z $1 ]; then
echo "Cluster name required!"
exit 1
fi
CLUSTER=$1
if [ -z $2 ]; then
echo "Must specify the number of slaves to start."
exit 1
fi
NUM_SLAVES=$2
# Import variables
bin=`dirname "$0"`
bin=`cd "$bin"; pwd`
. "$bin"/hbase-ec2-env.sh
if [ -z $AWS_ACCOUNT_ID ]; then
echo "Please set AWS_ACCOUNT_ID in $bin/hbase-ec2-env.sh."
exit 1
fi
type=$MASTER_INSTANCE_TYPE
[ -z "$type" ] && type=$SLAVE_INSTANCE_TYPE
arch=$MASTER_ARCH
[ -z "$arch" ] && arch=$SLAVE_ARCH
echo "Testing for existing master in group: $CLUSTER"
host=`ec2-describe-instances $TOOL_OPTS | awk '"RESERVATION" == $1 && "'$CLUSTER_MASTER'" == $4, "RESERVATION" == $1 && "'$CLUSTER_MASTER'" != $4'`
host=`echo "$host" | awk '"INSTANCE" == $1 && "running" == $6 {print $4}'`
if [ ! -z "$host" ]; then
echo "Master already running on: $host"
exit 0
fi
# Finding HBase image
[ -z "$AMI_IMAGE" ] && AMI_IMAGE=`ec2-describe-images $TOOL_OPTS -a | grep $S3_BUCKET | grep hbase | grep $HBASE_VERSION-$arch | grep available | awk '{print $2}'`
# Start a master
echo "Starting master with AMI $AMI_IMAGE (arch $arch)"
# Substituting zookeeper quorum
quorum=`cat $ZOOKEEPER_QUORUM_PATH`
sed -e "s|%ZOOKEEPER_QUORUM%|$quorum|" \
-e "s|%NUM_SLAVES%|$NUM_SLAVES|" \
-e "s|%EXTRA_PACKAGES%|$EXTRA_PACKAGES|" \
"$bin"/$USER_DATA_FILE > "$bin"/$USER_DATA_FILE.master
inst=`ec2-run-instances $TOOL_OPTS $AMI_IMAGE -n 1 -g $CLUSTER_MASTER -k root -f "$bin"/$USER_DATA_FILE.master -t $type | grep INSTANCE | awk '{print $2}'`
if [ "$ENABLE_ELASTIC_IPS" = "true" ] ; then
addr=`ec2-allocate-address $TOOL_OPTS | awk '{print $2}'`
ec2-associate-address $TOOL_OPTS $addr -i $inst
fi
echo -n "Waiting for instance $inst to start"
while true; do
printf "."
# get private dns
host=`ec2-describe-instances $TOOL_OPTS $inst | grep running | awk '{print $5}'`
if [ ! -z $host ]; then
echo " Started as $host"
break;
fi
sleep 1
done
rm -f "$bin"/$USER_DATA_FILE.master
# get public (elastic) hostname
host=`ec2-describe-instances $TOOL_OPTS $inst | grep INSTANCE | grep running | grep $host | awk '{print $4}'`
echo $host > $MASTER_ADDR_PATH
echo $addr > $MASTER_IP_PATH
# get zone
zone=`ec2-describe-instances $TOOL_OPTS $inst | grep INSTANCE | grep running | grep $host | awk '{print $11}'`
echo $zone > $MASTER_ZONE_PATH
while true; do
REPLY=`ssh $SSH_OPTS "root@$host" 'echo "hello"'`
if [ ! -z $REPLY ]; then
break;
fi
sleep 5
done
scp $SSH_OPTS $EC2_ROOT_SSH_KEY "root@$host:/root/.ssh/id_rsa"
ssh $SSH_OPTS "root@$host" "chmod 600 /root/.ssh/id_rsa"
echo "Master is $host in zone $zone"