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 | # Get a token for clients that don't support service catalog |
| 38 | # ========================================================== |
| 39 | |
| 40 | # manually create a token by querying keystone (sending JSON data). Keystone |
| 41 | # returns a token and catalog of endpoints. We use python to parse the token |
| 42 | # and save it. |
| 43 | |
| 44 | TOKEN=`curl -s -d "{\"auth\":{\"passwordCredentials\": {\"username\": \"$NOVA_USERNAME\", \"password\": \"$NOVA_PASSWORD\"}}}" -H "Content-type: application/json" http://$HOST_IP:5000/v2.0/tokens | python -c "import sys; import json; tok = json.loads(sys.stdin.read()); print tok['access']['token']['id'];"` |
| 45 | |
| 46 | # Launching a server |
| 47 | # ================== |
| 48 | |
| 49 | # List servers for tenant: |
| 50 | nova list |
| 51 | |
| 52 | # Images |
| 53 | # ------ |
| 54 | |
| 55 | # Nova has a **deprecated** way of listing images. |
| 56 | nova image-list |
| 57 | |
| 58 | # But we recommend using glance directly |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame^] | 59 | glance -f -A $TOKEN index |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 60 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame^] | 61 | # Grab the id of the image to launch |
| 62 | IMAGE=`glance -f -A $TOKEN index | egrep $DEFAULT_IMAGE_NAME | head -1 | cut -d" " -f1` |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 63 | |
| 64 | # determinine instance type |
| 65 | # ------------------------- |
| 66 | |
| 67 | # List of instance types: |
| 68 | nova flavor-list |
| 69 | |
Dean Troyer | 1d6e0e1 | 2011-12-23 12:45:13 -0600 | [diff] [blame] | 70 | INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2` |
| 71 | if [[ -z "$INSTANCE_TYPE" ]]; then |
| 72 | # 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] | 73 | INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2` |
| 74 | fi |
| 75 | |
| 76 | NAME="myserver" |
| 77 | |
| 78 | VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'` |
| 79 | |
| 80 | # Testing |
| 81 | # ======= |
| 82 | |
| 83 | # First check if it spins up (becomes active and responds to ping on |
| 84 | # internal ip). If you run this script from a nova node, you should |
| 85 | # bypass security groups and have direct access to the server. |
| 86 | |
| 87 | # Waiting for boot |
| 88 | # ---------------- |
| 89 | |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 90 | # check that the status is active within ACTIVE_TIMEOUT seconds |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame^] | 91 | 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] | 92 | echo "server didn't become active!" |
| 93 | exit 1 |
| 94 | fi |
| 95 | |
| 96 | # get the IP of the server |
| 97 | IP=`nova show $VM_UUID | grep "private network" | cut -d"|" -f3` |
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 |
| 120 | if [[ -n "`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f3 | sed 's/ //g'`" ]]; then |
| 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 |
| 127 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then |
| 128 | echo "Volume $VOL_NAME not created" |
| 129 | exit 1 |
| 130 | fi |
| 131 | |
| 132 | # Get volume ID |
| 133 | VOL_ID=`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f2 | sed 's/ //g'` |
| 134 | |
| 135 | # Attach to server |
| 136 | DEVICE=/dev/vdb |
| 137 | nova volume-attach $VM_UUID $VOL_ID $DEVICE |
| 138 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then |
| 139 | echo "Volume $VOL_NAME not attached to $NAME" |
| 140 | exit 1 |
| 141 | fi |
| 142 | |
| 143 | VOL_ATTACH=`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f6 | sed 's/ //g'` |
| 144 | if [[ "$VOL_ATTACH" != $VM_UUID ]]; then |
| 145 | echo "Volume not attached to correct instance" |
| 146 | exit 1 |
| 147 | fi |
| 148 | |
| 149 | # Detach volume |
| 150 | nova volume-detach $VM_UUID $VOL_ID |
| 151 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then |
| 152 | echo "Volume $VOL_NAME not detached from $NAME" |
| 153 | exit 1 |
| 154 | fi |
| 155 | |
| 156 | # Delete volume |
| 157 | nova volume-delete $VOL_ID |
| 158 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME; do sleep 1; done"; then |
| 159 | echo "Volume $VOL_NAME not deleted" |
| 160 | exit 1 |
| 161 | fi |
| 162 | |
| 163 | # shutdown the server |
| 164 | nova delete $NAME |