Skip to content

Commit e884fe0

Browse files
committed
Merge pull request #3250 from mboersma/ec2-provision-polling
feat(contrib/ec2): loop until instances have passed health checks
2 parents 8826c83 + 311f0f0 commit e884fe0

1 file changed

Lines changed: 56 additions & 4 deletions

File tree

contrib/ec2/provision-ec2-cluster.sh

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
if [ -z "$1" ]
77
then
8-
NAME=deis
8+
STACK_NAME=deis
99
else
10-
NAME=$1
10+
STACK_NAME=$1
1111
fi
1212

1313
set -e
@@ -41,8 +41,60 @@ $CONTRIB_DIR/util/check-user-data.sh
4141
# create an EC2 cloudformation stack based on CoreOS's default template
4242
aws cloudformation create-stack \
4343
--template-body "$($THIS_DIR/gen-json.py)" \
44-
--stack-name $NAME \
44+
--stack-name $STACK_NAME \
4545
--parameters "$(<$THIS_DIR/cloudformation.json)"
4646

47-
echo_green "Your Deis cluster has been successfully deployed to AWS CloudFormation and is currently starting."
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."
48100
echo_green "Please continue to follow the instructions in the documentation."

0 commit comments

Comments
 (0)