github.com/arkadijs/deis@v1.5.1/contrib/ec2/provision-ec2-cluster.sh (about)

     1  #!/usr/bin/env bash
     2  #
     3  # Usage: ./provision-ec2-cluster.sh [name]
     4  # The [name] is the CloudFormation stack name, and defaults to 'deis'
     5  
     6  if [ -z "$1" ]
     7    then
     8      STACK_NAME=deis
     9    else
    10      STACK_NAME=$1
    11  fi
    12  
    13  set -e
    14  
    15  THIS_DIR=$(cd $(dirname $0); pwd) # absolute path
    16  CONTRIB_DIR=$(dirname $THIS_DIR)
    17  
    18  source $CONTRIB_DIR/utils.sh
    19  
    20  # check for EC2 API tools in $PATH
    21  if ! which aws > /dev/null; then
    22    echo_red 'Please install the AWS command-line tool and ensure it is in your $PATH.'
    23    exit 1
    24  fi
    25  
    26  if [ -z "$DEIS_NUM_INSTANCES" ]; then
    27      DEIS_NUM_INSTANCES=3
    28  fi
    29  
    30  # make sure we have all VPC info
    31  if [ -n "$VPC_ID" ]; then
    32    if [ -z "$VPC_SUBNETS" ] || [ -z "$VPC_ZONES" ]; then
    33      echo_red 'To provision Deis in a VPC, you must also specify VPC_SUBNETS and VPC_ZONES.'
    34      exit 1
    35    fi
    36  fi
    37  
    38  # check that the CoreOS user-data file is valid
    39  $CONTRIB_DIR/util/check-user-data.sh
    40  
    41  # create an EC2 cloudformation stack based on CoreOS's default template
    42  aws cloudformation create-stack \
    43      --template-body "$($THIS_DIR/gen-json.py)" \
    44      --stack-name $STACK_NAME \
    45      --parameters "$(<$THIS_DIR/cloudformation.json)"
    46  
    47  # loop until the instances are created
    48  ATTEMPTS=45
    49  SLEEPTIME=10
    50  COUNTER=1
    51  INSTANCE_IDS=""
    52  until [ `wc -w <<< $INSTANCE_IDS` -eq $DEIS_NUM_INSTANCES ]; do
    53      if [ $COUNTER -gt $ATTEMPTS ]; then exit 1; fi  # timeout after 7 1/2 minutes
    54      if [ $COUNTER -ne 1 ]; then sleep $SLEEPTIME; fi
    55      echo "Waiting for instances to be created..."
    56      INSTANCE_IDS=$(aws ec2 describe-instances \
    57          --filters Name=tag:aws:cloudformation:stack-name,Values=$STACK_NAME Name=instance-state-name,Values=running \
    58          --query 'Reservations[].Instances[].[ InstanceId ]' \
    59          --output text)
    60      let COUNTER=COUNTER+1
    61  done
    62  
    63  # loop until the instances pass health checks
    64  COUNTER=1
    65  INSTANCE_STATUSES=""
    66  until [ `wc -w <<< $INSTANCE_STATUSES` -eq $DEIS_NUM_INSTANCES ]; do
    67      if [ $COUNTER -gt $ATTEMPTS ]; then exit 1; fi  # timeout after 7 1/2 minutes
    68      if [ $COUNTER -ne 1 ]; then sleep $SLEEPTIME; fi
    69      echo "Waiting for instances to pass initial health checks..."
    70      INSTANCE_STATUSES=$(aws ec2 describe-instance-status \
    71          --filters Name=instance-status.reachability,Values=passed \
    72          --instance-ids $INSTANCE_IDS \
    73          --query 'InstanceStatuses[].[ InstanceId ]' \
    74          --output text)
    75      let COUNTER=COUNTER+1
    76  done
    77  
    78  # print instance info
    79  echo "Instances are available:"
    80  aws ec2 describe-instances \
    81      --filters Name=tag:aws:cloudformation:stack-name,Values=$STACK_NAME Name=instance-state-name,Values=running \
    82      --query 'Reservations[].Instances[].[InstanceId,PublicIpAddress,InstanceType,Placement.AvailabilityZone,State.Name]' \
    83      --output text
    84  
    85  # get ELB public DNS name through cloudformation
    86  # TODO: is "first output value" going to be reliable enough?
    87  export ELB_DNS_NAME=$(aws cloudformation describe-stacks \
    88      --stack-name $STACK_NAME \
    89      --max-items 1 \
    90      --query 'Stacks[].[ Outputs[0].[ OutputValue ] ]' \
    91      --output=text)
    92  
    93  # get ELB friendly name through aws elb
    94  ELB_NAME=$(aws elb describe-load-balancers \
    95      --query 'LoadBalancerDescriptions[].[ DNSName,LoadBalancerName ]' \
    96      --output=text | grep -F $ELB_DNS_NAME | head -n1 | cut -f2)
    97  echo "Using ELB $ELB_NAME at $ELB_DNS_NAME"
    98  
    99  echo_green "Your Deis cluster has been successfully deployed to AWS CloudFormation and is started."
   100  echo_green "Please continue to follow the instructions in the documentation."