Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # **exercise.sh** - using the cloud can be fun |
| 4 | |
| 5 | # we will use the ``nova`` cli tool provided by the ``python-novaclient`` |
| 6 | # package |
Jesse Andrews | b19424f | 2011-09-14 22:03:04 -0700 | [diff] [blame] | 7 | # |
Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 8 | |
Jesse Andrews | b19424f | 2011-09-14 22:03:04 -0700 | [diff] [blame] | 9 | |
Vishvananda Ishaya | 9b35367 | 2011-10-20 10:07:10 -0700 | [diff] [blame] | 10 | # This script exits on an error so that errors don't compound and you see |
Jesse Andrews | b19424f | 2011-09-14 22:03:04 -0700 | [diff] [blame] | 11 | # only the first error that occured. |
| 12 | set -o errexit |
| 13 | |
Vishvananda Ishaya | 9b35367 | 2011-10-20 10:07:10 -0700 | [diff] [blame] | 14 | # Print the commands being run so that we can see the command that triggers |
Jesse Andrews | b19424f | 2011-09-14 22:03:04 -0700 | [diff] [blame] | 15 | # an error. It is also useful for following allowing as the install occurs. |
| 16 | set -o xtrace |
| 17 | |
| 18 | |
| 19 | # Settings |
| 20 | # ======== |
Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 21 | |
Anthony Young | 6ab10d4 | 2011-10-20 10:24:50 -0700 | [diff] [blame] | 22 | # Use openrc + stackrc + localrc for settings |
Jesse Andrews | 787af01 | 2011-11-01 16:44:19 -0700 | [diff] [blame] | 23 | pushd $(cd $(dirname "$0")/.. && pwd) |
Anthony Young | 6ab10d4 | 2011-10-20 10:24:50 -0700 | [diff] [blame] | 24 | source ./openrc |
Jesse Andrews | 787af01 | 2011-11-01 16:44:19 -0700 | [diff] [blame] | 25 | popd |
Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 26 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 27 | # Max time to wait while vm goes from build to active state |
| 28 | ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-30} |
Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 29 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 30 | # Max time till the vm is bootable |
| 31 | BOOT_TIMEOUT=${BOOT_TIMEOUT:-30} |
| 32 | |
| 33 | # Max time to wait for proper association and dis-association. |
| 34 | ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-15} |
| 35 | |
| 36 | # Instance type to create |
| 37 | DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny} |
| 38 | |
| 39 | # Boot this image, use first AMi image if unset |
| 40 | DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami} |
| 41 | |
| 42 | # Security group name |
| 43 | SECGROUP=${SECGROUP:-test_secgroup} |
| 44 | |
| 45 | # Default floating IP pool name |
Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 46 | DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova} |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 47 | |
| 48 | # Additional floating IP pool and range |
Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 49 | TEST_FLOATING_POOL=${TEST_FLOATING_POOL:-test} |
| 50 | |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 51 | # Launching a server |
| 52 | # ================== |
Jesse Andrews | b19424f | 2011-09-14 22:03:04 -0700 | [diff] [blame] | 53 | |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 54 | # List servers for tenant: |
Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 55 | nova list |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 56 | |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 57 | # Images |
| 58 | # ------ |
| 59 | |
| 60 | # Nova has a **deprecated** way of listing images. |
| 61 | nova image-list |
| 62 | |
| 63 | # But we recommend using glance directly |
Dean Troyer | 80756ea | 2012-02-01 18:01:01 -0600 | [diff] [blame] | 64 | glance -f index |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 65 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 66 | # Grab the id of the image to launch |
Dean Troyer | 80756ea | 2012-02-01 18:01:01 -0600 | [diff] [blame] | 67 | IMAGE=`glance -f index | egrep $DEFAULT_IMAGE_NAME | head -1 | cut -d" " -f1` |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 68 | |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 69 | # Security Groups |
| 70 | # --------------- |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 71 | |
| 72 | # List of secgroups: |
| 73 | nova secgroup-list |
| 74 | |
| 75 | # Create a secgroup |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 76 | if ! nova secgroup-list | grep -q $SECGROUP; then |
| 77 | nova secgroup-create $SECGROUP "$SECGROUP description" |
| 78 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then |
| 79 | echo "Security group not created" |
| 80 | exit 1 |
| 81 | fi |
| 82 | fi |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 83 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 84 | # determinine instance type |
| 85 | # ------------------------- |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 86 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 87 | # List of instance types: |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 88 | nova flavor-list |
| 89 | |
Dean Troyer | 1d6e0e1 | 2011-12-23 12:45:13 -0600 | [diff] [blame] | 90 | INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2` |
| 91 | if [[ -z "$INSTANCE_TYPE" ]]; then |
| 92 | # grab the first flavor in the list to launch if default doesn't exist |
| 93 | INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2` |
| 94 | fi |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 95 | |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 96 | NAME="myserver" |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 97 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 98 | VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'` |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 99 | |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 100 | # Testing |
| 101 | # ======= |
| 102 | |
| 103 | # First check if it spins up (becomes active and responds to ping on |
| 104 | # internal ip). If you run this script from a nova node, you should |
| 105 | # bypass security groups and have direct access to the server. |
| 106 | |
| 107 | # Waiting for boot |
| 108 | # ---------------- |
| 109 | |
Anthony Young | 79e807a | 2011-10-31 11:16:44 -0700 | [diff] [blame] | 110 | # check that the status is active within ACTIVE_TIMEOUT seconds |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 111 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then |
Jesse Andrews | 5a77483 | 2011-10-26 21:30:02 -0700 | [diff] [blame] | 112 | echo "server didn't become active!" |
| 113 | exit 1 |
| 114 | fi |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 115 | |
| 116 | # get the IP of the server |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 117 | IP=`nova show $VM_UUID | grep "private network" | cut -d"|" -f3` |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 118 | |
Anthony Young | 8ecd294 | 2011-10-24 22:58:14 -0700 | [diff] [blame] | 119 | # for single node deployments, we can ping private ips |
| 120 | MULTI_HOST=${MULTI_HOST:-0} |
Justin Shepherd | 56a505f | 2011-10-26 10:45:02 -0500 | [diff] [blame] | 121 | if [ "$MULTI_HOST" = "0" ]; then |
Anthony Young | 8ecd294 | 2011-10-24 22:58:14 -0700 | [diff] [blame] | 122 | # sometimes the first ping fails (10 seconds isn't enough time for the VM's |
Anthony Young | 79e807a | 2011-10-31 11:16:44 -0700 | [diff] [blame] | 123 | # network to respond?), so let's ping for a default of 15 seconds with a |
| 124 | # timeout of a second for each ping. |
| 125 | if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then |
Jesse Andrews | ab8dbce | 2011-10-26 21:23:20 -0700 | [diff] [blame] | 126 | echo "Couldn't ping server" |
| 127 | exit 1 |
| 128 | fi |
Anthony Young | 79e807a | 2011-10-31 11:16:44 -0700 | [diff] [blame] | 129 | else |
| 130 | # On a multi-host system, without vm net access, do a sleep to wait for the boot |
| 131 | sleep $BOOT_TIMEOUT |
Anthony Young | 8ecd294 | 2011-10-24 22:58:14 -0700 | [diff] [blame] | 132 | fi |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 133 | |
| 134 | # Security Groups & Floating IPs |
| 135 | # ------------------------------ |
| 136 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 137 | if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then |
| 138 | # allow icmp traffic (ping) |
| 139 | nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0 |
| 140 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list-rules $SECGROUP | grep -q icmp; do sleep 1; done"; then |
| 141 | echo "Security group rule not created" |
| 142 | exit 1 |
| 143 | fi |
| 144 | fi |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 145 | |
| 146 | # List rules for a secgroup |
| 147 | nova secgroup-list-rules $SECGROUP |
| 148 | |
Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 149 | # allocate a floating ip from default pool |
| 150 | FLOATING_IP=`nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | cut -d '|' -f2` |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 151 | |
Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 152 | # list floating addresses |
| 153 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then |
| 154 | echo "Floating IP not allocated" |
| 155 | exit 1 |
| 156 | fi |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 157 | |
| 158 | # add floating ip to our server |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 159 | nova add-floating-ip $VM_UUID $FLOATING_IP |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 160 | |
Anthony Young | 79e807a | 2011-10-31 11:16:44 -0700 | [diff] [blame] | 161 | # test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds |
| 162 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then |
Jesse Andrews | 5a77483 | 2011-10-26 21:30:02 -0700 | [diff] [blame] | 163 | echo "Couldn't ping server with floating ip" |
| 164 | exit 1 |
| 165 | fi |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 166 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 167 | # Allocate an IP from second floating pool |
Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 168 | TEST_FLOATING_IP=`nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | cut -d '|' -f2` |
| 169 | |
| 170 | # list floating addresses |
| 171 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep $TEST_FLOATING_POOL | grep -q $TEST_FLOATING_IP; do sleep 1; done"; then |
| 172 | echo "Floating IP not allocated" |
| 173 | exit 1 |
| 174 | fi |
| 175 | |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 176 | # dis-allow icmp traffic (ping) |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 177 | nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0 |
| 178 | |
Anthony Young | 1de18c6 | 2011-11-01 14:19:18 -0500 | [diff] [blame] | 179 | # FIXME (anthony): make xs support security groups |
Jesse Andrews | 16b6efa | 2011-11-10 11:46:18 -0800 | [diff] [blame] | 180 | if [ "$VIRT_DRIVER" != "xenserver" ]; then |
Anthony Young | 1de18c6 | 2011-11-01 14:19:18 -0500 | [diff] [blame] | 181 | # test we can aren't able to ping our floating ip within ASSOCIATE_TIMEOUT seconds |
| 182 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then |
| 183 | print "Security group failure - ping should not be allowed!" |
| 184 | echo "Couldn't ping server with floating ip" |
| 185 | exit 1 |
| 186 | fi |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 187 | fi |
| 188 | |
| 189 | # de-allocate the floating ip |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 190 | nova floating-ip-delete $FLOATING_IP |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 191 | |
Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 192 | # Delete second floating IP |
| 193 | nova floating-ip-delete $TEST_FLOATING_IP |
| 194 | |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 195 | # shutdown the server |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 196 | nova delete $VM_UUID |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 197 | |
Russell Bryant | 5836b15 | 2012-02-24 10:23:33 -0500 | [diff] [blame] | 198 | # make sure the VM shuts down within a reasonable time |
| 199 | if ! timeout $TERMINATE_TIMEOUT sh -c "while nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then |
| 200 | echo "server didn't shut down!" |
| 201 | exit 1 |
| 202 | fi |
| 203 | |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 204 | # Delete a secgroup |
| 205 | nova secgroup-delete $SECGROUP |