blob: 76e5202abc1ed4a35e29408166477948bbab8ac2 [file] [log] [blame]
Jesse Andrewsf6705492011-11-01 16:04:14 -07001#!/usr/bin/env bash
2
Dean Troyer27e32692012-03-16 16:16:56 -05003# **euca.sh**
4
Jesse Andrews9c7c9082011-11-23 10:10:53 -08005# we will use the ``euca2ools`` cli tool that wraps the python boto
Jesse Andrews9f186342011-11-01 16:05:40 -07006# library to test ec2 compatibility
Dean Troyer489bd2a2012-03-02 10:44:29 -06007
Dean Troyer27e32692012-03-16 16:16:56 -05008echo "*********************************************************************"
Dean Troyer489bd2a2012-03-02 10:44:29 -06009echo "Begin DevStack Exercise: $0"
Dean Troyer27e32692012-03-16 16:16:56 -050010echo "*********************************************************************"
Jesse Andrewsf6705492011-11-01 16:04:14 -070011
Jesse Andrewsf6705492011-11-01 16:04:14 -070012# This script exits on an error so that errors don't compound and you see
13# only the first error that occured.
14set -o errexit
15
16# Print the commands being run so that we can see the command that triggers
17# an error. It is also useful for following allowing as the install occurs.
18set -o xtrace
19
Dean Troyer27e32692012-03-16 16:16:56 -050020
Jesse Andrewsf6705492011-11-01 16:04:14 -070021# Settings
22# ========
23
Dean Troyer0bd24102012-03-08 00:33:54 -060024# Keep track of the current directory
25EXERCISE_DIR=$(cd $(dirname "$0") && pwd)
26TOP_DIR=$(cd $EXERCISE_DIR/..; pwd)
Dean Troyer489bd2a2012-03-02 10:44:29 -060027
28# Import common functions
Dean Troyer0bd24102012-03-08 00:33:54 -060029source $TOP_DIR/functions
Dean Troyer489bd2a2012-03-02 10:44:29 -060030
Dean Troyer0bd24102012-03-08 00:33:54 -060031# Import EC2 configuration
32source $TOP_DIR/eucarc
Jesse Andrewsf6705492011-11-01 16:04:14 -070033
Dean Troyer51fb4542012-03-09 22:21:59 -060034# Import exercise configuration
35source $TOP_DIR/exerciserc
Dean Troyer751c1522012-01-10 15:34:34 -060036
37# Instance type to create
38DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
39
Dean Troyer27e32692012-03-16 16:16:56 -050040
41# Launching a server
42# ==================
43
Anthony Youngabda4272011-12-16 20:16:20 +000044# Find a machine image to boot
Jesse Andrews9c7c9082011-11-23 10:10:53 -080045IMAGE=`euca-describe-images | grep machine | cut -f2 | head -n1`
Jesse Andrewsf6705492011-11-01 16:04:14 -070046
Anthony Youngabda4272011-12-16 20:16:20 +000047# Define secgroup
48SECGROUP=euca_secgroup
Jesse Andrewsf6705492011-11-01 16:04:14 -070049
Anthony Youngabda4272011-12-16 20:16:20 +000050# Add a secgroup
Dean Troyera9478412012-02-08 11:49:28 -060051if ! euca-describe-groups | grep -q $SECGROUP; then
Dean Troyer751c1522012-01-10 15:34:34 -060052 euca-add-group -d "$SECGROUP description" $SECGROUP
Dean Troyera9478412012-02-08 11:49:28 -060053 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 -060054 echo "Security group not created"
55 exit 1
56 fi
57fi
Anthony Youngabda4272011-12-16 20:16:20 +000058
59# Launch it
Dean Troyer1d6e0e12011-12-23 12:45:13 -060060INSTANCE=`euca-run-instances -g $SECGROUP -t $DEFAULT_INSTANCE_TYPE $IMAGE | grep INSTANCE | cut -f2`
Dean Troyer489bd2a2012-03-02 10:44:29 -060061die_if_not_set INSTANCE "Failure launching instance"
Anthony Youngabda4272011-12-16 20:16:20 +000062
63# Assure it has booted within a reasonable time
Dean Troyerc3844242011-12-30 14:27:02 -060064if ! 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 -040065 echo "server didn't become active within $RUNNING_TIMEOUT seconds"
Jesse Andrewsf6705492011-11-01 16:04:14 -070066 exit 1
67fi
68
Anthony Youngabda4272011-12-16 20:16:20 +000069# Allocate floating address
70FLOATING_IP=`euca-allocate-address | cut -f2`
Dean Troyer489bd2a2012-03-02 10:44:29 -060071die_if_not_set FLOATING_IP "Failure allocating floating IP"
Anthony Youngabda4272011-12-16 20:16:20 +000072
Dean Troyer751c1522012-01-10 15:34:34 -060073# Associate floating address
Dean Troyer27e32692012-03-16 16:16:56 -050074euca-associate-address -i $INSTANCE $FLOATING_IP || \
75 die "Failure associating address $FLOATING_IP to $INSTANCE"
Anthony Youngabda4272011-12-16 20:16:20 +000076
Anthony Youngabda4272011-12-16 20:16:20 +000077# Authorize pinging
Dean Troyer27e32692012-03-16 16:16:56 -050078euca-authorize -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP || \
79 die "Failure authorizing rule in $SECGROUP"
Anthony Youngabda4272011-12-16 20:16:20 +000080
Dean Troyerc3844242011-12-30 14:27:02 -060081# Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
Dean Troyerc3844242011-12-30 14:27:02 -060082if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
83 echo "Couldn't ping server with floating ip"
Anthony Youngabda4272011-12-16 20:16:20 +000084 exit 1
85fi
86
87# Revoke pinging
Dean Troyer27e32692012-03-16 16:16:56 -050088euca-revoke -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP || \
89 die "Failure revoking rule in $SECGROUP"
Anthony Youngabda4272011-12-16 20:16:20 +000090
Anthony Youngabda4272011-12-16 20:16:20 +000091# Release floating address
Dean Troyer27e32692012-03-16 16:16:56 -050092euca-disassociate-address $FLOATING_IP || \
93 die "Failure disassociating address $FLOATING_IP"
Anthony Youngabda4272011-12-16 20:16:20 +000094
Dean Troyer751c1522012-01-10 15:34:34 -060095# Wait just a tick for everything above to complete so release doesn't fail
96if ! timeout $ASSOCIATE_TIMEOUT sh -c "while euca-describe-addresses | grep $INSTANCE | grep -q $FLOATING_IP; do sleep 1; done"; then
97 echo "Floating ip $FLOATING_IP not disassociated within $ASSOCIATE_TIMEOUT seconds"
98 exit 1
99fi
100
Anthony Youngabda4272011-12-16 20:16:20 +0000101# Release floating address
Dean Troyer27e32692012-03-16 16:16:56 -0500102euca-release-address $FLOATING_IP || \
103 die "Failure releasing address $FLOATING_IP"
Anthony Youngabda4272011-12-16 20:16:20 +0000104
Dean Troyerc3844242011-12-30 14:27:02 -0600105# Wait just a tick for everything above to complete so terminate doesn't fail
106if ! timeout $ASSOCIATE_TIMEOUT sh -c "while euca-describe-addresses | grep -q $FLOATING_IP; do sleep 1; done"; then
107 echo "Floating ip $FLOATING_IP not released within $ASSOCIATE_TIMEOUT seconds"
108 exit 1
109fi
110
Anthony Youngabda4272011-12-16 20:16:20 +0000111# Terminate instance
Dean Troyer27e32692012-03-16 16:16:56 -0500112euca-terminate-instances $INSTANCE || \
113 die "Failure terminating instance $INSTANCE"
Russell Bryante7ed17e2012-02-21 17:43:33 -0500114
Russell Bryant243b26a2012-02-22 11:19:32 -0500115# Assure it has terminated within a reasonable time
116if ! timeout $TERMINATE_TIMEOUT sh -c "while euca-describe-instances $INSTANCE | grep -q running; do sleep 1; done"; then
117 echo "server didn't terminate within $TERMINATE_TIMEOUT seconds"
118 exit 1
119fi
120
Russell Bryante7ed17e2012-02-21 17:43:33 -0500121# Delete group
Dean Troyer27e32692012-03-16 16:16:56 -0500122euca-delete-group $SECGROUP || \
123 die "Failure deleting security group $SECGROUP"
Dean Troyer489bd2a2012-03-02 10:44:29 -0600124
125set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500126echo "*********************************************************************"
127echo "SUCCESS: End DevStack Exercise: $0"
128echo "*********************************************************************"