blob: 72c8729ecd1aae8d5798701785d6cf2220caa181 [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
12# only the first error that occured.
13set -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 Troyer51fb4542012-03-09 22:21:59 -060033# Import exercise configuration
34source $TOP_DIR/exerciserc
Dean Troyer751c1522012-01-10 15:34:34 -060035
Dean Troyer67787e62012-05-02 11:48:15 -050036# If cinder or n-vol are not enabled we exit with exitcode 55 which mean
37# exercise is skipped.
38is_service_enabled cinder n-vol || exit 55
39
Dean Troyer751c1522012-01-10 15:34:34 -060040# Instance type to create
41DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
42
43# Boot this image, use first AMi image if unset
44DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
45
Dean Troyer96288ba2012-08-17 14:11:55 -050046# Security group name
47SECGROUP=${SECGROUP:-vol_secgroup}
48
Dean Troyer27e32692012-03-16 16:16:56 -050049
Dean Troyera8dda172011-12-16 12:22:02 -060050# Launching a server
51# ==================
52
53# List servers for tenant:
54nova list
55
56# Images
57# ------
58
59# Nova has a **deprecated** way of listing images.
60nova image-list
61
62# But we recommend using glance directly
Dean Troyer45495252012-04-13 13:16:38 -050063glance image-list
Dean Troyera8dda172011-12-16 12:22:02 -060064
Dean Troyer751c1522012-01-10 15:34:34 -060065# Grab the id of the image to launch
Dean Troyer45495252012-04-13 13:16:38 -050066IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
Dean Troyera8dda172011-12-16 12:22:02 -060067
Dean Troyer96288ba2012-08-17 14:11:55 -050068# Security Groups
69# ---------------
70
71# List of secgroups:
72nova secgroup-list
73
74# Create a secgroup
75if ! nova secgroup-list | grep -q $SECGROUP; then
76 nova secgroup-create $SECGROUP "$SECGROUP description"
77 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
78 echo "Security group not created"
79 exit 1
80 fi
81fi
82
83# Configure Security Group Rules
84nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
85nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
86
Dean Troyera8dda172011-12-16 12:22:02 -060087# determinine instance type
88# -------------------------
89
90# List of instance types:
91nova flavor-list
92
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +000093INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1`
Dean Troyer1d6e0e12011-12-23 12:45:13 -060094if [[ -z "$INSTANCE_TYPE" ]]; then
95 # grab the first flavor in the list to launch if default doesn't exist
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +000096 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | get_field 1`
Dean Troyera8dda172011-12-16 12:22:02 -060097fi
98
Dean Troyer489bd2a2012-03-02 10:44:29 -060099NAME="ex-vol"
Dean Troyera8dda172011-12-16 12:22:02 -0600100
Vishvananda Ishaya854d8c92012-02-27 22:41:54 +0000101VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600102die_if_not_set VM_UUID "Failure launching $NAME"
103
Dean Troyera8dda172011-12-16 12:22:02 -0600104
105# Testing
106# =======
107
108# First check if it spins up (becomes active and responds to ping on
109# internal ip). If you run this script from a nova node, you should
110# bypass security groups and have direct access to the server.
111
112# Waiting for boot
113# ----------------
114
Dean Troyera8dda172011-12-16 12:22:02 -0600115# check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -0600116if ! 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 -0600117 echo "server didn't become active!"
118 exit 1
119fi
120
121# get the IP of the server
Nachi Uenofda946e2012-10-24 17:26:02 -0700122IP=`nova show $VM_UUID | grep "$PRIVATE_NETWORK_NAME" | get_field 2`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600123die_if_not_set IP "Failure retrieving IP address"
Dean Troyera8dda172011-12-16 12:22:02 -0600124
125# for single node deployments, we can ping private ips
Nachi Uenofda946e2012-10-24 17:26:02 -0700126ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
Dean Troyera8dda172011-12-16 12:22:02 -0600127
128# Volumes
129# -------
130
131VOL_NAME="myvol-$(openssl rand -hex 4)"
132
133# Verify it doesn't exist
John Griffith161e2802012-11-05 13:59:49 -0700134if [[ -n "`cinder list | grep $VOL_NAME | head -1 | get_field 2`" ]]; then
Dean Troyera8dda172011-12-16 12:22:02 -0600135 echo "Volume $VOL_NAME already exists"
136 exit 1
137fi
138
139# Create a new volume
John Griffith161e2802012-11-05 13:59:49 -0700140cinder create --display_name $VOL_NAME --display_description "test volume: $VOL_NAME" 1
Dean Troyer489bd2a2012-03-02 10:44:29 -0600141if [[ $? != 0 ]]; then
142 echo "Failure creating volume $VOL_NAME"
143 exit 1
144fi
John Griffith496ffc72012-09-26 15:09:52 -0600145
146start_time=`date +%s`
John Griffith161e2802012-11-05 13:59:49 -0700147if ! 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 -0600148 echo "Volume $VOL_NAME not created"
149 exit 1
150fi
John Griffith496ffc72012-09-26 15:09:52 -0600151end_time=`date +%s`
John Griffith161e2802012-11-05 13:59:49 -0700152echo "Completed cinder create in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600153
154# Get volume ID
John Griffith161e2802012-11-05 13:59:49 -0700155VOL_ID=`cinder list | grep $VOL_NAME | head -1 | get_field 1`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600156die_if_not_set VOL_ID "Failure retrieving volume ID for $VOL_NAME"
Dean Troyera8dda172011-12-16 12:22:02 -0600157
158# Attach to server
159DEVICE=/dev/vdb
John Griffith496ffc72012-09-26 15:09:52 -0600160start_time=`date +%s`
Dean Troyer27e32692012-03-16 16:16:56 -0500161nova volume-attach $VM_UUID $VOL_ID $DEVICE || \
162 die "Failure attaching volume $VOL_NAME to $NAME"
John Griffith161e2802012-11-05 13:59:49 -0700163if ! 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 -0600164 echo "Volume $VOL_NAME not attached to $NAME"
165 exit 1
166fi
John Griffith496ffc72012-09-26 15:09:52 -0600167end_time=`date +%s`
168echo "Completed volume-attach in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600169
John Griffith161e2802012-11-05 13:59:49 -0700170VOL_ATTACH=`cinder list | grep $VOL_NAME | head -1 | get_field -1`
Dean Troyer489bd2a2012-03-02 10:44:29 -0600171die_if_not_set VOL_ATTACH "Failure retrieving $VOL_NAME status"
Dean Troyera8dda172011-12-16 12:22:02 -0600172if [[ "$VOL_ATTACH" != $VM_UUID ]]; then
173 echo "Volume not attached to correct instance"
174 exit 1
175fi
176
177# Detach volume
John Griffith496ffc72012-09-26 15:09:52 -0600178start_time=`date +%s`
Dean Troyer27e32692012-03-16 16:16:56 -0500179nova volume-detach $VM_UUID $VOL_ID || die "Failure detaching volume $VOL_NAME from $NAME"
John Griffith161e2802012-11-05 13:59:49 -0700180if ! 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 -0600181 echo "Volume $VOL_NAME not detached from $NAME"
182 exit 1
183fi
John Griffith496ffc72012-09-26 15:09:52 -0600184end_time=`date +%s`
185echo "Completed volume-detach in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600186
187# Delete volume
John Griffith496ffc72012-09-26 15:09:52 -0600188start_time=`date +%s`
John Griffith161e2802012-11-05 13:59:49 -0700189cinder delete $VOL_ID || die "Failure deleting volume $VOL_NAME"
190if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME; do sleep 1; done"; then
Dean Troyera8dda172011-12-16 12:22:02 -0600191 echo "Volume $VOL_NAME not deleted"
192 exit 1
193fi
John Griffith496ffc72012-09-26 15:09:52 -0600194end_time=`date +%s`
John Griffith161e2802012-11-05 13:59:49 -0700195echo "Completed cinder delete in $((end_time - start_time)) seconds"
Dean Troyera8dda172011-12-16 12:22:02 -0600196
Dean Troyer96288ba2012-08-17 14:11:55 -0500197# Shutdown the server
198nova delete $VM_UUID || die "Failure deleting instance $NAME"
199
200# Wait for termination
201if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
202 echo "Server $NAME not deleted"
203 exit 1
204fi
205
206# Delete a secgroup
207nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP"
Dean Troyer489bd2a2012-03-02 10:44:29 -0600208
209set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500210echo "*********************************************************************"
211echo "SUCCESS: End DevStack Exercise: $0"
212echo "*********************************************************************"