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
|
|
|
|
|
|
|
# This script exits on an error so that errors don't compound and you see
|
|
|
|
# only the first error that occured.
|
|
|
|
set -o errexit
|
|
|
|
|
|
|
|
# Print the commands being run so that we can see the command that triggers
|
|
|
|
# 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
|
|
|
|
|
|
|
HOST=${HOST:-localhost}
|
2011-09-15 05:03:04 +00:00
|
|
|
|
|
|
|
# Nova original used project_id as the *account* that owned resources (servers,
|
|
|
|
# ip address, ...) With the addition of Keystone we have standardized on the
|
|
|
|
# term **tenant** as the entity that owns the resources. **novaclient** still
|
2011-10-06 14:10:24 +00:00
|
|
|
# uses the old deprecated terms project_id. Note that this field should now be
|
|
|
|
# set to tenant_name, not tenant_id.
|
|
|
|
export NOVA_PROJECT_ID=${TENANT:-demo}
|
2011-09-15 05:03:04 +00:00
|
|
|
|
|
|
|
# In addition to the owning entity (tenant), nova stores the entity performing
|
|
|
|
# the action as the **user**.
|
|
|
|
export NOVA_USERNAME=${USERNAME:-demo}
|
|
|
|
|
|
|
|
# With Keystone you pass the keystone password instead of an api key.
|
|
|
|
export NOVA_API_KEY=${PASSWORD:-secrete}
|
|
|
|
|
|
|
|
# With the addition of Keystone, to use an openstack cloud you should
|
|
|
|
# authenticate against keystone, which returns a **Token** and **Service
|
|
|
|
# Catalog**. The catalog contains the endpoint for all services the user/tenant
|
|
|
|
# has access to - including nova, glance, keystone, swift, ... We currently
|
|
|
|
# recommend using the 2.0 *auth api*.
|
|
|
|
#
|
|
|
|
# *NOTE*: Using the 2.0 *auth api* does mean that compute api is 2.0. We will
|
|
|
|
# use the 1.1 *compute api*
|
|
|
|
export NOVA_URL=${NOVA_URL:-http://$HOST:5000/v2.0/}
|
|
|
|
|
|
|
|
# Currently novaclient needs you to specify the *compute api* version. This
|
|
|
|
# needs to match the config of your catalog returned by Keystone.
|
2011-09-15 02:37:10 +00:00
|
|
|
export NOVA_VERSION=1.1
|
|
|
|
|
2011-09-27 07:29:28 +00:00
|
|
|
# FIXME - why does this need to be specified?
|
|
|
|
export NOVA_REGION_NAME=RegionOne
|
|
|
|
|
2011-10-16 01:37:25 +00:00
|
|
|
# set log level to DEBUG (helps debug issues)
|
|
|
|
export NOVACLIENT_DEBUG=1
|
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
|
|
|
|
|
|
|
# manually create a token by querying keystone (sending JSON data). Keystone
|
|
|
|
# returns a token and catalog of endpoints. We use python to parse the token
|
|
|
|
# and save it.
|
|
|
|
|
|
|
|
TOKEN=`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'];"`
|
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`
|
|
|
|
|
|
|
|
|
|
|
|
# Flavors
|
|
|
|
# -------
|
|
|
|
|
|
|
|
# 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`
|
|
|
|
|
|
|
|
NAME="firstpost"
|
|
|
|
|
|
|
|
nova boot --flavor $FLAVOR --image $IMAGE $NAME
|
|
|
|
|
|
|
|
# let's give it 10 seconds to launch
|
|
|
|
sleep 10
|
|
|
|
|
|
|
|
# check that the status is active
|
|
|
|
nova show $NAME | grep status | grep -q ACTIVE
|
|
|
|
|
|
|
|
# get the IP of the server
|
|
|
|
IP=`nova show $NAME | grep "private network" | cut -d"|" -f3`
|
|
|
|
|
|
|
|
# ping it once (timeout of a second)
|
|
|
|
ping -c1 -w1 $IP
|
|
|
|
|
|
|
|
# shutdown the server
|
|
|
|
nova delete $NAME
|
|
|
|
|
|
|
|
# FIXME: validate shutdown within 5 seconds
|
|
|
|
# (nova show $NAME returns 1 or status != ACTIVE)?
|
|
|
|
|