blob: 622fb185869728cc59d3b97b033edff608d13cc2 [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
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +000058# Helper function to grab a numbered field from python novaclient cli result
59# Fields are numbered starting with 1
60# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
61function get_field () {
62 while read data
63 do
64 if [ "$1" -lt 0 ]; then
65 field="(\$(NF$1))"
66 else
67 field="\$$(($1 + 1))"
68 fi
69 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
70 done
71}
72
Dean Troyera8dda172011-12-16 12:22:02 -060073# List of instance types:
74nova flavor-list
75
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +000076INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1`
Dean Troyer1d6e0e12011-12-23 12:45:13 -060077if [[ -z "$INSTANCE_TYPE" ]]; then
78 # grab the first flavor in the list to launch if default doesn't exist
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +000079 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | get_field 1`
Dean Troyera8dda172011-12-16 12:22:02 -060080fi
81
82NAME="myserver"
83
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +000084VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2`
Dean Troyera8dda172011-12-16 12:22:02 -060085
86# Testing
87# =======
88
89# First check if it spins up (becomes active and responds to ping on
90# internal ip). If you run this script from a nova node, you should
91# bypass security groups and have direct access to the server.
92
93# Waiting for boot
94# ----------------
95
Dean Troyera8dda172011-12-16 12:22:02 -060096# check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -060097if ! 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 -060098 echo "server didn't become active!"
99 exit 1
100fi
101
102# get the IP of the server
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000103IP=`nova show $VM_UUID | grep "private network" | get_field 2`
Dean Troyera8dda172011-12-16 12:22:02 -0600104
105# for single node deployments, we can ping private ips
106MULTI_HOST=${MULTI_HOST:-0}
107if [ "$MULTI_HOST" = "0" ]; then
108 # sometimes the first ping fails (10 seconds isn't enough time for the VM's
109 # network to respond?), so let's ping for a default of 15 seconds with a
110 # timeout of a second for each ping.
111 if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then
112 echo "Couldn't ping server"
113 exit 1
114 fi
115else
116 # On a multi-host system, without vm net access, do a sleep to wait for the boot
117 sleep $BOOT_TIMEOUT
118fi
119
120# Volumes
121# -------
122
123VOL_NAME="myvol-$(openssl rand -hex 4)"
124
125# Verify it doesn't exist
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000126if [[ -n "`nova volume-list | grep $VOL_NAME | head -1 | get_field 2`" ]]; then
Dean Troyera8dda172011-12-16 12:22:02 -0600127 echo "Volume $VOL_NAME already exists"
128 exit 1
129fi
130
131# Create a new volume
132nova volume-create --display_name $VOL_NAME --display_description "test volume: $VOL_NAME" 1
133if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
134 echo "Volume $VOL_NAME not created"
135 exit 1
136fi
137
138# Get volume ID
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000139VOL_ID=`nova volume-list | grep $VOL_NAME | head -1 | get_field 1`
Dean Troyera8dda172011-12-16 12:22:02 -0600140
141# Attach to server
142DEVICE=/dev/vdb
143nova volume-attach $VM_UUID $VOL_ID $DEVICE
144if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then
145 echo "Volume $VOL_NAME not attached to $NAME"
146 exit 1
147fi
148
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000149VOL_ATTACH=`nova volume-list | grep $VOL_NAME | head -1 | get_field -1`
Dean Troyera8dda172011-12-16 12:22:02 -0600150if [[ "$VOL_ATTACH" != $VM_UUID ]]; then
151 echo "Volume not attached to correct instance"
152 exit 1
153fi
154
155# Detach volume
156nova volume-detach $VM_UUID $VOL_ID
157if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
158 echo "Volume $VOL_NAME not detached from $NAME"
159 exit 1
160fi
161
162# Delete volume
163nova volume-delete $VOL_ID
164if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME; do sleep 1; done"; then
165 echo "Volume $VOL_NAME not deleted"
166 exit 1
167fi
168
169# shutdown the server
170nova delete $NAME