blob: 33e24589eb17cec7407cde4d76d58fc89224a376 [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
Dean Troyere4fa7212014-01-15 15:04:49 -060030# Import project functions
31source $TOP_DIR/lib/cinder
32
Dean Troyer489bd2a2012-03-02 10:44:29 -060033# Import configuration
Dean Troyer51fb4542012-03-09 22:21:59 -060034source $TOP_DIR/openrc
Dean Troyera8dda172011-12-16 12:22:02 -060035
Dean Troyer51fb4542012-03-09 22:21:59 -060036# Import exercise configuration
37source $TOP_DIR/exerciserc
Dean Troyer751c1522012-01-10 15:34:34 -060038
Joe Gordon6fd28112012-11-13 16:55:41 -080039# If cinder is not enabled we exit with exitcode 55 which mean
Dean Troyer67787e62012-05-02 11:48:15 -050040# exercise is skipped.
Joe Gordon6fd28112012-11-13 16:55:41 -080041is_service_enabled cinder || exit 55
Dean Troyer67787e62012-05-02 11:48:15 -050042
Dean Troyer2aa2a892013-08-04 19:53:19 -050043# Also skip if the hypervisor is Docker
44[[ "$VIRT_DRIVER" == "docker" ]] && exit 55
45
Dean Troyer751c1522012-01-10 15:34:34 -060046# Instance type to create
47DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
48
Dean Troyerda85cda2013-02-15 11:07:14 -060049# Boot this image, use first AMI image if unset
Dean Troyer751c1522012-01-10 15:34:34 -060050DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
51
Dean Troyer96288ba2012-08-17 14:11:55 -050052# Security group name
53SECGROUP=${SECGROUP:-vol_secgroup}
54
Dean Troyerda85cda2013-02-15 11:07:14 -060055# Instance and volume names
56VM_NAME=${VM_NAME:-ex-vol-inst}
57VOL_NAME="ex-vol-$(openssl rand -hex 4)"
58
Dean Troyer27e32692012-03-16 16:16:56 -050059
Dean Troyera8dda172011-12-16 12:22:02 -060060# Launching a server
61# ==================
62
63# List servers for tenant:
64nova list
65
66# Images
67# ------
68
Dean Troyerda85cda2013-02-15 11:07:14 -060069# List the images available
Dean Troyer45495252012-04-13 13:16:38 -050070glance image-list
Dean Troyera8dda172011-12-16 12:22:02 -060071
Dean Troyer751c1522012-01-10 15:34:34 -060072# Grab the id of the image to launch
Dean Troyer45495252012-04-13 13:16:38 -050073IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
Nachi Ueno07115eb2013-02-26 12:38:18 -080074die_if_not_set $LINENO IMAGE "Failure getting image $DEFAULT_IMAGE_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -060075
Dean Troyer96288ba2012-08-17 14:11:55 -050076# Security Groups
77# ---------------
78
Dean Troyerda85cda2013-02-15 11:07:14 -060079# List security groups
Dean Troyer96288ba2012-08-17 14:11:55 -050080nova secgroup-list
81
Chris Behrensc62c2b92013-07-24 03:56:13 -070082if is_service_enabled n-cell; then
83 # Cells does not support security groups, so force the use of "default"
84 SECGROUP="default"
85 echo "Using the default security group because of Cells."
86else
87 # Create a secgroup
88 if ! nova secgroup-list | grep -q $SECGROUP; then
89 nova secgroup-create $SECGROUP "$SECGROUP description"
90 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
91 echo "Security group not created"
92 exit 1
93 fi
Dean Troyer96288ba2012-08-17 14:11:55 -050094 fi
95fi
96
97# Configure Security Group Rules
Dean Troyer15bda3e2013-01-11 15:07:53 -060098if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
99 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
100fi
101if ! nova secgroup-list-rules $SECGROUP | grep -q " tcp .* 22 "; then
102 nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
103fi
Dean Troyer96288ba2012-08-17 14:11:55 -0500104
Dean Troyerda85cda2013-02-15 11:07:14 -0600105# List secgroup rules
106nova secgroup-list-rules $SECGROUP
Dean Troyera8dda172011-12-16 12:22:02 -0600107
Dean Troyerda85cda2013-02-15 11:07:14 -0600108# Set up instance
109# ---------------
110
111# List flavors
Dean Troyera8dda172011-12-16 12:22:02 -0600112nova flavor-list
113
Dean Troyerda85cda2013-02-15 11:07:14 -0600114# Select a flavor
115INSTANCE_TYPE=$(nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1)
Dean Troyer1d6e0e12011-12-23 12:45:13 -0600116if [[ -z "$INSTANCE_TYPE" ]]; then
117 # grab the first flavor in the list to launch if default doesn't exist
Sean Dague922c8ae2013-10-22 10:06:06 -0400118 INSTANCE_TYPE=$(nova flavor-list | head -n 4 | tail -n 1 | get_field 1)
DennyZhang23178a92013-10-22 17:07:32 -0500119 die_if_not_set $LINENO INSTANCE_TYPE "Failure retrieving INSTANCE_TYPE"
Dean Troyera8dda172011-12-16 12:22:02 -0600120fi
121
Dean Troyerda85cda2013-02-15 11:07:14 -0600122# Clean-up from previous runs
123nova delete $VM_NAME || true
124if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VM_NAME; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800125 die $LINENO "server didn't terminate!"
Dean Troyerda85cda2013-02-15 11:07:14 -0600126fi
Dean Troyera8dda172011-12-16 12:22:02 -0600127
Dean Troyerda85cda2013-02-15 11:07:14 -0600128# Boot instance
129# -------------
Dean Troyer489bd2a2012-03-02 10:44:29 -0600130
Dean Troyer526b79f2013-11-22 11:30:44 -0600131VM_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 -0800132die_if_not_set $LINENO VM_UUID "Failure launching $VM_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600133
Dean Troyerda85cda2013-02-15 11:07:14 -0600134# Check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -0600135if ! 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 -0800136 die $LINENO "server didn't become active!"
Dean Troyera8dda172011-12-16 12:22:02 -0600137fi
138
Dean Troyerda85cda2013-02-15 11:07:14 -0600139# Get the instance IP
Nachi Ueno6769b162013-08-12 18:18:56 -0700140IP=$(get_instance_ip $VM_UUID $PRIVATE_NETWORK_NAME)
141
Nachi Ueno07115eb2013-02-26 12:38:18 -0800142die_if_not_set $LINENO IP "Failure retrieving IP address"
Dean Troyera8dda172011-12-16 12:22:02 -0600143
Dean Troyerda85cda2013-02-15 11:07:14 -0600144# Private IPs can be pinged in single node deployments
Nachi Uenofda946e2012-10-24 17:26:02 -0700145ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
Dean Troyera8dda172011-12-16 12:22:02 -0600146
147# Volumes
148# -------
149
Dean Troyera8dda172011-12-16 12:22:02 -0600150# Verify it doesn't exist
Dean Troyerda85cda2013-02-15 11:07:14 -0600151if [[ -n $(cinder list | grep $VOL_NAME | head -1 | get_field 2) ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800152 die $LINENO "Volume $VOL_NAME already exists"
Dean Troyera8dda172011-12-16 12:22:02 -0600153fi
154
155# Create a new volume
Dean Troyerda85cda2013-02-15 11:07:14 -0600156start_time=$(date +%s)
Dean Troyer526b79f2013-11-22 11:30:44 -0600157cinder create --display-name $VOL_NAME --display-description "test volume: $VOL_NAME" $DEFAULT_VOLUME_SIZE || \
Nachi Ueno07115eb2013-02-26 12:38:18 -0800158 die $LINENO "Failure creating volume $VOL_NAME"
John Griffith161e2802012-11-05 13:59:49 -0700159if ! 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 -0800160 die $LINENO "Volume $VOL_NAME not created"
Dean Troyera8dda172011-12-16 12:22:02 -0600161fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600162end_time=$(date +%s)
John Griffith161e2802012-11-05 13:59:49 -0700163echo "Completed cinder create in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600164
165# Get volume ID
Dean Troyerda85cda2013-02-15 11:07:14 -0600166VOL_ID=$(cinder list | grep $VOL_NAME | head -1 | get_field 1)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800167die_if_not_set $LINENO VOL_ID "Failure retrieving volume ID for $VOL_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600168
169# Attach to server
170DEVICE=/dev/vdb
Dean Troyerda85cda2013-02-15 11:07:14 -0600171start_time=$(date +%s)
Dean Troyer27e32692012-03-16 16:16:56 -0500172nova volume-attach $VM_UUID $VOL_ID $DEVICE || \
Nachi Ueno07115eb2013-02-26 12:38:18 -0800173 die $LINENO "Failure attaching volume $VOL_NAME to $VM_NAME"
John Griffith161e2802012-11-05 13:59:49 -0700174if ! 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 -0800175 die $LINENO "Volume $VOL_NAME not attached to $VM_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600176fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600177end_time=$(date +%s)
John Griffith496ffc72012-09-26 15:09:52 -0600178echo "Completed volume-attach in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600179
Dean Troyerda85cda2013-02-15 11:07:14 -0600180VOL_ATTACH=$(cinder list | grep $VOL_NAME | head -1 | get_field -1)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800181die_if_not_set $LINENO VOL_ATTACH "Failure retrieving $VOL_NAME status"
Dean Troyera8dda172011-12-16 12:22:02 -0600182if [[ "$VOL_ATTACH" != $VM_UUID ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800183 die $LINENO "Volume not attached to correct instance"
Dean Troyera8dda172011-12-16 12:22:02 -0600184fi
185
Dean Troyerda85cda2013-02-15 11:07:14 -0600186# Clean up
187# --------
188
Dean Troyera8dda172011-12-16 12:22:02 -0600189# Detach volume
Dean Troyerda85cda2013-02-15 11:07:14 -0600190start_time=$(date +%s)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800191nova volume-detach $VM_UUID $VOL_ID || die $LINENO "Failure detaching volume $VOL_NAME from $VM_NAME"
John Griffith161e2802012-11-05 13:59:49 -0700192if ! 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 -0800193 die $LINENO "Volume $VOL_NAME not detached from $VM_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600194fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600195end_time=$(date +%s)
John Griffith496ffc72012-09-26 15:09:52 -0600196echo "Completed volume-detach in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600197
198# Delete volume
Dean Troyerda85cda2013-02-15 11:07:14 -0600199start_time=$(date +%s)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800200cinder delete $VOL_ID || die $LINENO "Failure deleting volume $VOL_NAME"
Adam Gandelman756c8422013-01-04 13:37:22 -0800201if ! timeout $ACTIVE_TIMEOUT sh -c "while cinder list | grep $VOL_NAME; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800202 die $LINENO "Volume $VOL_NAME not deleted"
Dean Troyera8dda172011-12-16 12:22:02 -0600203fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600204end_time=$(date +%s)
John Griffith161e2802012-11-05 13:59:49 -0700205echo "Completed cinder delete in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600206
Dean Troyerda85cda2013-02-15 11:07:14 -0600207# Delete instance
Nachi Ueno07115eb2013-02-26 12:38:18 -0800208nova delete $VM_UUID || die $LINENO "Failure deleting instance $VM_NAME"
Dean Troyer96288ba2012-08-17 14:11:55 -0500209if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800210 die $LINENO "Server $VM_NAME not deleted"
Dean Troyer96288ba2012-08-17 14:11:55 -0500211fi
212
Chris Behrensc62c2b92013-07-24 03:56:13 -0700213if [[ $SECGROUP = "default" ]] ; then
214 echo "Skipping deleting default security group"
215else
216 # Delete secgroup
217 nova secgroup-delete $SECGROUP || die $LINENO "Failure deleting security group $SECGROUP"
218fi
Dean Troyer489bd2a2012-03-02 10:44:29 -0600219
220set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500221echo "*********************************************************************"
222echo "SUCCESS: End DevStack Exercise: $0"
223echo "*********************************************************************"