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