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