blob: 2be2f62677e3ddb1d5b435f55366a653d11837e1 [file] [log] [blame]
Jesse Andrewsf6705492011-11-01 16:04:14 -07001#!/usr/bin/env bash
2
Jesse Andrews9c7c9082011-11-23 10:10:53 -08003# we will use the ``euca2ools`` cli tool that wraps the python boto
Jesse Andrews9f186342011-11-01 16:05:40 -07004# library to test ec2 compatibility
Dean Troyer489bd2a2012-03-02 10:44:29 -06005
6echo "**************************************************"
7echo "Begin DevStack Exercise: $0"
8echo "**************************************************"
Jesse Andrewsf6705492011-11-01 16:04:14 -07009
Jesse Andrewsf6705492011-11-01 16:04:14 -070010# This script exits on an error so that errors don't compound and you see
11# only the first error that occured.
12set -o errexit
13
14# Print the commands being run so that we can see the command that triggers
15# an error. It is also useful for following allowing as the install occurs.
16set -o xtrace
17
Jesse Andrewsf6705492011-11-01 16:04:14 -070018# Settings
19# ========
20
Dean Troyer0bd24102012-03-08 00:33:54 -060021# Keep track of the current directory
22EXERCISE_DIR=$(cd $(dirname "$0") && pwd)
23TOP_DIR=$(cd $EXERCISE_DIR/..; pwd)
Dean Troyer489bd2a2012-03-02 10:44:29 -060024
25# Import common functions
Dean Troyer0bd24102012-03-08 00:33:54 -060026source $TOP_DIR/functions
Dean Troyer489bd2a2012-03-02 10:44:29 -060027
Dean Troyer0bd24102012-03-08 00:33:54 -060028# Import EC2 configuration
29source $TOP_DIR/eucarc
Jesse Andrewsf6705492011-11-01 16:04:14 -070030
Dean Troyer751c1522012-01-10 15:34:34 -060031# Max time to wait while vm goes from build to active state
32ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-30}
33
34# Max time till the vm is bootable
35BOOT_TIMEOUT=${BOOT_TIMEOUT:-30}
36
37# Max time to wait for proper association and dis-association.
38ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-15}
39
40# Instance type to create
41DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
42
Anthony Youngabda4272011-12-16 20:16:20 +000043# Find a machine image to boot
Jesse Andrews9c7c9082011-11-23 10:10:53 -080044IMAGE=`euca-describe-images | grep machine | cut -f2 | head -n1`
Jesse Andrewsf6705492011-11-01 16:04:14 -070045
Anthony Youngabda4272011-12-16 20:16:20 +000046# Define secgroup
47SECGROUP=euca_secgroup
Jesse Andrewsf6705492011-11-01 16:04:14 -070048
Anthony Youngabda4272011-12-16 20:16:20 +000049# Add a secgroup
Dean Troyera9478412012-02-08 11:49:28 -060050if ! euca-describe-groups | grep -q $SECGROUP; then
Dean Troyer751c1522012-01-10 15:34:34 -060051 euca-add-group -d "$SECGROUP description" $SECGROUP
Dean Troyera9478412012-02-08 11:49:28 -060052 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! euca-describe-groups | grep -q $SECGROUP; do sleep 1; done"; then
Dean Troyer751c1522012-01-10 15:34:34 -060053 echo "Security group not created"
54 exit 1
55 fi
56fi
Anthony Youngabda4272011-12-16 20:16:20 +000057
58# Launch it
Dean Troyer1d6e0e12011-12-23 12:45:13 -060059INSTANCE=`euca-run-instances -g $SECGROUP -t $DEFAULT_INSTANCE_TYPE $IMAGE | grep INSTANCE | cut -f2`
Dean Troyer489bd2a2012-03-02 10:44:29 -060060die_if_not_set INSTANCE "Failure launching instance"
Anthony Youngabda4272011-12-16 20:16:20 +000061
62# Assure it has booted within a reasonable time
Dean Troyerc3844242011-12-30 14:27:02 -060063if ! timeout $RUNNING_TIMEOUT sh -c "while ! euca-describe-instances $INSTANCE | grep -q running; do sleep 1; done"; then
Todd Willey9a3066f2011-11-05 11:02:34 -040064 echo "server didn't become active within $RUNNING_TIMEOUT seconds"
Jesse Andrewsf6705492011-11-01 16:04:14 -070065 exit 1
66fi
67
Anthony Youngabda4272011-12-16 20:16:20 +000068# Allocate floating address
69FLOATING_IP=`euca-allocate-address | cut -f2`
Dean Troyer489bd2a2012-03-02 10:44:29 -060070die_if_not_set FLOATING_IP "Failure allocating floating IP"
Anthony Youngabda4272011-12-16 20:16:20 +000071
Dean Troyer751c1522012-01-10 15:34:34 -060072# Associate floating address
Anthony Youngabda4272011-12-16 20:16:20 +000073euca-associate-address -i $INSTANCE $FLOATING_IP
Dean Troyer489bd2a2012-03-02 10:44:29 -060074die_if_error "Failure associating address $FLOATING_IP to $INSTANCE"
Anthony Youngabda4272011-12-16 20:16:20 +000075
Anthony Youngabda4272011-12-16 20:16:20 +000076# Authorize pinging
77euca-authorize -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP
Dean Troyer489bd2a2012-03-02 10:44:29 -060078die_if_error "Failure authorizing rule in $SECGROUP"
Anthony Youngabda4272011-12-16 20:16:20 +000079
Dean Troyerc3844242011-12-30 14:27:02 -060080# Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
Dean Troyerc3844242011-12-30 14:27:02 -060081if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
82 echo "Couldn't ping server with floating ip"
Anthony Youngabda4272011-12-16 20:16:20 +000083 exit 1
84fi
85
86# Revoke pinging
87euca-revoke -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP
Dean Troyer489bd2a2012-03-02 10:44:29 -060088die_if_error "Failure revoking rule in $SECGROUP"
Anthony Youngabda4272011-12-16 20:16:20 +000089
Anthony Youngabda4272011-12-16 20:16:20 +000090# Release floating address
91euca-disassociate-address $FLOATING_IP
Dean Troyer489bd2a2012-03-02 10:44:29 -060092die_if_error "Failure disassociating address $FLOATING_IP"
Anthony Youngabda4272011-12-16 20:16:20 +000093
Dean Troyer751c1522012-01-10 15:34:34 -060094# Wait just a tick for everything above to complete so release doesn't fail
95if ! timeout $ASSOCIATE_TIMEOUT sh -c "while euca-describe-addresses | grep $INSTANCE | grep -q $FLOATING_IP; do sleep 1; done"; then
96 echo "Floating ip $FLOATING_IP not disassociated within $ASSOCIATE_TIMEOUT seconds"
97 exit 1
98fi
99
Anthony Youngabda4272011-12-16 20:16:20 +0000100# Release floating address
101euca-release-address $FLOATING_IP
Dean Troyer489bd2a2012-03-02 10:44:29 -0600102die_if_error "Failure releasing address $FLOATING_IP"
Anthony Youngabda4272011-12-16 20:16:20 +0000103
Dean Troyerc3844242011-12-30 14:27:02 -0600104# Wait just a tick for everything above to complete so terminate doesn't fail
105if ! timeout $ASSOCIATE_TIMEOUT sh -c "while euca-describe-addresses | grep -q $FLOATING_IP; do sleep 1; done"; then
106 echo "Floating ip $FLOATING_IP not released within $ASSOCIATE_TIMEOUT seconds"
107 exit 1
108fi
109
Anthony Youngabda4272011-12-16 20:16:20 +0000110# Terminate instance
Jesse Andrewsf6705492011-11-01 16:04:14 -0700111euca-terminate-instances $INSTANCE
Dean Troyer489bd2a2012-03-02 10:44:29 -0600112die_if_error "Failure terminating instance $INSTANCE"
Russell Bryante7ed17e2012-02-21 17:43:33 -0500113
Russell Bryant243b26a2012-02-22 11:19:32 -0500114# Assure it has terminated within a reasonable time
115if ! timeout $TERMINATE_TIMEOUT sh -c "while euca-describe-instances $INSTANCE | grep -q running; do sleep 1; done"; then
116 echo "server didn't terminate within $TERMINATE_TIMEOUT seconds"
117 exit 1
118fi
119
Russell Bryante7ed17e2012-02-21 17:43:33 -0500120# Delete group
121euca-delete-group $SECGROUP
Dean Troyer489bd2a2012-03-02 10:44:29 -0600122die_if_error "Failure deleting security group $SECGROUP"
123
124set +o xtrace
125echo "**************************************************"
126echo "End DevStack Exercise: $0"
127echo "**************************************************"