| 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 | 
 | 13 | # only the first error that occured. | 
 | 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_ZONE=cinder | 
 | 28 | VOLUME_SIZE=1 | 
 | 29 | ATTACH_DEVICE=/dev/vdc | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 30 |  | 
 | 31 | # Import common functions | 
| Dean Troyer | 0bd2410 | 2012-03-08 00:33:54 -0600 | [diff] [blame] | 32 | source $TOP_DIR/functions | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 33 |  | 
| Dean Troyer | 0bd2410 | 2012-03-08 00:33:54 -0600 | [diff] [blame] | 34 | # Import EC2 configuration | 
 | 35 | source $TOP_DIR/eucarc | 
| Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 36 |  | 
| Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -0600 | [diff] [blame] | 37 | # Import exercise configuration | 
 | 38 | source $TOP_DIR/exerciserc | 
| Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 39 |  | 
 | 40 | # Instance type to create | 
 | 41 | DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny} | 
 | 42 |  | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 43 |  | 
 | 44 | # Launching a server | 
 | 45 | # ================== | 
 | 46 |  | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 47 | # Find a machine image to boot | 
| Jesse Andrews | 9c7c908 | 2011-11-23 10:10:53 -0800 | [diff] [blame] | 48 | IMAGE=`euca-describe-images | grep machine | cut -f2 | head -n1` | 
| Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 49 |  | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 50 | # Define secgroup | 
 | 51 | SECGROUP=euca_secgroup | 
| Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 52 |  | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 53 | # Add a secgroup | 
| Dean Troyer | a947841 | 2012-02-08 11:49:28 -0600 | [diff] [blame] | 54 | if ! euca-describe-groups | grep -q $SECGROUP; then | 
| Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 55 |     euca-add-group -d "$SECGROUP description" $SECGROUP | 
| Dean Troyer | a947841 | 2012-02-08 11:49:28 -0600 | [diff] [blame] | 56 |     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] | 57 |         echo "Security group not created" | 
 | 58 |         exit 1 | 
 | 59 |     fi | 
 | 60 | fi | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 61 |  | 
 | 62 | # Launch it | 
| Dean Troyer | 1d6e0e1 | 2011-12-23 12:45:13 -0600 | [diff] [blame] | 63 | 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] | 64 | die_if_not_set INSTANCE "Failure launching instance" | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 65 |  | 
 | 66 | # Assure it has booted within a reasonable time | 
| Dean Troyer | c384424 | 2011-12-30 14:27:02 -0600 | [diff] [blame] | 67 | 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] | 68 |     echo "server didn't become active within $RUNNING_TIMEOUT seconds" | 
| Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 69 |     exit 1 | 
 | 70 | fi | 
 | 71 |  | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 72 | # Allocate floating address | 
 | 73 | FLOATING_IP=`euca-allocate-address | cut -f2` | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 74 | die_if_not_set FLOATING_IP "Failure allocating floating IP" | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 75 |  | 
| Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 76 | # Associate floating address | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 77 | euca-associate-address -i $INSTANCE $FLOATING_IP || \ | 
 | 78 |     die "Failure associating address $FLOATING_IP to $INSTANCE" | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 79 |  | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 80 | # Authorize pinging | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 81 | euca-authorize -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP || \ | 
 | 82 |     die "Failure authorizing rule in $SECGROUP" | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 83 |  | 
| Dean Troyer | c384424 | 2011-12-30 14:27:02 -0600 | [diff] [blame] | 84 | # Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds | 
| Dean Troyer | c384424 | 2011-12-30 14:27:02 -0600 | [diff] [blame] | 85 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then | 
 | 86 |     echo "Couldn't ping server with floating ip" | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 87 |     exit 1 | 
 | 88 | fi | 
 | 89 |  | 
 | 90 | # Revoke pinging | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 91 | euca-revoke -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP || \ | 
 | 92 |     die "Failure revoking rule in $SECGROUP" | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 93 |  | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 94 | # Release floating address | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 95 | euca-disassociate-address $FLOATING_IP || \ | 
 | 96 |     die "Failure disassociating address $FLOATING_IP" | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 97 |  | 
| Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 98 | # Wait just a tick for everything above to complete so release doesn't fail | 
 | 99 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while euca-describe-addresses | grep $INSTANCE | grep -q $FLOATING_IP; do sleep 1; done"; then | 
 | 100 |     echo "Floating ip $FLOATING_IP not disassociated within $ASSOCIATE_TIMEOUT seconds" | 
 | 101 |     exit 1 | 
 | 102 | fi | 
 | 103 |  | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 104 | # Release floating address | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 105 | euca-release-address $FLOATING_IP || \ | 
 | 106 |     die "Failure releasing address $FLOATING_IP" | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 107 |  | 
| Dean Troyer | c384424 | 2011-12-30 14:27:02 -0600 | [diff] [blame] | 108 | # Wait just a tick for everything above to complete so terminate doesn't fail | 
 | 109 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while euca-describe-addresses | grep -q $FLOATING_IP; do sleep 1; done"; then | 
 | 110 |     echo "Floating ip $FLOATING_IP not released within $ASSOCIATE_TIMEOUT seconds" | 
 | 111 |     exit 1 | 
 | 112 | fi | 
 | 113 |  | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 114 | # Terminate instance | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 115 | euca-terminate-instances $INSTANCE || \ | 
 | 116 |     die "Failure terminating instance $INSTANCE" | 
| Russell Bryant | e7ed17e | 2012-02-21 17:43:33 -0500 | [diff] [blame] | 117 |  | 
| Russell Bryant | 243b26a | 2012-02-22 11:19:32 -0500 | [diff] [blame] | 118 | # Assure it has terminated within a reasonable time | 
 | 119 | if ! timeout $TERMINATE_TIMEOUT sh -c "while euca-describe-instances $INSTANCE | grep -q running; do sleep 1; done"; then | 
 | 120 |     echo "server didn't terminate within $TERMINATE_TIMEOUT seconds" | 
 | 121 |     exit 1 | 
 | 122 | fi | 
 | 123 |  | 
| Russell Bryant | e7ed17e | 2012-02-21 17:43:33 -0500 | [diff] [blame] | 124 | # Delete group | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 125 | euca-delete-group $SECGROUP || \ | 
 | 126 |     die "Failure deleting security group $SECGROUP" | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 127 |  | 
 | 128 | set +o xtrace | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 129 | echo "*********************************************************************" | 
 | 130 | echo "SUCCESS: End DevStack Exercise: $0" | 
 | 131 | echo "*********************************************************************" |