blob: 0f25355f6238e893a26f84a88396daac355a4e2f [file] [log] [blame]
Dean Troyera8dda172011-12-16 12:22:02 -06001#!/usr/bin/env bash
2
Dean Troyer27e32692012-03-16 16:16:56 -05003# **volumes.sh**
4
Dean Troyera8dda172011-12-16 12:22:02 -06005# Test nova volumes with the nova command from python-novaclient
6
Dean Troyer27e32692012-03-16 16:16:56 -05007echo "*********************************************************************"
Dean Troyer489bd2a2012-03-02 10:44:29 -06008echo "Begin DevStack Exercise: $0"
Dean Troyer27e32692012-03-16 16:16:56 -05009echo "*********************************************************************"
Dean Troyer489bd2a2012-03-02 10:44:29 -060010
Dean Troyera8dda172011-12-16 12:22:02 -060011# This script exits on an error so that errors don't compound and you see
12# only the first error that occured.
13set -o errexit
14
15# Print the commands being run so that we can see the command that triggers
16# an error. It is also useful for following allowing as the install occurs.
17set -o xtrace
18
19
20# Settings
21# ========
22
Dean Troyer51fb4542012-03-09 22:21:59 -060023# Keep track of the current directory
24EXERCISE_DIR=$(cd $(dirname "$0") && pwd)
25TOP_DIR=$(cd $EXERCISE_DIR/..; pwd)
Dean Troyer489bd2a2012-03-02 10:44:29 -060026
27# Import common functions
Dean Troyer51fb4542012-03-09 22:21:59 -060028source $TOP_DIR/functions
Dean Troyer489bd2a2012-03-02 10:44:29 -060029
30# Import configuration
Dean Troyer51fb4542012-03-09 22:21:59 -060031source $TOP_DIR/openrc
Dean Troyera8dda172011-12-16 12:22:02 -060032
Dean Troyer51fb4542012-03-09 22:21:59 -060033# Import exercise configuration
34source $TOP_DIR/exerciserc
Dean Troyer751c1522012-01-10 15:34:34 -060035
Dean Troyer67787e62012-05-02 11:48:15 -050036# If cinder or n-vol are not enabled we exit with exitcode 55 which mean
37# exercise is skipped.
38is_service_enabled cinder n-vol || exit 55
39
Dean Troyer751c1522012-01-10 15:34:34 -060040# 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 Troyer27e32692012-03-16 16:16:56 -050046
Dean Troyera8dda172011-12-16 12:22:02 -060047# Launching a server
48# ==================
49
50# List servers for tenant:
51nova list
52
53# Images
54# ------
55
56# Nova has a **deprecated** way of listing images.
57nova image-list
58
59# But we recommend using glance directly
Dean Troyer45495252012-04-13 13:16:38 -050060glance image-list
Dean Troyera8dda172011-12-16 12:22:02 -060061
Dean Troyer751c1522012-01-10 15:34:34 -060062# Grab the id of the image to launch
Dean Troyer45495252012-04-13 13:16:38 -050063IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
Dean Troyera8dda172011-12-16 12:22:02 -060064
65# determinine instance type
66# -------------------------
67
68# List of instance types:
69nova flavor-list
70
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +000071INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1`
Dean Troyer1d6e0e12011-12-23 12:45:13 -060072if [[ -z "$INSTANCE_TYPE" ]]; then
73 # grab the first flavor in the list to launch if default doesn't exist
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +000074 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | get_field 1`
Dean Troyera8dda172011-12-16 12:22:02 -060075fi
76
Dean Troyer489bd2a2012-03-02 10:44:29 -060077NAME="ex-vol"
Dean Troyera8dda172011-12-16 12:22:02 -060078
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +000079VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2`
Dean Troyer489bd2a2012-03-02 10:44:29 -060080die_if_not_set VM_UUID "Failure launching $NAME"
81
Dean Troyera8dda172011-12-16 12:22:02 -060082
83# Testing
84# =======
85
86# First check if it spins up (becomes active and responds to ping on
87# internal ip). If you run this script from a nova node, you should
88# bypass security groups and have direct access to the server.
89
90# Waiting for boot
91# ----------------
92
Dean Troyera8dda172011-12-16 12:22:02 -060093# check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -060094if ! 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 -060095 echo "server didn't become active!"
96 exit 1
97fi
98
99# get the IP of the server
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000100IP=`nova show $VM_UUID | grep "private network" | get_field 2`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600101die_if_not_set IP "Failure retrieving IP address"
Dean Troyera8dda172011-12-16 12:22:02 -0600102
103# for single node deployments, we can ping private ips
Armando Migliaccio7d13f302012-04-19 22:26:16 +0100104MULTI_HOST=`trueorfalse False $MULTI_HOST`
105if [ "$MULTI_HOST" = "False" ]; then
Dean Troyera8dda172011-12-16 12:22:02 -0600106 # sometimes the first ping fails (10 seconds isn't enough time for the VM's
107 # network to respond?), so let's ping for a default of 15 seconds with a
108 # timeout of a second for each ping.
109 if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then
110 echo "Couldn't ping server"
111 exit 1
112 fi
113else
114 # On a multi-host system, without vm net access, do a sleep to wait for the boot
115 sleep $BOOT_TIMEOUT
116fi
117
118# Volumes
119# -------
120
121VOL_NAME="myvol-$(openssl rand -hex 4)"
122
123# Verify it doesn't exist
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000124if [[ -n "`nova volume-list | grep $VOL_NAME | head -1 | get_field 2`" ]]; then
Dean Troyera8dda172011-12-16 12:22:02 -0600125 echo "Volume $VOL_NAME already exists"
126 exit 1
127fi
128
129# Create a new volume
130nova volume-create --display_name $VOL_NAME --display_description "test volume: $VOL_NAME" 1
Dean Troyer489bd2a2012-03-02 10:44:29 -0600131if [[ $? != 0 ]]; then
132 echo "Failure creating volume $VOL_NAME"
133 exit 1
134fi
Dean Troyera8dda172011-12-16 12:22:02 -0600135if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
136 echo "Volume $VOL_NAME not created"
137 exit 1
138fi
139
140# Get volume ID
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000141VOL_ID=`nova volume-list | grep $VOL_NAME | head -1 | get_field 1`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600142die_if_not_set VOL_ID "Failure retrieving volume ID for $VOL_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600143
144# Attach to server
145DEVICE=/dev/vdb
Dean Troyer27e32692012-03-16 16:16:56 -0500146nova volume-attach $VM_UUID $VOL_ID $DEVICE || \
147 die "Failure attaching volume $VOL_NAME to $NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600148if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then
149 echo "Volume $VOL_NAME not attached to $NAME"
150 exit 1
151fi
152
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000153VOL_ATTACH=`nova volume-list | grep $VOL_NAME | head -1 | get_field -1`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600154die_if_not_set VOL_ATTACH "Failure retrieving $VOL_NAME status"
Dean Troyera8dda172011-12-16 12:22:02 -0600155if [[ "$VOL_ATTACH" != $VM_UUID ]]; then
156 echo "Volume not attached to correct instance"
157 exit 1
158fi
159
160# Detach volume
Dean Troyer27e32692012-03-16 16:16:56 -0500161nova volume-detach $VM_UUID $VOL_ID || die "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
Dean Troyer27e32692012-03-16 16:16:56 -0500168nova volume-delete $VOL_ID || die "Failure deleting volume $VOL_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600169if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME; do sleep 1; done"; then
170 echo "Volume $VOL_NAME not deleted"
171 exit 1
172fi
173
174# shutdown the server
Dean Troyer27e32692012-03-16 16:16:56 -0500175nova delete $NAME || die "Failure deleting instance $NAME"
Dean Troyer489bd2a2012-03-02 10:44:29 -0600176
177set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500178echo "*********************************************************************"
179echo "SUCCESS: End DevStack Exercise: $0"
180echo "*********************************************************************"