blob: 99b0f3bb1e2a071b42a82d12f084376542e83ccf [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
23source ./openrc
Jesse Andrewsb0191512011-09-14 19:37:10 -070024
Anthony Young94c889a2011-10-11 18:07:48 +000025# Get a token for clients that don't support service catalog
26# ==========================================================
Jesse Andrewsb9c77d62011-10-15 18:37:25 -070027
Vishvananda Ishaya9b353672011-10-20 10:07:10 -070028# manually create a token by querying keystone (sending JSON data). Keystone
Jesse Andrewsb9c77d62011-10-15 18:37:25 -070029# returns a token and catalog of endpoints. We use python to parse the token
30# and save it.
31
Jesse Andrewse61f3182011-10-24 13:43:04 -070032TOKEN=`curl -s -d "{\"auth\":{\"passwordCredentials\": {\"username\": \"$NOVA_USERNAME\", \"password\": \"$NOVA_API_KEY\"}}}" -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 +000033
Jesse Andrews593828d2011-09-14 22:44:50 -070034# Launching a server
35# ==================
Jesse Andrewsb19424f2011-09-14 22:03:04 -070036
Jesse Andrews593828d2011-09-14 22:44:50 -070037# List servers for tenant:
Jesse Andrewsb0191512011-09-14 19:37:10 -070038nova list
Jesse Andrews593828d2011-09-14 22:44:50 -070039
Jesse Andrews593828d2011-09-14 22:44:50 -070040# Images
41# ------
42
43# Nova has a **deprecated** way of listing images.
44nova image-list
45
46# But we recommend using glance directly
Jesse Andrews4e8847c2011-10-15 19:29:55 -070047glance -A $TOKEN index
Jesse Andrews593828d2011-09-14 22:44:50 -070048
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070049# Let's grab the id of the first AMI image to launch
50IMAGE=`glance -A $TOKEN index | egrep ami | cut -d" " -f1`
51
Anthony Young20a2cae2011-10-17 16:02:24 -070052# Security Groups
53# ---------------
54SECGROUP=test_secgroup
55
56# List of secgroups:
57nova secgroup-list
58
59# Create a secgroup
60nova secgroup-create $SECGROUP "test_secgroup description"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070061
Jesse Andrews6fc71012011-10-24 11:29:08 -070062# determine flavor
63# ----------------
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070064
65# List of flavors:
66nova flavor-list
67
68# and grab the first flavor in the list to launch
69FLAVOR=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2`
70
Anthony Young20a2cae2011-10-17 16:02:24 -070071NAME="myserver"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070072
Anthony Young20a2cae2011-10-17 16:02:24 -070073nova boot --flavor $FLAVOR --image $IMAGE $NAME --security_groups=$SECGROUP
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070074
Jesse Andrews6fc71012011-10-24 11:29:08 -070075# Testing
76# =======
77
78# First check if it spins up (becomes active and responds to ping on
79# internal ip). If you run this script from a nova node, you should
80# bypass security groups and have direct access to the server.
81
82# Waiting for boot
83# ----------------
84
Anthony Young79e807a2011-10-31 11:16:44 -070085# Max time to wait while vm goes from build to active state
86ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-10}
87
88# Max time till the vm is bootable
89BOOT_TIMEOUT=${BOOT_TIMEOUT:-15}
90
91# Max time to wait for proper association and dis-association.
92ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-10}
93
94# check that the status is active within ACTIVE_TIMEOUT seconds
95if ! 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 -070096 echo "server didn't become active!"
97 exit 1
98fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070099
100# get the IP of the server
101IP=`nova show $NAME | grep "private network" | cut -d"|" -f3`
102
Anthony Young8ecd2942011-10-24 22:58:14 -0700103# for single node deployments, we can ping private ips
104MULTI_HOST=${MULTI_HOST:-0}
Justin Shepherd56a505f2011-10-26 10:45:02 -0500105if [ "$MULTI_HOST" = "0" ]; then
Anthony Young8ecd2942011-10-24 22:58:14 -0700106 # sometimes the first ping fails (10 seconds isn't enough time for the VM's
Anthony Young79e807a2011-10-31 11:16:44 -0700107 # network to respond?), so let's ping for a default of 15 seconds with a
108 # timeout of a second for each ping.
109 if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then
Jesse Andrewsab8dbce2011-10-26 21:23:20 -0700110 echo "Couldn't ping server"
111 exit 1
112 fi
Anthony Young79e807a2011-10-31 11:16:44 -0700113else
114 # On a multi-host system, without vm net access, do a sleep to wait for the boot
115 sleep $BOOT_TIMEOUT
Anthony Young8ecd2942011-10-24 22:58:14 -0700116fi
Jesse Andrews6fc71012011-10-24 11:29:08 -0700117
118# Security Groups & Floating IPs
119# ------------------------------
120
121# allow icmp traffic (ping)
Anthony Young20a2cae2011-10-17 16:02:24 -0700122nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
123
124# List rules for a secgroup
125nova secgroup-list-rules $SECGROUP
126
127# allocate a floating ip
128nova floating-ip-create
129
130# store floating address
Jesse Andrews6fc71012011-10-24 11:29:08 -0700131FLOATING_IP=`nova floating-ip-list | grep None | head -1 | cut -d '|' -f2 | sed 's/ //g'`
Anthony Young20a2cae2011-10-17 16:02:24 -0700132
133# add floating ip to our server
Jesse Andrews6fc71012011-10-24 11:29:08 -0700134nova add-floating-ip $NAME $FLOATING_IP
Anthony Young20a2cae2011-10-17 16:02:24 -0700135
Anthony Young79e807a2011-10-31 11:16:44 -0700136# test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
137if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
Jesse Andrews5a774832011-10-26 21:30:02 -0700138 echo "Couldn't ping server with floating ip"
139 exit 1
140fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700141
Jesse Andrewsb7cc5bc2011-10-26 22:11:31 -0700142# pause the VM and verify we can't ping it anymore
143nova pause $NAME
144
Jesse Andrews467135e2011-10-27 14:06:33 -0700145sleep 2
Jesse Andrewsb7cc5bc2011-10-26 22:11:31 -0700146
Jesse Andrews467135e2011-10-27 14:06:33 -0700147if ( ping -c1 -w1 $IP); then
Jesse Andrewsb7cc5bc2011-10-26 22:11:31 -0700148 echo "Pause failure - ping shouldn't work"
149 exit 1
150fi
151
Jesse Andrews0c484fe2011-10-27 14:10:47 -0700152if ( ping -c1 -w1 $FLOATING_IP); then
153 echo "Pause failure - ping floating ips shouldn't work"
154 exit 1
155fi
156
Jesse Andrewsb7cc5bc2011-10-26 22:11:31 -0700157# unpause the VM and verify we can ping it again
158nova unpause $NAME
159
Jesse Andrews467135e2011-10-27 14:06:33 -0700160sleep 2
Jesse Andrewsb7cc5bc2011-10-26 22:11:31 -0700161
Jesse Andrews467135e2011-10-27 14:06:33 -0700162ping -c1 -w1 $IP
Jesse Andrewsb7cc5bc2011-10-26 22:11:31 -0700163
Jesse Andrews6fc71012011-10-24 11:29:08 -0700164# dis-allow icmp traffic (ping)
Anthony Young20a2cae2011-10-17 16:02:24 -0700165nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0
166
Anthony Young1de18c62011-11-01 14:19:18 -0500167# FIXME (anthony): make xs support security groups
168if [ "$VIRT_DRIVER" != "xenserver"]; then
169 # test we can aren't able to ping our floating ip within ASSOCIATE_TIMEOUT seconds
170 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
171 print "Security group failure - ping should not be allowed!"
172 echo "Couldn't ping server with floating ip"
173 exit 1
174 fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700175fi
176
177# de-allocate the floating ip
Jesse Andrews6fc71012011-10-24 11:29:08 -0700178nova floating-ip-delete $FLOATING_IP
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700179
180# shutdown the server
181nova delete $NAME
182
Anthony Young20a2cae2011-10-17 16:02:24 -0700183# Delete a secgroup
184nova secgroup-delete $SECGROUP
185
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700186# FIXME: validate shutdown within 5 seconds
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700187# (nova show $NAME returns 1 or status != ACTIVE)?
Vishvananda Ishayaf56e3952011-10-24 16:05:57 -0700188
189# Testing Euca2ools
190# ==================
191
192# make sure that we can describe instances
193euca-describe-instances