blob: 5abc7137b3691da18d6f6b8697148697cab5b2ad [file] [log] [blame]
Jesse Andrewsb0191512011-09-14 19:37:10 -07001#!/usr/bin/env bash
2
Dean Troyer27e32692012-03-16 16:16:56 -05003# **floating_ips.sh** - using the cloud can be fun
Jesse Andrewsb0191512011-09-14 19:37:10 -07004
Dean Troyerda85cda2013-02-15 11:07:14 -06005# Test instance connectivity with the ``nova`` command from ``python-novaclient``
Jesse Andrewsb0191512011-09-14 19:37:10 -07006
Dean Troyer27e32692012-03-16 16:16:56 -05007echo "*********************************************************************"
Dean Troyer489bd2a2012-03-02 10:44:29 -06008echo "Begin DevStack Exercise: $0"
Dean Troyer27e32692012-03-16 16:16:56 -05009echo "*********************************************************************"
Dean Troyer489bd2a2012-03-02 10:44:29 -060010
Vishvananda Ishaya9b353672011-10-20 10:07:10 -070011# This script exits on an error so that errors don't compound and you see
Joe Gordon46400262013-06-30 04:32:27 -070012# only the first error that occurred.
Jesse Andrewsb19424f2011-09-14 22:03:04 -070013set -o errexit
14
Vishvananda Ishaya9b353672011-10-20 10:07:10 -070015# Print the commands being run so that we can see the command that triggers
igor01acdab2016-07-29 13:11:53 +020016# an error. It is also useful for following as the install occurs.
Jesse Andrewsb19424f2011-09-14 22:03:04 -070017set -o xtrace
18
19
20# Settings
21# ========
Jesse Andrewsb0191512011-09-14 19:37:10 -070022
Dean Troyer51fb4542012-03-09 22:21:59 -060023# Keep track of the current directory
24EXERCISE_DIR=$(cd $(dirname "$0") && pwd)
25TOP_DIR=$(cd $EXERCISE_DIR/..; pwd)
Dean Troyer489bd2a2012-03-02 10:44:29 -060026
27# Import common functions
Dean Troyer51fb4542012-03-09 22:21:59 -060028source $TOP_DIR/functions
Dean Troyer489bd2a2012-03-02 10:44:29 -060029
30# Import configuration
Dean Troyer51fb4542012-03-09 22:21:59 -060031source $TOP_DIR/openrc
Jesse Andrewsb0191512011-09-14 19:37:10 -070032
Dean Troyere2907b42014-02-26 17:35:37 -060033# Import project functions
Sean M. Collins2a242512016-05-03 09:03:09 -040034source $TOP_DIR/lib/neutron
Dean Troyer5a9739a2015-03-25 11:33:51 -050035source $TOP_DIR/lib/neutron-legacy
Dean Troyere2907b42014-02-26 17:35:37 -060036
Dean Troyer51fb4542012-03-09 22:21:59 -060037# Import exercise configuration
38source $TOP_DIR/exerciserc
Dean Troyer751c1522012-01-10 15:34:34 -060039
Kiall Mac Innesa16c8212014-01-12 19:35:43 +000040# If nova api is not enabled we exit with exitcode 55 so that
41# the exercise is skipped
42is_service_enabled n-api || exit 55
43
Dean Troyer751c1522012-01-10 15:34:34 -060044# Instance type to create
45DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
46
Dean Troyerda85cda2013-02-15 11:07:14 -060047# Boot this image, use first AMI image if unset
Dean Troyer751c1522012-01-10 15:34:34 -060048DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
49
50# Security group name
51SECGROUP=${SECGROUP:-test_secgroup}
52
53# Default floating IP pool name
Aaron Rosen640f1e42013-03-26 16:52:53 -070054DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-public}
Dean Troyer751c1522012-01-10 15:34:34 -060055
56# Additional floating IP pool and range
Dean Troyer696ad332012-01-10 15:34:34 -060057TEST_FLOATING_POOL=${TEST_FLOATING_POOL:-test}
58
Dean Troyerda85cda2013-02-15 11:07:14 -060059# Instance name
60VM_NAME="ex-float"
61
Chris Behrensc62c2b92013-07-24 03:56:13 -070062# Cells does not support floating ips API calls
63is_service_enabled n-cell && exit 55
Dean Troyer27e32692012-03-16 16:16:56 -050064
Jesse Andrews593828d2011-09-14 22:44:50 -070065# Launching a server
66# ==================
Jesse Andrewsb19424f2011-09-14 22:03:04 -070067
Jesse Andrews593828d2011-09-14 22:44:50 -070068# List servers for tenant:
Jesse Andrewsb0191512011-09-14 19:37:10 -070069nova list
Jesse Andrews593828d2011-09-14 22:44:50 -070070
Jesse Andrews593828d2011-09-14 22:44:50 -070071# Images
72# ------
73
Dean Troyerda85cda2013-02-15 11:07:14 -060074# List the images available
Steve Martinelli5c206c22014-08-02 20:32:31 -040075openstack image list
Jesse Andrews593828d2011-09-14 22:44:50 -070076
Dean Troyer751c1522012-01-10 15:34:34 -060077# Grab the id of the image to launch
Steve Martinelli5c206c22014-08-02 20:32:31 -040078IMAGE=$(openstack image list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
Nachi Ueno07115eb2013-02-26 12:38:18 -080079die_if_not_set $LINENO IMAGE "Failure getting image $DEFAULT_IMAGE_NAME"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070080
Anthony Young20a2cae2011-10-17 16:02:24 -070081# Security Groups
82# ---------------
Anthony Young20a2cae2011-10-17 16:02:24 -070083
Dean Troyerda85cda2013-02-15 11:07:14 -060084# List security groups
Anthony Young20a2cae2011-10-17 16:02:24 -070085nova secgroup-list
86
87# Create a secgroup
Dean Troyer751c1522012-01-10 15:34:34 -060088if ! nova secgroup-list | grep -q $SECGROUP; then
89 nova secgroup-create $SECGROUP "$SECGROUP description"
90 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -080091 die $LINENO "Security group not created"
Dean Troyer751c1522012-01-10 15:34:34 -060092 fi
93fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070094
Dean Troyerda85cda2013-02-15 11:07:14 -060095# Configure Security Group Rules
96if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
97 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
98fi
99if ! nova secgroup-list-rules $SECGROUP | grep -q " tcp .* 22 "; then
100 nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
Dean Troyer1d6e0e12011-12-23 12:45:13 -0600101fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700102
Dean Troyerda85cda2013-02-15 11:07:14 -0600103# List secgroup rules
104nova secgroup-list-rules $SECGROUP
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700105
Dean Troyerda85cda2013-02-15 11:07:14 -0600106# Set up instance
107# ---------------
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700108
Dean Troyerda85cda2013-02-15 11:07:14 -0600109# List flavors
110nova flavor-list
Dean Troyerad101762012-06-27 22:04:40 -0500111
Dean Troyerda85cda2013-02-15 11:07:14 -0600112# Select a flavor
113INSTANCE_TYPE=$(nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1)
114if [[ -z "$INSTANCE_TYPE" ]]; then
115 # grab the first flavor in the list to launch if default doesn't exist
Sean Dague922c8ae2013-10-22 10:06:06 -0400116 INSTANCE_TYPE=$(nova flavor-list | head -n 4 | tail -n 1 | get_field 1)
DennyZhang23178a92013-10-22 17:07:32 -0500117 die_if_not_set $LINENO INSTANCE_TYPE "Failure retrieving INSTANCE_TYPE"
Dean Troyerda85cda2013-02-15 11:07:14 -0600118fi
Jesse Andrews6fc71012011-10-24 11:29:08 -0700119
Dean Troyerda85cda2013-02-15 11:07:14 -0600120# Clean-up from previous runs
121nova delete $VM_NAME || true
122if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VM_NAME; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800123 die $LINENO "server didn't terminate!"
Dean Troyerda85cda2013-02-15 11:07:14 -0600124 exit 1
125fi
Jesse Andrews6fc71012011-10-24 11:29:08 -0700126
Dean Troyerda85cda2013-02-15 11:07:14 -0600127# Boot instance
128# -------------
Jesse Andrews6fc71012011-10-24 11:29:08 -0700129
Dean Troyer526b79f2013-11-22 11:30:44 -0600130VM_UUID=$(nova boot --flavor $INSTANCE_TYPE --image $IMAGE --security-groups=$SECGROUP $VM_NAME | grep ' id ' | get_field 2)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800131die_if_not_set $LINENO VM_UUID "Failure launching $VM_NAME"
Dean Troyerda85cda2013-02-15 11:07:14 -0600132
133# Check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -0600134if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800135 die $LINENO "server didn't become active!"
Jesse Andrews5a774832011-10-26 21:30:02 -0700136fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700137
Dean Troyerda85cda2013-02-15 11:07:14 -0600138# Get the instance IP
Nachi Ueno6769b162013-08-12 18:18:56 -0700139IP=$(get_instance_ip $VM_UUID $PRIVATE_NETWORK_NAME)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800140die_if_not_set $LINENO IP "Failure retrieving IP address"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700141
Dean Troyerda85cda2013-02-15 11:07:14 -0600142# Private IPs can be pinged in single node deployments
Sean Dagueaf9bf862015-04-16 08:58:32 -0400143ping_check $IP $BOOT_TIMEOUT "$PRIVATE_NETWORK_NAME"
Jesse Andrews6fc71012011-10-24 11:29:08 -0700144
Dean Troyerda85cda2013-02-15 11:07:14 -0600145# Floating IPs
146# ------------
Jesse Andrews6fc71012011-10-24 11:29:08 -0700147
Dean Troyerda85cda2013-02-15 11:07:14 -0600148# Allocate a floating IP from the default pool
149FLOATING_IP=$(nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | get_field 1)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800150die_if_not_set $LINENO FLOATING_IP "Failure creating floating IP from pool $DEFAULT_FLOATING_POOL"
Anthony Young20a2cae2011-10-17 16:02:24 -0700151
Dean Troyerda85cda2013-02-15 11:07:14 -0600152# List floating addresses
Dean Troyer696ad332012-01-10 15:34:34 -0600153if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800154 die $LINENO "Floating IP not allocated"
Dean Troyer696ad332012-01-10 15:34:34 -0600155fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700156
Dean Troyerda85cda2013-02-15 11:07:14 -0600157# Add floating IP to our server
Dean Troyer27e32692012-03-16 16:16:56 -0500158nova add-floating-ip $VM_UUID $FLOATING_IP || \
Nachi Ueno07115eb2013-02-26 12:38:18 -0800159 die $LINENO "Failure adding floating IP $FLOATING_IP to $VM_NAME"
Anthony Young20a2cae2011-10-17 16:02:24 -0700160
Dean Troyerda85cda2013-02-15 11:07:14 -0600161# Test we can ping our floating IP within ASSOCIATE_TIMEOUT seconds
Sean Dagueaf9bf862015-04-16 08:58:32 -0400162ping_check $FLOATING_IP $ASSOCIATE_TIMEOUT "$PUBLIC_NETWORK_NAME"
Anthony Young20a2cae2011-10-17 16:02:24 -0700163
Mark McClainb05c8762013-07-06 23:29:39 -0400164if ! is_service_enabled neutron; then
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700165 # Allocate an IP from second floating pool
Dean Troyerda85cda2013-02-15 11:07:14 -0600166 TEST_FLOATING_IP=$(nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | get_field 1)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800167 die_if_not_set $LINENO TEST_FLOATING_IP "Failure creating floating IP in $TEST_FLOATING_POOL"
Dean Troyer696ad332012-01-10 15:34:34 -0600168
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700169 # list floating addresses
170 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 Ueno07115eb2013-02-26 12:38:18 -0800171 die $LINENO "Floating IP not allocated"
Sean Dague922c8ae2013-10-22 10:06:06 -0400172 fi
Dean Troyer696ad332012-01-10 15:34:34 -0600173fi
174
Dean Troyerda85cda2013-02-15 11:07:14 -0600175# Dis-allow icmp traffic (ping)
176nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0 || \
Nachi Ueno07115eb2013-02-26 12:38:18 -0800177 die $LINENO "Failure deleting security group rule from $SECGROUP"
Anthony Young20a2cae2011-10-17 16:02:24 -0700178
Brian Haley9b3602c2014-02-28 13:52:29 -0500179if ! timeout $ASSOCIATE_TIMEOUT sh -c "while nova secgroup-list-rules $SECGROUP | grep -q icmp; do sleep 1; done"; then
180 die $LINENO "Security group rule not deleted from $SECGROUP"
181fi
182
Anthony Young1de18c62011-11-01 14:19:18 -0500183# FIXME (anthony): make xs support security groups
Adam Gandelmanb875d012014-03-17 19:47:14 -0700184if [ "$VIRT_DRIVER" != "ironic" -a "$VIRT_DRIVER" != "xenserver" -a "$VIRT_DRIVER" != "openvz" ]; then
Dean Troyerda85cda2013-02-15 11:07:14 -0600185 # Test we can aren't able to ping our floating ip within ASSOCIATE_TIMEOUT seconds
Sean Dagueaf9bf862015-04-16 08:58:32 -0400186 ping_check $FLOATING_IP $ASSOCIATE_TIMEOUT "$PUBLIC_NETWORK_NAME" Fail
Anthony Young20a2cae2011-10-17 16:02:24 -0700187fi
188
Dean Troyerda85cda2013-02-15 11:07:14 -0600189# Clean up
190# --------
191
Mark McClainb05c8762013-07-06 23:29:39 -0400192if ! is_service_enabled neutron; then
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700193 # Delete second floating IP
Dean Troyerda85cda2013-02-15 11:07:14 -0600194 nova floating-ip-delete $TEST_FLOATING_IP || \
Nachi Ueno07115eb2013-02-26 12:38:18 -0800195 die $LINENO "Failure deleting floating IP $TEST_FLOATING_IP"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700196fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700197
Dean Troyerda85cda2013-02-15 11:07:14 -0600198# Delete the floating ip
199nova floating-ip-delete $FLOATING_IP || \
Nachi Ueno07115eb2013-02-26 12:38:18 -0800200 die $LINENO "Failure deleting floating IP $FLOATING_IP"
Nachi Uenofda946e2012-10-24 17:26:02 -0700201
Dean Troyerda85cda2013-02-15 11:07:14 -0600202# Delete instance
Nachi Ueno07115eb2013-02-26 12:38:18 -0800203nova delete $VM_UUID || die $LINENO "Failure deleting instance $VM_NAME"
Dean Troyer96288ba2012-08-17 14:11:55 -0500204# Wait for termination
205if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800206 die $LINENO "Server $VM_NAME not deleted"
Russell Bryant5836b152012-02-24 10:23:33 -0500207fi
208
Dean Troyerda85cda2013-02-15 11:07:14 -0600209# Delete secgroup
210nova secgroup-delete $SECGROUP || \
Nachi Ueno07115eb2013-02-26 12:38:18 -0800211 die $LINENO "Failure deleting security group $SECGROUP"
Dean Troyer489bd2a2012-03-02 10:44:29 -0600212
213set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500214echo "*********************************************************************"
215echo "SUCCESS: End DevStack Exercise: $0"
216echo "*********************************************************************"