blob: 45b8645bbb98758c64115b123debf844dfdb801f [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
Dean Troyer15bda3e2013-01-11 15:07:53 -060089if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
90 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
91fi
92if ! nova secgroup-list-rules $SECGROUP | grep -q " tcp .* 22 "; then
93 nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
94fi
Dean Troyer96288ba2012-08-17 14:11:55 -050095
Dean Troyera8dda172011-12-16 12:22:02 -060096# determinine instance type
97# -------------------------
98
99# List of instance types:
100nova flavor-list
101
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000102INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1`
Dean Troyer1d6e0e12011-12-23 12:45:13 -0600103if [[ -z "$INSTANCE_TYPE" ]]; then
104 # grab the first flavor in the list to launch if default doesn't exist
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000105 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | get_field 1`
Dean Troyera8dda172011-12-16 12:22:02 -0600106fi
107
Dean Troyer489bd2a2012-03-02 10:44:29 -0600108NAME="ex-vol"
Dean Troyera8dda172011-12-16 12:22:02 -0600109
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000110VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600111die_if_not_set VM_UUID "Failure launching $NAME"
112
Dean Troyera8dda172011-12-16 12:22:02 -0600113
114# Testing
115# =======
116
117# First check if it spins up (becomes active and responds to ping on
118# internal ip). If you run this script from a nova node, you should
119# bypass security groups and have direct access to the server.
120
121# Waiting for boot
122# ----------------
123
Dean Troyera8dda172011-12-16 12:22:02 -0600124# check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -0600125if ! 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 -0600126 echo "server didn't become active!"
127 exit 1
128fi
129
130# get the IP of the server
Nachi Uenofda946e2012-10-24 17:26:02 -0700131IP=`nova show $VM_UUID | grep "$PRIVATE_NETWORK_NAME" | get_field 2`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600132die_if_not_set IP "Failure retrieving IP address"
Dean Troyera8dda172011-12-16 12:22:02 -0600133
134# for single node deployments, we can ping private ips
Nachi Uenofda946e2012-10-24 17:26:02 -0700135ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
Dean Troyera8dda172011-12-16 12:22:02 -0600136
137# Volumes
138# -------
139
140VOL_NAME="myvol-$(openssl rand -hex 4)"
141
142# Verify it doesn't exist
John Griffith161e2802012-11-05 13:59:49 -0700143if [[ -n "`cinder list | grep $VOL_NAME | head -1 | get_field 2`" ]]; then
Dean Troyera8dda172011-12-16 12:22:02 -0600144 echo "Volume $VOL_NAME already exists"
145 exit 1
146fi
147
148# Create a new volume
Armando Migliacciob0d8a822012-12-13 16:08:48 +0000149cinder create --display_name $VOL_NAME --display_description "test volume: $VOL_NAME" $DEFAULT_VOLUME_SIZE
Dean Troyer489bd2a2012-03-02 10:44:29 -0600150if [[ $? != 0 ]]; then
151 echo "Failure creating volume $VOL_NAME"
152 exit 1
153fi
John Griffith496ffc72012-09-26 15:09:52 -0600154
155start_time=`date +%s`
John Griffith161e2802012-11-05 13:59:49 -0700156if ! 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 -0600157 echo "Volume $VOL_NAME not created"
158 exit 1
159fi
John Griffith496ffc72012-09-26 15:09:52 -0600160end_time=`date +%s`
John Griffith161e2802012-11-05 13:59:49 -0700161echo "Completed cinder create in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600162
163# Get volume ID
John Griffith161e2802012-11-05 13:59:49 -0700164VOL_ID=`cinder list | grep $VOL_NAME | head -1 | get_field 1`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600165die_if_not_set VOL_ID "Failure retrieving volume ID for $VOL_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600166
167# Attach to server
168DEVICE=/dev/vdb
John Griffith496ffc72012-09-26 15:09:52 -0600169start_time=`date +%s`
Dean Troyer27e32692012-03-16 16:16:56 -0500170nova volume-attach $VM_UUID $VOL_ID $DEVICE || \
171 die "Failure attaching volume $VOL_NAME to $NAME"
John Griffith161e2802012-11-05 13:59:49 -0700172if ! 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 -0600173 echo "Volume $VOL_NAME not attached to $NAME"
174 exit 1
175fi
John Griffith496ffc72012-09-26 15:09:52 -0600176end_time=`date +%s`
177echo "Completed volume-attach in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600178
John Griffith161e2802012-11-05 13:59:49 -0700179VOL_ATTACH=`cinder list | grep $VOL_NAME | head -1 | get_field -1`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600180die_if_not_set VOL_ATTACH "Failure retrieving $VOL_NAME status"
Dean Troyera8dda172011-12-16 12:22:02 -0600181if [[ "$VOL_ATTACH" != $VM_UUID ]]; then
182 echo "Volume not attached to correct instance"
183 exit 1
184fi
185
186# Detach volume
John Griffith496ffc72012-09-26 15:09:52 -0600187start_time=`date +%s`
Dean Troyer27e32692012-03-16 16:16:56 -0500188nova volume-detach $VM_UUID $VOL_ID || die "Failure detaching volume $VOL_NAME from $NAME"
John Griffith161e2802012-11-05 13:59:49 -0700189if ! 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 -0600190 echo "Volume $VOL_NAME not detached from $NAME"
191 exit 1
192fi
John Griffith496ffc72012-09-26 15:09:52 -0600193end_time=`date +%s`
194echo "Completed volume-detach in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600195
196# Delete volume
John Griffith496ffc72012-09-26 15:09:52 -0600197start_time=`date +%s`
John Griffith161e2802012-11-05 13:59:49 -0700198cinder delete $VOL_ID || die "Failure deleting volume $VOL_NAME"
Adam Gandelman756c8422013-01-04 13:37:22 -0800199if ! timeout $ACTIVE_TIMEOUT sh -c "while cinder list | grep $VOL_NAME; do sleep 1; done"; then
Dean Troyera8dda172011-12-16 12:22:02 -0600200 echo "Volume $VOL_NAME not deleted"
201 exit 1
202fi
John Griffith496ffc72012-09-26 15:09:52 -0600203end_time=`date +%s`
John Griffith161e2802012-11-05 13:59:49 -0700204echo "Completed cinder delete in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600205
Dean Troyer96288ba2012-08-17 14:11:55 -0500206# Shutdown the server
207nova delete $VM_UUID || die "Failure deleting instance $NAME"
208
209# Wait for termination
210if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
211 echo "Server $NAME not deleted"
212 exit 1
213fi
214
215# Delete a secgroup
216nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP"
Dean Troyer489bd2a2012-03-02 10:44:29 -0600217
218set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500219echo "*********************************************************************"
220echo "SUCCESS: End DevStack Exercise: $0"
221echo "*********************************************************************"