blob: 77c3498c0c4f9fdd6be59af0a61897e939ea9feb [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
Dean Troyer51fb4542012-03-09 22:21:59 -060021# Keep track of the current directory
22EXERCISE_DIR=$(cd $(dirname "$0") && pwd)
23TOP_DIR=$(cd $EXERCISE_DIR/..; pwd)
Dean Troyer489bd2a2012-03-02 10:44:29 -060024
25# Import common functions
Dean Troyer51fb4542012-03-09 22:21:59 -060026source $TOP_DIR/functions
Dean Troyer489bd2a2012-03-02 10:44:29 -060027
28# Import configuration
Dean Troyer51fb4542012-03-09 22:21:59 -060029source $TOP_DIR/openrc
Dean Troyera8dda172011-12-16 12:22:02 -060030
Dean Troyer51fb4542012-03-09 22:21:59 -060031# Import exercise configuration
32source $TOP_DIR/exerciserc
Dean Troyer751c1522012-01-10 15:34:34 -060033
34# Instance type to create
35DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
36
37# Boot this image, use first AMi image if unset
38DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
39
Dean Troyera8dda172011-12-16 12:22:02 -060040# Launching a server
41# ==================
42
43# List servers for tenant:
44nova list
45
46# Images
47# ------
48
49# Nova has a **deprecated** way of listing images.
50nova image-list
51
52# But we recommend using glance directly
Dean Troyer80756ea2012-02-01 18:01:01 -060053glance -f index
Dean Troyera8dda172011-12-16 12:22:02 -060054
Dean Troyer751c1522012-01-10 15:34:34 -060055# Grab the id of the image to launch
Dean Troyer80756ea2012-02-01 18:01:01 -060056IMAGE=`glance -f index | egrep $DEFAULT_IMAGE_NAME | head -1 | cut -d" " -f1`
Dean Troyera8dda172011-12-16 12:22:02 -060057
58# determinine instance type
59# -------------------------
60
61# List of instance types:
62nova flavor-list
63
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +000064INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1`
Dean Troyer1d6e0e12011-12-23 12:45:13 -060065if [[ -z "$INSTANCE_TYPE" ]]; then
66 # grab the first flavor in the list to launch if default doesn't exist
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +000067 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | get_field 1`
Dean Troyera8dda172011-12-16 12:22:02 -060068fi
69
Dean Troyer489bd2a2012-03-02 10:44:29 -060070NAME="ex-vol"
Dean Troyera8dda172011-12-16 12:22:02 -060071
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +000072VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2`
Dean Troyer489bd2a2012-03-02 10:44:29 -060073die_if_not_set VM_UUID "Failure launching $NAME"
74
Dean Troyera8dda172011-12-16 12:22:02 -060075
76# Testing
77# =======
78
79# First check if it spins up (becomes active and responds to ping on
80# internal ip). If you run this script from a nova node, you should
81# bypass security groups and have direct access to the server.
82
83# Waiting for boot
84# ----------------
85
Dean Troyera8dda172011-12-16 12:22:02 -060086# check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -060087if ! 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 -060088 echo "server didn't become active!"
89 exit 1
90fi
91
92# get the IP of the server
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +000093IP=`nova show $VM_UUID | grep "private network" | get_field 2`
Dean Troyer489bd2a2012-03-02 10:44:29 -060094die_if_not_set IP "Failure retrieving IP address"
Dean Troyera8dda172011-12-16 12:22:02 -060095
96# for single node deployments, we can ping private ips
97MULTI_HOST=${MULTI_HOST:-0}
98if [ "$MULTI_HOST" = "0" ]; then
99 # sometimes the first ping fails (10 seconds isn't enough time for the VM's
100 # network to respond?), so let's ping for a default of 15 seconds with a
101 # timeout of a second for each ping.
102 if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then
103 echo "Couldn't ping server"
104 exit 1
105 fi
106else
107 # On a multi-host system, without vm net access, do a sleep to wait for the boot
108 sleep $BOOT_TIMEOUT
109fi
110
111# Volumes
112# -------
113
114VOL_NAME="myvol-$(openssl rand -hex 4)"
115
116# Verify it doesn't exist
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000117if [[ -n "`nova volume-list | grep $VOL_NAME | head -1 | get_field 2`" ]]; then
Dean Troyera8dda172011-12-16 12:22:02 -0600118 echo "Volume $VOL_NAME already exists"
119 exit 1
120fi
121
122# Create a new volume
123nova volume-create --display_name $VOL_NAME --display_description "test volume: $VOL_NAME" 1
Dean Troyer489bd2a2012-03-02 10:44:29 -0600124if [[ $? != 0 ]]; then
125 echo "Failure creating volume $VOL_NAME"
126 exit 1
127fi
Dean Troyera8dda172011-12-16 12:22:02 -0600128if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
129 echo "Volume $VOL_NAME not created"
130 exit 1
131fi
132
133# Get volume ID
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000134VOL_ID=`nova volume-list | grep $VOL_NAME | head -1 | get_field 1`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600135die_if_not_set VOL_ID "Failure retrieving volume ID for $VOL_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600136
137# Attach to server
138DEVICE=/dev/vdb
139nova volume-attach $VM_UUID $VOL_ID $DEVICE
Dean Troyer489bd2a2012-03-02 10:44:29 -0600140die_if_error "Failure attaching volume $VOL_NAME to $NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600141if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then
142 echo "Volume $VOL_NAME not attached to $NAME"
143 exit 1
144fi
145
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000146VOL_ATTACH=`nova volume-list | grep $VOL_NAME | head -1 | get_field -1`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600147die_if_not_set VOL_ATTACH "Failure retrieving $VOL_NAME status"
Dean Troyera8dda172011-12-16 12:22:02 -0600148if [[ "$VOL_ATTACH" != $VM_UUID ]]; then
149 echo "Volume not attached to correct instance"
150 exit 1
151fi
152
153# Detach volume
154nova volume-detach $VM_UUID $VOL_ID
Dean Troyer489bd2a2012-03-02 10:44:29 -0600155die_if_error "Failure detaching volume $VOL_NAME from $NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600156if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
157 echo "Volume $VOL_NAME not detached from $NAME"
158 exit 1
159fi
160
161# Delete volume
162nova volume-delete $VOL_ID
Dean Troyer489bd2a2012-03-02 10:44:29 -0600163die_if_error "Failure deleting volume $VOL_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600164if ! 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
Dean Troyer489bd2a2012-03-02 10:44:29 -0600171die_if_error "Failure deleting instance $NAME"
172
173set +o xtrace
174echo "**************************************************"
175echo "End DevStack Exercise: $0"
176echo "**************************************************"