blob: fe06b6edd618e641d68aa70e1a976dfc03d19c03 [file] [log] [blame]
Dean Troyera8dda172011-12-16 12:22:02 -06001#!/usr/bin/env bash
2
3# Test nova volumes with the nova command from python-novaclient
4
5# This script exits on an error so that errors don't compound and you see
6# only the first error that occured.
7set -o errexit
8
9# Print the commands being run so that we can see the command that triggers
10# an error. It is also useful for following allowing as the install occurs.
11set -o xtrace
12
13
14# Settings
15# ========
16
17# Use openrc + stackrc + localrc for settings
18pushd $(cd $(dirname "$0")/.. && pwd)
19source ./openrc
20popd
21
22# Get a token for clients that don't support service catalog
23# ==========================================================
24
25# manually create a token by querying keystone (sending JSON data). Keystone
26# returns a token and catalog of endpoints. We use python to parse the token
27# and save it.
28
29TOKEN=`curl -s -d "{\"auth\":{\"passwordCredentials\": {\"username\": \"$NOVA_USERNAME\", \"password\": \"$NOVA_PASSWORD\"}}}" -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'];"`
30
31# Launching a server
32# ==================
33
34# List servers for tenant:
35nova list
36
37# Images
38# ------
39
40# Nova has a **deprecated** way of listing images.
41nova image-list
42
43# But we recommend using glance directly
44glance -A $TOKEN index
45
46# Let's grab the id of the first AMI image to launch
47IMAGE=`glance -A $TOKEN index | egrep ami | head -1 | cut -d" " -f1`
48
49# determinine instance type
50# -------------------------
51
52# List of instance types:
53nova flavor-list
54
55INSTANCE_NAME=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
56INSTANCE_TYPE=`nova flavor-list | grep $INSTANCE_NAME | cut -d"|" -f2`
57if [[ -z "`nova flavor-list | grep $INSTANCE_NAME | cut -d"|" -f2`" ]]; then
58 # and grab the first flavor in the list to launch
59 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2`
60fi
61
62NAME="myserver"
63
64VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'`
65
66# Testing
67# =======
68
69# First check if it spins up (becomes active and responds to ping on
70# internal ip). If you run this script from a nova node, you should
71# bypass security groups and have direct access to the server.
72
73# Waiting for boot
74# ----------------
75
76# Max time to wait while vm goes from build to active state
77ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-30}
78
79# Max time till the vm is bootable
80BOOT_TIMEOUT=${BOOT_TIMEOUT:-15}
81
82# Max time to wait for proper association and dis-association.
83ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-10}
84
85# check that the status is active within ACTIVE_TIMEOUT seconds
86if ! timeout $BOOT_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
87 echo "server didn't become active!"
88 exit 1
89fi
90
91# get the IP of the server
92IP=`nova show $VM_UUID | grep "private network" | cut -d"|" -f3`
93#VM_UUID=`nova list | grep $NAME | head -1 | cut -d'|' -f2 | sed 's/ //g'`
94
95# for single node deployments, we can ping private ips
96MULTI_HOST=${MULTI_HOST:-0}
97if [ "$MULTI_HOST" = "0" ]; then
98 # sometimes the first ping fails (10 seconds isn't enough time for the VM's
99 # network to respond?), so let's ping for a default of 15 seconds with a
100 # timeout of a second for each ping.
101 if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then
102 echo "Couldn't ping server"
103 exit 1
104 fi
105else
106 # On a multi-host system, without vm net access, do a sleep to wait for the boot
107 sleep $BOOT_TIMEOUT
108fi
109
110# Volumes
111# -------
112
113VOL_NAME="myvol-$(openssl rand -hex 4)"
114
115# Verify it doesn't exist
116if [[ -n "`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f3 | sed 's/ //g'`" ]]; then
117 echo "Volume $VOL_NAME already exists"
118 exit 1
119fi
120
121# Create a new volume
122nova volume-create --display_name $VOL_NAME --display_description "test volume: $VOL_NAME" 1
123if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
124 echo "Volume $VOL_NAME not created"
125 exit 1
126fi
127
128# Get volume ID
129VOL_ID=`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f2 | sed 's/ //g'`
130
131# Attach to server
132DEVICE=/dev/vdb
133nova volume-attach $VM_UUID $VOL_ID $DEVICE
134if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then
135 echo "Volume $VOL_NAME not attached to $NAME"
136 exit 1
137fi
138
139VOL_ATTACH=`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f6 | sed 's/ //g'`
140if [[ "$VOL_ATTACH" != $VM_UUID ]]; then
141 echo "Volume not attached to correct instance"
142 exit 1
143fi
144
145# Detach volume
146nova volume-detach $VM_UUID $VOL_ID
147if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
148 echo "Volume $VOL_NAME not detached from $NAME"
149 exit 1
150fi
151
152# Delete volume
153nova volume-delete $VOL_ID
154if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME; do sleep 1; done"; then
155 echo "Volume $VOL_NAME not deleted"
156 exit 1
157fi
158
159# shutdown the server
160nova delete $NAME