| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 1 | #!/usr/bin/env bash | 
 | 2 |  | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 3 | # **volumes.sh** | 
 | 4 |  | 
| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 5 | # Test nova volumes with the nova command from python-novaclient | 
 | 6 |  | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 7 | echo "*********************************************************************" | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 8 | echo "Begin DevStack Exercise: $0" | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 9 | echo "*********************************************************************" | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 10 |  | 
| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 11 | # This script exits on an error so that errors don't compound and you see | 
 | 12 | # only the first error that occured. | 
 | 13 | set -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. | 
 | 17 | set -o xtrace | 
 | 18 |  | 
 | 19 |  | 
 | 20 | # Settings | 
 | 21 | # ======== | 
 | 22 |  | 
| Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -0600 | [diff] [blame] | 23 | # Keep track of the current directory | 
 | 24 | EXERCISE_DIR=$(cd $(dirname "$0") && pwd) | 
 | 25 | TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 26 |  | 
 | 27 | # Import common functions | 
| Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -0600 | [diff] [blame] | 28 | source $TOP_DIR/functions | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 29 |  | 
 | 30 | # Import configuration | 
| Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -0600 | [diff] [blame] | 31 | source $TOP_DIR/openrc | 
| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 32 |  | 
| Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -0600 | [diff] [blame] | 33 | # Import exercise configuration | 
 | 34 | source $TOP_DIR/exerciserc | 
| Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 35 |  | 
 | 36 | # Instance type to create | 
 | 37 | DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny} | 
 | 38 |  | 
 | 39 | # Boot this image, use first AMi image if unset | 
 | 40 | DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami} | 
 | 41 |  | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 42 |  | 
| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 43 | # Launching a server | 
 | 44 | # ================== | 
 | 45 |  | 
 | 46 | # List servers for tenant: | 
 | 47 | nova list | 
 | 48 |  | 
 | 49 | # Images | 
 | 50 | # ------ | 
 | 51 |  | 
 | 52 | # Nova has a **deprecated** way of listing images. | 
 | 53 | nova image-list | 
 | 54 |  | 
 | 55 | # But we recommend using glance directly | 
| Dean Troyer | 4549525 | 2012-04-13 13:16:38 -0500 | [diff] [blame] | 56 | glance image-list | 
| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 57 |  | 
| Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 58 | # Grab the id of the image to launch | 
| Dean Troyer | 4549525 | 2012-04-13 13:16:38 -0500 | [diff] [blame] | 59 | IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1) | 
| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 60 |  | 
 | 61 | # determinine instance type | 
 | 62 | # ------------------------- | 
 | 63 |  | 
 | 64 | # List of instance types: | 
 | 65 | nova flavor-list | 
 | 66 |  | 
| Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 67 | INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1` | 
| Dean Troyer | 1d6e0e1 | 2011-12-23 12:45:13 -0600 | [diff] [blame] | 68 | if [[ -z "$INSTANCE_TYPE" ]]; then | 
 | 69 |     # grab the first flavor in the list to launch if default doesn't exist | 
| Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 70 |    INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | get_field 1` | 
| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 71 | fi | 
 | 72 |  | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 73 | NAME="ex-vol" | 
| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 74 |  | 
| Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 75 | VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2` | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 76 | die_if_not_set VM_UUID "Failure launching $NAME" | 
 | 77 |  | 
| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 78 |  | 
 | 79 | # Testing | 
 | 80 | # ======= | 
 | 81 |  | 
 | 82 | # First check if it spins up (becomes active and responds to ping on | 
 | 83 | # internal ip).  If you run this script from a nova node, you should | 
 | 84 | # bypass security groups and have direct access to the server. | 
 | 85 |  | 
 | 86 | # Waiting for boot | 
 | 87 | # ---------------- | 
 | 88 |  | 
| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 89 | # check that the status is active within ACTIVE_TIMEOUT seconds | 
| Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 90 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then | 
| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 91 |     echo "server didn't become active!" | 
 | 92 |     exit 1 | 
 | 93 | fi | 
 | 94 |  | 
 | 95 | # get the IP of the server | 
| Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 96 | IP=`nova show $VM_UUID | grep "private network" | get_field 2` | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 97 | die_if_not_set IP "Failure retrieving IP address" | 
| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 98 |  | 
 | 99 | # for single node deployments, we can ping private ips | 
 | 100 | MULTI_HOST=${MULTI_HOST:-0} | 
 | 101 | if [ "$MULTI_HOST" = "0" ]; then | 
 | 102 |     # sometimes the first ping fails (10 seconds isn't enough time for the VM's | 
 | 103 |     # network to respond?), so let's ping for a default of 15 seconds with a | 
 | 104 |     # timeout of a second for each ping. | 
 | 105 |     if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then | 
 | 106 |         echo "Couldn't ping server" | 
 | 107 |         exit 1 | 
 | 108 |     fi | 
 | 109 | else | 
 | 110 |     # On a multi-host system, without vm net access, do a sleep to wait for the boot | 
 | 111 |     sleep $BOOT_TIMEOUT | 
 | 112 | fi | 
 | 113 |  | 
 | 114 | # Volumes | 
 | 115 | # ------- | 
 | 116 |  | 
 | 117 | VOL_NAME="myvol-$(openssl rand -hex 4)" | 
 | 118 |  | 
 | 119 | # Verify it doesn't exist | 
| Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 120 | if [[ -n "`nova volume-list | grep $VOL_NAME | head -1 | get_field 2`" ]]; then | 
| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 121 |     echo "Volume $VOL_NAME already exists" | 
 | 122 |     exit 1 | 
 | 123 | fi | 
 | 124 |  | 
 | 125 | # Create a new volume | 
 | 126 | nova volume-create --display_name $VOL_NAME --display_description "test volume: $VOL_NAME" 1 | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 127 | if [[ $? != 0 ]]; then | 
 | 128 |     echo "Failure creating volume $VOL_NAME" | 
 | 129 |     exit 1 | 
 | 130 | fi | 
| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 131 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then | 
 | 132 |     echo "Volume $VOL_NAME not created" | 
 | 133 |     exit 1 | 
 | 134 | fi | 
 | 135 |  | 
 | 136 | # Get volume ID | 
| Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 137 | VOL_ID=`nova volume-list | grep $VOL_NAME | head -1 | get_field 1` | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 138 | die_if_not_set VOL_ID "Failure retrieving volume ID for $VOL_NAME" | 
| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 139 |  | 
 | 140 | # Attach to server | 
 | 141 | DEVICE=/dev/vdb | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 142 | nova volume-attach $VM_UUID $VOL_ID $DEVICE || \ | 
 | 143 |     die "Failure attaching volume $VOL_NAME to $NAME" | 
| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 144 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then | 
 | 145 |     echo "Volume $VOL_NAME not attached to $NAME" | 
 | 146 |     exit 1 | 
 | 147 | fi | 
 | 148 |  | 
| Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 149 | VOL_ATTACH=`nova volume-list | grep $VOL_NAME | head -1 | get_field -1` | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 150 | die_if_not_set VOL_ATTACH "Failure retrieving $VOL_NAME status" | 
| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 151 | if [[ "$VOL_ATTACH" != $VM_UUID ]]; then | 
 | 152 |     echo "Volume not attached to correct instance" | 
 | 153 |     exit 1 | 
 | 154 | fi | 
 | 155 |  | 
 | 156 | # Detach volume | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 157 | nova volume-detach $VM_UUID $VOL_ID || die "Failure detaching volume $VOL_NAME from $NAME" | 
| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 158 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then | 
 | 159 |     echo "Volume $VOL_NAME not detached from $NAME" | 
 | 160 |     exit 1 | 
 | 161 | fi | 
 | 162 |  | 
 | 163 | # Delete volume | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 164 | nova volume-delete $VOL_ID || die "Failure deleting volume $VOL_NAME" | 
| Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 165 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME; do sleep 1; done"; then | 
 | 166 |     echo "Volume $VOL_NAME not deleted" | 
 | 167 |     exit 1 | 
 | 168 | fi | 
 | 169 |  | 
 | 170 | # shutdown the server | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 171 | nova delete $NAME || die "Failure deleting instance $NAME" | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 172 |  | 
 | 173 | set +o xtrace | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 174 | echo "*********************************************************************" | 
 | 175 | echo "SUCCESS: End DevStack Exercise: $0" | 
 | 176 | echo "*********************************************************************" |