blob: ae5691f47ab4ab05230732fc5446ce55744fce32 [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
5# we will use the ``nova`` cli tool provided by the ``python-novaclient``
Dean Troyer27e32692012-03-16 16:16:56 -05006# package to work out the instance connectivity
Jesse Andrewsb0191512011-09-14 19:37:10 -07007
Dean Troyer27e32692012-03-16 16:16:56 -05008echo "*********************************************************************"
Dean Troyer489bd2a2012-03-02 10:44:29 -06009echo "Begin DevStack Exercise: $0"
Dean Troyer27e32692012-03-16 16:16:56 -050010echo "*********************************************************************"
Dean Troyer489bd2a2012-03-02 10:44:29 -060011
Vishvananda Ishaya9b353672011-10-20 10:07:10 -070012# This script exits on an error so that errors don't compound and you see
Jesse Andrewsb19424f2011-09-14 22:03:04 -070013# only the first error that occured.
14set -o errexit
15
Vishvananda Ishaya9b353672011-10-20 10:07:10 -070016# Print the commands being run so that we can see the command that triggers
Jesse Andrewsb19424f2011-09-14 22:03:04 -070017# an error. It is also useful for following allowing as the install occurs.
18set -o xtrace
19
20
21# Settings
22# ========
Jesse Andrewsb0191512011-09-14 19:37:10 -070023
Dean Troyer51fb4542012-03-09 22:21:59 -060024# Keep track of the current directory
25EXERCISE_DIR=$(cd $(dirname "$0") && pwd)
26TOP_DIR=$(cd $EXERCISE_DIR/..; pwd)
Dean Troyer489bd2a2012-03-02 10:44:29 -060027
28# Import common functions
Dean Troyer51fb4542012-03-09 22:21:59 -060029source $TOP_DIR/functions
Dean Troyer489bd2a2012-03-02 10:44:29 -060030
31# Import configuration
Dean Troyer51fb4542012-03-09 22:21:59 -060032source $TOP_DIR/openrc
Jesse Andrewsb0191512011-09-14 19:37:10 -070033
Nachi Ueno5db5bfa2012-10-29 11:25:29 -070034# Import quantum functions if needed
35if is_service_enabled quantum; then
36 source $TOP_DIR/lib/quantum
37 setup_quantum
38fi
39
Dean Troyer51fb4542012-03-09 22:21:59 -060040# Import exercise configuration
41source $TOP_DIR/exerciserc
Dean Troyer751c1522012-01-10 15:34:34 -060042
43# Instance type to create
44DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
45
46# Boot this image, use first AMi image if unset
47DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
48
49# Security group name
50SECGROUP=${SECGROUP:-test_secgroup}
51
52# Default floating IP pool name
Dean Troyer696ad332012-01-10 15:34:34 -060053DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova}
Dean Troyer751c1522012-01-10 15:34:34 -060054
55# Additional floating IP pool and range
Dean Troyer696ad332012-01-10 15:34:34 -060056TEST_FLOATING_POOL=${TEST_FLOATING_POOL:-test}
57
Dean Troyer27e32692012-03-16 16:16:56 -050058
Jesse Andrews593828d2011-09-14 22:44:50 -070059# Launching a server
60# ==================
Jesse Andrewsb19424f2011-09-14 22:03:04 -070061
Jesse Andrews593828d2011-09-14 22:44:50 -070062# List servers for tenant:
Jesse Andrewsb0191512011-09-14 19:37:10 -070063nova list
Jesse Andrews593828d2011-09-14 22:44:50 -070064
Jesse Andrews593828d2011-09-14 22:44:50 -070065# Images
66# ------
67
68# Nova has a **deprecated** way of listing images.
69nova image-list
70
71# But we recommend using glance directly
Dean Troyer45495252012-04-13 13:16:38 -050072glance image-list
Jesse Andrews593828d2011-09-14 22:44:50 -070073
Dean Troyer751c1522012-01-10 15:34:34 -060074# Grab the id of the image to launch
Dean Troyer45495252012-04-13 13:16:38 -050075IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070076
Anthony Young20a2cae2011-10-17 16:02:24 -070077# Security Groups
78# ---------------
Anthony Young20a2cae2011-10-17 16:02:24 -070079
80# List of secgroups:
81nova secgroup-list
82
83# Create a secgroup
Dean Troyer751c1522012-01-10 15:34:34 -060084if ! nova secgroup-list | grep -q $SECGROUP; then
85 nova secgroup-create $SECGROUP "$SECGROUP description"
86 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
87 echo "Security group not created"
88 exit 1
89 fi
90fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070091
Dean Troyerad101762012-06-27 22:04:40 -050092# Determinine instance type
Dean Troyer751c1522012-01-10 15:34:34 -060093# -------------------------
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070094
Dean Troyer751c1522012-01-10 15:34:34 -060095# List of instance types:
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070096nova flavor-list
97
Dean Troyer489bd2a2012-03-02 10:44:29 -060098INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1`
Dean Troyer1d6e0e12011-12-23 12:45:13 -060099if [[ -z "$INSTANCE_TYPE" ]]; then
100 # grab the first flavor in the list to launch if default doesn't exist
Dean Troyer489bd2a2012-03-02 10:44:29 -0600101 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | get_field 1`
Dean Troyer1d6e0e12011-12-23 12:45:13 -0600102fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700103
Dean Troyer489bd2a2012-03-02 10:44:29 -0600104NAME="ex-float"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700105
Dean Troyer489bd2a2012-03-02 10:44:29 -0600106VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2`
107die_if_not_set VM_UUID "Failure launching $NAME"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700108
Dean Troyerad101762012-06-27 22:04:40 -0500109
Jesse Andrews6fc71012011-10-24 11:29:08 -0700110# Testing
111# =======
112
113# First check if it spins up (becomes active and responds to ping on
114# internal ip). If you run this script from a nova node, you should
115# bypass security groups and have direct access to the server.
116
117# Waiting for boot
118# ----------------
119
Anthony Young79e807a2011-10-31 11:16:44 -0700120# check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -0600121if ! 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 -0700122 echo "server didn't become active!"
123 exit 1
124fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700125
126# get the IP of the server
Nachi Uenofda946e2012-10-24 17:26:02 -0700127IP=`nova show $VM_UUID | grep "$PRIVATE_NETWORK_NAME" | get_field 2`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600128die_if_not_set IP "Failure retrieving IP address"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700129
Nachi Uenofda946e2012-10-24 17:26:02 -0700130ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
Jesse Andrews6fc71012011-10-24 11:29:08 -0700131
132# Security Groups & Floating IPs
133# ------------------------------
134
Dean Troyer751c1522012-01-10 15:34:34 -0600135if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
136 # allow icmp traffic (ping)
137 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
138 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list-rules $SECGROUP | grep -q icmp; do sleep 1; done"; then
139 echo "Security group rule not created"
140 exit 1
141 fi
142fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700143
144# List rules for a secgroup
145nova secgroup-list-rules $SECGROUP
146
Dean Troyer696ad332012-01-10 15:34:34 -0600147# allocate a floating ip from default pool
Dean Troyer489bd2a2012-03-02 10:44:29 -0600148FLOATING_IP=`nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | get_field 1`
149die_if_not_set FLOATING_IP "Failure creating floating IP"
Anthony Young20a2cae2011-10-17 16:02:24 -0700150
Dean Troyer696ad332012-01-10 15:34:34 -0600151# list floating addresses
152if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then
153 echo "Floating IP not allocated"
154 exit 1
155fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700156
157# add floating ip to our server
Dean Troyer27e32692012-03-16 16:16:56 -0500158nova add-floating-ip $VM_UUID $FLOATING_IP || \
159 die "Failure adding floating IP $FLOATING_IP to $NAME"
Anthony Young20a2cae2011-10-17 16:02:24 -0700160
Anthony Young79e807a2011-10-31 11:16:44 -0700161# test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
Nachi Uenofda946e2012-10-24 17:26:02 -0700162ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT
Anthony Young20a2cae2011-10-17 16:02:24 -0700163
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700164if ! is_service_enabled quantum; then
165 # Allocate an IP from second floating pool
166 TEST_FLOATING_IP=`nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | get_field 1`
167 die_if_not_set 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
171 echo "Floating IP not allocated"
172 exit 1
173 fi
Dean Troyer696ad332012-01-10 15:34:34 -0600174fi
175
Jesse Andrews6fc71012011-10-24 11:29:08 -0700176# dis-allow icmp traffic (ping)
Dean Troyer27e32692012-03-16 16:16:56 -0500177nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0 || die "Failure deleting security group rule from $SECGROUP"
Anthony Young20a2cae2011-10-17 16:02:24 -0700178
Anthony Young1de18c62011-11-01 14:19:18 -0500179# FIXME (anthony): make xs support security groups
Devananda van der Veenc0c6f002012-07-06 17:49:12 -0700180if [ "$VIRT_DRIVER" != "xenserver" -a "$VIRT_DRIVER" != "openvz" ]; then
Anthony Young1de18c62011-11-01 14:19:18 -0500181 # test we can aren't able to ping our floating ip within ASSOCIATE_TIMEOUT seconds
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700182 ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT Fail
Anthony Young20a2cae2011-10-17 16:02:24 -0700183fi
184
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700185if ! is_service_enabled quantum; then
186 # Delete second floating IP
187 nova floating-ip-delete $TEST_FLOATING_IP || die "Failure deleting floating IP $TEST_FLOATING_IP"
188fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700189
190# de-allocate the floating ip
191nova floating-ip-delete $FLOATING_IP || die "Failure deleting floating IP $FLOATING_IP"
192
Dean Troyer96288ba2012-08-17 14:11:55 -0500193# Shutdown the server
Dean Troyer27e32692012-03-16 16:16:56 -0500194nova delete $VM_UUID || die "Failure deleting instance $NAME"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700195
Dean Troyer96288ba2012-08-17 14:11:55 -0500196# Wait for termination
197if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
198 echo "Server $NAME not deleted"
Russell Bryant5836b152012-02-24 10:23:33 -0500199 exit 1
200fi
201
Anthony Young20a2cae2011-10-17 16:02:24 -0700202# Delete a secgroup
Dean Troyer27e32692012-03-16 16:16:56 -0500203nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP"
Dean Troyer489bd2a2012-03-02 10:44:29 -0600204
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700205if is_service_enabled quantum; then
206 teardown_quantum
207fi
208
Dean Troyer489bd2a2012-03-02 10:44:29 -0600209set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500210echo "*********************************************************************"
211echo "SUCCESS: End DevStack Exercise: $0"
212echo "*********************************************************************"