[120] | 1 | #!/usr/bin/env bash |
---|
| 2 | |
---|
| 3 | # Licensed to the Apache Software Foundation (ASF) under one or more |
---|
| 4 | # contributor license agreements. See the NOTICE file distributed with |
---|
| 5 | # this work for additional information regarding copyright ownership. |
---|
| 6 | # The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
| 7 | # (the "License"); you may not use this file except in compliance with |
---|
| 8 | # the License. You may obtain a copy of the License at |
---|
| 9 | # |
---|
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | # |
---|
| 12 | # Unless required by applicable law or agreed to in writing, software |
---|
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | # See the License for the specific language governing permissions and |
---|
| 16 | # limitations under the License. |
---|
| 17 | |
---|
| 18 | # Create a Hadoop AMI. |
---|
| 19 | # Inspired by Jonathan Siegel's EC2 script (http://blogsiegel.blogspot.com/2006/08/sandboxing-amazon-ec2.html) |
---|
| 20 | |
---|
| 21 | # Import variables |
---|
| 22 | bin=`dirname "$0"` |
---|
| 23 | bin=`cd "$bin"; pwd` |
---|
| 24 | . "$bin"/hadoop-ec2-env.sh |
---|
| 25 | |
---|
| 26 | AMI_IMAGE=`ec2-describe-images -a | grep $S3_BUCKET | grep $HADOOP_VERSION | grep $ARCH | grep available | awk '{print $2}'` |
---|
| 27 | |
---|
| 28 | [ ! -z $AMI_IMAGE ] && echo "AMI already registered, use: ec2-deregister $AMI_IMAGE" && exit -1 |
---|
| 29 | |
---|
| 30 | echo "Starting a AMI with ID $BASE_AMI_IMAGE." |
---|
| 31 | OUTPUT=`ec2-run-instances $BASE_AMI_IMAGE -k $KEY_NAME -t $INSTANCE_TYPE` |
---|
| 32 | BOOTING_INSTANCE=`echo $OUTPUT | awk '{print $6}'` |
---|
| 33 | |
---|
| 34 | echo "Instance is $BOOTING_INSTANCE." |
---|
| 35 | |
---|
| 36 | echo "Polling server status (ec2-describe-instances $BOOTING_INSTANCE)" |
---|
| 37 | while true; do |
---|
| 38 | printf "." |
---|
| 39 | HOSTNAME=`ec2-describe-instances $BOOTING_INSTANCE | grep running | awk '{print $4}'` |
---|
| 40 | if [ ! -z $HOSTNAME ]; then |
---|
| 41 | break; |
---|
| 42 | fi |
---|
| 43 | sleep 1 |
---|
| 44 | done |
---|
| 45 | |
---|
| 46 | echo "The server is available at $HOSTNAME." |
---|
| 47 | while true; do |
---|
| 48 | REPLY=`ssh $SSH_OPTS "root@$HOSTNAME" 'echo "hello"'` |
---|
| 49 | if [ ! -z $REPLY ]; then |
---|
| 50 | break; |
---|
| 51 | fi |
---|
| 52 | sleep 5 |
---|
| 53 | done |
---|
| 54 | |
---|
| 55 | #read -p "Login first? [yes or no]: " answer |
---|
| 56 | |
---|
| 57 | if [ "$answer" == "yes" ]; then |
---|
| 58 | ssh $SSH_OPTS "root@$HOSTNAME" |
---|
| 59 | fi |
---|
| 60 | |
---|
| 61 | echo "Copying scripts." |
---|
| 62 | |
---|
| 63 | # Copy setup scripts |
---|
| 64 | scp $SSH_OPTS "$bin"/hadoop-ec2-env.sh "root@$HOSTNAME:/mnt" |
---|
| 65 | scp $SSH_OPTS "$bin"/image/create-hadoop-image-remote "root@$HOSTNAME:/mnt" |
---|
| 66 | scp $SSH_OPTS "$bin"/image/ec2-run-user-data "root@$HOSTNAME:/etc/init.d" |
---|
| 67 | |
---|
| 68 | # Copy private key and certificate (for bundling image) |
---|
| 69 | scp $SSH_OPTS $EC2_KEYDIR/pk*.pem "root@$HOSTNAME:/mnt" |
---|
| 70 | scp $SSH_OPTS $EC2_KEYDIR/cert*.pem "root@$HOSTNAME:/mnt" |
---|
| 71 | |
---|
| 72 | # Connect to it |
---|
| 73 | ssh $SSH_OPTS "root@$HOSTNAME" '/mnt/create-hadoop-image-remote' |
---|
| 74 | |
---|
| 75 | # Register image |
---|
| 76 | ec2-register $S3_BUCKET/hadoop-$HADOOP_VERSION-$ARCH.manifest.xml |
---|
| 77 | |
---|
| 78 | echo "Terminate with: ec2-terminate-instances $BOOTING_INSTANCE" |
---|