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