blob: 42f9cb4e72d6c71d581a7b81457217b1cfeb8da3 [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
John Griffith161e2802012-11-05 13:59:49 -07005# Test cinder volumes with the cinder command from python-cinderclient
Dean Troyera8dda172011-12-16 12:22:02 -06006
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
Joe Gordon6fd28112012-11-13 16:55:41 -080012# only the first error that occurred.
Dean Troyera8dda172011-12-16 12:22:02 -060013set -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
Nachi Ueno5db5bfa2012-10-29 11:25:29 -070036fi
37
Dean Troyer51fb4542012-03-09 22:21:59 -060038# Import exercise configuration
39source $TOP_DIR/exerciserc
Dean Troyer751c1522012-01-10 15:34:34 -060040
Joe Gordon6fd28112012-11-13 16:55:41 -080041# If cinder is not enabled we exit with exitcode 55 which mean
Dean Troyer67787e62012-05-02 11:48:15 -050042# exercise is skipped.
Joe Gordon6fd28112012-11-13 16:55:41 -080043is_service_enabled cinder || exit 55
Dean Troyer67787e62012-05-02 11:48:15 -050044
Dean Troyer751c1522012-01-10 15:34:34 -060045# Instance type to create
46DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
47
48# Boot this image, use first AMi image if unset
49DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
50
Dean Troyer96288ba2012-08-17 14:11:55 -050051# Security group name
52SECGROUP=${SECGROUP:-vol_secgroup}
53
Dean Troyer27e32692012-03-16 16:16:56 -050054
Dean Troyera8dda172011-12-16 12:22:02 -060055# Launching a server
56# ==================
57
58# List servers for tenant:
59nova list
60
61# Images
62# ------
63
64# Nova has a **deprecated** way of listing images.
65nova image-list
66
67# But we recommend using glance directly
Dean Troyer45495252012-04-13 13:16:38 -050068glance image-list
Dean Troyera8dda172011-12-16 12:22:02 -060069
Dean Troyer751c1522012-01-10 15:34:34 -060070# Grab the id of the image to launch
Dean Troyer45495252012-04-13 13:16:38 -050071IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
Dean Troyera8dda172011-12-16 12:22:02 -060072
Dean Troyer96288ba2012-08-17 14:11:55 -050073# Security Groups
74# ---------------
75
76# List of secgroups:
77nova secgroup-list
78
79# Create a secgroup
80if ! nova secgroup-list | grep -q $SECGROUP; then
81 nova secgroup-create $SECGROUP "$SECGROUP description"
82 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
83 echo "Security group not created"
84 exit 1
85 fi
86fi
87
88# Configure Security Group Rules
89nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
90nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
91
Dean Troyera8dda172011-12-16 12:22:02 -060092# determinine instance type
93# -------------------------
94
95# List of instance types:
96nova flavor-list
97
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +000098INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1`
Dean Troyer1d6e0e12011-12-23 12:45:13 -060099if [[ -z "$INSTANCE_TYPE" ]]; then
100 # grab the first flavor in the list to launch if default doesn't exist
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000101 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | get_field 1`
Dean Troyera8dda172011-12-16 12:22:02 -0600102fi
103
Dean Troyer489bd2a2012-03-02 10:44:29 -0600104NAME="ex-vol"
Dean Troyera8dda172011-12-16 12:22:02 -0600105
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000106VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600107die_if_not_set VM_UUID "Failure launching $NAME"
108
Dean Troyera8dda172011-12-16 12:22:02 -0600109
110# Testing
111# =======
112
113# First check if it spins up (becomes active and responds to ping on
114# internal ip). If you run this script from a nova node, you should
115# bypass security groups and have direct access to the server.
116
117# Waiting for boot
118# ----------------
119
Dean Troyera8dda172011-12-16 12:22:02 -0600120# check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -0600121if ! 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 -0600122 echo "server didn't become active!"
123 exit 1
124fi
125
126# get the IP of the server
Nachi Uenofda946e2012-10-24 17:26:02 -0700127IP=`nova show $VM_UUID | grep "$PRIVATE_NETWORK_NAME" | get_field 2`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600128die_if_not_set IP "Failure retrieving IP address"
Dean Troyera8dda172011-12-16 12:22:02 -0600129
130# for single node deployments, we can ping private ips
Nachi Uenofda946e2012-10-24 17:26:02 -0700131ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
Dean Troyera8dda172011-12-16 12:22:02 -0600132
133# Volumes
134# -------
135
136VOL_NAME="myvol-$(openssl rand -hex 4)"
137
138# Verify it doesn't exist
John Griffith161e2802012-11-05 13:59:49 -0700139if [[ -n "`cinder list | grep $VOL_NAME | head -1 | get_field 2`" ]]; then
Dean Troyera8dda172011-12-16 12:22:02 -0600140 echo "Volume $VOL_NAME already exists"
141 exit 1
142fi
143
144# Create a new volume
John Griffith161e2802012-11-05 13:59:49 -0700145cinder create --display_name $VOL_NAME --display_description "test volume: $VOL_NAME" 1
Dean Troyer489bd2a2012-03-02 10:44:29 -0600146if [[ $? != 0 ]]; then
147 echo "Failure creating volume $VOL_NAME"
148 exit 1
149fi
John Griffith496ffc72012-09-26 15:09:52 -0600150
151start_time=`date +%s`
John Griffith161e2802012-11-05 13:59:49 -0700152if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep available; do sleep 1; done"; then
Dean Troyera8dda172011-12-16 12:22:02 -0600153 echo "Volume $VOL_NAME not created"
154 exit 1
155fi
John Griffith496ffc72012-09-26 15:09:52 -0600156end_time=`date +%s`
John Griffith161e2802012-11-05 13:59:49 -0700157echo "Completed cinder create in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600158
159# Get volume ID
John Griffith161e2802012-11-05 13:59:49 -0700160VOL_ID=`cinder list | grep $VOL_NAME | head -1 | get_field 1`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600161die_if_not_set VOL_ID "Failure retrieving volume ID for $VOL_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600162
163# Attach to server
164DEVICE=/dev/vdb
John Griffith496ffc72012-09-26 15:09:52 -0600165start_time=`date +%s`
Dean Troyer27e32692012-03-16 16:16:56 -0500166nova volume-attach $VM_UUID $VOL_ID $DEVICE || \
167 die "Failure attaching volume $VOL_NAME to $NAME"
John Griffith161e2802012-11-05 13:59:49 -0700168if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then
Dean Troyera8dda172011-12-16 12:22:02 -0600169 echo "Volume $VOL_NAME not attached to $NAME"
170 exit 1
171fi
John Griffith496ffc72012-09-26 15:09:52 -0600172end_time=`date +%s`
173echo "Completed volume-attach in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600174
John Griffith161e2802012-11-05 13:59:49 -0700175VOL_ATTACH=`cinder list | grep $VOL_NAME | head -1 | get_field -1`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600176die_if_not_set VOL_ATTACH "Failure retrieving $VOL_NAME status"
Dean Troyera8dda172011-12-16 12:22:02 -0600177if [[ "$VOL_ATTACH" != $VM_UUID ]]; then
178 echo "Volume not attached to correct instance"
179 exit 1
180fi
181
182# Detach volume
John Griffith496ffc72012-09-26 15:09:52 -0600183start_time=`date +%s`
Dean Troyer27e32692012-03-16 16:16:56 -0500184nova volume-detach $VM_UUID $VOL_ID || die "Failure detaching volume $VOL_NAME from $NAME"
John Griffith161e2802012-11-05 13:59:49 -0700185if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep available; do sleep 1; done"; then
Dean Troyera8dda172011-12-16 12:22:02 -0600186 echo "Volume $VOL_NAME not detached from $NAME"
187 exit 1
188fi
John Griffith496ffc72012-09-26 15:09:52 -0600189end_time=`date +%s`
190echo "Completed volume-detach in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600191
192# Delete volume
John Griffith496ffc72012-09-26 15:09:52 -0600193start_time=`date +%s`
John Griffith161e2802012-11-05 13:59:49 -0700194cinder delete $VOL_ID || die "Failure deleting volume $VOL_NAME"
195if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME; do sleep 1; done"; then
Dean Troyera8dda172011-12-16 12:22:02 -0600196 echo "Volume $VOL_NAME not deleted"
197 exit 1
198fi
John Griffith496ffc72012-09-26 15:09:52 -0600199end_time=`date +%s`
John Griffith161e2802012-11-05 13:59:49 -0700200echo "Completed cinder delete in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600201
Dean Troyer96288ba2012-08-17 14:11:55 -0500202# Shutdown the server
203nova delete $VM_UUID || die "Failure deleting instance $NAME"
204
205# Wait for termination
206if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
207 echo "Server $NAME not deleted"
208 exit 1
209fi
210
211# Delete a secgroup
212nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP"
Dean Troyer489bd2a2012-03-02 10:44:29 -0600213
214set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500215echo "*********************************************************************"
216echo "SUCCESS: End DevStack Exercise: $0"
217echo "*********************************************************************"