blob: 36a6d6f63f251a0d05fcbc2d884424d46fd9289c [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
94# ping it once (timeout of a second)
Jesse Andrewsda892682011-10-15 20:14:07 -070095ping -c1 -w1 $IP || true
96
Vishvananda Ishaya9b353672011-10-20 10:07:10 -070097# sometimes the first ping fails (10 seconds isn't enough time for the VM's
Jesse Andrewsda892682011-10-15 20:14:07 -070098# network to respond?), so let's wait 5 seconds and really test ping
99sleep 5
100
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700101ping -c1 -w1 $IP
Jesse Andrews6fc71012011-10-24 11:29:08 -0700102
103# Security Groups & Floating IPs
104# ------------------------------
105
106# allow icmp traffic (ping)
Anthony Young20a2cae2011-10-17 16:02:24 -0700107nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
108
109# List rules for a secgroup
110nova secgroup-list-rules $SECGROUP
111
112# allocate a floating ip
113nova floating-ip-create
114
115# store floating address
Jesse Andrews6fc71012011-10-24 11:29:08 -0700116FLOATING_IP=`nova floating-ip-list | grep None | head -1 | cut -d '|' -f2 | sed 's/ //g'`
Anthony Young20a2cae2011-10-17 16:02:24 -0700117
118# add floating ip to our server
Jesse Andrews6fc71012011-10-24 11:29:08 -0700119nova add-floating-ip $NAME $FLOATING_IP
Anthony Young20a2cae2011-10-17 16:02:24 -0700120
121# sleep for a smidge
122sleep 1
123
Jesse Andrews6fc71012011-10-24 11:29:08 -0700124# ping our floating ip
125ping -c1 -w1 $FLOATING_IP
Anthony Young20a2cae2011-10-17 16:02:24 -0700126
Jesse Andrews6fc71012011-10-24 11:29:08 -0700127# dis-allow icmp traffic (ping)
Anthony Young20a2cae2011-10-17 16:02:24 -0700128nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0
129
130# sleep for a smidge
131sleep 1
132
Jesse Andrews6fc71012011-10-24 11:29:08 -0700133# ping our floating ip
134if ( ping -c1 -w1 $FLOATING_IP ); then
Anthony Young20a2cae2011-10-17 16:02:24 -0700135 print "Security group failure - ping should not be allowed!"
136 exit 1
137fi
138
139# de-allocate the floating ip
Jesse Andrews6fc71012011-10-24 11:29:08 -0700140nova floating-ip-delete $FLOATING_IP
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700141
142# shutdown the server
143nova delete $NAME
144
Anthony Young20a2cae2011-10-17 16:02:24 -0700145# Delete a secgroup
146nova secgroup-delete $SECGROUP
147
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700148# FIXME: validate shutdown within 5 seconds
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700149# (nova show $NAME returns 1 or status != ACTIVE)?
Vishvananda Ishayaf56e3952011-10-24 16:05:57 -0700150
151# Testing Euca2ools
152# ==================
153
154# make sure that we can describe instances
155euca-describe-instances