blob: b559965fc90fffc334b5540f0f64d4400022ca1e [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
Vishvananda Ishaya9b353672011-10-20 10:07:10 -070010# This script exits on an error so that errors don't compound and you see
Jesse Andrewsb19424f2011-09-14 22:03:04 -070011# only the first error that occured.
12set -o errexit
13
Vishvananda Ishaya9b353672011-10-20 10:07:10 -070014# Print the commands being run so that we can see the command that triggers
Jesse Andrewsb19424f2011-09-14 22:03:04 -070015# an error. It is also useful for following allowing as the install occurs.
16set -o xtrace
17
18
19# Settings
20# ========
Jesse Andrewsb0191512011-09-14 19:37:10 -070021
Anthony Young6ab10d42011-10-20 10:24:50 -070022# Use openrc + stackrc + localrc for settings
Jesse Andrews787af012011-11-01 16:44:19 -070023pushd $(cd $(dirname "$0")/.. && pwd)
Anthony Young6ab10d42011-10-20 10:24:50 -070024source ./openrc
Jesse Andrews787af012011-11-01 16:44:19 -070025popd
Jesse Andrewsb0191512011-09-14 19:37:10 -070026
Dean Troyer751c1522012-01-10 15:34:34 -060027# Max time to wait while vm goes from build to active state
28ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-30}
Dean Troyer696ad332012-01-10 15:34:34 -060029
Dean Troyer751c1522012-01-10 15:34:34 -060030# Max time till the vm is bootable
31BOOT_TIMEOUT=${BOOT_TIMEOUT:-30}
32
33# Max time to wait for proper association and dis-association.
34ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-15}
35
36# Instance type to create
37DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
38
39# Boot this image, use first AMi image if unset
40DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
41
42# Security group name
43SECGROUP=${SECGROUP:-test_secgroup}
44
45# Default floating IP pool name
Dean Troyer696ad332012-01-10 15:34:34 -060046DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova}
Dean Troyer751c1522012-01-10 15:34:34 -060047
48# Additional floating IP pool and range
Dean Troyer696ad332012-01-10 15:34:34 -060049TEST_FLOATING_POOL=${TEST_FLOATING_POOL:-test}
50
Jesse Andrews593828d2011-09-14 22:44:50 -070051# Launching a server
52# ==================
Jesse Andrewsb19424f2011-09-14 22:03:04 -070053
Jesse Andrews593828d2011-09-14 22:44:50 -070054# List servers for tenant:
Jesse Andrewsb0191512011-09-14 19:37:10 -070055nova list
Jesse Andrews593828d2011-09-14 22:44:50 -070056
Jesse Andrews593828d2011-09-14 22:44:50 -070057# Images
58# ------
59
60# Nova has a **deprecated** way of listing images.
61nova image-list
62
63# But we recommend using glance directly
Dean Troyer80756ea2012-02-01 18:01:01 -060064glance -f index
Jesse Andrews593828d2011-09-14 22:44:50 -070065
Dean Troyer751c1522012-01-10 15:34:34 -060066# Grab the id of the image to launch
Dean Troyer80756ea2012-02-01 18:01:01 -060067IMAGE=`glance -f index | egrep $DEFAULT_IMAGE_NAME | head -1 | cut -d" " -f1`
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070068
Anthony Young20a2cae2011-10-17 16:02:24 -070069# Security Groups
70# ---------------
Anthony Young20a2cae2011-10-17 16:02:24 -070071
72# List of secgroups:
73nova secgroup-list
74
75# Create a secgroup
Dean Troyer751c1522012-01-10 15:34:34 -060076if ! nova secgroup-list | grep -q $SECGROUP; then
77 nova secgroup-create $SECGROUP "$SECGROUP description"
78 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
79 echo "Security group not created"
80 exit 1
81 fi
82fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070083
Dean Troyer751c1522012-01-10 15:34:34 -060084# determinine instance type
85# -------------------------
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070086
Dean Troyer751c1522012-01-10 15:34:34 -060087# List of instance types:
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070088nova flavor-list
89
Dean Troyer1d6e0e12011-12-23 12:45:13 -060090INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2`
91if [[ -z "$INSTANCE_TYPE" ]]; then
92 # grab the first flavor in the list to launch if default doesn't exist
93 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2`
94fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070095
Anthony Young20a2cae2011-10-17 16:02:24 -070096NAME="myserver"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070097
Dean Troyer751c1522012-01-10 15:34:34 -060098VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'`
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070099
Jesse Andrews6fc71012011-10-24 11:29:08 -0700100# Testing
101# =======
102
103# First check if it spins up (becomes active and responds to ping on
104# internal ip). If you run this script from a nova node, you should
105# bypass security groups and have direct access to the server.
106
107# Waiting for boot
108# ----------------
109
Anthony Young79e807a2011-10-31 11:16:44 -0700110# check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -0600111if ! 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 -0700112 echo "server didn't become active!"
113 exit 1
114fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700115
116# get the IP of the server
Dean Troyer751c1522012-01-10 15:34:34 -0600117IP=`nova show $VM_UUID | grep "private network" | cut -d"|" -f3`
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700118
Anthony Young8ecd2942011-10-24 22:58:14 -0700119# for single node deployments, we can ping private ips
120MULTI_HOST=${MULTI_HOST:-0}
Justin Shepherd56a505f2011-10-26 10:45:02 -0500121if [ "$MULTI_HOST" = "0" ]; then
Anthony Young8ecd2942011-10-24 22:58:14 -0700122 # sometimes the first ping fails (10 seconds isn't enough time for the VM's
Anthony Young79e807a2011-10-31 11:16:44 -0700123 # network to respond?), so let's ping for a default of 15 seconds with a
124 # timeout of a second for each ping.
125 if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then
Jesse Andrewsab8dbce2011-10-26 21:23:20 -0700126 echo "Couldn't ping server"
127 exit 1
128 fi
Anthony Young79e807a2011-10-31 11:16:44 -0700129else
130 # On a multi-host system, without vm net access, do a sleep to wait for the boot
131 sleep $BOOT_TIMEOUT
Anthony Young8ecd2942011-10-24 22:58:14 -0700132fi
Jesse Andrews6fc71012011-10-24 11:29:08 -0700133
134# Security Groups & Floating IPs
135# ------------------------------
136
Dean Troyer751c1522012-01-10 15:34:34 -0600137if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
138 # allow icmp traffic (ping)
139 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
140 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list-rules $SECGROUP | grep -q icmp; do sleep 1; done"; then
141 echo "Security group rule not created"
142 exit 1
143 fi
144fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700145
146# List rules for a secgroup
147nova secgroup-list-rules $SECGROUP
148
Dean Troyer696ad332012-01-10 15:34:34 -0600149# allocate a floating ip from default pool
150FLOATING_IP=`nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | cut -d '|' -f2`
Anthony Young20a2cae2011-10-17 16:02:24 -0700151
Dean Troyer696ad332012-01-10 15:34:34 -0600152# list floating addresses
153if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then
154 echo "Floating IP not allocated"
155 exit 1
156fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700157
158# add floating ip to our server
Dean Troyer751c1522012-01-10 15:34:34 -0600159nova add-floating-ip $VM_UUID $FLOATING_IP
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
162if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
Jesse Andrews5a774832011-10-26 21:30:02 -0700163 echo "Couldn't ping server with floating ip"
164 exit 1
165fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700166
Dean Troyer751c1522012-01-10 15:34:34 -0600167# Allocate an IP from second floating pool
Dean Troyer696ad332012-01-10 15:34:34 -0600168TEST_FLOATING_IP=`nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | cut -d '|' -f2`
169
170# list floating addresses
171if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep $TEST_FLOATING_POOL | grep -q $TEST_FLOATING_IP; do sleep 1; done"; then
172 echo "Floating IP not allocated"
173 exit 1
174fi
175
Jesse Andrews6fc71012011-10-24 11:29:08 -0700176# dis-allow icmp traffic (ping)
Anthony Young20a2cae2011-10-17 16:02:24 -0700177nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0
178
Anthony Young1de18c62011-11-01 14:19:18 -0500179# FIXME (anthony): make xs support security groups
Jesse Andrews16b6efa2011-11-10 11:46:18 -0800180if [ "$VIRT_DRIVER" != "xenserver" ]; then
Anthony Young1de18c62011-11-01 14:19:18 -0500181 # test we can aren't able to ping our floating ip within ASSOCIATE_TIMEOUT seconds
182 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
183 print "Security group failure - ping should not be allowed!"
184 echo "Couldn't ping server with floating ip"
185 exit 1
186 fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700187fi
188
189# de-allocate the floating ip
Jesse Andrews6fc71012011-10-24 11:29:08 -0700190nova floating-ip-delete $FLOATING_IP
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700191
Dean Troyer696ad332012-01-10 15:34:34 -0600192# Delete second floating IP
193nova floating-ip-delete $TEST_FLOATING_IP
194
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700195# shutdown the server
Dean Troyer751c1522012-01-10 15:34:34 -0600196nova delete $VM_UUID
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700197
Russell Bryant5836b152012-02-24 10:23:33 -0500198# make sure the VM shuts down within a reasonable time
199if ! timeout $TERMINATE_TIMEOUT sh -c "while nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
200 echo "server didn't shut down!"
201 exit 1
202fi
203
Anthony Young20a2cae2011-10-17 16:02:24 -0700204# Delete a secgroup
205nova secgroup-delete $SECGROUP