blob: 61c7b08ae4e23bb9390617c11649ba2e318b562b [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 Andrews5a774832011-10-26 21:30:02 -070085# check that the status is active within 10 seconds
86if ! timeout 10 sh -c "while ! nova show $NAME | grep status | grep -q ACTIVE; do sleep 1; done"; then
87 echo "server didn't become active!"
88 exit 1
89fi
Jesse Andrewsd888e1c2011-10-15 20:01:12 -070090
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 # sometimes the first ping fails (10 seconds isn't enough time for the VM's
Jesse Andrewsac2e1662011-10-26 21:39:56 -070098 # network to respond?), so let's ping for 15 seconds with a timeout
Jesse Andrewsab8dbce2011-10-26 21:23:20 -070099 # of a second.
100 if ! timeout 15 sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then
101 echo "Couldn't ping server"
102 exit 1
103 fi
Anthony Young8ecd2942011-10-24 22:58:14 -0700104fi
Jesse Andrews6fc71012011-10-24 11:29:08 -0700105
106# Security Groups & Floating IPs
107# ------------------------------
108
109# allow icmp traffic (ping)
Anthony Young20a2cae2011-10-17 16:02:24 -0700110nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
111
112# List rules for a secgroup
113nova secgroup-list-rules $SECGROUP
114
115# allocate a floating ip
116nova floating-ip-create
117
118# store floating address
Jesse Andrews6fc71012011-10-24 11:29:08 -0700119FLOATING_IP=`nova floating-ip-list | grep None | head -1 | cut -d '|' -f2 | sed 's/ //g'`
Anthony Young20a2cae2011-10-17 16:02:24 -0700120
121# add floating ip to our server
Jesse Andrews6fc71012011-10-24 11:29:08 -0700122nova add-floating-ip $NAME $FLOATING_IP
Anthony Young20a2cae2011-10-17 16:02:24 -0700123
Jesse Andrews5a774832011-10-26 21:30:02 -0700124# test we can ping our floating ip within 10 seconds
125if ! timeout 10 sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
126 echo "Couldn't ping server with floating ip"
127 exit 1
128fi
Anthony Young20a2cae2011-10-17 16:02:24 -0700129
Jesse Andrewsb7cc5bc2011-10-26 22:11:31 -0700130# pause the VM and verify we can't ping it anymore
131nova pause $NAME
132
Jesse Andrews467135e2011-10-27 14:06:33 -0700133sleep 2
Jesse Andrewsb7cc5bc2011-10-26 22:11:31 -0700134
Jesse Andrews467135e2011-10-27 14:06:33 -0700135if ( ping -c1 -w1 $IP); then
Jesse Andrewsb7cc5bc2011-10-26 22:11:31 -0700136 echo "Pause failure - ping shouldn't work"
137 exit 1
138fi
139
Jesse Andrews0c484fe2011-10-27 14:10:47 -0700140if ( ping -c1 -w1 $FLOATING_IP); then
141 echo "Pause failure - ping floating ips shouldn't work"
142 exit 1
143fi
144
Jesse Andrewsb7cc5bc2011-10-26 22:11:31 -0700145# unpause the VM and verify we can ping it again
146nova unpause $NAME
147
Jesse Andrews467135e2011-10-27 14:06:33 -0700148sleep 2
Jesse Andrewsb7cc5bc2011-10-26 22:11:31 -0700149
Jesse Andrews467135e2011-10-27 14:06:33 -0700150ping -c1 -w1 $IP
Jesse Andrewsb7cc5bc2011-10-26 22:11:31 -0700151
Jesse Andrews6fc71012011-10-24 11:29:08 -0700152# dis-allow icmp traffic (ping)
Anthony Young20a2cae2011-10-17 16:02:24 -0700153nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0
154
Jesse Andrews5a774832011-10-26 21:30:02 -0700155# test we can aren't able to ping our floating ip within 10 seconds
156if ! timeout 10 sh -c "while ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
Anthony Young20a2cae2011-10-17 16:02:24 -0700157 print "Security group failure - ping should not be allowed!"
Jesse Andrews5a774832011-10-26 21:30:02 -0700158 echo "Couldn't ping server with floating ip"
Anthony Young20a2cae2011-10-17 16:02:24 -0700159 exit 1
160fi
161
162# de-allocate the floating ip
Jesse Andrews6fc71012011-10-24 11:29:08 -0700163nova floating-ip-delete $FLOATING_IP
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700164
165# shutdown the server
166nova delete $NAME
167
Anthony Young20a2cae2011-10-17 16:02:24 -0700168# Delete a secgroup
169nova secgroup-delete $SECGROUP
170
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700171# FIXME: validate shutdown within 5 seconds
Jesse Andrewsd888e1c2011-10-15 20:01:12 -0700172# (nova show $NAME returns 1 or status != ACTIVE)?
Vishvananda Ishayaf56e3952011-10-24 16:05:57 -0700173
174# Testing Euca2ools
175# ==================
176
177# make sure that we can describe instances
178euca-describe-instances