blob: 504fba1a0458829a61d64cf3e9bf8e77552421f8 [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 Troyerda85cda2013-02-15 11:07:14 -06005# 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
Dean Troyere2907b42014-02-26 17:35:37 -060033# Import project functions
34source $TOP_DIR/lib/cinder
35source $TOP_DIR/lib/neutron
36
Dean Troyer51fb4542012-03-09 22:21:59 -060037# Import exercise configuration
38source $TOP_DIR/exerciserc
Dean Troyer751c1522012-01-10 15:34:34 -060039
Joe Gordon6fd28112012-11-13 16:55:41 -080040# If cinder is not enabled we exit with exitcode 55 which mean
Dean Troyer67787e62012-05-02 11:48:15 -050041# exercise is skipped.
Joe Gordon6fd28112012-11-13 16:55:41 -080042is_service_enabled cinder || exit 55
Dean Troyer67787e62012-05-02 11:48:15 -050043
Adam Gandelmanb875d012014-03-17 19:47:14 -070044# Ironic does not currently support volume attachment.
45[ "$VIRT_DRIVER" == "ironic" ] && exit 55
46
Dean Troyer751c1522012-01-10 15:34:34 -060047# Instance type to create
48DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
49
Dean Troyerda85cda2013-02-15 11:07:14 -060050# Boot this image, use first AMI image if unset
Dean Troyer751c1522012-01-10 15:34:34 -060051DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
52
Dean Troyer96288ba2012-08-17 14:11:55 -050053# Security group name
54SECGROUP=${SECGROUP:-vol_secgroup}
55
Dean Troyerda85cda2013-02-15 11:07:14 -060056# Instance and volume names
57VM_NAME=${VM_NAME:-ex-vol-inst}
58VOL_NAME="ex-vol-$(openssl rand -hex 4)"
59
Dean Troyer27e32692012-03-16 16:16:56 -050060
Dean Troyera8dda172011-12-16 12:22:02 -060061# Launching a server
62# ==================
63
64# List servers for tenant:
65nova list
66
67# Images
68# ------
69
Dean Troyerda85cda2013-02-15 11:07:14 -060070# List the images available
Steve Martinelli5c206c22014-08-02 20:32:31 -040071openstack image list
Dean Troyera8dda172011-12-16 12:22:02 -060072
Dean Troyer751c1522012-01-10 15:34:34 -060073# Grab the id of the image to launch
Steve Martinelli5c206c22014-08-02 20:32:31 -040074IMAGE=$(openstack image list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
Nachi Ueno07115eb2013-02-26 12:38:18 -080075die_if_not_set $LINENO IMAGE "Failure getting image $DEFAULT_IMAGE_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -060076
Dean Troyer96288ba2012-08-17 14:11:55 -050077# Security Groups
78# ---------------
79
Dean Troyerda85cda2013-02-15 11:07:14 -060080# List security groups
Dean Troyer96288ba2012-08-17 14:11:55 -050081nova secgroup-list
82
Chris Behrensc62c2b92013-07-24 03:56:13 -070083if is_service_enabled n-cell; then
84 # Cells does not support security groups, so force the use of "default"
85 SECGROUP="default"
86 echo "Using the default security group because of Cells."
87else
88 # Create a secgroup
89 if ! nova secgroup-list | grep -q $SECGROUP; then
90 nova secgroup-create $SECGROUP "$SECGROUP description"
91 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
92 echo "Security group not created"
93 exit 1
94 fi
Dean Troyer96288ba2012-08-17 14:11:55 -050095 fi
96fi
97
98# Configure Security Group Rules
Dean Troyer15bda3e2013-01-11 15:07:53 -060099if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
100 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
101fi
102if ! nova secgroup-list-rules $SECGROUP | grep -q " tcp .* 22 "; then
103 nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
104fi
Dean Troyer96288ba2012-08-17 14:11:55 -0500105
Dean Troyerda85cda2013-02-15 11:07:14 -0600106# List secgroup rules
107nova secgroup-list-rules $SECGROUP
Dean Troyera8dda172011-12-16 12:22:02 -0600108
Dean Troyerda85cda2013-02-15 11:07:14 -0600109# Set up instance
110# ---------------
111
112# List flavors
Dean Troyera8dda172011-12-16 12:22:02 -0600113nova flavor-list
114
Dean Troyerda85cda2013-02-15 11:07:14 -0600115# Select a flavor
116INSTANCE_TYPE=$(nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1)
Dean Troyer1d6e0e12011-12-23 12:45:13 -0600117if [[ -z "$INSTANCE_TYPE" ]]; then
118 # grab the first flavor in the list to launch if default doesn't exist
Sean Dague922c8ae2013-10-22 10:06:06 -0400119 INSTANCE_TYPE=$(nova flavor-list | head -n 4 | tail -n 1 | get_field 1)
DennyZhang23178a92013-10-22 17:07:32 -0500120 die_if_not_set $LINENO INSTANCE_TYPE "Failure retrieving INSTANCE_TYPE"
Dean Troyera8dda172011-12-16 12:22:02 -0600121fi
122
Dean Troyerda85cda2013-02-15 11:07:14 -0600123# Clean-up from previous runs
124nova delete $VM_NAME || true
125if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VM_NAME; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800126 die $LINENO "server didn't terminate!"
Dean Troyerda85cda2013-02-15 11:07:14 -0600127fi
Dean Troyera8dda172011-12-16 12:22:02 -0600128
Dean Troyerda85cda2013-02-15 11:07:14 -0600129# Boot instance
130# -------------
Dean Troyer489bd2a2012-03-02 10:44:29 -0600131
Dean Troyer526b79f2013-11-22 11:30:44 -0600132VM_UUID=$(nova boot --flavor $INSTANCE_TYPE --image $IMAGE --security-groups=$SECGROUP $VM_NAME | grep ' id ' | get_field 2)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800133die_if_not_set $LINENO VM_UUID "Failure launching $VM_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600134
Dean Troyerda85cda2013-02-15 11:07:14 -0600135# Check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -0600136if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800137 die $LINENO "server didn't become active!"
Dean Troyera8dda172011-12-16 12:22:02 -0600138fi
139
Dean Troyerda85cda2013-02-15 11:07:14 -0600140# Get the instance IP
Nachi Ueno6769b162013-08-12 18:18:56 -0700141IP=$(get_instance_ip $VM_UUID $PRIVATE_NETWORK_NAME)
142
Nachi Ueno07115eb2013-02-26 12:38:18 -0800143die_if_not_set $LINENO IP "Failure retrieving IP address"
Dean Troyera8dda172011-12-16 12:22:02 -0600144
Dean Troyerda85cda2013-02-15 11:07:14 -0600145# Private IPs can be pinged in single node deployments
Nachi Uenofda946e2012-10-24 17:26:02 -0700146ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
Dean Troyera8dda172011-12-16 12:22:02 -0600147
148# Volumes
149# -------
150
Dean Troyera8dda172011-12-16 12:22:02 -0600151# Verify it doesn't exist
Dean Troyerda85cda2013-02-15 11:07:14 -0600152if [[ -n $(cinder list | grep $VOL_NAME | head -1 | get_field 2) ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800153 die $LINENO "Volume $VOL_NAME already exists"
Dean Troyera8dda172011-12-16 12:22:02 -0600154fi
155
156# Create a new volume
Dean Troyerda85cda2013-02-15 11:07:14 -0600157start_time=$(date +%s)
Dean Troyer526b79f2013-11-22 11:30:44 -0600158cinder create --display-name $VOL_NAME --display-description "test volume: $VOL_NAME" $DEFAULT_VOLUME_SIZE || \
Nachi Ueno07115eb2013-02-26 12:38:18 -0800159 die $LINENO "Failure creating volume $VOL_NAME"
John Griffith161e2802012-11-05 13:59:49 -0700160if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep available; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800161 die $LINENO "Volume $VOL_NAME not created"
Dean Troyera8dda172011-12-16 12:22:02 -0600162fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600163end_time=$(date +%s)
John Griffith161e2802012-11-05 13:59:49 -0700164echo "Completed cinder create in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600165
166# Get volume ID
Dean Troyerda85cda2013-02-15 11:07:14 -0600167VOL_ID=$(cinder list | grep $VOL_NAME | head -1 | get_field 1)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800168die_if_not_set $LINENO VOL_ID "Failure retrieving volume ID for $VOL_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600169
170# Attach to server
171DEVICE=/dev/vdb
Dean Troyerda85cda2013-02-15 11:07:14 -0600172start_time=$(date +%s)
Dean Troyer27e32692012-03-16 16:16:56 -0500173nova volume-attach $VM_UUID $VOL_ID $DEVICE || \
Nachi Ueno07115eb2013-02-26 12:38:18 -0800174 die $LINENO "Failure attaching volume $VOL_NAME to $VM_NAME"
John Griffith161e2802012-11-05 13:59:49 -0700175if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800176 die $LINENO "Volume $VOL_NAME not attached to $VM_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600177fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600178end_time=$(date +%s)
John Griffith496ffc72012-09-26 15:09:52 -0600179echo "Completed volume-attach in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600180
Dean Troyerda85cda2013-02-15 11:07:14 -0600181VOL_ATTACH=$(cinder list | grep $VOL_NAME | head -1 | get_field -1)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800182die_if_not_set $LINENO VOL_ATTACH "Failure retrieving $VOL_NAME status"
Dean Troyera8dda172011-12-16 12:22:02 -0600183if [[ "$VOL_ATTACH" != $VM_UUID ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800184 die $LINENO "Volume not attached to correct instance"
Dean Troyera8dda172011-12-16 12:22:02 -0600185fi
186
Dean Troyerda85cda2013-02-15 11:07:14 -0600187# Clean up
188# --------
189
Dean Troyera8dda172011-12-16 12:22:02 -0600190# Detach volume
Dean Troyerda85cda2013-02-15 11:07:14 -0600191start_time=$(date +%s)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800192nova volume-detach $VM_UUID $VOL_ID || die $LINENO "Failure detaching volume $VOL_NAME from $VM_NAME"
John Griffith161e2802012-11-05 13:59:49 -0700193if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep available; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800194 die $LINENO "Volume $VOL_NAME not detached from $VM_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600195fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600196end_time=$(date +%s)
John Griffith496ffc72012-09-26 15:09:52 -0600197echo "Completed volume-detach in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600198
199# Delete volume
Dean Troyerda85cda2013-02-15 11:07:14 -0600200start_time=$(date +%s)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800201cinder delete $VOL_ID || die $LINENO "Failure deleting volume $VOL_NAME"
Adam Gandelman756c8422013-01-04 13:37:22 -0800202if ! timeout $ACTIVE_TIMEOUT sh -c "while cinder list | grep $VOL_NAME; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800203 die $LINENO "Volume $VOL_NAME not deleted"
Dean Troyera8dda172011-12-16 12:22:02 -0600204fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600205end_time=$(date +%s)
John Griffith161e2802012-11-05 13:59:49 -0700206echo "Completed cinder delete in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600207
Dean Troyerda85cda2013-02-15 11:07:14 -0600208# Delete instance
Nachi Ueno07115eb2013-02-26 12:38:18 -0800209nova delete $VM_UUID || die $LINENO "Failure deleting instance $VM_NAME"
Dean Troyer96288ba2012-08-17 14:11:55 -0500210if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800211 die $LINENO "Server $VM_NAME not deleted"
Dean Troyer96288ba2012-08-17 14:11:55 -0500212fi
213
Chris Behrensc62c2b92013-07-24 03:56:13 -0700214if [[ $SECGROUP = "default" ]] ; then
215 echo "Skipping deleting default security group"
216else
217 # Delete secgroup
218 nova secgroup-delete $SECGROUP || die $LINENO "Failure deleting security group $SECGROUP"
219fi
Dean Troyer489bd2a2012-03-02 10:44:29 -0600220
221set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500222echo "*********************************************************************"
223echo "SUCCESS: End DevStack Exercise: $0"
224echo "*********************************************************************"