blob: 1fcc034bbf642a4d3af9c43ed441d0bfe17fad77 [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
Dean Troyer751c1522012-01-10 15:34:34 -060022# Max time to wait while vm goes from build to active state
23ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-30}
24
25# Max time till the vm is bootable
26BOOT_TIMEOUT=${BOOT_TIMEOUT:-30}
27
28# Max time to wait for proper association and dis-association.
29ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-15}
30
31# Instance type to create
32DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
33
34# Boot this image, use first AMi image if unset
35DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
36
Dean Troyera8dda172011-12-16 12:22:02 -060037# Launching a server
38# ==================
39
40# List servers for tenant:
41nova list
42
43# Images
44# ------
45
46# Nova has a **deprecated** way of listing images.
47nova image-list
48
49# But we recommend using glance directly
Dean Troyer80756ea2012-02-01 18:01:01 -060050glance -f index
Dean Troyera8dda172011-12-16 12:22:02 -060051
Dean Troyer751c1522012-01-10 15:34:34 -060052# Grab the id of the image to launch
Dean Troyer80756ea2012-02-01 18:01:01 -060053IMAGE=`glance -f index | egrep $DEFAULT_IMAGE_NAME | head -1 | cut -d" " -f1`
Dean Troyera8dda172011-12-16 12:22:02 -060054
55# determinine instance type
56# -------------------------
57
58# List of instance types:
59nova flavor-list
60
Dean Troyer1d6e0e12011-12-23 12:45:13 -060061INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2`
62if [[ -z "$INSTANCE_TYPE" ]]; then
63 # grab the first flavor in the list to launch if default doesn't exist
Dean Troyera8dda172011-12-16 12:22:02 -060064 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2`
65fi
66
67NAME="myserver"
68
69VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'`
70
71# Testing
72# =======
73
74# First check if it spins up (becomes active and responds to ping on
75# internal ip). If you run this script from a nova node, you should
76# bypass security groups and have direct access to the server.
77
78# Waiting for boot
79# ----------------
80
Dean Troyera8dda172011-12-16 12:22:02 -060081# check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -060082if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
Dean Troyera8dda172011-12-16 12:22:02 -060083 echo "server didn't become active!"
84 exit 1
85fi
86
87# get the IP of the server
88IP=`nova show $VM_UUID | grep "private network" | cut -d"|" -f3`
Dean Troyera8dda172011-12-16 12:22:02 -060089
90# for single node deployments, we can ping private ips
91MULTI_HOST=${MULTI_HOST:-0}
92if [ "$MULTI_HOST" = "0" ]; then
93 # sometimes the first ping fails (10 seconds isn't enough time for the VM's
94 # network to respond?), so let's ping for a default of 15 seconds with a
95 # timeout of a second for each ping.
96 if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then
97 echo "Couldn't ping server"
98 exit 1
99 fi
100else
101 # On a multi-host system, without vm net access, do a sleep to wait for the boot
102 sleep $BOOT_TIMEOUT
103fi
104
105# Volumes
106# -------
107
108VOL_NAME="myvol-$(openssl rand -hex 4)"
109
110# Verify it doesn't exist
111if [[ -n "`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f3 | sed 's/ //g'`" ]]; then
112 echo "Volume $VOL_NAME already exists"
113 exit 1
114fi
115
116# Create a new volume
117nova volume-create --display_name $VOL_NAME --display_description "test volume: $VOL_NAME" 1
118if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
119 echo "Volume $VOL_NAME not created"
120 exit 1
121fi
122
123# Get volume ID
124VOL_ID=`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f2 | sed 's/ //g'`
125
126# Attach to server
127DEVICE=/dev/vdb
128nova volume-attach $VM_UUID $VOL_ID $DEVICE
129if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then
130 echo "Volume $VOL_NAME not attached to $NAME"
131 exit 1
132fi
133
134VOL_ATTACH=`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f6 | sed 's/ //g'`
135if [[ "$VOL_ATTACH" != $VM_UUID ]]; then
136 echo "Volume not attached to correct instance"
137 exit 1
138fi
139
140# Detach volume
141nova volume-detach $VM_UUID $VOL_ID
142if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
143 echo "Volume $VOL_NAME not detached from $NAME"
144 exit 1
145fi
146
147# Delete volume
148nova volume-delete $VOL_ID
149if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME; do sleep 1; done"; then
150 echo "Volume $VOL_NAME not deleted"
151 exit 1
152fi
153
154# shutdown the server
155nova delete $NAME