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