blob: 9c207cd8dc5d14ea129c7602856f202ffe364e29 [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 Troyer696ad332012-01-10 15:34:34 -060027# Set some defaults
28
29DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova}
30TEST_FLOATING_POOL=${TEST_FLOATING_POOL:-test}
31
Anthony Young94c889a2011-10-11 18:07:48 +000032# Get a token for clients that don't support service catalog
33# ==========================================================
Jesse Andrewsb9c77d62011-10-15 18:37:25 -070034
Vishvananda Ishaya9b353672011-10-20 10:07:10 -070035# manually create a token by querying keystone (sending JSON data). Keystone
Jesse Andrewsb9c77d62011-10-15 18:37:25 -070036# returns a token and catalog of endpoints. We use python to parse the token
37# and save it.
38
Jesse Andrews38df1222011-11-20 09:55:44 -080039TOKEN=`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 +000040
Jesse Andrews593828d2011-09-14 22:44:50 -070041# Launching a server
42# ==================
Jesse Andrewsb19424f2011-09-14 22:03:04 -070043
Jesse Andrews593828d2011-09-14 22:44:50 -070044# List servers for tenant:
Jesse Andrewsb0191512011-09-14 19:37:10 -070045nova list
Jesse Andrews593828d2011-09-14 22:44:50 -070046
Jesse Andrews593828d2011-09-14 22:44:50 -070047# Images
48# ------
49
50# Nova has a **deprecated** way of listing images.
51nova image-list
52
53# But we recommend using glance directly
Jesse Andrews4e8847c2011-10-15 19:29:55 -070054glance -A $TOKEN index
Jesse Andrews593828d2011-09-14 22:44:50 -070055
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070056# Let's grab the id of the first AMI image to launch
57IMAGE=`glance -A $TOKEN index | egrep ami | cut -d" " -f1`
58
Anthony Young20a2cae2011-10-17 16:02:24 -070059# Security Groups
60# ---------------
61SECGROUP=test_secgroup
62
63# List of secgroups:
64nova secgroup-list
65
66# Create a secgroup
67nova secgroup-create $SECGROUP "test_secgroup description"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070068
Jesse Andrews6fc71012011-10-24 11:29:08 -070069# determine flavor
70# ----------------
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070071
72# List of flavors:
73nova flavor-list
74
Dean Troyer1d6e0e12011-12-23 12:45:13 -060075DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
76INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2`
77if [[ -z "$INSTANCE_TYPE" ]]; then
78 # grab the first flavor in the list to launch if default doesn't exist
79 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2`
80fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070081
Anthony Young20a2cae2011-10-17 16:02:24 -070082NAME="myserver"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070083
Dean Troyer1d6e0e12011-12-23 12:45:13 -060084nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070085
Jesse Andrews6fc71012011-10-24 11:29:08 -070086# Testing
87# =======
88
89# First check if it spins up (becomes active and responds to ping on
90# internal ip). If you run this script from a nova node, you should
91# bypass security groups and have direct access to the server.
92
93# Waiting for boot
94# ----------------
95
Jesse Andrews16b6efa2011-11-10 11:46:18 -080096# Max time to wait while vm goes from build to active state
97ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-10}
98
99# Max time till the vm is bootable
100BOOT_TIMEOUT=${BOOT_TIMEOUT:-15}
101
102# Max time to wait for proper association and dis-association.
103ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-10}
104
Anthony Young79e807a2011-10-31 11:16:44 -0700105# check that the status is active within ACTIVE_TIMEOUT seconds
106if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $NAME | grep status | grep -q ACTIVE; do sleep 1; done"; then
Jesse Andrews5a774832011-10-26 21:30:02 -0700107 echo "server didn't become active!"
108 exit 1
109fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700110
111# get the IP of the server
112IP=`nova show $NAME | grep "private network" | cut -d"|" -f3`
113
Anthony Young8ecd2942011-10-24 22:58:14 -0700114# for single node deployments, we can ping private ips
115MULTI_HOST=${MULTI_HOST:-0}
Justin Shepherd56a505f2011-10-26 10:45:02 -0500116if [ "$MULTI_HOST" = "0" ]; then
Anthony Young8ecd2942011-10-24 22:58:14 -0700117 # sometimes the first ping fails (10 seconds isn't enough time for the VM's
Anthony Young79e807a2011-10-31 11:16:44 -0700118 # network to respond?), so let's ping for a default of 15 seconds with a
119 # timeout of a second for each ping.
120 if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then
Jesse Andrewsab8dbce2011-10-26 21:23:20 -0700121 echo "Couldn't ping server"
122 exit 1
123 fi
Anthony Young79e807a2011-10-31 11:16:44 -0700124else
125 # On a multi-host system, without vm net access, do a sleep to wait for the boot
126 sleep $BOOT_TIMEOUT
Anthony Young8ecd2942011-10-24 22:58:14 -0700127fi
Jesse Andrews6fc71012011-10-24 11:29:08 -0700128
129# Security Groups & Floating IPs
130# ------------------------------
131
132# allow icmp traffic (ping)
Anthony Young20a2cae2011-10-17 16:02:24 -0700133nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
134
135# List rules for a secgroup
136nova secgroup-list-rules $SECGROUP
137
Dean Troyer696ad332012-01-10 15:34:34 -0600138# allocate a floating ip from default pool
139FLOATING_IP=`nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | cut -d '|' -f2`
Anthony Young20a2cae2011-10-17 16:02:24 -0700140
Dean Troyer696ad332012-01-10 15:34:34 -0600141# list floating addresses
142if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then
143 echo "Floating IP not allocated"
144 exit 1
145fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700146
147# add floating ip to our server
Jesse Andrews6fc71012011-10-24 11:29:08 -0700148nova add-floating-ip $NAME $FLOATING_IP
Anthony Young20a2cae2011-10-17 16:02:24 -0700149
Anthony Young79e807a2011-10-31 11:16:44 -0700150# test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
151if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
Jesse Andrews5a774832011-10-26 21:30:02 -0700152 echo "Couldn't ping server with floating ip"
153 exit 1
154fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700155
Dean Troyer696ad332012-01-10 15:34:34 -0600156# Allocate an IP from it
157TEST_FLOATING_IP=`nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | cut -d '|' -f2`
158
159# list floating addresses
160if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep $TEST_FLOATING_POOL | grep -q $TEST_FLOATING_IP; do sleep 1; done"; then
161 echo "Floating IP not allocated"
162 exit 1
163fi
164
Jesse Andrews6fc71012011-10-24 11:29:08 -0700165# dis-allow icmp traffic (ping)
Anthony Young20a2cae2011-10-17 16:02:24 -0700166nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0
167
Anthony Young1de18c62011-11-01 14:19:18 -0500168# FIXME (anthony): make xs support security groups
Jesse Andrews16b6efa2011-11-10 11:46:18 -0800169if [ "$VIRT_DRIVER" != "xenserver" ]; then
Anthony Young1de18c62011-11-01 14:19:18 -0500170 # test we can aren't able to ping our floating ip within ASSOCIATE_TIMEOUT seconds
171 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
172 print "Security group failure - ping should not be allowed!"
173 echo "Couldn't ping server with floating ip"
174 exit 1
175 fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700176fi
177
178# de-allocate the floating ip
Jesse Andrews6fc71012011-10-24 11:29:08 -0700179nova floating-ip-delete $FLOATING_IP
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700180
Dean Troyer696ad332012-01-10 15:34:34 -0600181# Delete second floating IP
182nova floating-ip-delete $TEST_FLOATING_IP
183
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700184# shutdown the server
185nova delete $NAME
186
Anthony Young20a2cae2011-10-17 16:02:24 -0700187# Delete a secgroup
188nova secgroup-delete $SECGROUP
189
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700190# FIXME: validate shutdown within 5 seconds
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700191# (nova show $NAME returns 1 or status != ACTIVE)?
Vishvananda Ishayaf56e3952011-10-24 16:05:57 -0700192