launch-hadoop-master 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env bash
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. # Launch an EC2 Hadoop master.
  17. set -o errexit
  18. if [ -z $1 ]; then
  19. echo "Cluster name required!"
  20. exit -1
  21. fi
  22. CLUSTER=$1
  23. # Import variables
  24. bin=`dirname "$0"`
  25. bin=`cd "$bin"; pwd`
  26. . "$bin"/hadoop-ec2-env.sh
  27. if [ -z $AWS_ACCOUNT_ID ]; then
  28. echo "Please set AWS_ACCOUNT_ID in $bin/hadoop-ec2-env.sh."
  29. exit -1
  30. fi
  31. echo "Testing for existing master in group: $CLUSTER"
  32. MASTER_EC2_HOST=`ec2-describe-instances | awk '"RESERVATION" == $1 && "'$CLUSTER_MASTER'" == $4, "RESERVATION" == $1 && "'$CLUSTER_MASTER'" != $4'`
  33. MASTER_EC2_HOST=`echo "$MASTER_EC2_HOST" | awk '"INSTANCE" == $1 && "running" == $6 {print $4}'`
  34. if [ ! -z "$MASTER_EC2_HOST" ]; then
  35. echo "Master already running on: $MASTER_EC2_HOST"
  36. MASTER_HOST=`ec2-describe-instances $INSTANCE | grep INSTANCE | grep running | grep $MASTER_EC2_HOST | awk '{print $5}'`
  37. echo $MASTER_HOST > $MASTER_PRIVATE_IP_PATH
  38. echo $MASTER_EC2_HOST > $MASTER_IP_PATH
  39. exit 0
  40. fi
  41. if ! ec2-describe-group $CLUSTER_MASTER > /dev/null 2>&1; then
  42. echo "Creating group $CLUSTER_MASTER"
  43. ec2-add-group $CLUSTER_MASTER -d "Group for Hadoop Master."
  44. ec2-authorize $CLUSTER_MASTER -o $CLUSTER_MASTER -u $AWS_ACCOUNT_ID
  45. ec2-authorize $CLUSTER_MASTER -p 22 # ssh
  46. if [ $ENABLE_WEB_PORTS == "true" ]; then
  47. ec2-authorize $CLUSTER_MASTER -p 50030 # JobTracker web interface
  48. ec2-authorize $CLUSTER_MASTER -p 50060 # TaskTracker web interface
  49. ec2-authorize $CLUSTER_MASTER -p 50070 # NameNode web interface
  50. ec2-authorize $CLUSTER_MASTER -p 50075 # DataNode web interface
  51. fi
  52. fi
  53. if ! ec2-describe-group $CLUSTER > /dev/null 2>&1; then
  54. echo "Creating group $CLUSTER"
  55. ec2-add-group $CLUSTER -d "Group for Hadoop Slaves."
  56. ec2-authorize $CLUSTER -o $CLUSTER -u $AWS_ACCOUNT_ID
  57. ec2-authorize $CLUSTER -p 22 # ssh
  58. if [ $ENABLE_WEB_PORTS == "true" ]; then
  59. ec2-authorize $CLUSTER -p 50030 # JobTracker web interface
  60. ec2-authorize $CLUSTER -p 50060 # TaskTracker web interface
  61. ec2-authorize $CLUSTER -p 50070 # NameNode web interface
  62. ec2-authorize $CLUSTER -p 50075 # DataNode web interface
  63. fi
  64. ec2-authorize $CLUSTER_MASTER -o $CLUSTER -u $AWS_ACCOUNT_ID
  65. ec2-authorize $CLUSTER -o $CLUSTER_MASTER -u $AWS_ACCOUNT_ID
  66. fi
  67. # Finding Hadoop image
  68. AMI_IMAGE=`ec2-describe-images -a | grep $S3_BUCKET | grep $HADOOP_VERSION | grep $ARCH | grep available | awk '{print $2}'`
  69. # Start a master
  70. echo "Starting master with AMI $AMI_IMAGE"
  71. USER_DATA="MASTER_HOST=master,MAX_MAP_TASKS=$MAX_MAP_TASKS,MAX_REDUCE_TASKS=$MAX_REDUCE_TASKS,COMPRESS=$COMPRESS"
  72. INSTANCE=`ec2-run-instances $AMI_IMAGE -n 1 -g $CLUSTER_MASTER -k $KEY_NAME -f "$bin"/$USER_DATA_FILE -t $INSTANCE_TYPE $KERNEL_ARG | grep INSTANCE | awk '{print $2}'`
  73. echo "Waiting for instance $INSTANCE to start"
  74. while true; do
  75. printf "."
  76. # get private dns
  77. MASTER_HOST=`ec2-describe-instances $INSTANCE | grep running | awk '{print $5}'`
  78. if [ ! -z $MASTER_HOST ]; then
  79. echo "Started as $MASTER_HOST"
  80. break;
  81. fi
  82. sleep 1
  83. done
  84. MASTER_EC2_HOST=`ec2-describe-instances $INSTANCE | grep INSTANCE | grep running | grep $MASTER_HOST | awk '{print $4}'`
  85. echo $MASTER_HOST > $MASTER_PRIVATE_IP_PATH
  86. echo $MASTER_EC2_HOST > $MASTER_IP_PATH
  87. MASTER_EC2_ZONE=`ec2-describe-instances $INSTANCE | grep INSTANCE | grep running | grep $MASTER_HOST | awk '{print $11}'`
  88. echo $MASTER_EC2_ZONE > $MASTER_ZONE_PATH
  89. while true; do
  90. if ssh $SSH_OPTS "root@$MASTER_EC2_HOST" 'echo "hello"' > /dev/null 2>&1; then
  91. break;
  92. fi
  93. sleep 5
  94. done
  95. echo "Copying private key to master"
  96. scp $SSH_OPTS $PRIVATE_KEY_PATH "root@$MASTER_EC2_HOST:/root/.ssh/id_rsa"
  97. ssh $SSH_OPTS "root@$MASTER_EC2_HOST" "chmod 600 /root/.ssh/id_rsa"
  98. MASTER_IP=`dig +short $MASTER_EC2_HOST`
  99. echo "Master is $MASTER_EC2_HOST, ip is $MASTER_IP, zone is $MASTER_EC2_ZONE."