blob: f7b5240b4b8eb3ebe55fd6b1a49c597d65cab1a9 [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
Anthony Young94c889a2011-10-11 18:07:48 +000051# Get a token for clients that don't support service catalog
52# ==========================================================
Jesse Andrewsb9c77d62011-10-15 18:37:25 -070053
Vishvananda Ishaya9b353672011-10-20 10:07:10 -070054# manually create a token by querying keystone (sending JSON data). Keystone
Jesse Andrewsb9c77d62011-10-15 18:37:25 -070055# returns a token and catalog of endpoints. We use python to parse the token
56# and save it.
57
Jesse Andrews38df1222011-11-20 09:55:44 -080058TOKEN=`curl -s -d "{\"auth\":{\"passwordCredentials\": {\"username\": \"$NOVA_USERNAME\", \"password\": \"$NOVA_PASSWORD\"}}}" -H "Content-type: application/json" http://$HOST_IP:5000/v2.0/tokens | python -c "import sys; import json; tok = json.loads(sys.stdin.read()); print tok['access']['token']['id'];"`
Anthony Young94c889a2011-10-11 18:07:48 +000059
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 Troyer751c1522012-01-10 15:34:34 -060073glance -f -A $TOKEN 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
76IMAGE=`glance -f -A $TOKEN 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 Troyer1d6e0e12011-12-23 12:45:13 -060099INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2`
100if [[ -z "$INSTANCE_TYPE" ]]; then
101 # grab the first flavor in the list to launch if default doesn't exist
102 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2`
103fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700104
Anthony Young20a2cae2011-10-17 16:02:24 -0700105NAME="myserver"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700106
Dean Troyer751c1522012-01-10 15:34:34 -0600107VM_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 -0700108
Jesse Andrews6fc71012011-10-24 11:29:08 -0700109# Testing
110# =======
111
112# First check if it spins up (becomes active and responds to ping on
113# internal ip). If you run this script from a nova node, you should
114# bypass security groups and have direct access to the server.
115
116# Waiting for boot
117# ----------------
118
Anthony Young79e807a2011-10-31 11:16:44 -0700119# check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -0600120if ! 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 -0700121 echo "server didn't become active!"
122 exit 1
123fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700124
125# get the IP of the server
Dean Troyer751c1522012-01-10 15:34:34 -0600126IP=`nova show $VM_UUID | grep "private network" | cut -d"|" -f3`
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700127
Anthony Young8ecd2942011-10-24 22:58:14 -0700128# for single node deployments, we can ping private ips
129MULTI_HOST=${MULTI_HOST:-0}
Justin Shepherd56a505f2011-10-26 10:45:02 -0500130if [ "$MULTI_HOST" = "0" ]; then
Anthony Young8ecd2942011-10-24 22:58:14 -0700131 # sometimes the first ping fails (10 seconds isn't enough time for the VM's
Anthony Young79e807a2011-10-31 11:16:44 -0700132 # network to respond?), so let's ping for a default of 15 seconds with a
133 # timeout of a second for each ping.
134 if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then
Jesse Andrewsab8dbce2011-10-26 21:23:20 -0700135 echo "Couldn't ping server"
136 exit 1
137 fi
Anthony Young79e807a2011-10-31 11:16:44 -0700138else
139 # On a multi-host system, without vm net access, do a sleep to wait for the boot
140 sleep $BOOT_TIMEOUT
Anthony Young8ecd2942011-10-24 22:58:14 -0700141fi
Jesse Andrews6fc71012011-10-24 11:29:08 -0700142
143# Security Groups & Floating IPs
144# ------------------------------
145
Dean Troyer751c1522012-01-10 15:34:34 -0600146if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
147 # allow icmp traffic (ping)
148 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
149 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list-rules $SECGROUP | grep -q icmp; do sleep 1; done"; then
150 echo "Security group rule not created"
151 exit 1
152 fi
153fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700154
155# List rules for a secgroup
156nova secgroup-list-rules $SECGROUP
157
Dean Troyer696ad332012-01-10 15:34:34 -0600158# allocate a floating ip from default pool
159FLOATING_IP=`nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | cut -d '|' -f2`
Anthony Young20a2cae2011-10-17 16:02:24 -0700160
Dean Troyer696ad332012-01-10 15:34:34 -0600161# list floating addresses
162if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then
163 echo "Floating IP not allocated"
164 exit 1
165fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700166
167# add floating ip to our server
Dean Troyer751c1522012-01-10 15:34:34 -0600168nova add-floating-ip $VM_UUID $FLOATING_IP
Anthony Young20a2cae2011-10-17 16:02:24 -0700169
Anthony Young79e807a2011-10-31 11:16:44 -0700170# test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
171if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
Jesse Andrews5a774832011-10-26 21:30:02 -0700172 echo "Couldn't ping server with floating ip"
173 exit 1
174fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700175
Dean Troyer751c1522012-01-10 15:34:34 -0600176# Allocate an IP from second floating pool
Dean Troyer696ad332012-01-10 15:34:34 -0600177TEST_FLOATING_IP=`nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | cut -d '|' -f2`
178
179# list floating addresses
180if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep $TEST_FLOATING_POOL | grep -q $TEST_FLOATING_IP; do sleep 1; done"; then
181 echo "Floating IP not allocated"
182 exit 1
183fi
184
Jesse Andrews6fc71012011-10-24 11:29:08 -0700185# dis-allow icmp traffic (ping)
Anthony Young20a2cae2011-10-17 16:02:24 -0700186nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0
187
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
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700200
Dean Troyer696ad332012-01-10 15:34:34 -0600201# Delete second floating IP
202nova floating-ip-delete $TEST_FLOATING_IP
203
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700204# shutdown the server
Dean Troyer751c1522012-01-10 15:34:34 -0600205nova delete $VM_UUID
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700206
Anthony Young20a2cae2011-10-17 16:02:24 -0700207# Delete a secgroup
208nova secgroup-delete $SECGROUP
209
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700210# FIXME: validate shutdown within 5 seconds
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700211# (nova show $NAME returns 1 or status != ACTIVE)?