| 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_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 |  | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 36 | # Import quantum functions if needed | 
 | 37 | if is_service_enabled quantum; then | 
 | 38 |     source $TOP_DIR/lib/quantum | 
 | 39 |     setup_quantum | 
 | 40 | fi | 
 | 41 |  | 
| Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -0600 | [diff] [blame] | 42 | # Import exercise configuration | 
 | 43 | source $TOP_DIR/exerciserc | 
| Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 44 |  | 
 | 45 | # Instance type to create | 
 | 46 | DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny} | 
 | 47 |  | 
| Devananda van der Veen | c0c6f00 | 2012-07-06 17:49:12 -0700 | [diff] [blame] | 48 | # Boot this image, use first AMI-format image if unset | 
 | 49 | DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami} | 
 | 50 |  | 
| Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 51 | # Security group name | 
 | 52 | SECGROUP=${SECGROUP:-euca_secgroup} | 
 | 53 |  | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 54 |  | 
 | 55 | # Launching a server | 
 | 56 | # ================== | 
 | 57 |  | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 58 | # Find a machine image to boot | 
| Devananda van der Veen | c0c6f00 | 2012-07-06 17:49:12 -0700 | [diff] [blame] | 59 | IMAGE=`euca-describe-images | grep machine | grep ${DEFAULT_IMAGE_NAME} | cut -f2 | head -n1` | 
| Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 60 |  | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 61 | # Add a secgroup | 
| Dean Troyer | a947841 | 2012-02-08 11:49:28 -0600 | [diff] [blame] | 62 | if ! euca-describe-groups | grep -q $SECGROUP; then | 
| Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 63 |     euca-add-group -d "$SECGROUP description" $SECGROUP | 
| Dean Troyer | a947841 | 2012-02-08 11:49:28 -0600 | [diff] [blame] | 64 |     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] | 65 |         echo "Security group not created" | 
 | 66 |         exit 1 | 
 | 67 |     fi | 
 | 68 | fi | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 69 |  | 
 | 70 | # Launch it | 
| Dean Troyer | 1d6e0e1 | 2011-12-23 12:45:13 -0600 | [diff] [blame] | 71 | 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] | 72 | die_if_not_set INSTANCE "Failure launching instance" | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 73 |  | 
 | 74 | # Assure it has booted within a reasonable time | 
| Dean Troyer | c384424 | 2011-12-30 14:27:02 -0600 | [diff] [blame] | 75 | 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] | 76 |     echo "server didn't become active within $RUNNING_TIMEOUT seconds" | 
| Jesse Andrews | f670549 | 2011-11-01 16:04:14 -0700 | [diff] [blame] | 77 |     exit 1 | 
 | 78 | fi | 
 | 79 |  | 
| Chuck Short | 3725895 | 2012-08-07 10:38:44 -0500 | [diff] [blame] | 80 | # Volumes | 
 | 81 | # ------- | 
| Joe Gordon | 6fd2811 | 2012-11-13 16:55:41 -0800 | [diff] [blame] | 82 | if [[ "$ENABLED_SERVICES" =~ "c-vol" ]]; then | 
| Vishvananda Ishaya | dc9e288 | 2012-09-21 23:20:06 +0000 | [diff] [blame] | 83 |    VOLUME_ZONE=`euca-describe-availability-zones | head -n1 | cut -f2` | 
 | 84 |    die_if_not_set VOLUME_ZONE "Failure to find zone for volume" | 
 | 85 |  | 
| Chuck Short | 3725895 | 2012-08-07 10:38:44 -0500 | [diff] [blame] | 86 |    VOLUME=`euca-create-volume -s 1 -z $VOLUME_ZONE | cut -f2` | 
 | 87 |    die_if_not_set VOLUME "Failure to create volume" | 
 | 88 |  | 
 | 89 |    # Test that volume has been created | 
 | 90 |    VOLUME=`euca-describe-volumes | cut -f2` | 
| Vishvananda Ishaya | dc9e288 | 2012-09-21 23:20:06 +0000 | [diff] [blame] | 91 |    die_if_not_set VOLUME "Failure to get volume" | 
| Chuck Short | 3725895 | 2012-08-07 10:38:44 -0500 | [diff] [blame] | 92 |  | 
 | 93 |    # Test volume has become available | 
 | 94 |    if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! euca-describe-volumes $VOLUME | grep -q available; do sleep 1; done"; then | 
 | 95 |        echo "volume didnt become available within $RUNNING_TIMEOUT seconds" | 
 | 96 |        exit 1 | 
 | 97 |    fi | 
 | 98 |  | 
 | 99 |    # Attach volume to an instance | 
 | 100 |    euca-attach-volume -i $INSTANCE -d $ATTACH_DEVICE $VOLUME || \ | 
 | 101 |        die "Failure attaching volume $VOLUME to $INSTANCE" | 
 | 102 |    if ! timeout $ACTIVE_TIMEOUT sh -c "while ! euca-describe-volumes $VOLUME | grep -q in-use; do sleep 1; done"; then | 
 | 103 |        echo "Could not attach $VOLUME to $INSTANCE" | 
 | 104 |        exit 1 | 
 | 105 |    fi | 
 | 106 |  | 
 | 107 |    # Detach volume from an instance | 
 | 108 |    euca-detach-volume $VOLUME || \ | 
 | 109 |        die "Failure detaching volume $VOLUME to $INSTANCE" | 
 | 110 |     if ! timeout $ACTIVE_TIMEOUT sh -c "while ! euca-describe-volumes $VOLUME | grep -q available; do sleep 1; done"; then | 
 | 111 |         echo "Could not detach $VOLUME to $INSTANCE" | 
 | 112 |         exit 1 | 
 | 113 |     fi | 
 | 114 |  | 
 | 115 |     # Remove volume | 
 | 116 |     euca-delete-volume $VOLUME || \ | 
 | 117 |         die "Failure to delete volume" | 
 | 118 |     if ! timeout $ACTIVE_TIMEOUT sh -c "while euca-describe-volumes | grep $VOLUME; do sleep 1; done"; then | 
 | 119 |        echo "Could not delete $VOLUME" | 
 | 120 |        exit 1 | 
 | 121 |     fi | 
 | 122 | else | 
 | 123 |     echo "Volume Tests Skipped" | 
 | 124 | fi | 
 | 125 |  | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 126 | # Allocate floating address | 
 | 127 | FLOATING_IP=`euca-allocate-address | cut -f2` | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 128 | die_if_not_set FLOATING_IP "Failure allocating floating IP" | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 129 |  | 
| Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 130 | # Associate floating address | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 131 | euca-associate-address -i $INSTANCE $FLOATING_IP || \ | 
 | 132 |     die "Failure associating address $FLOATING_IP to $INSTANCE" | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 133 |  | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 134 | # Authorize pinging | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 135 | euca-authorize -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP || \ | 
 | 136 |     die "Failure authorizing rule in $SECGROUP" | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 137 |  | 
| Dean Troyer | c384424 | 2011-12-30 14:27:02 -0600 | [diff] [blame] | 138 | # Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 139 | ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 140 |  | 
 | 141 | # Revoke pinging | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 142 | euca-revoke -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP || \ | 
 | 143 |     die "Failure revoking rule in $SECGROUP" | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 144 |  | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 145 | # Release floating address | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 146 | euca-disassociate-address $FLOATING_IP || \ | 
 | 147 |     die "Failure disassociating address $FLOATING_IP" | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 148 |  | 
| Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 149 | # Wait just a tick for everything above to complete so release doesn't fail | 
 | 150 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while euca-describe-addresses | grep $INSTANCE | grep -q $FLOATING_IP; do sleep 1; done"; then | 
 | 151 |     echo "Floating ip $FLOATING_IP not disassociated within $ASSOCIATE_TIMEOUT seconds" | 
 | 152 |     exit 1 | 
 | 153 | fi | 
 | 154 |  | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 155 | # Release floating address | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 156 | euca-release-address $FLOATING_IP || \ | 
 | 157 |     die "Failure releasing address $FLOATING_IP" | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 158 |  | 
| Dean Troyer | c384424 | 2011-12-30 14:27:02 -0600 | [diff] [blame] | 159 | # Wait just a tick for everything above to complete so terminate doesn't fail | 
 | 160 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while euca-describe-addresses | grep -q $FLOATING_IP; do sleep 1; done"; then | 
 | 161 |     echo "Floating ip $FLOATING_IP not released within $ASSOCIATE_TIMEOUT seconds" | 
 | 162 |     exit 1 | 
 | 163 | fi | 
 | 164 |  | 
| Anthony Young | abda427 | 2011-12-16 20:16:20 +0000 | [diff] [blame] | 165 | # Terminate instance | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 166 | euca-terminate-instances $INSTANCE || \ | 
 | 167 |     die "Failure terminating instance $INSTANCE" | 
| Russell Bryant | e7ed17e | 2012-02-21 17:43:33 -0500 | [diff] [blame] | 168 |  | 
| Russell Bryant | 243b26a | 2012-02-22 11:19:32 -0500 | [diff] [blame] | 169 | # Assure it has terminated within a reasonable time | 
| Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 170 | if ! timeout $TERMINATE_TIMEOUT sh -c "while euca-describe-instances $INSTANCE | grep -q $INSTANCE; do sleep 1; done"; then | 
| Russell Bryant | 243b26a | 2012-02-22 11:19:32 -0500 | [diff] [blame] | 171 |     echo "server didn't terminate within $TERMINATE_TIMEOUT seconds" | 
 | 172 |     exit 1 | 
 | 173 | fi | 
 | 174 |  | 
| Russell Bryant | e7ed17e | 2012-02-21 17:43:33 -0500 | [diff] [blame] | 175 | # Delete group | 
| Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 176 | euca-delete-group $SECGROUP || die "Failure deleting security group $SECGROUP" | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 177 |  | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 178 | if is_service_enabled quantum; then | 
 | 179 |     teardown_quantum | 
 | 180 | fi | 
 | 181 |  | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 182 | set +o xtrace | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 183 | echo "*********************************************************************" | 
 | 184 | echo "SUCCESS: End DevStack Exercise: $0" | 
 | 185 | echo "*********************************************************************" |