blob: e7c3560894425976dbba1ebf7e993c0e5dd92916 [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
igor01acdab2016-07-29 13:11:53 +020016# an error. It is also useful for following as the install occurs.
Dean Troyera8dda172011-12-16 12:22:02 -060017set -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
Sean M. Collins2a242512016-05-03 09:03:09 -040035source $TOP_DIR/lib/neutron
Dean Troyer5a9739a2015-03-25 11:33:51 -050036source $TOP_DIR/lib/neutron-legacy
Dean Troyere2907b42014-02-26 17:35:37 -060037
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
Adam Gandelmanb875d012014-03-17 19:47:14 -070045# Ironic does not currently support volume attachment.
46[ "$VIRT_DRIVER" == "ironic" ] && exit 55
47
Dean Troyer751c1522012-01-10 15:34:34 -060048# Instance type to create
49DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
50
Dean Troyerda85cda2013-02-15 11:07:14 -060051# Boot this image, use first AMI image if unset
Dean Troyer751c1522012-01-10 15:34:34 -060052DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
53
Dean Troyer96288ba2012-08-17 14:11:55 -050054# Security group name
55SECGROUP=${SECGROUP:-vol_secgroup}
56
Dean Troyerda85cda2013-02-15 11:07:14 -060057# Instance and volume names
58VM_NAME=${VM_NAME:-ex-vol-inst}
59VOL_NAME="ex-vol-$(openssl rand -hex 4)"
60
Dean Troyer27e32692012-03-16 16:16:56 -050061
Dean Troyera8dda172011-12-16 12:22:02 -060062# Launching a server
63# ==================
64
65# List servers for tenant:
66nova list
67
68# Images
69# ------
70
Dean Troyerda85cda2013-02-15 11:07:14 -060071# List the images available
Steve Martinelli5c206c22014-08-02 20:32:31 -040072openstack image list
Dean Troyera8dda172011-12-16 12:22:02 -060073
Dean Troyer751c1522012-01-10 15:34:34 -060074# Grab the id of the image to launch
Steve Martinelli5c206c22014-08-02 20:32:31 -040075IMAGE=$(openstack image list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
Nachi Ueno07115eb2013-02-26 12:38:18 -080076die_if_not_set $LINENO IMAGE "Failure getting image $DEFAULT_IMAGE_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -060077
Dean Troyer96288ba2012-08-17 14:11:55 -050078# Security Groups
79# ---------------
80
Dean Troyerda85cda2013-02-15 11:07:14 -060081# List security groups
Dean Troyer96288ba2012-08-17 14:11:55 -050082nova secgroup-list
83
Chris Behrensc62c2b92013-07-24 03:56:13 -070084if is_service_enabled n-cell; then
85 # Cells does not support security groups, so force the use of "default"
86 SECGROUP="default"
87 echo "Using the default security group because of Cells."
88else
89 # Create a secgroup
90 if ! nova secgroup-list | grep -q $SECGROUP; then
91 nova secgroup-create $SECGROUP "$SECGROUP description"
92 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
93 echo "Security group not created"
94 exit 1
95 fi
Dean Troyer96288ba2012-08-17 14:11:55 -050096 fi
97fi
98
99# Configure Security Group Rules
Dean Troyer15bda3e2013-01-11 15:07:53 -0600100if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
101 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
102fi
103if ! nova secgroup-list-rules $SECGROUP | grep -q " tcp .* 22 "; then
104 nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
105fi
Dean Troyer96288ba2012-08-17 14:11:55 -0500106
Dean Troyerda85cda2013-02-15 11:07:14 -0600107# List secgroup rules
108nova secgroup-list-rules $SECGROUP
Dean Troyera8dda172011-12-16 12:22:02 -0600109
Dean Troyerda85cda2013-02-15 11:07:14 -0600110# Set up instance
111# ---------------
112
113# List flavors
Dean Troyera8dda172011-12-16 12:22:02 -0600114nova flavor-list
115
Dean Troyerda85cda2013-02-15 11:07:14 -0600116# Select a flavor
117INSTANCE_TYPE=$(nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1)
Dean Troyer1d6e0e12011-12-23 12:45:13 -0600118if [[ -z "$INSTANCE_TYPE" ]]; then
119 # grab the first flavor in the list to launch if default doesn't exist
Sean Dague922c8ae2013-10-22 10:06:06 -0400120 INSTANCE_TYPE=$(nova flavor-list | head -n 4 | tail -n 1 | get_field 1)
DennyZhang23178a92013-10-22 17:07:32 -0500121 die_if_not_set $LINENO INSTANCE_TYPE "Failure retrieving INSTANCE_TYPE"
Dean Troyera8dda172011-12-16 12:22:02 -0600122fi
123
Dean Troyerda85cda2013-02-15 11:07:14 -0600124# Clean-up from previous runs
125nova delete $VM_NAME || true
126if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VM_NAME; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800127 die $LINENO "server didn't terminate!"
Dean Troyerda85cda2013-02-15 11:07:14 -0600128fi
Dean Troyera8dda172011-12-16 12:22:02 -0600129
Dean Troyerda85cda2013-02-15 11:07:14 -0600130# Boot instance
131# -------------
Dean Troyer489bd2a2012-03-02 10:44:29 -0600132
Dean Troyer526b79f2013-11-22 11:30:44 -0600133VM_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 -0800134die_if_not_set $LINENO VM_UUID "Failure launching $VM_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600135
Dean Troyerda85cda2013-02-15 11:07:14 -0600136# Check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -0600137if ! 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 -0800138 die $LINENO "server didn't become active!"
Dean Troyera8dda172011-12-16 12:22:02 -0600139fi
140
Dean Troyerda85cda2013-02-15 11:07:14 -0600141# Get the instance IP
Nachi Ueno6769b162013-08-12 18:18:56 -0700142IP=$(get_instance_ip $VM_UUID $PRIVATE_NETWORK_NAME)
143
Nachi Ueno07115eb2013-02-26 12:38:18 -0800144die_if_not_set $LINENO IP "Failure retrieving IP address"
Dean Troyera8dda172011-12-16 12:22:02 -0600145
Dean Troyerda85cda2013-02-15 11:07:14 -0600146# Private IPs can be pinged in single node deployments
Sean Dagueaf9bf862015-04-16 08:58:32 -0400147ping_check $IP $BOOT_TIMEOUT "$PRIVATE_NETWORK_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600148
149# Volumes
150# -------
151
Dean Troyera8dda172011-12-16 12:22:02 -0600152# Verify it doesn't exist
Dean Troyerda85cda2013-02-15 11:07:14 -0600153if [[ -n $(cinder list | grep $VOL_NAME | head -1 | get_field 2) ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800154 die $LINENO "Volume $VOL_NAME already exists"
Dean Troyera8dda172011-12-16 12:22:02 -0600155fi
156
157# Create a new volume
Dean Troyerda85cda2013-02-15 11:07:14 -0600158start_time=$(date +%s)
Dean Troyer526b79f2013-11-22 11:30:44 -0600159cinder create --display-name $VOL_NAME --display-description "test volume: $VOL_NAME" $DEFAULT_VOLUME_SIZE || \
Nachi Ueno07115eb2013-02-26 12:38:18 -0800160 die $LINENO "Failure creating volume $VOL_NAME"
John Griffith161e2802012-11-05 13:59:49 -0700161if ! 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 -0800162 die $LINENO "Volume $VOL_NAME not created"
Dean Troyera8dda172011-12-16 12:22:02 -0600163fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600164end_time=$(date +%s)
John Griffith161e2802012-11-05 13:59:49 -0700165echo "Completed cinder create in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600166
167# Get volume ID
Dean Troyerda85cda2013-02-15 11:07:14 -0600168VOL_ID=$(cinder list | grep $VOL_NAME | head -1 | get_field 1)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800169die_if_not_set $LINENO VOL_ID "Failure retrieving volume ID for $VOL_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600170
171# Attach to server
172DEVICE=/dev/vdb
Dean Troyerda85cda2013-02-15 11:07:14 -0600173start_time=$(date +%s)
Dean Troyer27e32692012-03-16 16:16:56 -0500174nova volume-attach $VM_UUID $VOL_ID $DEVICE || \
Nachi Ueno07115eb2013-02-26 12:38:18 -0800175 die $LINENO "Failure attaching volume $VOL_NAME to $VM_NAME"
John Griffith161e2802012-11-05 13:59:49 -0700176if ! 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 -0800177 die $LINENO "Volume $VOL_NAME not attached to $VM_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600178fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600179end_time=$(date +%s)
John Griffith496ffc72012-09-26 15:09:52 -0600180echo "Completed volume-attach in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600181
Dean Troyerda85cda2013-02-15 11:07:14 -0600182VOL_ATTACH=$(cinder list | grep $VOL_NAME | head -1 | get_field -1)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800183die_if_not_set $LINENO VOL_ATTACH "Failure retrieving $VOL_NAME status"
Dean Troyera8dda172011-12-16 12:22:02 -0600184if [[ "$VOL_ATTACH" != $VM_UUID ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800185 die $LINENO "Volume not attached to correct instance"
Dean Troyera8dda172011-12-16 12:22:02 -0600186fi
187
Dean Troyerda85cda2013-02-15 11:07:14 -0600188# Clean up
189# --------
190
Dean Troyera8dda172011-12-16 12:22:02 -0600191# Detach volume
Dean Troyerda85cda2013-02-15 11:07:14 -0600192start_time=$(date +%s)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800193nova volume-detach $VM_UUID $VOL_ID || die $LINENO "Failure detaching volume $VOL_NAME from $VM_NAME"
John Griffith161e2802012-11-05 13:59:49 -0700194if ! 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 -0800195 die $LINENO "Volume $VOL_NAME not detached from $VM_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600196fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600197end_time=$(date +%s)
John Griffith496ffc72012-09-26 15:09:52 -0600198echo "Completed volume-detach in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600199
200# Delete volume
Dean Troyerda85cda2013-02-15 11:07:14 -0600201start_time=$(date +%s)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800202cinder delete $VOL_ID || die $LINENO "Failure deleting volume $VOL_NAME"
Adam Gandelman756c8422013-01-04 13:37:22 -0800203if ! timeout $ACTIVE_TIMEOUT sh -c "while cinder list | grep $VOL_NAME; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800204 die $LINENO "Volume $VOL_NAME not deleted"
Dean Troyera8dda172011-12-16 12:22:02 -0600205fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600206end_time=$(date +%s)
John Griffith161e2802012-11-05 13:59:49 -0700207echo "Completed cinder delete in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600208
Dean Troyerda85cda2013-02-15 11:07:14 -0600209# Delete instance
Nachi Ueno07115eb2013-02-26 12:38:18 -0800210nova delete $VM_UUID || die $LINENO "Failure deleting instance $VM_NAME"
Dean Troyer96288ba2012-08-17 14:11:55 -0500211if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800212 die $LINENO "Server $VM_NAME not deleted"
Dean Troyer96288ba2012-08-17 14:11:55 -0500213fi
214
Chris Behrensc62c2b92013-07-24 03:56:13 -0700215if [[ $SECGROUP = "default" ]] ; then
216 echo "Skipping deleting default security group"
217else
218 # Delete secgroup
219 nova secgroup-delete $SECGROUP || die $LINENO "Failure deleting security group $SECGROUP"
220fi
Dean Troyer489bd2a2012-03-02 10:44:29 -0600221
222set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500223echo "*********************************************************************"
224echo "SUCCESS: End DevStack Exercise: $0"
225echo "*********************************************************************"