2011-09-15 02:37:10 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# **exercise.sh** - using the cloud can be fun
|
|
|
|
|
|
|
|
# we will use the ``nova`` cli tool provided by the ``python-novaclient``
|
|
|
|
# package
|
2011-09-15 05:03:04 +00:00
|
|
|
#
|
2011-09-15 02:37:10 +00:00
|
|
|
|
2011-09-15 05:03:04 +00:00
|
|
|
|
2011-10-20 17:07:10 +00:00
|
|
|
# This script exits on an error so that errors don't compound and you see
|
2011-09-15 05:03:04 +00:00
|
|
|
# only the first error that occured.
|
|
|
|
set -o errexit
|
|
|
|
|
2011-10-20 17:07:10 +00:00
|
|
|
# Print the commands being run so that we can see the command that triggers
|
2011-09-15 05:03:04 +00:00
|
|
|
# an error. It is also useful for following allowing as the install occurs.
|
|
|
|
set -o xtrace
|
|
|
|
|
|
|
|
|
|
|
|
# Settings
|
|
|
|
# ========
|
2011-09-15 02:37:10 +00:00
|
|
|
|
2011-10-20 17:24:50 +00:00
|
|
|
# Use openrc + stackrc + localrc for settings
|
2011-11-01 23:44:19 +00:00
|
|
|
pushd $(cd $(dirname "$0")/.. && pwd)
|
2011-10-20 17:24:50 +00:00
|
|
|
source ./openrc
|
2011-11-01 23:44:19 +00:00
|
|
|
popd
|
2011-09-15 02:37:10 +00:00
|
|
|
|
2011-10-11 18:07:48 +00:00
|
|
|
# Get a token for clients that don't support service catalog
|
|
|
|
# ==========================================================
|
2011-10-16 01:37:25 +00:00
|
|
|
|
2011-10-20 17:07:10 +00:00
|
|
|
# manually create a token by querying keystone (sending JSON data). Keystone
|
2011-10-16 01:37:25 +00:00
|
|
|
# returns a token and catalog of endpoints. We use python to parse the token
|
|
|
|
# and save it.
|
|
|
|
|
2011-10-24 20:43:04 +00:00
|
|
|
TOKEN=`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'];"`
|
2011-10-11 18:07:48 +00:00
|
|
|
|
2011-09-15 05:44:50 +00:00
|
|
|
# Launching a server
|
|
|
|
# ==================
|
2011-09-15 05:03:04 +00:00
|
|
|
|
2011-09-15 05:44:50 +00:00
|
|
|
# List servers for tenant:
|
2011-09-15 02:37:10 +00:00
|
|
|
nova list
|
2011-09-15 05:44:50 +00:00
|
|
|
|
|
|
|
# Images
|
|
|
|
# ------
|
|
|
|
|
|
|
|
# Nova has a **deprecated** way of listing images.
|
|
|
|
nova image-list
|
|
|
|
|
|
|
|
# But we recommend using glance directly
|
2011-10-16 02:29:55 +00:00
|
|
|
glance -A $TOKEN index
|
2011-09-15 05:44:50 +00:00
|
|
|
|
2011-10-16 03:01:12 +00:00
|
|
|
# Let's grab the id of the first AMI image to launch
|
|
|
|
IMAGE=`glance -A $TOKEN index | egrep ami | cut -d" " -f1`
|
|
|
|
|
2011-10-17 23:02:24 +00:00
|
|
|
# Security Groups
|
|
|
|
# ---------------
|
|
|
|
SECGROUP=test_secgroup
|
|
|
|
|
|
|
|
# List of secgroups:
|
|
|
|
nova secgroup-list
|
|
|
|
|
|
|
|
# Create a secgroup
|
|
|
|
nova secgroup-create $SECGROUP "test_secgroup description"
|
2011-10-16 03:01:12 +00:00
|
|
|
|
2011-10-24 18:29:08 +00:00
|
|
|
# determine flavor
|
|
|
|
# ----------------
|
2011-10-16 03:01:12 +00:00
|
|
|
|
|
|
|
# List of flavors:
|
|
|
|
nova flavor-list
|
|
|
|
|
|
|
|
# and grab the first flavor in the list to launch
|
|
|
|
FLAVOR=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2`
|
|
|
|
|
2011-10-17 23:02:24 +00:00
|
|
|
NAME="myserver"
|
2011-10-16 03:01:12 +00:00
|
|
|
|
2011-10-17 23:02:24 +00:00
|
|
|
nova boot --flavor $FLAVOR --image $IMAGE $NAME --security_groups=$SECGROUP
|
2011-10-16 03:01:12 +00:00
|
|
|
|
2011-10-24 18:29:08 +00:00
|
|
|
# Testing
|
|
|
|
# =======
|
|
|
|
|
|
|
|
# First check if it spins up (becomes active and responds to ping on
|
|
|
|
# internal ip). If you run this script from a nova node, you should
|
|
|
|
# bypass security groups and have direct access to the server.
|
|
|
|
|
|
|
|
# Waiting for boot
|
|
|
|
# ----------------
|
|
|
|
|
2011-10-31 18:16:44 +00:00
|
|
|
# Max time to wait while vm goes from build to active state
|
|
|
|
ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-10}
|
|
|
|
|
|
|
|
# Max time till the vm is bootable
|
|
|
|
BOOT_TIMEOUT=${BOOT_TIMEOUT:-15}
|
|
|
|
|
|
|
|
# Max time to wait for proper association and dis-association.
|
|
|
|
ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-10}
|
|
|
|
|
|
|
|
# check that the status is active within ACTIVE_TIMEOUT seconds
|
|
|
|
if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $NAME | grep status | grep -q ACTIVE; do sleep 1; done"; then
|
2011-10-27 04:30:02 +00:00
|
|
|
echo "server didn't become active!"
|
|
|
|
exit 1
|
|
|
|
fi
|
2011-10-16 03:01:12 +00:00
|
|
|
|
|
|
|
# get the IP of the server
|
|
|
|
IP=`nova show $NAME | grep "private network" | cut -d"|" -f3`
|
|
|
|
|
2011-10-25 05:58:14 +00:00
|
|
|
# for single node deployments, we can ping private ips
|
|
|
|
MULTI_HOST=${MULTI_HOST:-0}
|
2011-10-26 15:45:02 +00:00
|
|
|
if [ "$MULTI_HOST" = "0" ]; then
|
2011-10-25 05:58:14 +00:00
|
|
|
# sometimes the first ping fails (10 seconds isn't enough time for the VM's
|
2011-10-31 18:16:44 +00:00
|
|
|
# network to respond?), so let's ping for a default of 15 seconds with a
|
|
|
|
# timeout of a second for each ping.
|
|
|
|
if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then
|
2011-10-27 04:23:20 +00:00
|
|
|
echo "Couldn't ping server"
|
|
|
|
exit 1
|
|
|
|
fi
|
2011-10-31 18:16:44 +00:00
|
|
|
else
|
|
|
|
# On a multi-host system, without vm net access, do a sleep to wait for the boot
|
|
|
|
sleep $BOOT_TIMEOUT
|
2011-10-25 05:58:14 +00:00
|
|
|
fi
|
2011-10-24 18:29:08 +00:00
|
|
|
|
|
|
|
# Security Groups & Floating IPs
|
|
|
|
# ------------------------------
|
|
|
|
|
|
|
|
# allow icmp traffic (ping)
|
2011-10-17 23:02:24 +00:00
|
|
|
nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
|
|
|
|
|
|
|
|
# List rules for a secgroup
|
|
|
|
nova secgroup-list-rules $SECGROUP
|
|
|
|
|
|
|
|
# allocate a floating ip
|
|
|
|
nova floating-ip-create
|
|
|
|
|
|
|
|
# store floating address
|
2011-10-24 18:29:08 +00:00
|
|
|
FLOATING_IP=`nova floating-ip-list | grep None | head -1 | cut -d '|' -f2 | sed 's/ //g'`
|
2011-10-17 23:02:24 +00:00
|
|
|
|
|
|
|
# add floating ip to our server
|
2011-10-24 18:29:08 +00:00
|
|
|
nova add-floating-ip $NAME $FLOATING_IP
|
2011-10-17 23:02:24 +00:00
|
|
|
|
2011-10-31 18:16:44 +00:00
|
|
|
# test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
|
|
|
|
if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
|
2011-10-27 04:30:02 +00:00
|
|
|
echo "Couldn't ping server with floating ip"
|
|
|
|
exit 1
|
|
|
|
fi
|
2011-10-17 23:02:24 +00:00
|
|
|
|
2011-10-27 05:11:31 +00:00
|
|
|
# pause the VM and verify we can't ping it anymore
|
|
|
|
nova pause $NAME
|
|
|
|
|
2011-10-27 21:06:33 +00:00
|
|
|
sleep 2
|
2011-10-27 05:11:31 +00:00
|
|
|
|
2011-10-27 21:06:33 +00:00
|
|
|
if ( ping -c1 -w1 $IP); then
|
2011-10-27 05:11:31 +00:00
|
|
|
echo "Pause failure - ping shouldn't work"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2011-10-27 21:10:47 +00:00
|
|
|
if ( ping -c1 -w1 $FLOATING_IP); then
|
|
|
|
echo "Pause failure - ping floating ips shouldn't work"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2011-10-27 05:11:31 +00:00
|
|
|
# unpause the VM and verify we can ping it again
|
|
|
|
nova unpause $NAME
|
|
|
|
|
2011-10-27 21:06:33 +00:00
|
|
|
sleep 2
|
2011-10-27 05:11:31 +00:00
|
|
|
|
2011-10-27 21:06:33 +00:00
|
|
|
ping -c1 -w1 $IP
|
2011-10-27 05:11:31 +00:00
|
|
|
|
2011-10-24 18:29:08 +00:00
|
|
|
# dis-allow icmp traffic (ping)
|
2011-10-17 23:02:24 +00:00
|
|
|
nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0
|
|
|
|
|
2011-11-01 19:19:18 +00:00
|
|
|
# FIXME (anthony): make xs support security groups
|
|
|
|
if [ "$VIRT_DRIVER" != "xenserver"]; then
|
|
|
|
# test we can aren't able to ping our floating ip within ASSOCIATE_TIMEOUT seconds
|
|
|
|
if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
|
|
|
|
print "Security group failure - ping should not be allowed!"
|
|
|
|
echo "Couldn't ping server with floating ip"
|
|
|
|
exit 1
|
|
|
|
fi
|
2011-10-17 23:02:24 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# de-allocate the floating ip
|
2011-10-24 18:29:08 +00:00
|
|
|
nova floating-ip-delete $FLOATING_IP
|
2011-10-16 03:01:12 +00:00
|
|
|
|
|
|
|
# shutdown the server
|
|
|
|
nova delete $NAME
|
|
|
|
|
2011-10-17 23:02:24 +00:00
|
|
|
# Delete a secgroup
|
|
|
|
nova secgroup-delete $SECGROUP
|
|
|
|
|
2011-10-20 17:07:10 +00:00
|
|
|
# FIXME: validate shutdown within 5 seconds
|
2011-10-16 03:01:12 +00:00
|
|
|
# (nova show $NAME returns 1 or status != ACTIVE)?
|
2011-10-24 23:05:57 +00:00
|
|
|
|