blob: 82f29eb441320b67a1833a089e830103fc0e20e4 [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
Dean Troyer51fb4542012-03-09 22:21:59 -060034# Import exercise configuration
35source $TOP_DIR/exerciserc
Dean Troyer751c1522012-01-10 15:34:34 -060036
37# Instance type to create
38DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
39
40# Boot this image, use first AMi image if unset
41DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
42
43# Security group name
44SECGROUP=${SECGROUP:-test_secgroup}
45
46# Default floating IP pool name
Dean Troyer696ad332012-01-10 15:34:34 -060047DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova}
Dean Troyer751c1522012-01-10 15:34:34 -060048
49# Additional floating IP pool and range
Dean Troyer696ad332012-01-10 15:34:34 -060050TEST_FLOATING_POOL=${TEST_FLOATING_POOL:-test}
51
Dean Troyer27e32692012-03-16 16:16:56 -050052
Jesse Andrews593828d2011-09-14 22:44:50 -070053# Launching a server
54# ==================
Jesse Andrewsb19424f2011-09-14 22:03:04 -070055
Jesse Andrews593828d2011-09-14 22:44:50 -070056# List servers for tenant:
Jesse Andrewsb0191512011-09-14 19:37:10 -070057nova list
Jesse Andrews593828d2011-09-14 22:44:50 -070058
Jesse Andrews593828d2011-09-14 22:44:50 -070059# Images
60# ------
61
62# Nova has a **deprecated** way of listing images.
63nova image-list
64
65# But we recommend using glance directly
Dean Troyer45495252012-04-13 13:16:38 -050066glance image-list
Jesse Andrews593828d2011-09-14 22:44:50 -070067
Dean Troyer751c1522012-01-10 15:34:34 -060068# Grab the id of the image to launch
Dean Troyer45495252012-04-13 13:16:38 -050069IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070070
Anthony Young20a2cae2011-10-17 16:02:24 -070071# Security Groups
72# ---------------
Anthony Young20a2cae2011-10-17 16:02:24 -070073
74# List of secgroups:
75nova secgroup-list
76
77# Create a secgroup
Dean Troyer751c1522012-01-10 15:34:34 -060078if ! nova secgroup-list | grep -q $SECGROUP; then
79 nova secgroup-create $SECGROUP "$SECGROUP description"
80 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
81 echo "Security group not created"
82 exit 1
83 fi
84fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070085
Dean Troyer751c1522012-01-10 15:34:34 -060086# determinine instance type
87# -------------------------
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070088
Dean Troyer751c1522012-01-10 15:34:34 -060089# List of instance types:
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070090nova flavor-list
91
Dean Troyer489bd2a2012-03-02 10:44:29 -060092INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1`
Dean Troyer1d6e0e12011-12-23 12:45:13 -060093if [[ -z "$INSTANCE_TYPE" ]]; then
94 # grab the first flavor in the list to launch if default doesn't exist
Dean Troyer489bd2a2012-03-02 10:44:29 -060095 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | get_field 1`
Dean Troyer1d6e0e12011-12-23 12:45:13 -060096fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070097
Dean Troyer489bd2a2012-03-02 10:44:29 -060098NAME="ex-float"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070099
Dean Troyer489bd2a2012-03-02 10:44:29 -0600100VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2`
101die_if_not_set VM_UUID "Failure launching $NAME"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700102
Jesse Andrews6fc71012011-10-24 11:29:08 -0700103# Testing
104# =======
105
106# First check if it spins up (becomes active and responds to ping on
107# internal ip). If you run this script from a nova node, you should
108# bypass security groups and have direct access to the server.
109
110# Waiting for boot
111# ----------------
112
Anthony Young79e807a2011-10-31 11:16:44 -0700113# check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -0600114if ! 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 -0700115 echo "server didn't become active!"
116 exit 1
117fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700118
119# get the IP of the server
Dean Troyer489bd2a2012-03-02 10:44:29 -0600120IP=`nova show $VM_UUID | grep "private network" | get_field 2`
121die_if_not_set IP "Failure retrieving IP address"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700122
Anthony Young8ecd2942011-10-24 22:58:14 -0700123# for single node deployments, we can ping private ips
Armando Migliaccio7d13f302012-04-19 22:26:16 +0100124MULTI_HOST=`trueorfalse False $MULTI_HOST`
125if [ "$MULTI_HOST" = "False" ]; then
Anthony Young8ecd2942011-10-24 22:58:14 -0700126 # sometimes the first ping fails (10 seconds isn't enough time for the VM's
Anthony Young79e807a2011-10-31 11:16:44 -0700127 # network to respond?), so let's ping for a default of 15 seconds with a
128 # timeout of a second for each ping.
129 if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then
Jesse Andrewsab8dbce2011-10-26 21:23:20 -0700130 echo "Couldn't ping server"
131 exit 1
132 fi
Anthony Young79e807a2011-10-31 11:16:44 -0700133else
134 # On a multi-host system, without vm net access, do a sleep to wait for the boot
135 sleep $BOOT_TIMEOUT
Anthony Young8ecd2942011-10-24 22:58:14 -0700136fi
Jesse Andrews6fc71012011-10-24 11:29:08 -0700137
138# Security Groups & Floating IPs
139# ------------------------------
140
Dean Troyer751c1522012-01-10 15:34:34 -0600141if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
142 # allow icmp traffic (ping)
143 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
144 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list-rules $SECGROUP | grep -q icmp; do sleep 1; done"; then
145 echo "Security group rule not created"
146 exit 1
147 fi
148fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700149
150# List rules for a secgroup
151nova secgroup-list-rules $SECGROUP
152
Dean Troyer696ad332012-01-10 15:34:34 -0600153# allocate a floating ip from default pool
Dean Troyer489bd2a2012-03-02 10:44:29 -0600154FLOATING_IP=`nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | get_field 1`
155die_if_not_set FLOATING_IP "Failure creating floating IP"
Anthony Young20a2cae2011-10-17 16:02:24 -0700156
Dean Troyer696ad332012-01-10 15:34:34 -0600157# list floating addresses
158if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then
159 echo "Floating IP not allocated"
160 exit 1
161fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700162
163# add floating ip to our server
Dean Troyer27e32692012-03-16 16:16:56 -0500164nova add-floating-ip $VM_UUID $FLOATING_IP || \
165 die "Failure adding floating IP $FLOATING_IP to $NAME"
Anthony Young20a2cae2011-10-17 16:02:24 -0700166
Anthony Young79e807a2011-10-31 11:16:44 -0700167# test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
168if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
Jesse Andrews5a774832011-10-26 21:30:02 -0700169 echo "Couldn't ping server with floating ip"
170 exit 1
171fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700172
Dean Troyer751c1522012-01-10 15:34:34 -0600173# Allocate an IP from second floating pool
Dean Troyer489bd2a2012-03-02 10:44:29 -0600174TEST_FLOATING_IP=`nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | get_field 1`
175die_if_not_set TEST_FLOATING_IP "Failure creating floating IP in $TEST_FLOATING_POOL"
Dean Troyer696ad332012-01-10 15:34:34 -0600176
177# list floating addresses
178if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep $TEST_FLOATING_POOL | grep -q $TEST_FLOATING_IP; do sleep 1; done"; then
179 echo "Floating IP not allocated"
180 exit 1
181fi
182
Jesse Andrews6fc71012011-10-24 11:29:08 -0700183# dis-allow icmp traffic (ping)
Dean Troyer27e32692012-03-16 16:16:56 -0500184nova 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 -0700185
Anthony Young1de18c62011-11-01 14:19:18 -0500186# FIXME (anthony): make xs support security groups
Jesse Andrews16b6efa2011-11-10 11:46:18 -0800187if [ "$VIRT_DRIVER" != "xenserver" ]; then
Anthony Young1de18c62011-11-01 14:19:18 -0500188 # test we can aren't able to ping our floating ip within ASSOCIATE_TIMEOUT seconds
189 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
190 print "Security group failure - ping should not be allowed!"
191 echo "Couldn't ping server with floating ip"
192 exit 1
193 fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700194fi
195
196# de-allocate the floating ip
Dean Troyer27e32692012-03-16 16:16:56 -0500197nova floating-ip-delete $FLOATING_IP || die "Failure deleting floating IP $FLOATING_IP"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700198
Dean Troyer696ad332012-01-10 15:34:34 -0600199# Delete second floating IP
Dean Troyer27e32692012-03-16 16:16:56 -0500200nova floating-ip-delete $TEST_FLOATING_IP || die "Failure deleting floating IP $TEST_FLOATING_IP"
Dean Troyer696ad332012-01-10 15:34:34 -0600201
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700202# shutdown the server
Dean Troyer27e32692012-03-16 16:16:56 -0500203nova delete $VM_UUID || die "Failure deleting instance $NAME"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700204
Russell Bryant5836b152012-02-24 10:23:33 -0500205# make sure the VM shuts down within a reasonable time
206if ! timeout $TERMINATE_TIMEOUT sh -c "while nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
207 echo "server didn't shut down!"
208 exit 1
209fi
210
Anthony Young20a2cae2011-10-17 16:02:24 -0700211# Delete a secgroup
Dean Troyer27e32692012-03-16 16:16:56 -0500212nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP"
Dean Troyer489bd2a2012-03-02 10:44:29 -0600213
214set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500215echo "*********************************************************************"
216echo "SUCCESS: End DevStack Exercise: $0"
217echo "*********************************************************************"