blob: f2b9d0362f21caaf933b79dd3d41e54bdde0f422 [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
Dean Troyer51fb4542012-03-09 22:21:59 -060026# Keep track of the current directory
27EXERCISE_DIR=$(cd $(dirname "$0") && pwd)
28TOP_DIR=$(cd $EXERCISE_DIR/..; pwd)
Dean Troyer489bd2a2012-03-02 10:44:29 -060029
30# Import common functions
Dean Troyer51fb4542012-03-09 22:21:59 -060031source $TOP_DIR/functions
Dean Troyer489bd2a2012-03-02 10:44:29 -060032
33# Import configuration
Dean Troyer51fb4542012-03-09 22:21:59 -060034source $TOP_DIR/openrc
Jesse Andrewsb0191512011-09-14 19:37:10 -070035
Dean Troyer51fb4542012-03-09 22:21:59 -060036# Import exercise configuration
37source $TOP_DIR/exerciserc
Dean Troyer751c1522012-01-10 15:34:34 -060038
39# Instance type to create
40DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
41
42# Boot this image, use first AMi image if unset
43DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
44
45# Security group name
46SECGROUP=${SECGROUP:-test_secgroup}
47
48# Default floating IP pool name
Dean Troyer696ad332012-01-10 15:34:34 -060049DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova}
Dean Troyer751c1522012-01-10 15:34:34 -060050
51# Additional floating IP pool and range
Dean Troyer696ad332012-01-10 15:34:34 -060052TEST_FLOATING_POOL=${TEST_FLOATING_POOL:-test}
53
Jesse Andrews593828d2011-09-14 22:44:50 -070054# Launching a server
55# ==================
Jesse Andrewsb19424f2011-09-14 22:03:04 -070056
Jesse Andrews593828d2011-09-14 22:44:50 -070057# List servers for tenant:
Jesse Andrewsb0191512011-09-14 19:37:10 -070058nova list
Jesse Andrews593828d2011-09-14 22:44:50 -070059
Jesse Andrews593828d2011-09-14 22:44:50 -070060# Images
61# ------
62
63# Nova has a **deprecated** way of listing images.
64nova image-list
65
66# But we recommend using glance directly
Dean Troyer80756ea2012-02-01 18:01:01 -060067glance -f index
Jesse Andrews593828d2011-09-14 22:44:50 -070068
Dean Troyer751c1522012-01-10 15:34:34 -060069# Grab the id of the image to launch
Dean Troyer80756ea2012-02-01 18:01:01 -060070IMAGE=`glance -f index | egrep $DEFAULT_IMAGE_NAME | head -1 | cut -d" " -f1`
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070071
Anthony Young20a2cae2011-10-17 16:02:24 -070072# Security Groups
73# ---------------
Anthony Young20a2cae2011-10-17 16:02:24 -070074
75# List of secgroups:
76nova secgroup-list
77
78# Create a secgroup
Dean Troyer751c1522012-01-10 15:34:34 -060079if ! nova secgroup-list | grep -q $SECGROUP; then
80 nova secgroup-create $SECGROUP "$SECGROUP description"
81 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
82 echo "Security group not created"
83 exit 1
84 fi
85fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070086
Dean Troyer751c1522012-01-10 15:34:34 -060087# determinine instance type
88# -------------------------
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070089
Dean Troyer751c1522012-01-10 15:34:34 -060090# List of instance types:
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070091nova flavor-list
92
Dean Troyer489bd2a2012-03-02 10:44:29 -060093INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1`
Dean Troyer1d6e0e12011-12-23 12:45:13 -060094if [[ -z "$INSTANCE_TYPE" ]]; then
95 # grab the first flavor in the list to launch if default doesn't exist
Dean Troyer489bd2a2012-03-02 10:44:29 -060096 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | get_field 1`
Dean Troyer1d6e0e12011-12-23 12:45:13 -060097fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070098
Dean Troyer489bd2a2012-03-02 10:44:29 -060099NAME="ex-float"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700100
Dean Troyer489bd2a2012-03-02 10:44:29 -0600101VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2`
102die_if_not_set VM_UUID "Failure launching $NAME"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700103
Jesse Andrews6fc71012011-10-24 11:29:08 -0700104# Testing
105# =======
106
107# First check if it spins up (becomes active and responds to ping on
108# internal ip). If you run this script from a nova node, you should
109# bypass security groups and have direct access to the server.
110
111# Waiting for boot
112# ----------------
113
Anthony Young79e807a2011-10-31 11:16:44 -0700114# check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -0600115if ! 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 -0700116 echo "server didn't become active!"
117 exit 1
118fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700119
120# get the IP of the server
Dean Troyer489bd2a2012-03-02 10:44:29 -0600121IP=`nova show $VM_UUID | grep "private network" | get_field 2`
122die_if_not_set IP "Failure retrieving IP address"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700123
Anthony Young8ecd2942011-10-24 22:58:14 -0700124# for single node deployments, we can ping private ips
125MULTI_HOST=${MULTI_HOST:-0}
Justin Shepherd56a505f2011-10-26 10:45:02 -0500126if [ "$MULTI_HOST" = "0" ]; then
Anthony Young8ecd2942011-10-24 22:58:14 -0700127 # sometimes the first ping fails (10 seconds isn't enough time for the VM's
Anthony Young79e807a2011-10-31 11:16:44 -0700128 # network to respond?), so let's ping for a default of 15 seconds with a
129 # timeout of a second for each ping.
130 if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then
Jesse Andrewsab8dbce2011-10-26 21:23:20 -0700131 echo "Couldn't ping server"
132 exit 1
133 fi
Anthony Young79e807a2011-10-31 11:16:44 -0700134else
135 # On a multi-host system, without vm net access, do a sleep to wait for the boot
136 sleep $BOOT_TIMEOUT
Anthony Young8ecd2942011-10-24 22:58:14 -0700137fi
Jesse Andrews6fc71012011-10-24 11:29:08 -0700138
139# Security Groups & Floating IPs
140# ------------------------------
141
Dean Troyer751c1522012-01-10 15:34:34 -0600142if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
143 # allow icmp traffic (ping)
144 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
145 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list-rules $SECGROUP | grep -q icmp; do sleep 1; done"; then
146 echo "Security group rule not created"
147 exit 1
148 fi
149fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700150
151# List rules for a secgroup
152nova secgroup-list-rules $SECGROUP
153
Dean Troyer696ad332012-01-10 15:34:34 -0600154# allocate a floating ip from default pool
Dean Troyer489bd2a2012-03-02 10:44:29 -0600155FLOATING_IP=`nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | get_field 1`
156die_if_not_set FLOATING_IP "Failure creating floating IP"
Anthony Young20a2cae2011-10-17 16:02:24 -0700157
Dean Troyer696ad332012-01-10 15:34:34 -0600158# list floating addresses
159if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then
160 echo "Floating IP not allocated"
161 exit 1
162fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700163
164# add floating ip to our server
Dean Troyer751c1522012-01-10 15:34:34 -0600165nova add-floating-ip $VM_UUID $FLOATING_IP
Dean Troyer489bd2a2012-03-02 10:44:29 -0600166die_if_error "Failure adding floating IP $FLOATING_IP to $NAME"
Anthony Young20a2cae2011-10-17 16:02:24 -0700167
Anthony Young79e807a2011-10-31 11:16:44 -0700168# test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
169if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
Jesse Andrews5a774832011-10-26 21:30:02 -0700170 echo "Couldn't ping server with floating ip"
171 exit 1
172fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700173
Dean Troyer751c1522012-01-10 15:34:34 -0600174# Allocate an IP from second floating pool
Dean Troyer489bd2a2012-03-02 10:44:29 -0600175TEST_FLOATING_IP=`nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | get_field 1`
176die_if_not_set TEST_FLOATING_IP "Failure creating floating IP in $TEST_FLOATING_POOL"
Dean Troyer696ad332012-01-10 15:34:34 -0600177
178# list floating addresses
179if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep $TEST_FLOATING_POOL | grep -q $TEST_FLOATING_IP; do sleep 1; done"; then
180 echo "Floating IP not allocated"
181 exit 1
182fi
183
Jesse Andrews6fc71012011-10-24 11:29:08 -0700184# dis-allow icmp traffic (ping)
Anthony Young20a2cae2011-10-17 16:02:24 -0700185nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0
Dean Troyer489bd2a2012-03-02 10:44:29 -0600186die_if_error "Failure deleting security group rule from $SECGROUP"
Anthony Young20a2cae2011-10-17 16:02:24 -0700187
Anthony Young1de18c62011-11-01 14:19:18 -0500188# FIXME (anthony): make xs support security groups
Jesse Andrews16b6efa2011-11-10 11:46:18 -0800189if [ "$VIRT_DRIVER" != "xenserver" ]; then
Anthony Young1de18c62011-11-01 14:19:18 -0500190 # test we can aren't able to ping our floating ip within ASSOCIATE_TIMEOUT seconds
191 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
192 print "Security group failure - ping should not be allowed!"
193 echo "Couldn't ping server with floating ip"
194 exit 1
195 fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700196fi
197
198# de-allocate the floating ip
Jesse Andrews6fc71012011-10-24 11:29:08 -0700199nova floating-ip-delete $FLOATING_IP
Dean Troyer489bd2a2012-03-02 10:44:29 -0600200die_if_error "Failure deleting floating IP $FLOATING_IP"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700201
Dean Troyer696ad332012-01-10 15:34:34 -0600202# Delete second floating IP
203nova floating-ip-delete $TEST_FLOATING_IP
Dean Troyer489bd2a2012-03-02 10:44:29 -0600204die_if_error "Failure deleting floating IP $TEST_FLOATING_IP"
Dean Troyer696ad332012-01-10 15:34:34 -0600205
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700206# shutdown the server
Dean Troyer751c1522012-01-10 15:34:34 -0600207nova delete $VM_UUID
Dean Troyer489bd2a2012-03-02 10:44:29 -0600208die_if_error "Failure deleting instance $NAME"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700209
Russell Bryant5836b152012-02-24 10:23:33 -0500210# make sure the VM shuts down within a reasonable time
211if ! timeout $TERMINATE_TIMEOUT sh -c "while nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
212 echo "server didn't shut down!"
213 exit 1
214fi
215
Anthony Young20a2cae2011-10-17 16:02:24 -0700216# Delete a secgroup
217nova secgroup-delete $SECGROUP
Dean Troyer489bd2a2012-03-02 10:44:29 -0600218die_if_error "Failure deleting security group $SECGROUP"
219
220set +o xtrace
221echo "**************************************************"
222echo "End DevStack Exercise: $0"
223echo "**************************************************"