blob: 703c7aacdb0661ad16afbe0370d20b356fbf429a [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 Troyer51fb4542012-03-09 22:21:59 -060031# Import exercise configuration
32source $TOP_DIR/exerciserc
Dean Troyer751c1522012-01-10 15:34:34 -060033
34# Instance type to create
35DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
36
Anthony Youngabda4272011-12-16 20:16:20 +000037# Find a machine image to boot
Jesse Andrews9c7c9082011-11-23 10:10:53 -080038IMAGE=`euca-describe-images | grep machine | cut -f2 | head -n1`
Jesse Andrewsf6705492011-11-01 16:04:14 -070039
Anthony Youngabda4272011-12-16 20:16:20 +000040# Define secgroup
41SECGROUP=euca_secgroup
Jesse Andrewsf6705492011-11-01 16:04:14 -070042
Anthony Youngabda4272011-12-16 20:16:20 +000043# Add a secgroup
Dean Troyera9478412012-02-08 11:49:28 -060044if ! euca-describe-groups | grep -q $SECGROUP; then
Dean Troyer751c1522012-01-10 15:34:34 -060045 euca-add-group -d "$SECGROUP description" $SECGROUP
Dean Troyera9478412012-02-08 11:49:28 -060046 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 -060047 echo "Security group not created"
48 exit 1
49 fi
50fi
Anthony Youngabda4272011-12-16 20:16:20 +000051
52# Launch it
Dean Troyer1d6e0e12011-12-23 12:45:13 -060053INSTANCE=`euca-run-instances -g $SECGROUP -t $DEFAULT_INSTANCE_TYPE $IMAGE | grep INSTANCE | cut -f2`
Dean Troyer489bd2a2012-03-02 10:44:29 -060054die_if_not_set INSTANCE "Failure launching instance"
Anthony Youngabda4272011-12-16 20:16:20 +000055
56# Assure it has booted within a reasonable time
Dean Troyerc3844242011-12-30 14:27:02 -060057if ! 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 -040058 echo "server didn't become active within $RUNNING_TIMEOUT seconds"
Jesse Andrewsf6705492011-11-01 16:04:14 -070059 exit 1
60fi
61
Anthony Youngabda4272011-12-16 20:16:20 +000062# Allocate floating address
63FLOATING_IP=`euca-allocate-address | cut -f2`
Dean Troyer489bd2a2012-03-02 10:44:29 -060064die_if_not_set FLOATING_IP "Failure allocating floating IP"
Anthony Youngabda4272011-12-16 20:16:20 +000065
Dean Troyer751c1522012-01-10 15:34:34 -060066# Associate floating address
Anthony Youngabda4272011-12-16 20:16:20 +000067euca-associate-address -i $INSTANCE $FLOATING_IP
Dean Troyer489bd2a2012-03-02 10:44:29 -060068die_if_error "Failure associating address $FLOATING_IP to $INSTANCE"
Anthony Youngabda4272011-12-16 20:16:20 +000069
Anthony Youngabda4272011-12-16 20:16:20 +000070# Authorize pinging
71euca-authorize -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP
Dean Troyer489bd2a2012-03-02 10:44:29 -060072die_if_error "Failure authorizing rule in $SECGROUP"
Anthony Youngabda4272011-12-16 20:16:20 +000073
Dean Troyerc3844242011-12-30 14:27:02 -060074# Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
Dean Troyerc3844242011-12-30 14:27:02 -060075if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
76 echo "Couldn't ping server with floating ip"
Anthony Youngabda4272011-12-16 20:16:20 +000077 exit 1
78fi
79
80# Revoke pinging
81euca-revoke -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP
Dean Troyer489bd2a2012-03-02 10:44:29 -060082die_if_error "Failure revoking rule in $SECGROUP"
Anthony Youngabda4272011-12-16 20:16:20 +000083
Anthony Youngabda4272011-12-16 20:16:20 +000084# Release floating address
85euca-disassociate-address $FLOATING_IP
Dean Troyer489bd2a2012-03-02 10:44:29 -060086die_if_error "Failure disassociating address $FLOATING_IP"
Anthony Youngabda4272011-12-16 20:16:20 +000087
Dean Troyer751c1522012-01-10 15:34:34 -060088# Wait just a tick for everything above to complete so release doesn't fail
89if ! timeout $ASSOCIATE_TIMEOUT sh -c "while euca-describe-addresses | grep $INSTANCE | grep -q $FLOATING_IP; do sleep 1; done"; then
90 echo "Floating ip $FLOATING_IP not disassociated within $ASSOCIATE_TIMEOUT seconds"
91 exit 1
92fi
93
Anthony Youngabda4272011-12-16 20:16:20 +000094# Release floating address
95euca-release-address $FLOATING_IP
Dean Troyer489bd2a2012-03-02 10:44:29 -060096die_if_error "Failure releasing address $FLOATING_IP"
Anthony Youngabda4272011-12-16 20:16:20 +000097
Dean Troyerc3844242011-12-30 14:27:02 -060098# Wait just a tick for everything above to complete so terminate doesn't fail
99if ! timeout $ASSOCIATE_TIMEOUT sh -c "while euca-describe-addresses | grep -q $FLOATING_IP; do sleep 1; done"; then
100 echo "Floating ip $FLOATING_IP not released within $ASSOCIATE_TIMEOUT seconds"
101 exit 1
102fi
103
Anthony Youngabda4272011-12-16 20:16:20 +0000104# Terminate instance
Jesse Andrewsf6705492011-11-01 16:04:14 -0700105euca-terminate-instances $INSTANCE
Dean Troyer489bd2a2012-03-02 10:44:29 -0600106die_if_error "Failure terminating instance $INSTANCE"
Russell Bryante7ed17e2012-02-21 17:43:33 -0500107
Russell Bryant243b26a2012-02-22 11:19:32 -0500108# Assure it has terminated within a reasonable time
109if ! timeout $TERMINATE_TIMEOUT sh -c "while euca-describe-instances $INSTANCE | grep -q running; do sleep 1; done"; then
110 echo "server didn't terminate within $TERMINATE_TIMEOUT seconds"
111 exit 1
112fi
113
Russell Bryante7ed17e2012-02-21 17:43:33 -0500114# Delete group
115euca-delete-group $SECGROUP
Dean Troyer489bd2a2012-03-02 10:44:29 -0600116die_if_error "Failure deleting security group $SECGROUP"
117
118set +o xtrace
119echo "**************************************************"
120echo "End DevStack Exercise: $0"
121echo "**************************************************"