blob: 8533993d51d97b03ccca3e27850ba48630e147a1 [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
Nachi Ueno5db5bfa2012-10-29 11:25:29 -070033# Import quantum functions if needed
34if is_service_enabled quantum; then
35 source $TOP_DIR/lib/quantum
36 setup_quantum
37fi
38
Dean Troyer51fb4542012-03-09 22:21:59 -060039# Import exercise configuration
40source $TOP_DIR/exerciserc
Dean Troyer751c1522012-01-10 15:34:34 -060041
Dean Troyer67787e62012-05-02 11:48:15 -050042# If cinder or n-vol are not enabled we exit with exitcode 55 which mean
43# exercise is skipped.
44is_service_enabled cinder n-vol || exit 55
45
Dean Troyer751c1522012-01-10 15:34:34 -060046# Instance type to create
47DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
48
49# Boot this image, use first AMi image if unset
50DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
51
Dean Troyer96288ba2012-08-17 14:11:55 -050052# Security group name
53SECGROUP=${SECGROUP:-vol_secgroup}
54
Dean Troyer27e32692012-03-16 16:16:56 -050055
Dean Troyera8dda172011-12-16 12:22:02 -060056# Launching a server
57# ==================
58
59# List servers for tenant:
60nova list
61
62# Images
63# ------
64
65# Nova has a **deprecated** way of listing images.
66nova image-list
67
68# But we recommend using glance directly
Dean Troyer45495252012-04-13 13:16:38 -050069glance image-list
Dean Troyera8dda172011-12-16 12:22:02 -060070
Dean Troyer751c1522012-01-10 15:34:34 -060071# Grab the id of the image to launch
Dean Troyer45495252012-04-13 13:16:38 -050072IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
Dean Troyera8dda172011-12-16 12:22:02 -060073
Dean Troyer96288ba2012-08-17 14:11:55 -050074# Security Groups
75# ---------------
76
77# List of secgroups:
78nova secgroup-list
79
80# Create a secgroup
81if ! nova secgroup-list | grep -q $SECGROUP; then
82 nova secgroup-create $SECGROUP "$SECGROUP description"
83 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
84 echo "Security group not created"
85 exit 1
86 fi
87fi
88
89# Configure Security Group Rules
90nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
91nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
92
Dean Troyera8dda172011-12-16 12:22:02 -060093# determinine instance type
94# -------------------------
95
96# List of instance types:
97nova flavor-list
98
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +000099INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1`
Dean Troyer1d6e0e12011-12-23 12:45:13 -0600100if [[ -z "$INSTANCE_TYPE" ]]; then
101 # grab the first flavor in the list to launch if default doesn't exist
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000102 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | get_field 1`
Dean Troyera8dda172011-12-16 12:22:02 -0600103fi
104
Dean Troyer489bd2a2012-03-02 10:44:29 -0600105NAME="ex-vol"
Dean Troyera8dda172011-12-16 12:22:02 -0600106
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000107VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600108die_if_not_set VM_UUID "Failure launching $NAME"
109
Dean Troyera8dda172011-12-16 12:22:02 -0600110
111# Testing
112# =======
113
114# First check if it spins up (becomes active and responds to ping on
115# internal ip). If you run this script from a nova node, you should
116# bypass security groups and have direct access to the server.
117
118# Waiting for boot
119# ----------------
120
Dean Troyera8dda172011-12-16 12:22:02 -0600121# check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -0600122if ! 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 -0600123 echo "server didn't become active!"
124 exit 1
125fi
126
127# get the IP of the server
Nachi Uenofda946e2012-10-24 17:26:02 -0700128IP=`nova show $VM_UUID | grep "$PRIVATE_NETWORK_NAME" | get_field 2`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600129die_if_not_set IP "Failure retrieving IP address"
Dean Troyera8dda172011-12-16 12:22:02 -0600130
131# for single node deployments, we can ping private ips
Nachi Uenofda946e2012-10-24 17:26:02 -0700132ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
Dean Troyera8dda172011-12-16 12:22:02 -0600133
134# Volumes
135# -------
136
137VOL_NAME="myvol-$(openssl rand -hex 4)"
138
139# Verify it doesn't exist
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000140if [[ -n "`nova volume-list | grep $VOL_NAME | head -1 | get_field 2`" ]]; then
Dean Troyera8dda172011-12-16 12:22:02 -0600141 echo "Volume $VOL_NAME already exists"
142 exit 1
143fi
144
145# Create a new volume
146nova volume-create --display_name $VOL_NAME --display_description "test volume: $VOL_NAME" 1
Dean Troyer489bd2a2012-03-02 10:44:29 -0600147if [[ $? != 0 ]]; then
148 echo "Failure creating volume $VOL_NAME"
149 exit 1
150fi
John Griffith496ffc72012-09-26 15:09:52 -0600151
152start_time=`date +%s`
Dean Troyera8dda172011-12-16 12:22:02 -0600153if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
154 echo "Volume $VOL_NAME not created"
155 exit 1
156fi
John Griffith496ffc72012-09-26 15:09:52 -0600157end_time=`date +%s`
158echo "Completed volume-create in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600159
160# Get volume ID
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000161VOL_ID=`nova volume-list | grep $VOL_NAME | head -1 | get_field 1`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600162die_if_not_set VOL_ID "Failure retrieving volume ID for $VOL_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600163
164# Attach to server
165DEVICE=/dev/vdb
John Griffith496ffc72012-09-26 15:09:52 -0600166start_time=`date +%s`
Dean Troyer27e32692012-03-16 16:16:56 -0500167nova volume-attach $VM_UUID $VOL_ID $DEVICE || \
168 die "Failure attaching volume $VOL_NAME to $NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600169if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then
170 echo "Volume $VOL_NAME not attached to $NAME"
171 exit 1
172fi
John Griffith496ffc72012-09-26 15:09:52 -0600173end_time=`date +%s`
174echo "Completed volume-attach in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600175
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000176VOL_ATTACH=`nova volume-list | grep $VOL_NAME | head -1 | get_field -1`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600177die_if_not_set VOL_ATTACH "Failure retrieving $VOL_NAME status"
Dean Troyera8dda172011-12-16 12:22:02 -0600178if [[ "$VOL_ATTACH" != $VM_UUID ]]; then
179 echo "Volume not attached to correct instance"
180 exit 1
181fi
182
183# Detach volume
John Griffith496ffc72012-09-26 15:09:52 -0600184start_time=`date +%s`
Dean Troyer27e32692012-03-16 16:16:56 -0500185nova volume-detach $VM_UUID $VOL_ID || die "Failure detaching volume $VOL_NAME from $NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600186if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
187 echo "Volume $VOL_NAME not detached from $NAME"
188 exit 1
189fi
John Griffith496ffc72012-09-26 15:09:52 -0600190end_time=`date +%s`
191echo "Completed volume-detach in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600192
193# Delete volume
John Griffith496ffc72012-09-26 15:09:52 -0600194start_time=`date +%s`
Dean Troyer27e32692012-03-16 16:16:56 -0500195nova volume-delete $VOL_ID || die "Failure deleting volume $VOL_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600196if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME; do sleep 1; done"; then
197 echo "Volume $VOL_NAME not deleted"
198 exit 1
199fi
John Griffith496ffc72012-09-26 15:09:52 -0600200end_time=`date +%s`
201echo "Completed volume-delete in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600202
Dean Troyer96288ba2012-08-17 14:11:55 -0500203# Shutdown the server
204nova delete $VM_UUID || die "Failure deleting instance $NAME"
205
206# Wait for termination
207if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
208 echo "Server $NAME not deleted"
209 exit 1
210fi
211
212# Delete a secgroup
213nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP"
Dean Troyer489bd2a2012-03-02 10:44:29 -0600214
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700215if is_service_enabled quantum; then
216 teardown_quantum
217fi
218
Dean Troyer489bd2a2012-03-02 10:44:29 -0600219set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500220echo "*********************************************************************"
221echo "SUCCESS: End DevStack Exercise: $0"
222echo "*********************************************************************"