blob: 3f2c94e5d676ebd8b38fb85ba3a038ad14c73d49 [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
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070085# let's give it 10 seconds to launch
86sleep 10
87
88# check that the status is active
89nova show $NAME | grep status | grep -q ACTIVE
90
91# get the IP of the server
92IP=`nova show $NAME | grep "private network" | cut -d"|" -f3`
93
Anthony Young8ecd2942011-10-24 22:58:14 -070094# for single node deployments, we can ping private ips
95MULTI_HOST=${MULTI_HOST:-0}
Justin Shepherd56a505f2011-10-26 10:45:02 -050096if [ "$MULTI_HOST" = "0" ]; then
Anthony Young8ecd2942011-10-24 22:58:14 -070097 # ping it once (timeout of a second)
98 ping -c1 -w1 $IP || true
Jesse Andrewsda892682011-10-15 20:14:07 -070099
Anthony Young8ecd2942011-10-24 22:58:14 -0700100 # sometimes the first ping fails (10 seconds isn't enough time for the VM's
101 # network to respond?), so let's wait 5 seconds and really test ping
102 sleep 5
Jesse Andrewsda892682011-10-15 20:14:07 -0700103
Anthony Young8ecd2942011-10-24 22:58:14 -0700104 ping -c1 -w1 $IP
105fi
Jesse Andrews6fc71012011-10-24 11:29:08 -0700106
107# Security Groups & Floating IPs
108# ------------------------------
109
110# allow icmp traffic (ping)
Anthony Young20a2cae2011-10-17 16:02:24 -0700111nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
112
113# List rules for a secgroup
114nova secgroup-list-rules $SECGROUP
115
116# allocate a floating ip
117nova floating-ip-create
118
119# store floating address
Jesse Andrews6fc71012011-10-24 11:29:08 -0700120FLOATING_IP=`nova floating-ip-list | grep None | head -1 | cut -d '|' -f2 | sed 's/ //g'`
Anthony Young20a2cae2011-10-17 16:02:24 -0700121
122# add floating ip to our server
Jesse Andrews6fc71012011-10-24 11:29:08 -0700123nova add-floating-ip $NAME $FLOATING_IP
Anthony Young20a2cae2011-10-17 16:02:24 -0700124
125# sleep for a smidge
Anthony Young9d8e8cf2011-10-25 00:34:35 -0700126sleep 5
Anthony Young20a2cae2011-10-17 16:02:24 -0700127
Jesse Andrews6fc71012011-10-24 11:29:08 -0700128# ping our floating ip
129ping -c1 -w1 $FLOATING_IP
Anthony Young20a2cae2011-10-17 16:02:24 -0700130
Jesse Andrews6fc71012011-10-24 11:29:08 -0700131# dis-allow icmp traffic (ping)
Anthony Young20a2cae2011-10-17 16:02:24 -0700132nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0
133
134# sleep for a smidge
Anthony Young9d8e8cf2011-10-25 00:34:35 -0700135sleep 5
Anthony Young20a2cae2011-10-17 16:02:24 -0700136
Jesse Andrews6fc71012011-10-24 11:29:08 -0700137# ping our floating ip
138if ( ping -c1 -w1 $FLOATING_IP ); then
Anthony Young20a2cae2011-10-17 16:02:24 -0700139 print "Security group failure - ping should not be allowed!"
140 exit 1
141fi
142
143# de-allocate the floating ip
Jesse Andrews6fc71012011-10-24 11:29:08 -0700144nova floating-ip-delete $FLOATING_IP
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700145
146# shutdown the server
147nova delete $NAME
148
Anthony Young20a2cae2011-10-17 16:02:24 -0700149# Delete a secgroup
150nova secgroup-delete $SECGROUP
151
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700152# FIXME: validate shutdown within 5 seconds
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700153# (nova show $NAME returns 1 or status != ACTIVE)?
Vishvananda Ishayaf56e3952011-10-24 16:05:57 -0700154
155# Testing Euca2ools
156# ==================
157
158# make sure that we can describe instances
159euca-describe-instances