| Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash | 
|  | 2 |  | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 3 | # **floating_ips.sh** - using the cloud can be fun | 
| Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 4 |  | 
|  | 5 | # we will use the ``nova`` cli tool provided by the ``python-novaclient`` | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 6 | # package to work out the instance connectivity | 
| Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [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 "*********************************************************************" | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 11 |  | 
| Vishvananda Ishaya | 9b35367 | 2011-10-20 10:07:10 -0700 | [diff] [blame] | 12 | # 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] | 13 | # only the first error that occured. | 
|  | 14 | set -o errexit | 
|  | 15 |  | 
| Vishvananda Ishaya | 9b35367 | 2011-10-20 10:07:10 -0700 | [diff] [blame] | 16 | # 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] | 17 | # an error.  It is also useful for following allowing as the install occurs. | 
|  | 18 | set -o xtrace | 
|  | 19 |  | 
|  | 20 |  | 
|  | 21 | # Settings | 
|  | 22 | # ======== | 
| Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 23 |  | 
| Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -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 | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 27 |  | 
|  | 28 | # Import common functions | 
| Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -0600 | [diff] [blame] | 29 | source $TOP_DIR/functions | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 30 |  | 
|  | 31 | # Import configuration | 
| Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -0600 | [diff] [blame] | 32 | source $TOP_DIR/openrc | 
| Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 33 |  | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 34 | # Import quantum functions if needed | 
|  | 35 | if is_service_enabled quantum; then | 
|  | 36 | source $TOP_DIR/lib/quantum | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 37 | fi | 
|  | 38 |  | 
| Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -0600 | [diff] [blame] | 39 | # Import exercise configuration | 
|  | 40 | source $TOP_DIR/exerciserc | 
| Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 41 |  | 
|  | 42 | # Instance type to create | 
|  | 43 | DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny} | 
|  | 44 |  | 
|  | 45 | # Boot this image, use first AMi image if unset | 
|  | 46 | DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami} | 
|  | 47 |  | 
|  | 48 | # Security group name | 
|  | 49 | SECGROUP=${SECGROUP:-test_secgroup} | 
|  | 50 |  | 
|  | 51 | # Default floating IP pool name | 
| Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 52 | DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova} | 
| Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 53 |  | 
|  | 54 | # Additional floating IP pool and range | 
| Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 55 | TEST_FLOATING_POOL=${TEST_FLOATING_POOL:-test} | 
|  | 56 |  | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 57 |  | 
| Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 58 | # Launching a server | 
|  | 59 | # ================== | 
| Jesse Andrews | b19424f | 2011-09-14 22:03:04 -0700 | [diff] [blame] | 60 |  | 
| Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 61 | # List servers for tenant: | 
| Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 62 | nova list | 
| Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 63 |  | 
| Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 64 | # Images | 
|  | 65 | # ------ | 
|  | 66 |  | 
|  | 67 | # Nova has a **deprecated** way of listing images. | 
|  | 68 | nova image-list | 
|  | 69 |  | 
|  | 70 | # But we recommend using glance directly | 
| Dean Troyer | 4549525 | 2012-04-13 13:16:38 -0500 | [diff] [blame] | 71 | glance image-list | 
| Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 72 |  | 
| Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 73 | # Grab the id of the image to launch | 
| Dean Troyer | 4549525 | 2012-04-13 13:16:38 -0500 | [diff] [blame] | 74 | IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1) | 
| Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 75 |  | 
| Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 76 | # Security Groups | 
|  | 77 | # --------------- | 
| Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 78 |  | 
|  | 79 | # List of secgroups: | 
|  | 80 | nova secgroup-list | 
|  | 81 |  | 
|  | 82 | # Create a secgroup | 
| Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 83 | if ! nova secgroup-list | grep -q $SECGROUP; then | 
|  | 84 | nova secgroup-create $SECGROUP "$SECGROUP description" | 
|  | 85 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then | 
|  | 86 | echo "Security group not created" | 
|  | 87 | exit 1 | 
|  | 88 | fi | 
|  | 89 | fi | 
| Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 90 |  | 
| Dean Troyer | ad10176 | 2012-06-27 22:04:40 -0500 | [diff] [blame] | 91 | # Determinine instance type | 
| Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 92 | # ------------------------- | 
| Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 93 |  | 
| Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 94 | # List of instance types: | 
| Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 95 | nova flavor-list | 
|  | 96 |  | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 97 | INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1` | 
| Dean Troyer | 1d6e0e1 | 2011-12-23 12:45:13 -0600 | [diff] [blame] | 98 | if [[ -z "$INSTANCE_TYPE" ]]; then | 
|  | 99 | # 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] | 100 | 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] | 101 | fi | 
| Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 102 |  | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 103 | NAME="ex-float" | 
| 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 | VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2` | 
|  | 106 | die_if_not_set VM_UUID "Failure launching $NAME" | 
| Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 107 |  | 
| Dean Troyer | ad10176 | 2012-06-27 22:04:40 -0500 | [diff] [blame] | 108 |  | 
| Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 109 | # Testing | 
|  | 110 | # ======= | 
|  | 111 |  | 
|  | 112 | # First check if it spins up (becomes active and responds to ping on | 
|  | 113 | # internal ip).  If you run this script from a nova node, you should | 
|  | 114 | # bypass security groups and have direct access to the server. | 
|  | 115 |  | 
|  | 116 | # Waiting for boot | 
|  | 117 | # ---------------- | 
|  | 118 |  | 
| Anthony Young | 79e807a | 2011-10-31 11:16:44 -0700 | [diff] [blame] | 119 | # check that the status is active within ACTIVE_TIMEOUT seconds | 
| Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 120 | 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] | 121 | echo "server didn't become active!" | 
|  | 122 | exit 1 | 
|  | 123 | fi | 
| Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 124 |  | 
|  | 125 | # get the IP of the server | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 126 | IP=`nova show $VM_UUID | grep "$PRIVATE_NETWORK_NAME" | get_field 2` | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 127 | die_if_not_set IP "Failure retrieving IP address" | 
| Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 128 |  | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 129 | ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT | 
| Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 130 |  | 
|  | 131 | # Security Groups & Floating IPs | 
|  | 132 | # ------------------------------ | 
|  | 133 |  | 
| Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 134 | if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then | 
|  | 135 | # allow icmp traffic (ping) | 
|  | 136 | nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0 | 
|  | 137 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list-rules $SECGROUP | grep -q icmp; do sleep 1; done"; then | 
|  | 138 | echo "Security group rule not created" | 
|  | 139 | exit 1 | 
|  | 140 | fi | 
|  | 141 | fi | 
| Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 142 |  | 
|  | 143 | # List rules for a secgroup | 
|  | 144 | nova secgroup-list-rules $SECGROUP | 
|  | 145 |  | 
| Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 146 | # allocate a floating ip from default pool | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 147 | FLOATING_IP=`nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | get_field 1` | 
|  | 148 | die_if_not_set FLOATING_IP "Failure creating floating IP" | 
| Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 149 |  | 
| Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 150 | # list floating addresses | 
|  | 151 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then | 
|  | 152 | echo "Floating IP not allocated" | 
|  | 153 | exit 1 | 
|  | 154 | fi | 
| Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 155 |  | 
|  | 156 | # add floating ip to our server | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 157 | nova add-floating-ip $VM_UUID $FLOATING_IP || \ | 
|  | 158 | die "Failure adding floating IP $FLOATING_IP to $NAME" | 
| Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 159 |  | 
| Anthony Young | 79e807a | 2011-10-31 11:16:44 -0700 | [diff] [blame] | 160 | # test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 161 | ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT | 
| Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 162 |  | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 163 | if ! is_service_enabled quantum; then | 
|  | 164 | # Allocate an IP from second floating pool | 
|  | 165 | TEST_FLOATING_IP=`nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | get_field 1` | 
|  | 166 | 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] | 167 |  | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 168 | # list floating addresses | 
|  | 169 | 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 | 
|  | 170 | echo "Floating IP not allocated" | 
|  | 171 | exit 1 | 
|  | 172 | fi | 
| Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 173 | fi | 
|  | 174 |  | 
| Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 175 | # dis-allow icmp traffic (ping) | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 176 | nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0 || die "Failure deleting security group rule from $SECGROUP" | 
| Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 177 |  | 
| Anthony Young | 1de18c6 | 2011-11-01 14:19:18 -0500 | [diff] [blame] | 178 | # FIXME (anthony): make xs support security groups | 
| Devananda van der Veen | c0c6f00 | 2012-07-06 17:49:12 -0700 | [diff] [blame] | 179 | if [ "$VIRT_DRIVER" != "xenserver" -a "$VIRT_DRIVER" != "openvz" ]; then | 
| Anthony Young | 1de18c6 | 2011-11-01 14:19:18 -0500 | [diff] [blame] | 180 | # test we can aren't able to ping our floating ip within ASSOCIATE_TIMEOUT seconds | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 181 | ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT Fail | 
| Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 182 | fi | 
|  | 183 |  | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 184 | if ! is_service_enabled quantum; then | 
|  | 185 | # Delete second floating IP | 
|  | 186 | nova floating-ip-delete $TEST_FLOATING_IP || die "Failure deleting floating IP $TEST_FLOATING_IP" | 
|  | 187 | fi | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 188 |  | 
|  | 189 | # de-allocate the floating ip | 
|  | 190 | nova floating-ip-delete $FLOATING_IP || die "Failure deleting floating IP $FLOATING_IP" | 
|  | 191 |  | 
| Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 192 | # Shutdown the server | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 193 | nova delete $VM_UUID || die "Failure deleting instance $NAME" | 
| Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 194 |  | 
| Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 195 | # Wait for termination | 
|  | 196 | if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then | 
|  | 197 | echo "Server $NAME not deleted" | 
| Russell Bryant | 5836b15 | 2012-02-24 10:23:33 -0500 | [diff] [blame] | 198 | exit 1 | 
|  | 199 | fi | 
|  | 200 |  | 
| Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 201 | # Delete a secgroup | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 202 | nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP" | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 203 |  | 
|  | 204 | set +o xtrace | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 205 | echo "*********************************************************************" | 
|  | 206 | echo "SUCCESS: End DevStack Exercise: $0" | 
|  | 207 | echo "*********************************************************************" |