blob: 135c8c1c34d81eefe830028ecc88aae8d2719bf9 [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 Andrews38df1222011-11-20 09:55:44 -080034TOKEN=`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 +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
Dean Troyer1d6e0e12011-12-23 12:45:13 -060070DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
71INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2`
72if [[ -z "$INSTANCE_TYPE" ]]; then
73 # grab the first flavor in the list to launch if default doesn't exist
74 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2`
75fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070076
Anthony Young20a2cae2011-10-17 16:02:24 -070077NAME="myserver"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070078
Dean Troyer1d6e0e12011-12-23 12:45:13 -060079nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070080
Jesse Andrews6fc71012011-10-24 11:29:08 -070081# Testing
82# =======
83
84# First check if it spins up (becomes active and responds to ping on
85# internal ip). If you run this script from a nova node, you should
86# bypass security groups and have direct access to the server.
87
88# Waiting for boot
89# ----------------
90
Jesse Andrews16b6efa2011-11-10 11:46:18 -080091# Max time to wait while vm goes from build to active state
92ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-10}
93
94# Max time till the vm is bootable
95BOOT_TIMEOUT=${BOOT_TIMEOUT:-15}
96
97# Max time to wait for proper association and dis-association.
98ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-10}
99
Anthony Young79e807a2011-10-31 11:16:44 -0700100# check that the status is active within ACTIVE_TIMEOUT seconds
101if ! 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 -0700102 echo "server didn't become active!"
103 exit 1
104fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700105
106# get the IP of the server
107IP=`nova show $NAME | grep "private network" | cut -d"|" -f3`
108
Anthony Young8ecd2942011-10-24 22:58:14 -0700109# for single node deployments, we can ping private ips
110MULTI_HOST=${MULTI_HOST:-0}
Justin Shepherd56a505f2011-10-26 10:45:02 -0500111if [ "$MULTI_HOST" = "0" ]; then
Anthony Young8ecd2942011-10-24 22:58:14 -0700112 # sometimes the first ping fails (10 seconds isn't enough time for the VM's
Anthony Young79e807a2011-10-31 11:16:44 -0700113 # network to respond?), so let's ping for a default of 15 seconds with a
114 # timeout of a second for each ping.
115 if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then
Jesse Andrewsab8dbce2011-10-26 21:23:20 -0700116 echo "Couldn't ping server"
117 exit 1
118 fi
Anthony Young79e807a2011-10-31 11:16:44 -0700119else
120 # On a multi-host system, without vm net access, do a sleep to wait for the boot
121 sleep $BOOT_TIMEOUT
Anthony Young8ecd2942011-10-24 22:58:14 -0700122fi
Jesse Andrews6fc71012011-10-24 11:29:08 -0700123
124# Security Groups & Floating IPs
125# ------------------------------
126
127# allow icmp traffic (ping)
Anthony Young20a2cae2011-10-17 16:02:24 -0700128nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
129
130# List rules for a secgroup
131nova secgroup-list-rules $SECGROUP
132
133# allocate a floating ip
134nova floating-ip-create
135
136# store floating address
Jesse Andrews6fc71012011-10-24 11:29:08 -0700137FLOATING_IP=`nova floating-ip-list | grep None | head -1 | cut -d '|' -f2 | sed 's/ //g'`
Anthony Young20a2cae2011-10-17 16:02:24 -0700138
139# add floating ip to our server
Jesse Andrews6fc71012011-10-24 11:29:08 -0700140nova add-floating-ip $NAME $FLOATING_IP
Anthony Young20a2cae2011-10-17 16:02:24 -0700141
Anthony Young79e807a2011-10-31 11:16:44 -0700142# test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
143if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
Jesse Andrews5a774832011-10-26 21:30:02 -0700144 echo "Couldn't ping server with floating ip"
145 exit 1
146fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700147
Jesse Andrews6fc71012011-10-24 11:29:08 -0700148# dis-allow icmp traffic (ping)
Anthony Young20a2cae2011-10-17 16:02:24 -0700149nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0
150
Anthony Young1de18c62011-11-01 14:19:18 -0500151# FIXME (anthony): make xs support security groups
Jesse Andrews16b6efa2011-11-10 11:46:18 -0800152if [ "$VIRT_DRIVER" != "xenserver" ]; then
Anthony Young1de18c62011-11-01 14:19:18 -0500153 # test we can aren't able to ping our floating ip within ASSOCIATE_TIMEOUT seconds
154 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
155 print "Security group failure - ping should not be allowed!"
156 echo "Couldn't ping server with floating ip"
157 exit 1
158 fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700159fi
160
161# de-allocate the floating ip
Jesse Andrews6fc71012011-10-24 11:29:08 -0700162nova floating-ip-delete $FLOATING_IP
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700163
164# shutdown the server
165nova delete $NAME
166
Anthony Young20a2cae2011-10-17 16:02:24 -0700167# Delete a secgroup
168nova secgroup-delete $SECGROUP
169
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700170# FIXME: validate shutdown within 5 seconds
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700171# (nova show $NAME returns 1 or status != ACTIVE)?
Vishvananda Ishayaf56e3952011-10-24 16:05:57 -0700172