blob: 35ff403785a90291997026b8eaf4f0da3d11ab96 [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
32TOKEN=`curl -s -d "{\"auth\":{\"passwordCredentials\": {\"username\": \"$NOVA_USERNAME\", \"password\": \"$NOVA_API_KEY\"}}}" -H "Content-type: application/json" http://$HOST: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
62# Flavors
63# -------
64
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
75# let's give it 10 seconds to launch
76sleep 10
77
78# check that the status is active
79nova show $NAME | grep status | grep -q ACTIVE
80
81# get the IP of the server
82IP=`nova show $NAME | grep "private network" | cut -d"|" -f3`
83
84# ping it once (timeout of a second)
Jesse Andrewsda892682011-10-15 20:14:07 -070085ping -c1 -w1 $IP || true
86
Vishvananda Ishaya9b353672011-10-20 10:07:10 -070087# sometimes the first ping fails (10 seconds isn't enough time for the VM's
Jesse Andrewsda892682011-10-15 20:14:07 -070088# network to respond?), so let's wait 5 seconds and really test ping
89sleep 5
90
Vishvananda Ishaya9b353672011-10-20 10:07:10 -070091ping -c1 -w1 $IP
Anthony Young20a2cae2011-10-17 16:02:24 -070092# allow icmp traffic
93nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
94
95# List rules for a secgroup
96nova secgroup-list-rules $SECGROUP
97
98# allocate a floating ip
99nova floating-ip-create
100
101# store floating address
102FIP=`nova floating-ip-list | grep None | head -1 | cut -d '|' -f2 | sed 's/ //g'`
103
104# add floating ip to our server
105nova add-floating-ip $NAME $FIP
106
107# sleep for a smidge
108sleep 1
109
110# ping our fip
111ping -c1 -w1 $FIP
112
113# dis-allow icmp traffic
114nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0
115
116# sleep for a smidge
117sleep 1
118
119# ping our fip
120if ( ping -c1 -w1 $FIP); then
121 print "Security group failure - ping should not be allowed!"
122 exit 1
123fi
124
125# de-allocate the floating ip
126nova floating-ip-delete $FIP
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700127
128# shutdown the server
129nova delete $NAME
130
Anthony Young20a2cae2011-10-17 16:02:24 -0700131# Delete a secgroup
132nova secgroup-delete $SECGROUP
133
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700134# FIXME: validate shutdown within 5 seconds
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700135# (nova show $NAME returns 1 or status != ACTIVE)?