Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 3 | # **euca.sh** |
| 4 | |
Jesse Andrews | 9c7c908 | 2011-11-23 10:10:53 -0800 | [diff] [blame] | 5 | # we will use the ``euca2ools`` cli tool that wraps the python boto |
Jesse Andrews | 9f18634 | 2011-11-01 16:05:40 -0700 | [diff] [blame] | 6 | # library to test ec2 compatibility |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 7 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 8 | echo "*********************************************************************" |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 9 | echo "Begin DevStack Exercise: $0" |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 10 | echo "*********************************************************************" |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 11 | |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 12 | # This script exits on an error so that errors don't compound and you see |
Joe Gordon | 4640026 | 2013-06-30 04:32:27 -0700 | [diff] [blame] | 13 | # only the first error that occurred. |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 14 | set -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. |
| 18 | set -o xtrace |
| 19 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 20 | |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 21 | # Settings |
| 22 | # ======== |
| 23 | |
Dean Troyer | 0bd2410 | 2012-03-08 00:33:54 -0600 | [diff] [blame] | 24 | # Keep track of the current directory |
| 25 | EXERCISE_DIR=$(cd $(dirname "$0") && pwd) |
| 26 | TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) |
Dean Troyer | 67787e6 | 2012-05-02 11:48:15 -0500 | [diff] [blame] | 27 | VOLUME_SIZE=1 |
| 28 | ATTACH_DEVICE=/dev/vdc |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 29 | |
| 30 | # Import common functions |
Dean Troyer | 0bd2410 | 2012-03-08 00:33:54 -0600 | [diff] [blame] | 31 | source $TOP_DIR/functions |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 32 | |
Dean Troyer | 0bd2410 | 2012-03-08 00:33:54 -0600 | [diff] [blame] | 33 | # Import EC2 configuration |
| 34 | source $TOP_DIR/eucarc |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 35 | |
Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -0600 | [diff] [blame] | 36 | # Import exercise configuration |
| 37 | source $TOP_DIR/exerciserc |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 38 | |
Adam Gandelman | b875d01 | 2014-03-17 19:47:14 -0700 | [diff] [blame] | 39 | # Import project functions |
| 40 | source $TOP_DIR/lib/neutron |
| 41 | |
Kiall Mac Innes | a16c821 | 2014-01-12 19:35:43 +0000 | [diff] [blame] | 42 | # If nova api is not enabled we exit with exitcode 55 so that |
| 43 | # the exercise is skipped |
| 44 | is_service_enabled n-api || exit 55 |
| 45 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 46 | # Instance type to create |
| 47 | DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny} |
| 48 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 49 | # Boot this image, use first AMI image if unset |
Devananda van der Veen | c0c6f00 | 2012-07-06 17:49:12 -0700 | [diff] [blame] | 50 | DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami} |
| 51 | |
Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 52 | # Security group name |
| 53 | SECGROUP=${SECGROUP:-euca_secgroup} |
| 54 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 55 | |
| 56 | # Launching a server |
| 57 | # ================== |
| 58 | |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 59 | # Find a machine image to boot |
Devananda van der Veen | c0c6f00 | 2012-07-06 17:49:12 -0700 | [diff] [blame] | 60 | IMAGE=`euca-describe-images | grep machine | grep ${DEFAULT_IMAGE_NAME} | cut -f2 | head -n1` |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 61 | die_if_not_set $LINENO IMAGE "Failure getting image $DEFAULT_IMAGE_NAME" |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 62 | |
Chris Behrens | c62c2b9 | 2013-07-24 03:56:13 -0700 | [diff] [blame] | 63 | if 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." |
| 67 | else |
| 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 Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 74 | fi |
| 75 | fi |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 76 | |
| 77 | # Launch it |
Dean Troyer | 1d6e0e1 | 2011-12-23 12:45:13 -0600 | [diff] [blame] | 78 | INSTANCE=`euca-run-instances -g $SECGROUP -t $DEFAULT_INSTANCE_TYPE $IMAGE | grep INSTANCE | cut -f2` |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 79 | die_if_not_set $LINENO INSTANCE "Failure launching instance" |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 80 | |
| 81 | # Assure it has booted within a reasonable time |
Dean Troyer | c384424 | 2011-12-30 14:27:02 -0600 | [diff] [blame] | 82 | if ! timeout $RUNNING_TIMEOUT sh -c "while ! euca-describe-instances $INSTANCE | grep -q running; do sleep 1; done"; then |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 83 | die $LINENO "server didn't become active within $RUNNING_TIMEOUT seconds" |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 84 | fi |
| 85 | |
Chuck Short | 3725895 | 2012-08-07 10:38:44 -0500 | [diff] [blame] | 86 | # Volumes |
| 87 | # ------- |
Adam Gandelman | b875d01 | 2014-03-17 19:47:14 -0700 | [diff] [blame] | 88 | if is_service_enabled c-vol && ! is_service_enabled n-cell && [ "$VIRT_DRIVER" != "ironic" ]; then |
Sean Dague | 922c8ae | 2013-10-22 10:06:06 -0400 | [diff] [blame] | 89 | 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 Ishaya | dc9e288 | 2012-09-21 23:20:06 +0000 | [diff] [blame] | 91 | |
Sean Dague | 922c8ae | 2013-10-22 10:06:06 -0400 | [diff] [blame] | 92 | VOLUME=`euca-create-volume -s 1 -z $VOLUME_ZONE | cut -f2` |
| 93 | die_if_not_set $LINENO VOLUME "Failure to create volume" |
Chuck Short | 3725895 | 2012-08-07 10:38:44 -0500 | [diff] [blame] | 94 | |
Sean Dague | 922c8ae | 2013-10-22 10:06:06 -0400 | [diff] [blame] | 95 | # 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 Short | 3725895 | 2012-08-07 10:38:44 -0500 | [diff] [blame] | 98 | |
Sean Dague | 922c8ae | 2013-10-22 10:06:06 -0400 | [diff] [blame] | 99 | # 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 Short | 3725895 | 2012-08-07 10:38:44 -0500 | [diff] [blame] | 103 | |
Sean Dague | 922c8ae | 2013-10-22 10:06:06 -0400 | [diff] [blame] | 104 | # 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 Short | 3725895 | 2012-08-07 10:38:44 -0500 | [diff] [blame] | 110 | |
Sean Dague | 922c8ae | 2013-10-22 10:06:06 -0400 | [diff] [blame] | 111 | # Detach volume from an instance |
| 112 | euca-detach-volume $VOLUME || \ |
| 113 | die $LINENO "Failure detaching volume $VOLUME to $INSTANCE" |
Chuck Short | 3725895 | 2012-08-07 10:38:44 -0500 | [diff] [blame] | 114 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! euca-describe-volumes $VOLUME | grep -q available; do sleep 1; done"; then |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 115 | die $LINENO "Could not detach $VOLUME to $INSTANCE" |
Chuck Short | 3725895 | 2012-08-07 10:38:44 -0500 | [diff] [blame] | 116 | fi |
| 117 | |
| 118 | # Remove volume |
| 119 | euca-delete-volume $VOLUME || \ |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 120 | die $LINENO "Failure to delete volume" |
Chuck Short | 3725895 | 2012-08-07 10:38:44 -0500 | [diff] [blame] | 121 | if ! timeout $ACTIVE_TIMEOUT sh -c "while euca-describe-volumes | grep $VOLUME; do sleep 1; done"; then |
Sean Dague | 922c8ae | 2013-10-22 10:06:06 -0400 | [diff] [blame] | 122 | die $LINENO "Could not delete $VOLUME" |
Chuck Short | 3725895 | 2012-08-07 10:38:44 -0500 | [diff] [blame] | 123 | fi |
| 124 | else |
| 125 | echo "Volume Tests Skipped" |
| 126 | fi |
| 127 | |
Chris Behrens | c62c2b9 | 2013-07-24 03:56:13 -0700 | [diff] [blame] | 128 | if is_service_enabled n-cell; then |
| 129 | echo "Floating IP Tests Skipped because of Cells." |
| 130 | else |
| 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 Fazekas | 1a794a3 | 2013-08-06 15:25:01 +0200 | [diff] [blame] | 134 | # describe all instances at this moment |
| 135 | euca-describe-instances |
Chris Behrens | c62c2b9 | 2013-07-24 03:56:13 -0700 | [diff] [blame] | 136 | # Associate floating address |
| 137 | euca-associate-address -i $INSTANCE $FLOATING_IP || \ |
| 138 | die $LINENO "Failure associating address $FLOATING_IP to $INSTANCE" |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 139 | |
Chris Behrens | c62c2b9 | 2013-07-24 03:56:13 -0700 | [diff] [blame] | 140 | # 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 Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 143 | |
Chris Behrens | c62c2b9 | 2013-07-24 03:56:13 -0700 | [diff] [blame] | 144 | # Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds |
| 145 | ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 146 | |
Chris Behrens | c62c2b9 | 2013-07-24 03:56:13 -0700 | [diff] [blame] | 147 | # 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 Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 150 | |
Chris Behrens | c62c2b9 | 2013-07-24 03:56:13 -0700 | [diff] [blame] | 151 | # Release floating address |
| 152 | euca-disassociate-address $FLOATING_IP || \ |
| 153 | die $LINENO "Failure disassociating address $FLOATING_IP" |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 154 | |
Chris Behrens | c62c2b9 | 2013-07-24 03:56:13 -0700 | [diff] [blame] | 155 | # 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 Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 159 | |
Chris Behrens | c62c2b9 | 2013-07-24 03:56:13 -0700 | [diff] [blame] | 160 | # Release floating address |
| 161 | euca-release-address $FLOATING_IP || \ |
| 162 | die $LINENO "Failure releasing address $FLOATING_IP" |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 163 | |
Chris Behrens | c62c2b9 | 2013-07-24 03:56:13 -0700 | [diff] [blame] | 164 | # 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 Troyer | c384424 | 2011-12-30 14:27:02 -0600 | [diff] [blame] | 168 | fi |
| 169 | |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 170 | # Terminate instance |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 171 | euca-terminate-instances $INSTANCE || \ |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 172 | die $LINENO "Failure terminating instance $INSTANCE" |
Russell Bryant | e7ed17e | 2012-02-21 17:43:33 -0500 | [diff] [blame] | 173 | |
Michael Still | 796342c | 2012-12-28 11:08:20 +1100 | [diff] [blame] | 174 | # 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 Stanley | 18b0906 | 2013-06-10 00:23:38 +0000 | [diff] [blame] | 178 | if ! timeout $TERMINATE_TIMEOUT sh -c "while euca-describe-instances $INSTANCE | grep -ve '\(InstanceNotFound\|InvalidInstanceID\.NotFound\)' | grep -q $INSTANCE; do sleep 1; done"; then |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 179 | die $LINENO "server didn't terminate within $TERMINATE_TIMEOUT seconds" |
Russell Bryant | 243b26a | 2012-02-22 11:19:32 -0500 | [diff] [blame] | 180 | fi |
| 181 | |
Chris Behrens | c62c2b9 | 2013-07-24 03:56:13 -0700 | [diff] [blame] | 182 | if [[ "$SECGROUP" = "default" ]] ; then |
| 183 | echo "Skipping deleting default security group" |
| 184 | else |
| 185 | # Delete secgroup |
| 186 | euca-delete-group $SECGROUP || die $LINENO "Failure deleting security group $SECGROUP" |
| 187 | fi |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 188 | |
| 189 | set +o xtrace |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 190 | echo "*********************************************************************" |
| 191 | echo "SUCCESS: End DevStack Exercise: $0" |
| 192 | echo "*********************************************************************" |