blob: f9c47523e602bb3527067c6511d16a8721c75b7b [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
Joe Gordon46400262013-06-30 04:32:27 -070013# only the first error that occurred.
Jesse Andrewsf6705492011-11-01 16:04:14 -070014set -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 Troyer67787e62012-05-02 11:48:15 -050027VOLUME_SIZE=1
28ATTACH_DEVICE=/dev/vdc
Dean Troyer489bd2a2012-03-02 10:44:29 -060029
30# Import common functions
Dean Troyer0bd24102012-03-08 00:33:54 -060031source $TOP_DIR/functions
Dean Troyer489bd2a2012-03-02 10:44:29 -060032
Dean Troyer0bd24102012-03-08 00:33:54 -060033# Import EC2 configuration
34source $TOP_DIR/eucarc
Jesse Andrewsf6705492011-11-01 16:04:14 -070035
Dean Troyer51fb4542012-03-09 22:21:59 -060036# Import exercise configuration
37source $TOP_DIR/exerciserc
Dean Troyer751c1522012-01-10 15:34:34 -060038
Adam Gandelmanb875d012014-03-17 19:47:14 -070039# Import project functions
40source $TOP_DIR/lib/neutron
41
Kiall Mac Innesa16c8212014-01-12 19:35:43 +000042# If nova api is not enabled we exit with exitcode 55 so that
43# the exercise is skipped
44is_service_enabled n-api || exit 55
45
Dean Troyer751c1522012-01-10 15:34:34 -060046# Instance type to create
47DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
48
Dean Troyerda85cda2013-02-15 11:07:14 -060049# Boot this image, use first AMI image if unset
Devananda van der Veenc0c6f002012-07-06 17:49:12 -070050DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
51
Dean Troyer96288ba2012-08-17 14:11:55 -050052# Security group name
53SECGROUP=${SECGROUP:-euca_secgroup}
54
Dean Troyer27e32692012-03-16 16:16:56 -050055
56# Launching a server
57# ==================
58
Anthony Youngabda4272011-12-16 20:16:20 +000059# Find a machine image to boot
Devananda van der Veenc0c6f002012-07-06 17:49:12 -070060IMAGE=`euca-describe-images | grep machine | grep ${DEFAULT_IMAGE_NAME} | cut -f2 | head -n1`
Nachi Ueno07115eb2013-02-26 12:38:18 -080061die_if_not_set $LINENO IMAGE "Failure getting image $DEFAULT_IMAGE_NAME"
Jesse Andrewsf6705492011-11-01 16:04:14 -070062
Chris Behrensc62c2b92013-07-24 03:56:13 -070063if is_service_enabled n-cell; then
64 # Cells does not support security groups, so force the use of "default"
65 SECGROUP="default"
66 echo "Using the default security group because of Cells."
67else
68 # Add a secgroup
69 if ! euca-describe-groups | grep -q $SECGROUP; then
70 euca-add-group -d "$SECGROUP description" $SECGROUP
71 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! euca-describe-groups | grep -q $SECGROUP; do sleep 1; done"; then
72 die $LINENO "Security group not created"
73 fi
Dean Troyer751c1522012-01-10 15:34:34 -060074 fi
75fi
Anthony Youngabda4272011-12-16 20:16:20 +000076
77# Launch it
Dean Troyer1d6e0e12011-12-23 12:45:13 -060078INSTANCE=`euca-run-instances -g $SECGROUP -t $DEFAULT_INSTANCE_TYPE $IMAGE | grep INSTANCE | cut -f2`
Nachi Ueno07115eb2013-02-26 12:38:18 -080079die_if_not_set $LINENO INSTANCE "Failure launching instance"
Anthony Youngabda4272011-12-16 20:16:20 +000080
81# Assure it has booted within a reasonable time
Dean Troyerc3844242011-12-30 14:27:02 -060082if ! timeout $RUNNING_TIMEOUT sh -c "while ! euca-describe-instances $INSTANCE | grep -q running; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -080083 die $LINENO "server didn't become active within $RUNNING_TIMEOUT seconds"
Jesse Andrewsf6705492011-11-01 16:04:14 -070084fi
85
Chuck Short37258952012-08-07 10:38:44 -050086# Volumes
87# -------
Adam Gandelmanb875d012014-03-17 19:47:14 -070088if is_service_enabled c-vol && ! is_service_enabled n-cell && [ "$VIRT_DRIVER" != "ironic" ]; then
Sean Dague922c8ae2013-10-22 10:06:06 -040089 VOLUME_ZONE=`euca-describe-availability-zones | head -n1 | cut -f2`
90 die_if_not_set $LINENO VOLUME_ZONE "Failure to find zone for volume"
Vishvananda Ishayadc9e2882012-09-21 23:20:06 +000091
Sean Dague922c8ae2013-10-22 10:06:06 -040092 VOLUME=`euca-create-volume -s 1 -z $VOLUME_ZONE | cut -f2`
93 die_if_not_set $LINENO VOLUME "Failure to create volume"
Chuck Short37258952012-08-07 10:38:44 -050094
Sean Dague922c8ae2013-10-22 10:06:06 -040095 # Test that volume has been created
96 VOLUME=`euca-describe-volumes $VOLUME | cut -f2`
97 die_if_not_set $LINENO VOLUME "Failure to get volume"
Chuck Short37258952012-08-07 10:38:44 -050098
Sean Dague922c8ae2013-10-22 10:06:06 -040099 # Test volume has become available
100 if ! timeout $RUNNING_TIMEOUT sh -c "while ! euca-describe-volumes $VOLUME | grep -q available; do sleep 1; done"; then
101 die $LINENO "volume didn't become available within $RUNNING_TIMEOUT seconds"
102 fi
Chuck Short37258952012-08-07 10:38:44 -0500103
Sean Dague922c8ae2013-10-22 10:06:06 -0400104 # Attach volume to an instance
105 euca-attach-volume -i $INSTANCE -d $ATTACH_DEVICE $VOLUME || \
106 die $LINENO "Failure attaching volume $VOLUME to $INSTANCE"
107 if ! timeout $ACTIVE_TIMEOUT sh -c "while ! euca-describe-volumes $VOLUME | grep -A 1 in-use | grep -q attach; do sleep 1; done"; then
108 die $LINENO "Could not attach $VOLUME to $INSTANCE"
109 fi
Chuck Short37258952012-08-07 10:38:44 -0500110
Sean Dague922c8ae2013-10-22 10:06:06 -0400111 # Detach volume from an instance
112 euca-detach-volume $VOLUME || \
113 die $LINENO "Failure detaching volume $VOLUME to $INSTANCE"
Chuck Short37258952012-08-07 10:38:44 -0500114 if ! timeout $ACTIVE_TIMEOUT sh -c "while ! euca-describe-volumes $VOLUME | grep -q available; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800115 die $LINENO "Could not detach $VOLUME to $INSTANCE"
Chuck Short37258952012-08-07 10:38:44 -0500116 fi
117
118 # Remove volume
119 euca-delete-volume $VOLUME || \
Nachi Ueno07115eb2013-02-26 12:38:18 -0800120 die $LINENO "Failure to delete volume"
Chuck Short37258952012-08-07 10:38:44 -0500121 if ! timeout $ACTIVE_TIMEOUT sh -c "while euca-describe-volumes | grep $VOLUME; do sleep 1; done"; then
Sean Dague922c8ae2013-10-22 10:06:06 -0400122 die $LINENO "Could not delete $VOLUME"
Chuck Short37258952012-08-07 10:38:44 -0500123 fi
124else
125 echo "Volume Tests Skipped"
126fi
127
Chris Behrensc62c2b92013-07-24 03:56:13 -0700128if is_service_enabled n-cell; then
129 echo "Floating IP Tests Skipped because of Cells."
130else
131 # Allocate floating address
132 FLOATING_IP=`euca-allocate-address | cut -f2`
133 die_if_not_set $LINENO FLOATING_IP "Failure allocating floating IP"
Attila Fazekas1a794a32013-08-06 15:25:01 +0200134 # describe all instances at this moment
135 euca-describe-instances
Chris Behrensc62c2b92013-07-24 03:56:13 -0700136 # Associate floating address
137 euca-associate-address -i $INSTANCE $FLOATING_IP || \
138 die $LINENO "Failure associating address $FLOATING_IP to $INSTANCE"
Anthony Youngabda4272011-12-16 20:16:20 +0000139
Chris Behrensc62c2b92013-07-24 03:56:13 -0700140 # Authorize pinging
141 euca-authorize -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP || \
142 die $LINENO "Failure authorizing rule in $SECGROUP"
Anthony Youngabda4272011-12-16 20:16:20 +0000143
Chris Behrensc62c2b92013-07-24 03:56:13 -0700144 # Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
145 ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT
Anthony Youngabda4272011-12-16 20:16:20 +0000146
Chris Behrensc62c2b92013-07-24 03:56:13 -0700147 # Revoke pinging
148 euca-revoke -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP || \
149 die $LINENO "Failure revoking rule in $SECGROUP"
Anthony Youngabda4272011-12-16 20:16:20 +0000150
Chris Behrensc62c2b92013-07-24 03:56:13 -0700151 # Release floating address
152 euca-disassociate-address $FLOATING_IP || \
153 die $LINENO "Failure disassociating address $FLOATING_IP"
Anthony Youngabda4272011-12-16 20:16:20 +0000154
Chris Behrensc62c2b92013-07-24 03:56:13 -0700155 # Wait just a tick for everything above to complete so release doesn't fail
156 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while euca-describe-addresses | grep $INSTANCE | grep -q $FLOATING_IP; do sleep 1; done"; then
157 die $LINENO "Floating ip $FLOATING_IP not disassociated within $ASSOCIATE_TIMEOUT seconds"
158 fi
Dean Troyer751c1522012-01-10 15:34:34 -0600159
Chris Behrensc62c2b92013-07-24 03:56:13 -0700160 # Release floating address
161 euca-release-address $FLOATING_IP || \
162 die $LINENO "Failure releasing address $FLOATING_IP"
Anthony Youngabda4272011-12-16 20:16:20 +0000163
Chris Behrensc62c2b92013-07-24 03:56:13 -0700164 # Wait just a tick for everything above to complete so terminate doesn't fail
165 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while euca-describe-addresses | grep -q $FLOATING_IP; do sleep 1; done"; then
166 die $LINENO "Floating ip $FLOATING_IP not released within $ASSOCIATE_TIMEOUT seconds"
167 fi
Dean Troyerc3844242011-12-30 14:27:02 -0600168fi
169
Anthony Youngabda4272011-12-16 20:16:20 +0000170# Terminate instance
Dean Troyer27e32692012-03-16 16:16:56 -0500171euca-terminate-instances $INSTANCE || \
Nachi Ueno07115eb2013-02-26 12:38:18 -0800172 die $LINENO "Failure terminating instance $INSTANCE"
Russell Bryante7ed17e2012-02-21 17:43:33 -0500173
Michael Still796342c2012-12-28 11:08:20 +1100174# Assure it has terminated within a reasonable time. The behaviour of this
175# case changed with bug/836978. Requesting the status of an invalid instance
176# will now return an error message including the instance id, so we need to
177# filter that out.
Jeremy Stanley18b09062013-06-10 00:23:38 +0000178if ! timeout $TERMINATE_TIMEOUT sh -c "while euca-describe-instances $INSTANCE | grep -ve '\(InstanceNotFound\|InvalidInstanceID\.NotFound\)' | grep -q $INSTANCE; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800179 die $LINENO "server didn't terminate within $TERMINATE_TIMEOUT seconds"
Russell Bryant243b26a2012-02-22 11:19:32 -0500180fi
181
Chris Behrensc62c2b92013-07-24 03:56:13 -0700182if [[ "$SECGROUP" = "default" ]] ; then
183 echo "Skipping deleting default security group"
184else
185 # Delete secgroup
186 euca-delete-group $SECGROUP || die $LINENO "Failure deleting security group $SECGROUP"
187fi
Dean Troyer489bd2a2012-03-02 10:44:29 -0600188
189set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500190echo "*********************************************************************"
191echo "SUCCESS: End DevStack Exercise: $0"
192echo "*********************************************************************"