exercises/volumes.sh
a8dda170
 #!/usr/bin/env bash
 
27e32699
 # **volumes.sh**
 
da85cdad
 # Test cinder volumes with the ``cinder`` command from ``python-cinderclient``
a8dda170
 
27e32699
 echo "*********************************************************************"
489bd2a6
 echo "Begin DevStack Exercise: $0"
27e32699
 echo "*********************************************************************"
489bd2a6
 
a8dda170
 # This script exits on an error so that errors don't compound and you see
6fd28117
 # only the first error that occurred.
a8dda170
 set -o errexit
 
 # Print the commands being run so that we can see the command that triggers
 # an error.  It is also useful for following allowing as the install occurs.
 set -o xtrace
 
 
 # Settings
 # ========
 
51fb454f
 # Keep track of the current directory
 EXERCISE_DIR=$(cd $(dirname "$0") && pwd)
 TOP_DIR=$(cd $EXERCISE_DIR/..; pwd)
489bd2a6
 
 # Import common functions
51fb454f
 source $TOP_DIR/functions
489bd2a6
 
 # Import configuration
51fb454f
 source $TOP_DIR/openrc
a8dda170
 
5db5bfa2
 # Import quantum functions if needed
 if is_service_enabled quantum; then
     source $TOP_DIR/lib/quantum
 fi
 
51fb454f
 # Import exercise configuration
 source $TOP_DIR/exerciserc
751c1524
 
6fd28117
 # If cinder is not enabled we exit with exitcode 55 which mean
67787e6b
 # exercise is skipped.
6fd28117
 is_service_enabled cinder || exit 55
67787e6b
 
751c1524
 # Instance type to create
 DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
 
da85cdad
 # Boot this image, use first AMI image if unset
751c1524
 DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
 
96288ba9
 # Security group name
 SECGROUP=${SECGROUP:-vol_secgroup}
 
da85cdad
 # Instance and volume names
 VM_NAME=${VM_NAME:-ex-vol-inst}
 VOL_NAME="ex-vol-$(openssl rand -hex 4)"
 
27e32699
 
a8dda170
 # Launching a server
 # ==================
 
 # List servers for tenant:
 nova list
 
 # Images
 # ------
 
da85cdad
 # List the images available
45495258
 glance image-list
a8dda170
 
751c1524
 # Grab the id of the image to launch
45495258
 IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
07115eb5
 die_if_not_set $LINENO IMAGE "Failure getting image $DEFAULT_IMAGE_NAME"
a8dda170
 
96288ba9
 # Security Groups
 # ---------------
 
da85cdad
 # List security groups
96288ba9
 nova secgroup-list
 
 # Create a secgroup
 if ! nova secgroup-list | grep -q $SECGROUP; then
     nova secgroup-create $SECGROUP "$SECGROUP description"
     if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
         echo "Security group not created"
         exit 1
     fi
 fi
 
 # Configure Security Group Rules
15bda3e4
 if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
     nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
 fi
 if ! nova secgroup-list-rules $SECGROUP | grep -q " tcp .* 22 "; then
     nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
 fi
96288ba9
 
da85cdad
 # List secgroup rules
 nova secgroup-list-rules $SECGROUP
 
 # Set up instance
 # ---------------
a8dda170
 
da85cdad
 # List flavors
a8dda170
 nova flavor-list
 
da85cdad
 # Select a flavor
 INSTANCE_TYPE=$(nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1)
1d6e0e19
 if [[ -z "$INSTANCE_TYPE" ]]; then
     # grab the first flavor in the list to launch if default doesn't exist
da85cdad
    INSTANCE_TYPE=$(nova flavor-list | head -n 4 | tail -n 1 | get_field 1)
a8dda170
 fi
 
da85cdad
 # Clean-up from previous runs
 nova delete $VM_NAME || true
 if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VM_NAME; do sleep 1; done"; then
07115eb5
     die $LINENO "server didn't terminate!"
da85cdad
 fi
a8dda170
 
da85cdad
 # Boot instance
 # -------------
a8dda170
 
da85cdad
 VM_UUID=$(nova boot --flavor $INSTANCE_TYPE --image $IMAGE --security_groups=$SECGROUP $VM_NAME | grep ' id ' | get_field 2)
07115eb5
 die_if_not_set $LINENO VM_UUID "Failure launching $VM_NAME"
a8dda170
 
da85cdad
 # Check that the status is active within ACTIVE_TIMEOUT seconds
751c1524
 if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
07115eb5
     die $LINENO "server didn't become active!"
a8dda170
 fi
 
da85cdad
 # Get the instance IP
 IP=$(nova show $VM_UUID | grep "$PRIVATE_NETWORK_NAME" | get_field 2)
07115eb5
 die_if_not_set $LINENO IP "Failure retrieving IP address"
a8dda170
 
da85cdad
 # Private IPs can be pinged in single node deployments
fda946e3
 ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
a8dda170
 
 # Volumes
 # -------
 
 # Verify it doesn't exist
da85cdad
 if [[ -n $(cinder list | grep $VOL_NAME | head -1 | get_field 2) ]]; then
07115eb5
     die $LINENO "Volume $VOL_NAME already exists"
a8dda170
 fi
 
 # Create a new volume
da85cdad
 start_time=$(date +%s)
 cinder create --display_name $VOL_NAME --display_description "test volume: $VOL_NAME" $DEFAULT_VOLUME_SIZE || \
07115eb5
     die $LINENO "Failure creating volume $VOL_NAME"
161e2807
 if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep available; do sleep 1; done"; then
07115eb5
     die $LINENO "Volume $VOL_NAME not created"
a8dda170
 fi
da85cdad
 end_time=$(date +%s)
161e2807
 echo "Completed cinder create in $((end_time - start_time)) seconds"
a8dda170
 
 # Get volume ID
da85cdad
 VOL_ID=$(cinder list | grep $VOL_NAME | head -1 | get_field 1)
07115eb5
 die_if_not_set $LINENO VOL_ID "Failure retrieving volume ID for $VOL_NAME"
a8dda170
 
 # Attach to server
 DEVICE=/dev/vdb
da85cdad
 start_time=$(date +%s)
27e32699
 nova volume-attach $VM_UUID $VOL_ID $DEVICE || \
07115eb5
     die $LINENO "Failure attaching volume $VOL_NAME to $VM_NAME"
161e2807
 if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then
07115eb5
     die $LINENO "Volume $VOL_NAME not attached to $VM_NAME"
a8dda170
 fi
da85cdad
 end_time=$(date +%s)
496ffc74
 echo "Completed volume-attach in $((end_time - start_time)) seconds"
a8dda170
 
da85cdad
 VOL_ATTACH=$(cinder list | grep $VOL_NAME | head -1 | get_field -1)
07115eb5
 die_if_not_set $LINENO VOL_ATTACH "Failure retrieving $VOL_NAME status"
a8dda170
 if [[ "$VOL_ATTACH" != $VM_UUID ]]; then
07115eb5
     die $LINENO "Volume not attached to correct instance"
a8dda170
 fi
 
da85cdad
 # Clean up
 # --------
 
a8dda170
 # Detach volume
da85cdad
 start_time=$(date +%s)
07115eb5
 nova volume-detach $VM_UUID $VOL_ID || die $LINENO "Failure detaching volume $VOL_NAME from $VM_NAME"
161e2807
 if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep available; do sleep 1; done"; then
07115eb5
     die $LINENO "Volume $VOL_NAME not detached from $VM_NAME"
a8dda170
 fi
da85cdad
 end_time=$(date +%s)
496ffc74
 echo "Completed volume-detach in $((end_time - start_time)) seconds"
a8dda170
 
 # Delete volume
da85cdad
 start_time=$(date +%s)
07115eb5
 cinder delete $VOL_ID || die $LINENO "Failure deleting volume $VOL_NAME"
756c842a
 if ! timeout $ACTIVE_TIMEOUT sh -c "while cinder list | grep $VOL_NAME; do sleep 1; done"; then
07115eb5
     die $LINENO "Volume $VOL_NAME not deleted"
a8dda170
 fi
da85cdad
 end_time=$(date +%s)
161e2807
 echo "Completed cinder delete in $((end_time - start_time)) seconds"
a8dda170
 
da85cdad
 # Delete instance
07115eb5
 nova delete $VM_UUID || die $LINENO "Failure deleting instance $VM_NAME"
96288ba9
 if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
07115eb5
     die $LINENO "Server $VM_NAME not deleted"
96288ba9
 fi
 
da85cdad
 # Delete secgroup
07115eb5
 nova secgroup-delete $SECGROUP || die $LINENO "Failure deleting security group $SECGROUP"
489bd2a6
 
 set +o xtrace
27e32699
 echo "*********************************************************************"
 echo "SUCCESS: End DevStack Exercise: $0"
 echo "*********************************************************************"