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 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 5 | # Test instance connectivity with the ``nova`` command from ``python-novaclient`` |
Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 6 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 7 | echo "*********************************************************************" |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 8 | echo "Begin DevStack Exercise: $0" |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 9 | echo "*********************************************************************" |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 10 | |
Vishvananda Ishaya | 9b35367 | 2011-10-20 10:07:10 -0700 | [diff] [blame] | 11 | # 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] | 12 | # only the first error that occured. |
| 13 | set -o errexit |
| 14 | |
Vishvananda Ishaya | 9b35367 | 2011-10-20 10:07:10 -0700 | [diff] [blame] | 15 | # 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] | 16 | # an error. It is also useful for following allowing as the install occurs. |
| 17 | set -o xtrace |
| 18 | |
| 19 | |
| 20 | # Settings |
| 21 | # ======== |
Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 22 | |
Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -0600 | [diff] [blame] | 23 | # Keep track of the current directory |
| 24 | EXERCISE_DIR=$(cd $(dirname "$0") && pwd) |
| 25 | TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 26 | |
| 27 | # Import common functions |
Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -0600 | [diff] [blame] | 28 | source $TOP_DIR/functions |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 29 | |
| 30 | # Import configuration |
Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -0600 | [diff] [blame] | 31 | source $TOP_DIR/openrc |
Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 32 | |
Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 33 | # Import quantum functions if needed |
| 34 | if is_service_enabled quantum; then |
| 35 | source $TOP_DIR/lib/quantum |
Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 36 | fi |
| 37 | |
Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -0600 | [diff] [blame] | 38 | # Import exercise configuration |
| 39 | source $TOP_DIR/exerciserc |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 40 | |
| 41 | # Instance type to create |
| 42 | DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny} |
| 43 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 44 | # Boot this image, use first AMI image if unset |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 45 | DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami} |
| 46 | |
| 47 | # Security group name |
| 48 | SECGROUP=${SECGROUP:-test_secgroup} |
| 49 | |
| 50 | # Default floating IP pool name |
Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 51 | DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova} |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 52 | |
| 53 | # Additional floating IP pool and range |
Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 54 | TEST_FLOATING_POOL=${TEST_FLOATING_POOL:-test} |
| 55 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 56 | # Instance name |
| 57 | VM_NAME="ex-float" |
| 58 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 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 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 69 | # List the images available |
Dean Troyer | 4549525 | 2012-04-13 13:16:38 -0500 | [diff] [blame] | 70 | glance image-list |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 71 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 72 | # Grab the id of the image to launch |
Dean Troyer | 4549525 | 2012-04-13 13:16:38 -0500 | [diff] [blame] | 73 | IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1) |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame^] | 74 | die_if_not_set $LINENO IMAGE "Failure getting image $DEFAULT_IMAGE_NAME" |
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 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 79 | # List security groups |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 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 |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame^] | 86 | die $LINENO "Security group not created" |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 87 | fi |
| 88 | fi |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 89 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 90 | # Configure Security Group Rules |
| 91 | if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then |
| 92 | nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0 |
| 93 | fi |
| 94 | if ! nova secgroup-list-rules $SECGROUP | grep -q " tcp .* 22 "; then |
| 95 | nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0 |
Dean Troyer | 1d6e0e1 | 2011-12-23 12:45:13 -0600 | [diff] [blame] | 96 | fi |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 97 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 98 | # List secgroup rules |
| 99 | nova secgroup-list-rules $SECGROUP |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 100 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 101 | # Set up instance |
| 102 | # --------------- |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 103 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 104 | # List flavors |
| 105 | nova flavor-list |
Dean Troyer | ad10176 | 2012-06-27 22:04:40 -0500 | [diff] [blame] | 106 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 107 | # Select a flavor |
| 108 | INSTANCE_TYPE=$(nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1) |
| 109 | if [[ -z "$INSTANCE_TYPE" ]]; then |
| 110 | # grab the first flavor in the list to launch if default doesn't exist |
| 111 | INSTANCE_TYPE=$(nova flavor-list | head -n 4 | tail -n 1 | get_field 1) |
| 112 | fi |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 113 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 114 | # Clean-up from previous runs |
| 115 | nova delete $VM_NAME || true |
| 116 | if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VM_NAME; do sleep 1; done"; then |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame^] | 117 | die $LINENO "server didn't terminate!" |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 118 | exit 1 |
| 119 | fi |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 120 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 121 | # Boot instance |
| 122 | # ------------- |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 123 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 124 | VM_UUID=$(nova boot --flavor $INSTANCE_TYPE --image $IMAGE --security_groups=$SECGROUP $VM_NAME | grep ' id ' | get_field 2) |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame^] | 125 | die_if_not_set $LINENO VM_UUID "Failure launching $VM_NAME" |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 126 | |
| 127 | # Check that the status is active within ACTIVE_TIMEOUT seconds |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 128 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame^] | 129 | die $LINENO "server didn't become active!" |
Jesse Andrews | 5a77483 | 2011-10-26 21:30:02 -0700 | [diff] [blame] | 130 | fi |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 131 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 132 | # Get the instance IP |
| 133 | IP=$(nova show $VM_UUID | grep "$PRIVATE_NETWORK_NAME" | get_field 2) |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame^] | 134 | die_if_not_set $LINENO IP "Failure retrieving IP address" |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 135 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 136 | # Private IPs can be pinged in single node deployments |
Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 137 | ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 138 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 139 | # Floating IPs |
| 140 | # ------------ |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 141 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 142 | # Allocate a floating IP from the default pool |
| 143 | FLOATING_IP=$(nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | get_field 1) |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame^] | 144 | die_if_not_set $LINENO FLOATING_IP "Failure creating floating IP from pool $DEFAULT_FLOATING_POOL" |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 145 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 146 | # List floating addresses |
Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 147 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame^] | 148 | die $LINENO "Floating IP not allocated" |
Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 149 | fi |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 150 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 151 | # Add floating IP to our server |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 152 | nova add-floating-ip $VM_UUID $FLOATING_IP || \ |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame^] | 153 | die $LINENO "Failure adding floating IP $FLOATING_IP to $VM_NAME" |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 154 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 155 | # Test we can ping our floating IP within ASSOCIATE_TIMEOUT seconds |
Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 156 | ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 157 | |
Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 158 | if ! is_service_enabled quantum; then |
| 159 | # Allocate an IP from second floating pool |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 160 | TEST_FLOATING_IP=$(nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | get_field 1) |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame^] | 161 | die_if_not_set $LINENO TEST_FLOATING_IP "Failure creating floating IP in $TEST_FLOATING_POOL" |
Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 162 | |
Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 163 | # list floating addresses |
| 164 | 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 |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame^] | 165 | die $LINENO "Floating IP not allocated" |
Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 166 | fi |
Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 167 | fi |
| 168 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 169 | # Dis-allow icmp traffic (ping) |
| 170 | nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0 || \ |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame^] | 171 | die $LINENO "Failure deleting security group rule from $SECGROUP" |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 172 | |
Anthony Young | 1de18c6 | 2011-11-01 14:19:18 -0500 | [diff] [blame] | 173 | # FIXME (anthony): make xs support security groups |
Devananda van der Veen | c0c6f00 | 2012-07-06 17:49:12 -0700 | [diff] [blame] | 174 | if [ "$VIRT_DRIVER" != "xenserver" -a "$VIRT_DRIVER" != "openvz" ]; then |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 175 | # 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] | 176 | ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT Fail |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 177 | fi |
| 178 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 179 | # Clean up |
| 180 | # -------- |
| 181 | |
Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 182 | if ! is_service_enabled quantum; then |
| 183 | # Delete second floating IP |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 184 | nova floating-ip-delete $TEST_FLOATING_IP || \ |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame^] | 185 | die $LINENO "Failure deleting floating IP $TEST_FLOATING_IP" |
Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 186 | fi |
Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 187 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 188 | # Delete the floating ip |
| 189 | nova floating-ip-delete $FLOATING_IP || \ |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame^] | 190 | die $LINENO "Failure deleting floating IP $FLOATING_IP" |
Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 191 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 192 | # Delete instance |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame^] | 193 | nova delete $VM_UUID || die $LINENO "Failure deleting instance $VM_NAME" |
Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 194 | # Wait for termination |
| 195 | if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame^] | 196 | die $LINENO "Server $VM_NAME not deleted" |
Russell Bryant | 5836b15 | 2012-02-24 10:23:33 -0500 | [diff] [blame] | 197 | fi |
| 198 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 199 | # Delete secgroup |
| 200 | nova secgroup-delete $SECGROUP || \ |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame^] | 201 | die $LINENO "Failure deleting security group $SECGROUP" |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 202 | |
| 203 | set +o xtrace |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 204 | echo "*********************************************************************" |
| 205 | echo "SUCCESS: End DevStack Exercise: $0" |
| 206 | echo "*********************************************************************" |