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 |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 5 | # |
| 6 | |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 7 | # This script exits on an error so that errors don't compound and you see |
| 8 | # only the first error that occured. |
| 9 | set -o errexit |
| 10 | |
| 11 | # Print the commands being run so that we can see the command that triggers |
| 12 | # an error. It is also useful for following allowing as the install occurs. |
| 13 | set -o xtrace |
| 14 | |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 15 | # Settings |
| 16 | # ======== |
| 17 | |
| 18 | # Use openrc + stackrc + localrc for settings |
Jesse Andrews | 787af01 | 2011-11-01 16:44:19 -0700 | [diff] [blame] | 19 | pushd $(cd $(dirname "$0")/.. && pwd) |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 20 | source ./openrc |
Jesse Andrews | 787af01 | 2011-11-01 16:44:19 -0700 | [diff] [blame] | 21 | popd |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 22 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame^] | 23 | # Max time to wait while vm goes from build to active state |
| 24 | ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-30} |
| 25 | |
| 26 | # Max time till the vm is bootable |
| 27 | BOOT_TIMEOUT=${BOOT_TIMEOUT:-30} |
| 28 | |
| 29 | # Max time to wait for proper association and dis-association. |
| 30 | ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-15} |
| 31 | |
| 32 | # Instance type to create |
| 33 | DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny} |
| 34 | |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 35 | # Find a machine image to boot |
Jesse Andrews | 9c7c908 | 2011-11-23 10:10:53 -0800 | [diff] [blame] | 36 | IMAGE=`euca-describe-images | grep machine | cut -f2 | head -n1` |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 37 | |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 38 | # Define secgroup |
| 39 | SECGROUP=euca_secgroup |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 40 | |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 41 | # Add a secgroup |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame^] | 42 | if ! euca-describe-group | grep -q $SECGROUP; then |
| 43 | euca-add-group -d "$SECGROUP description" $SECGROUP |
| 44 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! euca-describe-group | grep -q $SECGROUP; do sleep 1; done"; then |
| 45 | echo "Security group not created" |
| 46 | exit 1 |
| 47 | fi |
| 48 | fi |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 49 | |
| 50 | # Launch it |
Dean Troyer | 1d6e0e1 | 2011-12-23 12:45:13 -0600 | [diff] [blame] | 51 | INSTANCE=`euca-run-instances -g $SECGROUP -t $DEFAULT_INSTANCE_TYPE $IMAGE | grep INSTANCE | cut -f2` |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 52 | |
| 53 | # Assure it has booted within a reasonable time |
Dean Troyer | c384424 | 2011-12-30 14:27:02 -0600 | [diff] [blame] | 54 | 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] | 55 | echo "server didn't become active within $RUNNING_TIMEOUT seconds" |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 56 | exit 1 |
| 57 | fi |
| 58 | |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 59 | # Allocate floating address |
| 60 | FLOATING_IP=`euca-allocate-address | cut -f2` |
| 61 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame^] | 62 | # Associate floating address |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 63 | euca-associate-address -i $INSTANCE $FLOATING_IP |
| 64 | |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 65 | # Authorize pinging |
| 66 | euca-authorize -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP |
| 67 | |
Dean Troyer | c384424 | 2011-12-30 14:27:02 -0600 | [diff] [blame] | 68 | # Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds |
Dean Troyer | c384424 | 2011-12-30 14:27:02 -0600 | [diff] [blame] | 69 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then |
| 70 | echo "Couldn't ping server with floating ip" |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 71 | exit 1 |
| 72 | fi |
| 73 | |
| 74 | # Revoke pinging |
| 75 | euca-revoke -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP |
| 76 | |
| 77 | # Delete group |
| 78 | euca-delete-group $SECGROUP |
| 79 | |
| 80 | # Release floating address |
| 81 | euca-disassociate-address $FLOATING_IP |
| 82 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame^] | 83 | # Wait just a tick for everything above to complete so release doesn't fail |
| 84 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while euca-describe-addresses | grep $INSTANCE | grep -q $FLOATING_IP; do sleep 1; done"; then |
| 85 | echo "Floating ip $FLOATING_IP not disassociated within $ASSOCIATE_TIMEOUT seconds" |
| 86 | exit 1 |
| 87 | fi |
| 88 | |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 89 | # Release floating address |
| 90 | euca-release-address $FLOATING_IP |
| 91 | |
Dean Troyer | c384424 | 2011-12-30 14:27:02 -0600 | [diff] [blame] | 92 | # Wait just a tick for everything above to complete so terminate doesn't fail |
| 93 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while euca-describe-addresses | grep -q $FLOATING_IP; do sleep 1; done"; then |
| 94 | echo "Floating ip $FLOATING_IP not released within $ASSOCIATE_TIMEOUT seconds" |
| 95 | exit 1 |
| 96 | fi |
| 97 | |
Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 98 | # Terminate instance |
Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 99 | euca-terminate-instances $INSTANCE |