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 |
Joe Gordon | 4640026 | 2013-06-30 04:32:27 -0700 | [diff] [blame] | 12 | # only the first error that occurred. |
Jesse Andrews | b19424f | 2011-09-14 22:03:04 -0700 | [diff] [blame] | 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 | |
Mark McClain | b05c876 | 2013-07-06 23:29:39 -0400 | [diff] [blame] | 33 | # Import neutron functions if needed |
| 34 | if is_service_enabled neutron; then |
| 35 | source $TOP_DIR/lib/neutron |
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 |
Aaron Rosen | 640f1e4 | 2013-03-26 16:52:53 -0700 | [diff] [blame] | 51 | DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-public} |
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 | |
Chris Behrens | c62c2b9 | 2013-07-24 03:56:13 -0700 | [diff] [blame] | 59 | # Cells does not support floating ips API calls |
| 60 | is_service_enabled n-cell && exit 55 |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 61 | |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 62 | # Launching a server |
| 63 | # ================== |
Jesse Andrews | b19424f | 2011-09-14 22:03:04 -0700 | [diff] [blame] | 64 | |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 65 | # List servers for tenant: |
Jesse Andrews | b019151 | 2011-09-14 19:37:10 -0700 | [diff] [blame] | 66 | nova list |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 67 | |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 68 | # Images |
| 69 | # ------ |
| 70 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 71 | # List the images available |
Dean Troyer | 4549525 | 2012-04-13 13:16:38 -0500 | [diff] [blame] | 72 | glance image-list |
Jesse Andrews | 593828d | 2011-09-14 22:44:50 -0700 | [diff] [blame] | 73 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 74 | # Grab the id of the image to launch |
Dean Troyer | 4549525 | 2012-04-13 13:16:38 -0500 | [diff] [blame] | 75 | IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1) |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 76 | die_if_not_set $LINENO IMAGE "Failure getting image $DEFAULT_IMAGE_NAME" |
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 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 81 | # List security groups |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 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 |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 88 | die $LINENO "Security group not created" |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 89 | fi |
| 90 | fi |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 91 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 92 | # Configure Security Group Rules |
| 93 | if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then |
| 94 | nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0 |
| 95 | fi |
| 96 | if ! nova secgroup-list-rules $SECGROUP | grep -q " tcp .* 22 "; then |
| 97 | 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] | 98 | fi |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 99 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 100 | # List secgroup rules |
| 101 | nova secgroup-list-rules $SECGROUP |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 102 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 103 | # Set up instance |
| 104 | # --------------- |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 105 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 106 | # List flavors |
| 107 | nova flavor-list |
Dean Troyer | ad10176 | 2012-06-27 22:04:40 -0500 | [diff] [blame] | 108 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 109 | # Select a flavor |
| 110 | INSTANCE_TYPE=$(nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1) |
| 111 | if [[ -z "$INSTANCE_TYPE" ]]; then |
| 112 | # grab the first flavor in the list to launch if default doesn't exist |
| 113 | INSTANCE_TYPE=$(nova flavor-list | head -n 4 | tail -n 1 | get_field 1) |
| 114 | fi |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 115 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 116 | # Clean-up from previous runs |
| 117 | nova delete $VM_NAME || true |
| 118 | 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] | 119 | die $LINENO "server didn't terminate!" |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 120 | exit 1 |
| 121 | fi |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 122 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 123 | # Boot instance |
| 124 | # ------------- |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 125 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 126 | 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] | 127 | die_if_not_set $LINENO VM_UUID "Failure launching $VM_NAME" |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 128 | |
| 129 | # Check that the status is active within ACTIVE_TIMEOUT seconds |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 130 | 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] | 131 | die $LINENO "server didn't become active!" |
Jesse Andrews | 5a77483 | 2011-10-26 21:30:02 -0700 | [diff] [blame] | 132 | fi |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 133 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 134 | # Get the instance IP |
Nachi Ueno | 6769b16 | 2013-08-12 18:18:56 -0700 | [diff] [blame^] | 135 | IP=$(get_instance_ip $VM_UUID $PRIVATE_NETWORK_NAME) |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 136 | die_if_not_set $LINENO IP "Failure retrieving IP address" |
Jesse Andrews | d888e1c | 2011-10-15 20:01:12 -0700 | [diff] [blame] | 137 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 138 | # Private IPs can be pinged in single node deployments |
Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 139 | ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 140 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 141 | # Floating IPs |
| 142 | # ------------ |
Jesse Andrews | 6fc7101 | 2011-10-24 11:29:08 -0700 | [diff] [blame] | 143 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 144 | # Allocate a floating IP from the default pool |
| 145 | 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] | 146 | 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] | 147 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 148 | # List floating addresses |
Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 149 | 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] | 150 | die $LINENO "Floating IP not allocated" |
Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 151 | fi |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 152 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 153 | # Add floating IP to our server |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 154 | nova add-floating-ip $VM_UUID $FLOATING_IP || \ |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 155 | die $LINENO "Failure adding floating IP $FLOATING_IP to $VM_NAME" |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 156 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 157 | # Test we can ping our floating IP within ASSOCIATE_TIMEOUT seconds |
Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 158 | ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 159 | |
Mark McClain | b05c876 | 2013-07-06 23:29:39 -0400 | [diff] [blame] | 160 | if ! is_service_enabled neutron; then |
Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 161 | # Allocate an IP from second floating pool |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 162 | 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] | 163 | 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] | 164 | |
Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 165 | # list floating addresses |
| 166 | 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] | 167 | die $LINENO "Floating IP not allocated" |
Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 168 | fi |
Dean Troyer | 696ad33 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 169 | fi |
| 170 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 171 | # Dis-allow icmp traffic (ping) |
| 172 | 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] | 173 | die $LINENO "Failure deleting security group rule from $SECGROUP" |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 174 | |
Anthony Young | 1de18c6 | 2011-11-01 14:19:18 -0500 | [diff] [blame] | 175 | # FIXME (anthony): make xs support security groups |
Devananda van der Veen | c0c6f00 | 2012-07-06 17:49:12 -0700 | [diff] [blame] | 176 | if [ "$VIRT_DRIVER" != "xenserver" -a "$VIRT_DRIVER" != "openvz" ]; then |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 177 | # 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] | 178 | ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT Fail |
Anthony Young | 20a2cae | 2011-10-17 16:02:24 -0700 | [diff] [blame] | 179 | fi |
| 180 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 181 | # Clean up |
| 182 | # -------- |
| 183 | |
Mark McClain | b05c876 | 2013-07-06 23:29:39 -0400 | [diff] [blame] | 184 | if ! is_service_enabled neutron; then |
Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 185 | # Delete second floating IP |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 186 | nova floating-ip-delete $TEST_FLOATING_IP || \ |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 187 | die $LINENO "Failure deleting floating IP $TEST_FLOATING_IP" |
Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 188 | fi |
Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 189 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 190 | # Delete the floating ip |
| 191 | nova floating-ip-delete $FLOATING_IP || \ |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 192 | die $LINENO "Failure deleting floating IP $FLOATING_IP" |
Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 193 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 194 | # Delete instance |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 195 | nova delete $VM_UUID || die $LINENO "Failure deleting instance $VM_NAME" |
Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 196 | # Wait for termination |
| 197 | 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] | 198 | die $LINENO "Server $VM_NAME not deleted" |
Russell Bryant | 5836b15 | 2012-02-24 10:23:33 -0500 | [diff] [blame] | 199 | fi |
| 200 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 201 | # Delete secgroup |
| 202 | nova secgroup-delete $SECGROUP || \ |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 203 | die $LINENO "Failure deleting security group $SECGROUP" |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 204 | |
| 205 | set +o xtrace |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 206 | echo "*********************************************************************" |
| 207 | echo "SUCCESS: End DevStack Exercise: $0" |
| 208 | echo "*********************************************************************" |