blob: a812401a09e12aa063200882e30e2d2284d3fed7 [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
Dean Troyer489bd2a2012-03-02 10:44:29 -06005echo "**************************************************"
6echo "Begin DevStack Exercise: $0"
7echo "**************************************************"
8
Dean Troyera8dda172011-12-16 12:22:02 -06009# This script exits on an error so that errors don't compound and you see
10# only the first error that occured.
11set -o errexit
12
13# Print the commands being run so that we can see the command that triggers
14# an error. It is also useful for following allowing as the install occurs.
15set -o xtrace
16
17
18# Settings
19# ========
20
21# Use openrc + stackrc + localrc for settings
Dean Troyer489bd2a2012-03-02 10:44:29 -060022pushd $(cd $(dirname "$0")/.. && pwd) >/dev/null
23
24# Import common functions
25source ./functions
26
27# Import configuration
Dean Troyera8dda172011-12-16 12:22:02 -060028source ./openrc
Dean Troyer489bd2a2012-03-02 10:44:29 -060029popd >/dev/null
Dean Troyera8dda172011-12-16 12:22:02 -060030
Dean Troyer751c1522012-01-10 15:34:34 -060031# Max time to wait while vm goes from build to active state
32ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-30}
33
34# Max time till the vm is bootable
35BOOT_TIMEOUT=${BOOT_TIMEOUT:-30}
36
37# Max time to wait for proper association and dis-association.
38ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-15}
39
40# Instance type to create
41DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
42
43# Boot this image, use first AMi image if unset
44DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
45
Dean Troyera8dda172011-12-16 12:22:02 -060046# Launching a server
47# ==================
48
49# List servers for tenant:
50nova list
51
52# Images
53# ------
54
55# Nova has a **deprecated** way of listing images.
56nova image-list
57
58# But we recommend using glance directly
Dean Troyer80756ea2012-02-01 18:01:01 -060059glance -f index
Dean Troyera8dda172011-12-16 12:22:02 -060060
Dean Troyer751c1522012-01-10 15:34:34 -060061# Grab the id of the image to launch
Dean Troyer80756ea2012-02-01 18:01:01 -060062IMAGE=`glance -f index | egrep $DEFAULT_IMAGE_NAME | head -1 | cut -d" " -f1`
Dean Troyera8dda172011-12-16 12:22:02 -060063
64# determinine instance type
65# -------------------------
66
67# List of instance types:
68nova flavor-list
69
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +000070INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1`
Dean Troyer1d6e0e12011-12-23 12:45:13 -060071if [[ -z "$INSTANCE_TYPE" ]]; then
72 # grab the first flavor in the list to launch if default doesn't exist
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +000073 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | get_field 1`
Dean Troyera8dda172011-12-16 12:22:02 -060074fi
75
Dean Troyer489bd2a2012-03-02 10:44:29 -060076NAME="ex-vol"
Dean Troyera8dda172011-12-16 12:22:02 -060077
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +000078VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2`
Dean Troyer489bd2a2012-03-02 10:44:29 -060079die_if_not_set VM_UUID "Failure launching $NAME"
80
Dean Troyera8dda172011-12-16 12:22:02 -060081
82# Testing
83# =======
84
85# First check if it spins up (becomes active and responds to ping on
86# internal ip). If you run this script from a nova node, you should
87# bypass security groups and have direct access to the server.
88
89# Waiting for boot
90# ----------------
91
Dean Troyera8dda172011-12-16 12:22:02 -060092# check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -060093if ! 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 -060094 echo "server didn't become active!"
95 exit 1
96fi
97
98# get the IP of the server
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +000099IP=`nova show $VM_UUID | grep "private network" | get_field 2`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600100die_if_not_set IP "Failure retrieving IP address"
Dean Troyera8dda172011-12-16 12:22:02 -0600101
102# for single node deployments, we can ping private ips
103MULTI_HOST=${MULTI_HOST:-0}
104if [ "$MULTI_HOST" = "0" ]; then
105 # sometimes the first ping fails (10 seconds isn't enough time for the VM's
106 # network to respond?), so let's ping for a default of 15 seconds with a
107 # timeout of a second for each ping.
108 if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then
109 echo "Couldn't ping server"
110 exit 1
111 fi
112else
113 # On a multi-host system, without vm net access, do a sleep to wait for the boot
114 sleep $BOOT_TIMEOUT
115fi
116
117# Volumes
118# -------
119
120VOL_NAME="myvol-$(openssl rand -hex 4)"
121
122# Verify it doesn't exist
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000123if [[ -n "`nova volume-list | grep $VOL_NAME | head -1 | get_field 2`" ]]; then
Dean Troyera8dda172011-12-16 12:22:02 -0600124 echo "Volume $VOL_NAME already exists"
125 exit 1
126fi
127
128# Create a new volume
129nova volume-create --display_name $VOL_NAME --display_description "test volume: $VOL_NAME" 1
Dean Troyer489bd2a2012-03-02 10:44:29 -0600130if [[ $? != 0 ]]; then
131 echo "Failure creating volume $VOL_NAME"
132 exit 1
133fi
Dean Troyera8dda172011-12-16 12:22:02 -0600134if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
135 echo "Volume $VOL_NAME not created"
136 exit 1
137fi
138
139# Get volume ID
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000140VOL_ID=`nova volume-list | grep $VOL_NAME | head -1 | get_field 1`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600141die_if_not_set VOL_ID "Failure retrieving volume ID for $VOL_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600142
143# Attach to server
144DEVICE=/dev/vdb
145nova volume-attach $VM_UUID $VOL_ID $DEVICE
Dean Troyer489bd2a2012-03-02 10:44:29 -0600146die_if_error "Failure attaching volume $VOL_NAME to $NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600147if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then
148 echo "Volume $VOL_NAME not attached to $NAME"
149 exit 1
150fi
151
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000152VOL_ATTACH=`nova volume-list | grep $VOL_NAME | head -1 | get_field -1`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600153die_if_not_set VOL_ATTACH "Failure retrieving $VOL_NAME status"
Dean Troyera8dda172011-12-16 12:22:02 -0600154if [[ "$VOL_ATTACH" != $VM_UUID ]]; then
155 echo "Volume not attached to correct instance"
156 exit 1
157fi
158
159# Detach volume
160nova volume-detach $VM_UUID $VOL_ID
Dean Troyer489bd2a2012-03-02 10:44:29 -0600161die_if_error "Failure detaching volume $VOL_NAME from $NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600162if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
163 echo "Volume $VOL_NAME not detached from $NAME"
164 exit 1
165fi
166
167# Delete volume
168nova volume-delete $VOL_ID
Dean Troyer489bd2a2012-03-02 10:44:29 -0600169die_if_error "Failure deleting volume $VOL_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600170if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME; do sleep 1; done"; then
171 echo "Volume $VOL_NAME not deleted"
172 exit 1
173fi
174
175# shutdown the server
176nova delete $NAME
Dean Troyer489bd2a2012-03-02 10:44:29 -0600177die_if_error "Failure deleting instance $NAME"
178
179set +o xtrace
180echo "**************************************************"
181echo "End DevStack Exercise: $0"
182echo "**************************************************"