blob: 34ab69d9881c9446d1d8cbf72a9dd176f8f054c4 [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
Jesse Andrewsb19424f2011-09-14 22:03:04 -070012# only the first error that occured.
13set -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
Jesse Andrewsb19424f2011-09-14 22:03:04 -070016# an error. It is also useful for following allowing as the install occurs.
17set -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
Nachi Ueno5db5bfa2012-10-29 11:25:29 -070033# Import quantum functions if needed
34if is_service_enabled quantum; then
35 source $TOP_DIR/lib/quantum
Nachi Ueno5db5bfa2012-10-29 11:25:29 -070036fi
37
Dean Troyer51fb4542012-03-09 22:21:59 -060038# Import exercise configuration
39source $TOP_DIR/exerciserc
Dean Troyer751c1522012-01-10 15:34:34 -060040
41# Instance type to create
42DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
43
Dean Troyerda85cda2013-02-15 11:07:14 -060044# Boot this image, use first AMI image if unset
Dean Troyer751c1522012-01-10 15:34:34 -060045DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
46
47# Security group name
48SECGROUP=${SECGROUP:-test_secgroup}
49
50# Default floating IP pool name
Dean Troyer696ad332012-01-10 15:34:34 -060051DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova}
Dean Troyer751c1522012-01-10 15:34:34 -060052
53# Additional floating IP pool and range
Dean Troyer696ad332012-01-10 15:34:34 -060054TEST_FLOATING_POOL=${TEST_FLOATING_POOL:-test}
55
Dean Troyerda85cda2013-02-15 11:07:14 -060056# Instance name
57VM_NAME="ex-float"
58
Dean Troyer27e32692012-03-16 16:16:56 -050059
Jesse Andrews593828d2011-09-14 22:44:50 -070060# Launching a server
61# ==================
Jesse Andrewsb19424f2011-09-14 22:03:04 -070062
Jesse Andrews593828d2011-09-14 22:44:50 -070063# List servers for tenant:
Jesse Andrewsb0191512011-09-14 19:37:10 -070064nova list
Jesse Andrews593828d2011-09-14 22:44:50 -070065
Jesse Andrews593828d2011-09-14 22:44:50 -070066# Images
67# ------
68
Dean Troyerda85cda2013-02-15 11:07:14 -060069# List the images available
Dean Troyer45495252012-04-13 13:16:38 -050070glance image-list
Jesse Andrews593828d2011-09-14 22:44:50 -070071
Dean Troyer751c1522012-01-10 15:34:34 -060072# Grab the id of the image to launch
Dean Troyer45495252012-04-13 13:16:38 -050073IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
Dean Troyerda85cda2013-02-15 11:07:14 -060074die_if_not_set IMAGE "Failure getting image $DEFAULT_IMAGE_NAME"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070075
Anthony Young20a2cae2011-10-17 16:02:24 -070076# Security Groups
77# ---------------
Anthony Young20a2cae2011-10-17 16:02:24 -070078
Dean Troyerda85cda2013-02-15 11:07:14 -060079# List security groups
Anthony Young20a2cae2011-10-17 16:02:24 -070080nova secgroup-list
81
82# Create a secgroup
Dean Troyer751c1522012-01-10 15:34:34 -060083if ! 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
89fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070090
Dean Troyerda85cda2013-02-15 11:07:14 -060091# Configure Security Group Rules
92if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
93 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
94fi
95if ! nova secgroup-list-rules $SECGROUP | grep -q " tcp .* 22 "; then
96 nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
Dean Troyer1d6e0e12011-12-23 12:45:13 -060097fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070098
Dean Troyerda85cda2013-02-15 11:07:14 -060099# List secgroup rules
100nova secgroup-list-rules $SECGROUP
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700101
Dean Troyerda85cda2013-02-15 11:07:14 -0600102# Set up instance
103# ---------------
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700104
Dean Troyerda85cda2013-02-15 11:07:14 -0600105# List flavors
106nova flavor-list
Dean Troyerad101762012-06-27 22:04:40 -0500107
Dean Troyerda85cda2013-02-15 11:07:14 -0600108# Select a flavor
109INSTANCE_TYPE=$(nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1)
110if [[ -z "$INSTANCE_TYPE" ]]; then
111 # grab the first flavor in the list to launch if default doesn't exist
112 INSTANCE_TYPE=$(nova flavor-list | head -n 4 | tail -n 1 | get_field 1)
113fi
Jesse Andrews6fc71012011-10-24 11:29:08 -0700114
Dean Troyerda85cda2013-02-15 11:07:14 -0600115# Clean-up from previous runs
116nova delete $VM_NAME || true
117if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VM_NAME; do sleep 1; done"; then
118 echo "server didn't terminate!"
119 exit 1
120fi
Jesse Andrews6fc71012011-10-24 11:29:08 -0700121
Dean Troyerda85cda2013-02-15 11:07:14 -0600122# Boot instance
123# -------------
Jesse Andrews6fc71012011-10-24 11:29:08 -0700124
Dean Troyerda85cda2013-02-15 11:07:14 -0600125VM_UUID=$(nova boot --flavor $INSTANCE_TYPE --image $IMAGE --security_groups=$SECGROUP $VM_NAME | grep ' id ' | get_field 2)
126die_if_not_set VM_UUID "Failure launching $VM_NAME"
127
128# Check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -0600129if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
Jesse Andrews5a774832011-10-26 21:30:02 -0700130 echo "server didn't become active!"
131 exit 1
132fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700133
Dean Troyerda85cda2013-02-15 11:07:14 -0600134# Get the instance IP
135IP=$(nova show $VM_UUID | grep "$PRIVATE_NETWORK_NAME" | get_field 2)
Dean Troyer489bd2a2012-03-02 10:44:29 -0600136die_if_not_set IP "Failure retrieving IP address"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700137
Dean Troyerda85cda2013-02-15 11:07:14 -0600138# Private IPs can be pinged in single node deployments
Nachi Uenofda946e2012-10-24 17:26:02 -0700139ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
Jesse Andrews6fc71012011-10-24 11:29:08 -0700140
Dean Troyerda85cda2013-02-15 11:07:14 -0600141# Floating IPs
142# ------------
Jesse Andrews6fc71012011-10-24 11:29:08 -0700143
Dean Troyerda85cda2013-02-15 11:07:14 -0600144# Allocate a floating IP from the default pool
145FLOATING_IP=$(nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | get_field 1)
146die_if_not_set FLOATING_IP "Failure creating floating IP from pool $DEFAULT_FLOATING_POOL"
Anthony Young20a2cae2011-10-17 16:02:24 -0700147
Dean Troyerda85cda2013-02-15 11:07:14 -0600148# List floating addresses
Dean Troyer696ad332012-01-10 15:34:34 -0600149if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then
150 echo "Floating IP not allocated"
151 exit 1
152fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700153
Dean Troyerda85cda2013-02-15 11:07:14 -0600154# Add floating IP to our server
Dean Troyer27e32692012-03-16 16:16:56 -0500155nova add-floating-ip $VM_UUID $FLOATING_IP || \
Dean Troyerda85cda2013-02-15 11:07:14 -0600156 die "Failure adding floating IP $FLOATING_IP to $VM_NAME"
Anthony Young20a2cae2011-10-17 16:02:24 -0700157
Dean Troyerda85cda2013-02-15 11:07:14 -0600158# Test we can ping our floating IP within ASSOCIATE_TIMEOUT seconds
Nachi Uenofda946e2012-10-24 17:26:02 -0700159ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT
Anthony Young20a2cae2011-10-17 16:02:24 -0700160
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700161if ! is_service_enabled quantum; then
162 # Allocate an IP from second floating pool
Dean Troyerda85cda2013-02-15 11:07:14 -0600163 TEST_FLOATING_IP=$(nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | get_field 1)
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700164 die_if_not_set TEST_FLOATING_IP "Failure creating floating IP in $TEST_FLOATING_POOL"
Dean Troyer696ad332012-01-10 15:34:34 -0600165
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700166 # list floating addresses
167 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
168 echo "Floating IP not allocated"
169 exit 1
170 fi
Dean Troyer696ad332012-01-10 15:34:34 -0600171fi
172
Dean Troyerda85cda2013-02-15 11:07:14 -0600173# Dis-allow icmp traffic (ping)
174nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0 || \
175 die "Failure deleting security group rule from $SECGROUP"
Anthony Young20a2cae2011-10-17 16:02:24 -0700176
Anthony Young1de18c62011-11-01 14:19:18 -0500177# FIXME (anthony): make xs support security groups
Devananda van der Veenc0c6f002012-07-06 17:49:12 -0700178if [ "$VIRT_DRIVER" != "xenserver" -a "$VIRT_DRIVER" != "openvz" ]; then
Dean Troyerda85cda2013-02-15 11:07:14 -0600179 # Test we can aren't able to ping our floating ip within ASSOCIATE_TIMEOUT seconds
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700180 ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT Fail
Anthony Young20a2cae2011-10-17 16:02:24 -0700181fi
182
Dean Troyerda85cda2013-02-15 11:07:14 -0600183# Clean up
184# --------
185
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700186if ! is_service_enabled quantum; then
187 # Delete second floating IP
Dean Troyerda85cda2013-02-15 11:07:14 -0600188 nova floating-ip-delete $TEST_FLOATING_IP || \
189 die "Failure deleting floating IP $TEST_FLOATING_IP"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700190fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700191
Dean Troyerda85cda2013-02-15 11:07:14 -0600192# Delete the floating ip
193nova floating-ip-delete $FLOATING_IP || \
194 die "Failure deleting floating IP $FLOATING_IP"
Nachi Uenofda946e2012-10-24 17:26:02 -0700195
Dean Troyerda85cda2013-02-15 11:07:14 -0600196# Delete instance
197nova delete $VM_UUID || die "Failure deleting instance $VM_NAME"
Dean Troyer96288ba2012-08-17 14:11:55 -0500198# Wait for termination
199if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
Dean Troyerda85cda2013-02-15 11:07:14 -0600200 echo "Server $VM_NAME not deleted"
Russell Bryant5836b152012-02-24 10:23:33 -0500201 exit 1
202fi
203
Dean Troyerda85cda2013-02-15 11:07:14 -0600204# Delete secgroup
205nova secgroup-delete $SECGROUP || \
206 die "Failure deleting security group $SECGROUP"
Dean Troyer489bd2a2012-03-02 10:44:29 -0600207
208set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500209echo "*********************************************************************"
210echo "SUCCESS: End DevStack Exercise: $0"
211echo "*********************************************************************"