blob: a47f1ffc232eaa19a8a66c74c7581a4cf68ce5a6 [file] [log] [blame]
Jesse Andrewsb0191512011-09-14 19:37:10 -07001#!/usr/bin/env bash
2
3# **exercise.sh** - using the cloud can be fun
4
5# we will use the ``nova`` cli tool provided by the ``python-novaclient``
6# package
Jesse Andrewsb19424f2011-09-14 22:03:04 -07007#
Jesse Andrewsb0191512011-09-14 19:37:10 -07008
Jesse Andrewsb19424f2011-09-14 22:03:04 -07009
Dean Troyer489bd2a2012-03-02 10:44:29 -060010echo "**************************************************"
11echo "Begin DevStack Exercise: $0"
12echo "**************************************************"
13
Vishvananda Ishaya9b353672011-10-20 10:07:10 -070014# This script exits on an error so that errors don't compound and you see
Jesse Andrewsb19424f2011-09-14 22:03:04 -070015# only the first error that occured.
16set -o errexit
17
Vishvananda Ishaya9b353672011-10-20 10:07:10 -070018# Print the commands being run so that we can see the command that triggers
Jesse Andrewsb19424f2011-09-14 22:03:04 -070019# an error. It is also useful for following allowing as the install occurs.
20set -o xtrace
21
22
23# Settings
24# ========
Jesse Andrewsb0191512011-09-14 19:37:10 -070025
Anthony Young6ab10d42011-10-20 10:24:50 -070026# Use openrc + stackrc + localrc for settings
Dean Troyer489bd2a2012-03-02 10:44:29 -060027pushd $(cd $(dirname "$0")/.. && pwd) >/dev/null
28
29# Import common functions
30source ./functions
31
32# Import configuration
Anthony Young6ab10d42011-10-20 10:24:50 -070033source ./openrc
Dean Troyer489bd2a2012-03-02 10:44:29 -060034popd >/dev/null
Jesse Andrewsb0191512011-09-14 19:37:10 -070035
Dean Troyer751c1522012-01-10 15:34:34 -060036# Max time to wait while vm goes from build to active state
37ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-30}
Dean Troyer696ad332012-01-10 15:34:34 -060038
Dean Troyer751c1522012-01-10 15:34:34 -060039# Max time till the vm is bootable
40BOOT_TIMEOUT=${BOOT_TIMEOUT:-30}
41
42# Max time to wait for proper association and dis-association.
43ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-15}
44
45# Instance type to create
46DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
47
48# Boot this image, use first AMi image if unset
49DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
50
51# Security group name
52SECGROUP=${SECGROUP:-test_secgroup}
53
54# Default floating IP pool name
Dean Troyer696ad332012-01-10 15:34:34 -060055DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova}
Dean Troyer751c1522012-01-10 15:34:34 -060056
57# Additional floating IP pool and range
Dean Troyer696ad332012-01-10 15:34:34 -060058TEST_FLOATING_POOL=${TEST_FLOATING_POOL:-test}
59
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
69# Nova has a **deprecated** way of listing images.
70nova image-list
71
72# But we recommend using glance directly
Dean Troyer80756ea2012-02-01 18:01:01 -060073glance -f index
Jesse Andrews593828d2011-09-14 22:44:50 -070074
Dean Troyer751c1522012-01-10 15:34:34 -060075# Grab the id of the image to launch
Dean Troyer80756ea2012-02-01 18:01:01 -060076IMAGE=`glance -f index | egrep $DEFAULT_IMAGE_NAME | head -1 | cut -d" " -f1`
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070077
Anthony Young20a2cae2011-10-17 16:02:24 -070078# Security Groups
79# ---------------
Anthony Young20a2cae2011-10-17 16:02:24 -070080
81# List of secgroups:
82nova secgroup-list
83
84# Create a secgroup
Dean Troyer751c1522012-01-10 15:34:34 -060085if ! 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
88 echo "Security group not created"
89 exit 1
90 fi
91fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070092
Dean Troyer751c1522012-01-10 15:34:34 -060093# determinine instance type
94# -------------------------
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070095
Dean Troyer751c1522012-01-10 15:34:34 -060096# List of instance types:
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070097nova flavor-list
98
Dean Troyer489bd2a2012-03-02 10:44:29 -060099INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1`
Dean Troyer1d6e0e12011-12-23 12:45:13 -0600100if [[ -z "$INSTANCE_TYPE" ]]; then
101 # grab the first flavor in the list to launch if default doesn't exist
Dean Troyer489bd2a2012-03-02 10:44:29 -0600102 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | get_field 1`
Dean Troyer1d6e0e12011-12-23 12:45:13 -0600103fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700104
Dean Troyer489bd2a2012-03-02 10:44:29 -0600105NAME="ex-float"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700106
Dean Troyer489bd2a2012-03-02 10:44:29 -0600107VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2`
108die_if_not_set VM_UUID "Failure launching $NAME"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700109
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
Dean Troyer489bd2a2012-03-02 10:44:29 -0600127IP=`nova show $VM_UUID | grep "private network" | get_field 2`
128die_if_not_set IP "Failure retrieving IP address"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700129
Anthony Young8ecd2942011-10-24 22:58:14 -0700130# for single node deployments, we can ping private ips
131MULTI_HOST=${MULTI_HOST:-0}
Justin Shepherd56a505f2011-10-26 10:45:02 -0500132if [ "$MULTI_HOST" = "0" ]; then
Anthony Young8ecd2942011-10-24 22:58:14 -0700133 # sometimes the first ping fails (10 seconds isn't enough time for the VM's
Anthony Young79e807a2011-10-31 11:16:44 -0700134 # network to respond?), so let's ping for a default of 15 seconds with a
135 # timeout of a second for each ping.
136 if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then
Jesse Andrewsab8dbce2011-10-26 21:23:20 -0700137 echo "Couldn't ping server"
138 exit 1
139 fi
Anthony Young79e807a2011-10-31 11:16:44 -0700140else
141 # On a multi-host system, without vm net access, do a sleep to wait for the boot
142 sleep $BOOT_TIMEOUT
Anthony Young8ecd2942011-10-24 22:58:14 -0700143fi
Jesse Andrews6fc71012011-10-24 11:29:08 -0700144
145# Security Groups & Floating IPs
146# ------------------------------
147
Dean Troyer751c1522012-01-10 15:34:34 -0600148if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
149 # allow icmp traffic (ping)
150 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
151 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list-rules $SECGROUP | grep -q icmp; do sleep 1; done"; then
152 echo "Security group rule not created"
153 exit 1
154 fi
155fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700156
157# List rules for a secgroup
158nova secgroup-list-rules $SECGROUP
159
Dean Troyer696ad332012-01-10 15:34:34 -0600160# allocate a floating ip from default pool
Dean Troyer489bd2a2012-03-02 10:44:29 -0600161FLOATING_IP=`nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | get_field 1`
162die_if_not_set FLOATING_IP "Failure creating floating IP"
Anthony Young20a2cae2011-10-17 16:02:24 -0700163
Dean Troyer696ad332012-01-10 15:34:34 -0600164# list floating addresses
165if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then
166 echo "Floating IP not allocated"
167 exit 1
168fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700169
170# add floating ip to our server
Dean Troyer751c1522012-01-10 15:34:34 -0600171nova add-floating-ip $VM_UUID $FLOATING_IP
Dean Troyer489bd2a2012-03-02 10:44:29 -0600172die_if_error "Failure adding floating IP $FLOATING_IP to $NAME"
Anthony Young20a2cae2011-10-17 16:02:24 -0700173
Anthony Young79e807a2011-10-31 11:16:44 -0700174# test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
175if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
Jesse Andrews5a774832011-10-26 21:30:02 -0700176 echo "Couldn't ping server with floating ip"
177 exit 1
178fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700179
Dean Troyer751c1522012-01-10 15:34:34 -0600180# Allocate an IP from second floating pool
Dean Troyer489bd2a2012-03-02 10:44:29 -0600181TEST_FLOATING_IP=`nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | get_field 1`
182die_if_not_set TEST_FLOATING_IP "Failure creating floating IP in $TEST_FLOATING_POOL"
Dean Troyer696ad332012-01-10 15:34:34 -0600183
184# list floating addresses
185if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep $TEST_FLOATING_POOL | grep -q $TEST_FLOATING_IP; do sleep 1; done"; then
186 echo "Floating IP not allocated"
187 exit 1
188fi
189
Jesse Andrews6fc71012011-10-24 11:29:08 -0700190# dis-allow icmp traffic (ping)
Anthony Young20a2cae2011-10-17 16:02:24 -0700191nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0
Dean Troyer489bd2a2012-03-02 10:44:29 -0600192die_if_error "Failure deleting security group rule from $SECGROUP"
Anthony Young20a2cae2011-10-17 16:02:24 -0700193
Anthony Young1de18c62011-11-01 14:19:18 -0500194# FIXME (anthony): make xs support security groups
Jesse Andrews16b6efa2011-11-10 11:46:18 -0800195if [ "$VIRT_DRIVER" != "xenserver" ]; then
Anthony Young1de18c62011-11-01 14:19:18 -0500196 # test we can aren't able to ping our floating ip within ASSOCIATE_TIMEOUT seconds
197 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
198 print "Security group failure - ping should not be allowed!"
199 echo "Couldn't ping server with floating ip"
200 exit 1
201 fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700202fi
203
204# de-allocate the floating ip
Jesse Andrews6fc71012011-10-24 11:29:08 -0700205nova floating-ip-delete $FLOATING_IP
Dean Troyer489bd2a2012-03-02 10:44:29 -0600206die_if_error "Failure deleting floating IP $FLOATING_IP"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700207
Dean Troyer696ad332012-01-10 15:34:34 -0600208# Delete second floating IP
209nova floating-ip-delete $TEST_FLOATING_IP
Dean Troyer489bd2a2012-03-02 10:44:29 -0600210die_if_error "Failure deleting floating IP $TEST_FLOATING_IP"
Dean Troyer696ad332012-01-10 15:34:34 -0600211
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700212# shutdown the server
Dean Troyer751c1522012-01-10 15:34:34 -0600213nova delete $VM_UUID
Dean Troyer489bd2a2012-03-02 10:44:29 -0600214die_if_error "Failure deleting instance $NAME"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700215
Russell Bryant5836b152012-02-24 10:23:33 -0500216# make sure the VM shuts down within a reasonable time
217if ! timeout $TERMINATE_TIMEOUT sh -c "while nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
218 echo "server didn't shut down!"
219 exit 1
220fi
221
Anthony Young20a2cae2011-10-17 16:02:24 -0700222# Delete a secgroup
223nova secgroup-delete $SECGROUP
Dean Troyer489bd2a2012-03-02 10:44:29 -0600224die_if_error "Failure deleting security group $SECGROUP"
225
226set +o xtrace
227echo "**************************************************"
228echo "End DevStack Exercise: $0"
229echo "**************************************************"