Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
Jesse Andrews | 9c7c908 | 2011-11-23 10:10:53 -0800 | [diff] [blame] | 3 | # we will use the ``euca2ools`` cli tool that wraps the python boto |
Jesse Andrews | 9f18634 | 2011-11-01 16:05:40 -0700 | [diff] [blame] | 4 | # library to test ec2 compatibility |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 5 | |
| 6 | echo "**************************************************" |
| 7 | echo "Begin DevStack Exercise: $0" |
| 8 | echo "**************************************************" |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 9 | |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 10 | # This script exits on an error so that errors don't compound and you see |
| 11 | # only the first error that occured. |
| 12 | set -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. |
| 16 | set -o xtrace |
| 17 | |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 18 | # Settings |
| 19 | # ======== |
| 20 | |
Dean Troyer | 0bd2410 | 2012-03-08 00:33:54 -0600 | [diff] [blame^] | 21 | # Keep track of the current directory |
| 22 | EXERCISE_DIR=$(cd $(dirname "$0") && pwd) |
| 23 | TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 24 | |
| 25 | # Import common functions |
Dean Troyer | 0bd2410 | 2012-03-08 00:33:54 -0600 | [diff] [blame^] | 26 | source $TOP_DIR/functions |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 27 | |
Dean Troyer | 0bd2410 | 2012-03-08 00:33:54 -0600 | [diff] [blame^] | 28 | # Import EC2 configuration |
| 29 | source $TOP_DIR/eucarc |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 30 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 31 | # Max time to wait while vm goes from build to active state |
| 32 | ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-30} |
| 33 | |
| 34 | # Max time till the vm is bootable |
| 35 | BOOT_TIMEOUT=${BOOT_TIMEOUT:-30} |
| 36 | |
| 37 | # Max time to wait for proper association and dis-association. |
| 38 | ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-15} |
| 39 | |
| 40 | # Instance type to create |
| 41 | DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny} |
| 42 | |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 43 | # Find a machine image to boot |
Jesse Andrews | 9c7c908 | 2011-11-23 10:10:53 -0800 | [diff] [blame] | 44 | IMAGE=`euca-describe-images | grep machine | cut -f2 | head -n1` |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 45 | |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 46 | # Define secgroup |
| 47 | SECGROUP=euca_secgroup |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 48 | |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 49 | # Add a secgroup |
Dean Troyer | a947841 | 2012-02-08 11:49:28 -0600 | [diff] [blame] | 50 | if ! euca-describe-groups | grep -q $SECGROUP; then |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 51 | euca-add-group -d "$SECGROUP description" $SECGROUP |
Dean Troyer | a947841 | 2012-02-08 11:49:28 -0600 | [diff] [blame] | 52 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! euca-describe-groups | grep -q $SECGROUP; do sleep 1; done"; then |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 53 | echo "Security group not created" |
| 54 | exit 1 |
| 55 | fi |
| 56 | fi |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 57 | |
| 58 | # Launch it |
Dean Troyer | 1d6e0e1 | 2011-12-23 12:45:13 -0600 | [diff] [blame] | 59 | INSTANCE=`euca-run-instances -g $SECGROUP -t $DEFAULT_INSTANCE_TYPE $IMAGE | grep INSTANCE | cut -f2` |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 60 | die_if_not_set INSTANCE "Failure launching instance" |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 61 | |
| 62 | # Assure it has booted within a reasonable time |
Dean Troyer | c384424 | 2011-12-30 14:27:02 -0600 | [diff] [blame] | 63 | if ! timeout $RUNNING_TIMEOUT sh -c "while ! euca-describe-instances $INSTANCE | grep -q running; do sleep 1; done"; then |
Todd Willey | 9a3066f | 2011-11-05 11:02:34 -0400 | [diff] [blame] | 64 | echo "server didn't become active within $RUNNING_TIMEOUT seconds" |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 65 | exit 1 |
| 66 | fi |
| 67 | |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 68 | # Allocate floating address |
| 69 | FLOATING_IP=`euca-allocate-address | cut -f2` |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 70 | die_if_not_set FLOATING_IP "Failure allocating floating IP" |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 71 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 72 | # Associate floating address |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 73 | euca-associate-address -i $INSTANCE $FLOATING_IP |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 74 | die_if_error "Failure associating address $FLOATING_IP to $INSTANCE" |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 75 | |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 76 | # Authorize pinging |
| 77 | euca-authorize -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 78 | die_if_error "Failure authorizing rule in $SECGROUP" |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 79 | |
Dean Troyer | c384424 | 2011-12-30 14:27:02 -0600 | [diff] [blame] | 80 | # Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds |
Dean Troyer | c384424 | 2011-12-30 14:27:02 -0600 | [diff] [blame] | 81 | if ! 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 Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 83 | exit 1 |
| 84 | fi |
| 85 | |
| 86 | # Revoke pinging |
| 87 | euca-revoke -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 88 | die_if_error "Failure revoking rule in $SECGROUP" |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 89 | |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 90 | # Release floating address |
| 91 | euca-disassociate-address $FLOATING_IP |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 92 | die_if_error "Failure disassociating address $FLOATING_IP" |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 93 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 94 | # Wait just a tick for everything above to complete so release doesn't fail |
| 95 | if ! 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 |
| 98 | fi |
| 99 | |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 100 | # Release floating address |
| 101 | euca-release-address $FLOATING_IP |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 102 | die_if_error "Failure releasing address $FLOATING_IP" |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 103 | |
Dean Troyer | c384424 | 2011-12-30 14:27:02 -0600 | [diff] [blame] | 104 | # Wait just a tick for everything above to complete so terminate doesn't fail |
| 105 | if ! 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 |
| 108 | fi |
| 109 | |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 110 | # Terminate instance |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 111 | euca-terminate-instances $INSTANCE |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 112 | die_if_error "Failure terminating instance $INSTANCE" |
Russell Bryant | e7ed17e | 2012-02-21 17:43:33 -0500 | [diff] [blame] | 113 | |
Russell Bryant | 243b26a | 2012-02-22 11:19:32 -0500 | [diff] [blame] | 114 | # Assure it has terminated within a reasonable time |
| 115 | if ! 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 |
| 118 | fi |
| 119 | |
Russell Bryant | e7ed17e | 2012-02-21 17:43:33 -0500 | [diff] [blame] | 120 | # Delete group |
| 121 | euca-delete-group $SECGROUP |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 122 | die_if_error "Failure deleting security group $SECGROUP" |
| 123 | |
| 124 | set +o xtrace |
| 125 | echo "**************************************************" |
| 126 | echo "End DevStack Exercise: $0" |
| 127 | echo "**************************************************" |