blob: 75046d1ad59c8a1230045b77a4e516265d7ed49b [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
Anthony Young94c889a2011-10-11 18:07:48 +000027# Get a token for clients that don't support service catalog
28# ==========================================================
Jesse Andrewsb9c77d62011-10-15 18:37:25 -070029
Vishvananda Ishaya9b353672011-10-20 10:07:10 -070030# manually create a token by querying keystone (sending JSON data). Keystone
Jesse Andrewsb9c77d62011-10-15 18:37:25 -070031# returns a token and catalog of endpoints. We use python to parse the token
32# and save it.
33
Jesse Andrewse61f3182011-10-24 13:43:04 -070034TOKEN=`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 +000035
Jesse Andrews593828d2011-09-14 22:44:50 -070036# Launching a server
37# ==================
Jesse Andrewsb19424f2011-09-14 22:03:04 -070038
Jesse Andrews593828d2011-09-14 22:44:50 -070039# List servers for tenant:
Jesse Andrewsb0191512011-09-14 19:37:10 -070040nova list
Jesse Andrews593828d2011-09-14 22:44:50 -070041
Jesse Andrews593828d2011-09-14 22:44:50 -070042# Images
43# ------
44
45# Nova has a **deprecated** way of listing images.
46nova image-list
47
48# But we recommend using glance directly
Jesse Andrews4e8847c2011-10-15 19:29:55 -070049glance -A $TOKEN index
Jesse Andrews593828d2011-09-14 22:44:50 -070050
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070051# Let's grab the id of the first AMI image to launch
52IMAGE=`glance -A $TOKEN index | egrep ami | cut -d" " -f1`
53
Anthony Young20a2cae2011-10-17 16:02:24 -070054# Security Groups
55# ---------------
56SECGROUP=test_secgroup
57
58# List of secgroups:
59nova secgroup-list
60
61# Create a secgroup
62nova secgroup-create $SECGROUP "test_secgroup description"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070063
Jesse Andrews6fc71012011-10-24 11:29:08 -070064# determine flavor
65# ----------------
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070066
67# List of flavors:
68nova flavor-list
69
70# and grab the first flavor in the list to launch
71FLAVOR=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2`
72
Anthony Young20a2cae2011-10-17 16:02:24 -070073NAME="myserver"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070074
Anthony Young20a2cae2011-10-17 16:02:24 -070075nova boot --flavor $FLAVOR --image $IMAGE $NAME --security_groups=$SECGROUP
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070076
Jesse Andrews6fc71012011-10-24 11:29:08 -070077# Testing
78# =======
79
80# First check if it spins up (becomes active and responds to ping on
81# internal ip). If you run this script from a nova node, you should
82# bypass security groups and have direct access to the server.
83
84# Waiting for boot
85# ----------------
86
Jesse Andrews16b6efa2011-11-10 11:46:18 -080087# Max time to wait while vm goes from build to active state
88ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-10}
89
90# Max time till the vm is bootable
91BOOT_TIMEOUT=${BOOT_TIMEOUT:-15}
92
93# Max time to wait for proper association and dis-association.
94ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-10}
95
Anthony Young79e807a2011-10-31 11:16:44 -070096# check that the status is active within ACTIVE_TIMEOUT seconds
97if ! 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 -070098 echo "server didn't become active!"
99 exit 1
100fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700101
102# get the IP of the server
103IP=`nova show $NAME | grep "private network" | cut -d"|" -f3`
104
Anthony Young8ecd2942011-10-24 22:58:14 -0700105# for single node deployments, we can ping private ips
106MULTI_HOST=${MULTI_HOST:-0}
Justin Shepherd56a505f2011-10-26 10:45:02 -0500107if [ "$MULTI_HOST" = "0" ]; then
Anthony Young8ecd2942011-10-24 22:58:14 -0700108 # sometimes the first ping fails (10 seconds isn't enough time for the VM's
Anthony Young79e807a2011-10-31 11:16:44 -0700109 # network to respond?), so let's ping for a default of 15 seconds with a
110 # timeout of a second for each ping.
111 if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then
Jesse Andrewsab8dbce2011-10-26 21:23:20 -0700112 echo "Couldn't ping server"
113 exit 1
114 fi
Anthony Young79e807a2011-10-31 11:16:44 -0700115else
116 # On a multi-host system, without vm net access, do a sleep to wait for the boot
117 sleep $BOOT_TIMEOUT
Anthony Young8ecd2942011-10-24 22:58:14 -0700118fi
Jesse Andrews6fc71012011-10-24 11:29:08 -0700119
120# Security Groups & Floating IPs
121# ------------------------------
122
123# allow icmp traffic (ping)
Anthony Young20a2cae2011-10-17 16:02:24 -0700124nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
125
126# List rules for a secgroup
127nova secgroup-list-rules $SECGROUP
128
129# allocate a floating ip
130nova floating-ip-create
131
132# store floating address
Jesse Andrews6fc71012011-10-24 11:29:08 -0700133FLOATING_IP=`nova floating-ip-list | grep None | head -1 | cut -d '|' -f2 | sed 's/ //g'`
Anthony Young20a2cae2011-10-17 16:02:24 -0700134
135# add floating ip to our server
Jesse Andrews6fc71012011-10-24 11:29:08 -0700136nova add-floating-ip $NAME $FLOATING_IP
Anthony Young20a2cae2011-10-17 16:02:24 -0700137
Anthony Young79e807a2011-10-31 11:16:44 -0700138# test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
139if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
Jesse Andrews5a774832011-10-26 21:30:02 -0700140 echo "Couldn't ping server with floating ip"
141 exit 1
142fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700143
Jesse Andrewsb7cc5bc2011-10-26 22:11:31 -0700144# pause the VM and verify we can't ping it anymore
145nova pause $NAME
146
Jesse Andrews467135e2011-10-27 14:06:33 -0700147sleep 2
Jesse Andrewsb7cc5bc2011-10-26 22:11:31 -0700148
Jesse Andrews467135e2011-10-27 14:06:33 -0700149if ( ping -c1 -w1 $IP); then
Jesse Andrewsb7cc5bc2011-10-26 22:11:31 -0700150 echo "Pause failure - ping shouldn't work"
151 exit 1
152fi
153
Jesse Andrews0c484fe2011-10-27 14:10:47 -0700154if ( ping -c1 -w1 $FLOATING_IP); then
155 echo "Pause failure - ping floating ips shouldn't work"
156 exit 1
157fi
158
Jesse Andrewsb7cc5bc2011-10-26 22:11:31 -0700159# unpause the VM and verify we can ping it again
160nova unpause $NAME
161
Jesse Andrews467135e2011-10-27 14:06:33 -0700162sleep 2
Jesse Andrewsb7cc5bc2011-10-26 22:11:31 -0700163
Jesse Andrews467135e2011-10-27 14:06:33 -0700164ping -c1 -w1 $IP
Jesse Andrewsb7cc5bc2011-10-26 22:11:31 -0700165
Jesse Andrews6fc71012011-10-24 11:29:08 -0700166# dis-allow icmp traffic (ping)
Anthony Young20a2cae2011-10-17 16:02:24 -0700167nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0
168
Anthony Young1de18c62011-11-01 14:19:18 -0500169# FIXME (anthony): make xs support security groups
Jesse Andrews16b6efa2011-11-10 11:46:18 -0800170if [ "$VIRT_DRIVER" != "xenserver" ]; then
Anthony Young1de18c62011-11-01 14:19:18 -0500171 # test we can aren't able to ping our floating ip within ASSOCIATE_TIMEOUT seconds
172 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
173 print "Security group failure - ping should not be allowed!"
174 echo "Couldn't ping server with floating ip"
175 exit 1
176 fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700177fi
178
179# de-allocate the floating ip
Jesse Andrews6fc71012011-10-24 11:29:08 -0700180nova floating-ip-delete $FLOATING_IP
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700181
182# shutdown the server
183nova delete $NAME
184
Anthony Young20a2cae2011-10-17 16:02:24 -0700185# Delete a secgroup
186nova secgroup-delete $SECGROUP
187
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700188# FIXME: validate shutdown within 5 seconds
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700189# (nova show $NAME returns 1 or status != ACTIVE)?
Vishvananda Ishayaf56e3952011-10-24 16:05:57 -0700190