blob: b2b391c5d7acf55fd330146562e404de7aeca689 [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
Mark McClainb05c8762013-07-06 23:29:39 -040033# Import neutron functions if needed
34if is_service_enabled neutron; then
35 source $TOP_DIR/lib/neutron
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
Dean Troyerda85cda2013-02-15 11:07:14 -060048# Boot this image, use first AMI image if unset
Dean Troyer751c1522012-01-10 15:34:34 -060049DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
50
Dean Troyer96288ba2012-08-17 14:11:55 -050051# Security group name
52SECGROUP=${SECGROUP:-vol_secgroup}
53
Dean Troyerda85cda2013-02-15 11:07:14 -060054# Instance and volume names
55VM_NAME=${VM_NAME:-ex-vol-inst}
56VOL_NAME="ex-vol-$(openssl rand -hex 4)"
57
Dean Troyer27e32692012-03-16 16:16:56 -050058
Dean Troyera8dda172011-12-16 12:22:02 -060059# Launching a server
60# ==================
61
62# List servers for tenant:
63nova list
64
65# Images
66# ------
67
Dean Troyerda85cda2013-02-15 11:07:14 -060068# List the images available
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)
Nachi Ueno07115eb2013-02-26 12:38:18 -080073die_if_not_set $LINENO IMAGE "Failure getting image $DEFAULT_IMAGE_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -060074
Dean Troyer96288ba2012-08-17 14:11:55 -050075# Security Groups
76# ---------------
77
Dean Troyerda85cda2013-02-15 11:07:14 -060078# List security groups
Dean Troyer96288ba2012-08-17 14:11:55 -050079nova secgroup-list
80
Chris Behrensc62c2b92013-07-24 03:56:13 -070081if is_service_enabled n-cell; then
82 # Cells does not support security groups, so force the use of "default"
83 SECGROUP="default"
84 echo "Using the default security group because of Cells."
85else
86 # Create a secgroup
87 if ! nova secgroup-list | grep -q $SECGROUP; then
88 nova secgroup-create $SECGROUP "$SECGROUP description"
89 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
90 echo "Security group not created"
91 exit 1
92 fi
Dean Troyer96288ba2012-08-17 14:11:55 -050093 fi
94fi
95
96# Configure Security Group Rules
Dean Troyer15bda3e2013-01-11 15:07:53 -060097if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
98 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
99fi
100if ! nova secgroup-list-rules $SECGROUP | grep -q " tcp .* 22 "; then
101 nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
102fi
Dean Troyer96288ba2012-08-17 14:11:55 -0500103
Dean Troyerda85cda2013-02-15 11:07:14 -0600104# List secgroup rules
105nova secgroup-list-rules $SECGROUP
Dean Troyera8dda172011-12-16 12:22:02 -0600106
Dean Troyerda85cda2013-02-15 11:07:14 -0600107# Set up instance
108# ---------------
109
110# List flavors
Dean Troyera8dda172011-12-16 12:22:02 -0600111nova flavor-list
112
Dean Troyerda85cda2013-02-15 11:07:14 -0600113# Select a flavor
114INSTANCE_TYPE=$(nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1)
Dean Troyer1d6e0e12011-12-23 12:45:13 -0600115if [[ -z "$INSTANCE_TYPE" ]]; then
116 # grab the first flavor in the list to launch if default doesn't exist
Dean Troyerda85cda2013-02-15 11:07:14 -0600117 INSTANCE_TYPE=$(nova flavor-list | head -n 4 | tail -n 1 | get_field 1)
Dean Troyera8dda172011-12-16 12:22:02 -0600118fi
119
Dean Troyerda85cda2013-02-15 11:07:14 -0600120# Clean-up from previous runs
121nova delete $VM_NAME || true
122if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VM_NAME; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800123 die $LINENO "server didn't terminate!"
Dean Troyerda85cda2013-02-15 11:07:14 -0600124fi
Dean Troyera8dda172011-12-16 12:22:02 -0600125
Dean Troyerda85cda2013-02-15 11:07:14 -0600126# Boot instance
127# -------------
Dean Troyer489bd2a2012-03-02 10:44:29 -0600128
Dean Troyerda85cda2013-02-15 11:07:14 -0600129VM_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 -0800130die_if_not_set $LINENO VM_UUID "Failure launching $VM_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600131
Dean Troyerda85cda2013-02-15 11:07:14 -0600132# Check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -0600133if ! 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 -0800134 die $LINENO "server didn't become active!"
Dean Troyera8dda172011-12-16 12:22:02 -0600135fi
136
Dean Troyerda85cda2013-02-15 11:07:14 -0600137# Get the instance IP
138IP=$(nova show $VM_UUID | grep "$PRIVATE_NETWORK_NAME" | get_field 2)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800139die_if_not_set $LINENO IP "Failure retrieving IP address"
Dean Troyera8dda172011-12-16 12:22:02 -0600140
Dean Troyerda85cda2013-02-15 11:07:14 -0600141# Private IPs can be pinged in single node deployments
Nachi Uenofda946e2012-10-24 17:26:02 -0700142ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
Dean Troyera8dda172011-12-16 12:22:02 -0600143
144# Volumes
145# -------
146
Dean Troyera8dda172011-12-16 12:22:02 -0600147# Verify it doesn't exist
Dean Troyerda85cda2013-02-15 11:07:14 -0600148if [[ -n $(cinder list | grep $VOL_NAME | head -1 | get_field 2) ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800149 die $LINENO "Volume $VOL_NAME already exists"
Dean Troyera8dda172011-12-16 12:22:02 -0600150fi
151
152# Create a new volume
Dean Troyerda85cda2013-02-15 11:07:14 -0600153start_time=$(date +%s)
154cinder create --display_name $VOL_NAME --display_description "test volume: $VOL_NAME" $DEFAULT_VOLUME_SIZE || \
Nachi Ueno07115eb2013-02-26 12:38:18 -0800155 die $LINENO "Failure creating volume $VOL_NAME"
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
Nachi Ueno07115eb2013-02-26 12:38:18 -0800157 die $LINENO "Volume $VOL_NAME not created"
Dean Troyera8dda172011-12-16 12:22:02 -0600158fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600159end_time=$(date +%s)
John Griffith161e2802012-11-05 13:59:49 -0700160echo "Completed cinder create in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600161
162# Get volume ID
Dean Troyerda85cda2013-02-15 11:07:14 -0600163VOL_ID=$(cinder list | grep $VOL_NAME | head -1 | get_field 1)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800164die_if_not_set $LINENO VOL_ID "Failure retrieving volume ID for $VOL_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600165
166# Attach to server
167DEVICE=/dev/vdb
Dean Troyerda85cda2013-02-15 11:07:14 -0600168start_time=$(date +%s)
Dean Troyer27e32692012-03-16 16:16:56 -0500169nova volume-attach $VM_UUID $VOL_ID $DEVICE || \
Nachi Ueno07115eb2013-02-26 12:38:18 -0800170 die $LINENO "Failure attaching volume $VOL_NAME to $VM_NAME"
John Griffith161e2802012-11-05 13:59:49 -0700171if ! 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 -0800172 die $LINENO "Volume $VOL_NAME not attached to $VM_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600173fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600174end_time=$(date +%s)
John Griffith496ffc72012-09-26 15:09:52 -0600175echo "Completed volume-attach in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600176
Dean Troyerda85cda2013-02-15 11:07:14 -0600177VOL_ATTACH=$(cinder list | grep $VOL_NAME | head -1 | get_field -1)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800178die_if_not_set $LINENO VOL_ATTACH "Failure retrieving $VOL_NAME status"
Dean Troyera8dda172011-12-16 12:22:02 -0600179if [[ "$VOL_ATTACH" != $VM_UUID ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800180 die $LINENO "Volume not attached to correct instance"
Dean Troyera8dda172011-12-16 12:22:02 -0600181fi
182
Dean Troyerda85cda2013-02-15 11:07:14 -0600183# Clean up
184# --------
185
Dean Troyera8dda172011-12-16 12:22:02 -0600186# Detach volume
Dean Troyerda85cda2013-02-15 11:07:14 -0600187start_time=$(date +%s)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800188nova volume-detach $VM_UUID $VOL_ID || die $LINENO "Failure detaching volume $VOL_NAME from $VM_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
Nachi Ueno07115eb2013-02-26 12:38:18 -0800190 die $LINENO "Volume $VOL_NAME not detached from $VM_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600191fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600192end_time=$(date +%s)
John Griffith496ffc72012-09-26 15:09:52 -0600193echo "Completed volume-detach in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600194
195# Delete volume
Dean Troyerda85cda2013-02-15 11:07:14 -0600196start_time=$(date +%s)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800197cinder delete $VOL_ID || die $LINENO "Failure deleting volume $VOL_NAME"
Adam Gandelman756c8422013-01-04 13:37:22 -0800198if ! timeout $ACTIVE_TIMEOUT sh -c "while cinder list | grep $VOL_NAME; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800199 die $LINENO "Volume $VOL_NAME not deleted"
Dean Troyera8dda172011-12-16 12:22:02 -0600200fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600201end_time=$(date +%s)
John Griffith161e2802012-11-05 13:59:49 -0700202echo "Completed cinder delete in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600203
Dean Troyerda85cda2013-02-15 11:07:14 -0600204# Delete instance
Nachi Ueno07115eb2013-02-26 12:38:18 -0800205nova delete $VM_UUID || die $LINENO "Failure deleting instance $VM_NAME"
Dean Troyer96288ba2012-08-17 14:11:55 -0500206if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800207 die $LINENO "Server $VM_NAME not deleted"
Dean Troyer96288ba2012-08-17 14:11:55 -0500208fi
209
Chris Behrensc62c2b92013-07-24 03:56:13 -0700210if [[ $SECGROUP = "default" ]] ; then
211 echo "Skipping deleting default security group"
212else
213 # Delete secgroup
214 nova secgroup-delete $SECGROUP || die $LINENO "Failure deleting security group $SECGROUP"
215fi
Dean Troyer489bd2a2012-03-02 10:44:29 -0600216
217set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500218echo "*********************************************************************"
219echo "SUCCESS: End DevStack Exercise: $0"
220echo "*********************************************************************"