blob: f35adeff962e6f4b6bea350bafdaf21ccb37a7c8 [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
10# This script exits on an error so that errors don't compound and you see
11# only the first error that occured.
12set -o errexit
13
14# Print the commands being run so that we can see the command that triggers
15# 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 Young583bad02011-10-18 08:22:30 -070022# Use stackrc and localrc for settings
23source ./stackrc
24
Jesse Andrewsb0191512011-09-14 19:37:10 -070025HOST=${HOST:-localhost}
Jesse Andrewsb0191512011-09-14 19:37:10 -070026
Jesse Andrewsb19424f2011-09-14 22:03:04 -070027# Nova original used project_id as the *account* that owned resources (servers,
28# ip address, ...) With the addition of Keystone we have standardized on the
29# term **tenant** as the entity that owns the resources. **novaclient** still
Anthony Young0edde7d2011-10-06 07:10:24 -070030# uses the old deprecated terms project_id. Note that this field should now be
31# set to tenant_name, not tenant_id.
32export NOVA_PROJECT_ID=${TENANT:-demo}
Jesse Andrewsb19424f2011-09-14 22:03:04 -070033
34# In addition to the owning entity (tenant), nova stores the entity performing
35# the action as the **user**.
36export NOVA_USERNAME=${USERNAME:-demo}
37
38# With Keystone you pass the keystone password instead of an api key.
Anthony Young583bad02011-10-18 08:22:30 -070039export NOVA_API_KEY=${ADMIN_PASSWORD:-secrete}
Jesse Andrewsb19424f2011-09-14 22:03:04 -070040
41# With the addition of Keystone, to use an openstack cloud you should
42# authenticate against keystone, which returns a **Token** and **Service
43# Catalog**. The catalog contains the endpoint for all services the user/tenant
44# has access to - including nova, glance, keystone, swift, ... We currently
45# recommend using the 2.0 *auth api*.
46#
47# *NOTE*: Using the 2.0 *auth api* does mean that compute api is 2.0. We will
48# use the 1.1 *compute api*
49export NOVA_URL=${NOVA_URL:-http://$HOST:5000/v2.0/}
50
51# Currently novaclient needs you to specify the *compute api* version. This
52# needs to match the config of your catalog returned by Keystone.
Jesse Andrewsb0191512011-09-14 19:37:10 -070053export NOVA_VERSION=1.1
54
Anthony Young43876902011-09-27 00:29:28 -070055# FIXME - why does this need to be specified?
56export NOVA_REGION_NAME=RegionOne
57
Jesse Andrewsb9c77d62011-10-15 18:37:25 -070058# set log level to DEBUG (helps debug issues)
59export NOVACLIENT_DEBUG=1
Jesse Andrewsb0191512011-09-14 19:37:10 -070060
Anthony Young94c889a2011-10-11 18:07:48 +000061# Get a token for clients that don't support service catalog
62# ==========================================================
Jesse Andrewsb9c77d62011-10-15 18:37:25 -070063
64# manually create a token by querying keystone (sending JSON data). Keystone
65# returns a token and catalog of endpoints. We use python to parse the token
66# and save it.
67
68TOKEN=`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 +000069
Jesse Andrews593828d2011-09-14 22:44:50 -070070# Launching a server
71# ==================
Jesse Andrewsb19424f2011-09-14 22:03:04 -070072
Jesse Andrews593828d2011-09-14 22:44:50 -070073# List servers for tenant:
Jesse Andrewsb0191512011-09-14 19:37:10 -070074nova list
Jesse Andrews593828d2011-09-14 22:44:50 -070075
Jesse Andrews593828d2011-09-14 22:44:50 -070076# Images
77# ------
78
79# Nova has a **deprecated** way of listing images.
80nova image-list
81
82# But we recommend using glance directly
Jesse Andrews4e8847c2011-10-15 19:29:55 -070083glance -A $TOKEN index
Jesse Andrews593828d2011-09-14 22:44:50 -070084
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070085# Let's grab the id of the first AMI image to launch
86IMAGE=`glance -A $TOKEN index | egrep ami | cut -d" " -f1`
87
Anthony Young20a2cae2011-10-17 16:02:24 -070088# Security Groups
89# ---------------
90SECGROUP=test_secgroup
91
92# List of secgroups:
93nova secgroup-list
94
95# Create a secgroup
96nova secgroup-create $SECGROUP "test_secgroup description"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070097
98# Flavors
99# -------
100
101# List of flavors:
102nova flavor-list
103
104# and grab the first flavor in the list to launch
105FLAVOR=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2`
106
Anthony Young20a2cae2011-10-17 16:02:24 -0700107NAME="myserver"
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700108
Anthony Young20a2cae2011-10-17 16:02:24 -0700109nova boot --flavor $FLAVOR --image $IMAGE $NAME --security_groups=$SECGROUP
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700110
111# let's give it 10 seconds to launch
112sleep 10
113
114# check that the status is active
115nova show $NAME | grep status | grep -q ACTIVE
116
117# get the IP of the server
118IP=`nova show $NAME | grep "private network" | cut -d"|" -f3`
119
120# ping it once (timeout of a second)
Jesse Andrewsda892682011-10-15 20:14:07 -0700121ping -c1 -w1 $IP || true
122
123# sometimes the first ping fails (10 seconds isn't enough time for the VM's
124# network to respond?), so let's wait 5 seconds and really test ping
125sleep 5
126
127ping -c1 -w1 $IP
Anthony Young20a2cae2011-10-17 16:02:24 -0700128# allow icmp traffic
129nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
130
131# List rules for a secgroup
132nova secgroup-list-rules $SECGROUP
133
134# allocate a floating ip
135nova floating-ip-create
136
137# store floating address
138FIP=`nova floating-ip-list | grep None | head -1 | cut -d '|' -f2 | sed 's/ //g'`
139
140# add floating ip to our server
141nova add-floating-ip $NAME $FIP
142
143# sleep for a smidge
144sleep 1
145
146# ping our fip
147ping -c1 -w1 $FIP
148
149# dis-allow icmp traffic
150nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0
151
152# sleep for a smidge
153sleep 1
154
155# ping our fip
156if ( ping -c1 -w1 $FIP); then
157 print "Security group failure - ping should not be allowed!"
158 exit 1
159fi
160
161# de-allocate the floating ip
162nova floating-ip-delete $FIP
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
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700170# FIXME: validate shutdown within 5 seconds
171# (nova show $NAME returns 1 or status != ACTIVE)?