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 | |
Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 58 | # Helper function to grab a numbered field from python novaclient cli result |
| 59 | # Fields are numbered starting with 1 |
| 60 | # Reverse syntax is supported: -1 is the last field, -2 is second to last, etc. |
| 61 | function get_field () { |
| 62 | while read data |
| 63 | do |
| 64 | if [ "$1" -lt 0 ]; then |
| 65 | field="(\$(NF$1))" |
| 66 | else |
| 67 | field="\$$(($1 + 1))" |
| 68 | fi |
| 69 | echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}" |
| 70 | done |
| 71 | } |
| 72 | |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 73 | # List of instance types: |
| 74 | nova flavor-list |
| 75 | |
Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 76 | INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1` |
Dean Troyer | 1d6e0e1 | 2011-12-23 12:45:13 -0600 | [diff] [blame] | 77 | if [[ -z "$INSTANCE_TYPE" ]]; then |
| 78 | # 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] | 79 | 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] | 80 | fi |
| 81 | |
| 82 | NAME="myserver" |
| 83 | |
Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 84 | VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2` |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 85 | |
| 86 | # Testing |
| 87 | # ======= |
| 88 | |
| 89 | # First check if it spins up (becomes active and responds to ping on |
| 90 | # internal ip). If you run this script from a nova node, you should |
| 91 | # bypass security groups and have direct access to the server. |
| 92 | |
| 93 | # Waiting for boot |
| 94 | # ---------------- |
| 95 | |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 96 | # check that the status is active within ACTIVE_TIMEOUT seconds |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 97 | 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] | 98 | echo "server didn't become active!" |
| 99 | exit 1 |
| 100 | fi |
| 101 | |
| 102 | # get the IP of the server |
Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 103 | IP=`nova show $VM_UUID | grep "private network" | get_field 2` |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 104 | |
| 105 | # for single node deployments, we can ping private ips |
| 106 | MULTI_HOST=${MULTI_HOST:-0} |
| 107 | if [ "$MULTI_HOST" = "0" ]; then |
| 108 | # sometimes the first ping fails (10 seconds isn't enough time for the VM's |
| 109 | # network to respond?), so let's ping for a default of 15 seconds with a |
| 110 | # timeout of a second for each ping. |
| 111 | if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then |
| 112 | echo "Couldn't ping server" |
| 113 | exit 1 |
| 114 | fi |
| 115 | else |
| 116 | # On a multi-host system, without vm net access, do a sleep to wait for the boot |
| 117 | sleep $BOOT_TIMEOUT |
| 118 | fi |
| 119 | |
| 120 | # Volumes |
| 121 | # ------- |
| 122 | |
| 123 | VOL_NAME="myvol-$(openssl rand -hex 4)" |
| 124 | |
| 125 | # Verify it doesn't exist |
Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 126 | 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] | 127 | echo "Volume $VOL_NAME already exists" |
| 128 | exit 1 |
| 129 | fi |
| 130 | |
| 131 | # Create a new volume |
| 132 | nova volume-create --display_name $VOL_NAME --display_description "test volume: $VOL_NAME" 1 |
| 133 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then |
| 134 | echo "Volume $VOL_NAME not created" |
| 135 | exit 1 |
| 136 | fi |
| 137 | |
| 138 | # Get volume ID |
Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 139 | VOL_ID=`nova volume-list | grep $VOL_NAME | head -1 | get_field 1` |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 140 | |
| 141 | # Attach to server |
| 142 | DEVICE=/dev/vdb |
| 143 | nova volume-attach $VM_UUID $VOL_ID $DEVICE |
| 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 | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 150 | if [[ "$VOL_ATTACH" != $VM_UUID ]]; then |
| 151 | echo "Volume not attached to correct instance" |
| 152 | exit 1 |
| 153 | fi |
| 154 | |
| 155 | # Detach volume |
| 156 | nova volume-detach $VM_UUID $VOL_ID |
| 157 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then |
| 158 | echo "Volume $VOL_NAME not detached from $NAME" |
| 159 | exit 1 |
| 160 | fi |
| 161 | |
| 162 | # Delete volume |
| 163 | nova volume-delete $VOL_ID |
| 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 |