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 | |
| 22 | # Get a token for clients that don't support service catalog |
| 23 | # ========================================================== |
| 24 | |
| 25 | # manually create a token by querying keystone (sending JSON data). Keystone |
| 26 | # returns a token and catalog of endpoints. We use python to parse the token |
| 27 | # and save it. |
| 28 | |
| 29 | 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'];"` |
| 30 | |
| 31 | # Launching a server |
| 32 | # ================== |
| 33 | |
| 34 | # List servers for tenant: |
| 35 | nova list |
| 36 | |
| 37 | # Images |
| 38 | # ------ |
| 39 | |
| 40 | # Nova has a **deprecated** way of listing images. |
| 41 | nova image-list |
| 42 | |
| 43 | # But we recommend using glance directly |
| 44 | glance -A $TOKEN index |
| 45 | |
| 46 | # Let's grab the id of the first AMI image to launch |
| 47 | IMAGE=`glance -A $TOKEN index | egrep ami | head -1 | cut -d" " -f1` |
| 48 | |
| 49 | # determinine instance type |
| 50 | # ------------------------- |
| 51 | |
| 52 | # List of instance types: |
| 53 | nova flavor-list |
| 54 | |
| 55 | INSTANCE_NAME=${DEFAULT_INSTANCE_TYPE:-m1.tiny} |
| 56 | INSTANCE_TYPE=`nova flavor-list | grep $INSTANCE_NAME | cut -d"|" -f2` |
| 57 | if [[ -z "`nova flavor-list | grep $INSTANCE_NAME | cut -d"|" -f2`" ]]; then |
| 58 | # and grab the first flavor in the list to launch |
| 59 | INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2` |
| 60 | fi |
| 61 | |
| 62 | NAME="myserver" |
| 63 | |
| 64 | VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'` |
| 65 | |
| 66 | # Testing |
| 67 | # ======= |
| 68 | |
| 69 | # First check if it spins up (becomes active and responds to ping on |
| 70 | # internal ip). If you run this script from a nova node, you should |
| 71 | # bypass security groups and have direct access to the server. |
| 72 | |
| 73 | # Waiting for boot |
| 74 | # ---------------- |
| 75 | |
| 76 | # Max time to wait while vm goes from build to active state |
| 77 | ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-30} |
| 78 | |
| 79 | # Max time till the vm is bootable |
| 80 | BOOT_TIMEOUT=${BOOT_TIMEOUT:-15} |
| 81 | |
| 82 | # Max time to wait for proper association and dis-association. |
| 83 | ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-10} |
| 84 | |
| 85 | # check that the status is active within ACTIVE_TIMEOUT seconds |
| 86 | if ! timeout $BOOT_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then |
| 87 | echo "server didn't become active!" |
| 88 | exit 1 |
| 89 | fi |
| 90 | |
| 91 | # get the IP of the server |
| 92 | IP=`nova show $VM_UUID | grep "private network" | cut -d"|" -f3` |
| 93 | #VM_UUID=`nova list | grep $NAME | head -1 | cut -d'|' -f2 | sed 's/ //g'` |
| 94 | |
| 95 | # for single node deployments, we can ping private ips |
| 96 | MULTI_HOST=${MULTI_HOST:-0} |
| 97 | if [ "$MULTI_HOST" = "0" ]; then |
| 98 | # sometimes the first ping fails (10 seconds isn't enough time for the VM's |
| 99 | # network to respond?), so let's ping for a default of 15 seconds with a |
| 100 | # timeout of a second for each ping. |
| 101 | if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then |
| 102 | echo "Couldn't ping server" |
| 103 | exit 1 |
| 104 | fi |
| 105 | else |
| 106 | # On a multi-host system, without vm net access, do a sleep to wait for the boot |
| 107 | sleep $BOOT_TIMEOUT |
| 108 | fi |
| 109 | |
| 110 | # Volumes |
| 111 | # ------- |
| 112 | |
| 113 | VOL_NAME="myvol-$(openssl rand -hex 4)" |
| 114 | |
| 115 | # Verify it doesn't exist |
| 116 | if [[ -n "`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f3 | sed 's/ //g'`" ]]; then |
| 117 | echo "Volume $VOL_NAME already exists" |
| 118 | exit 1 |
| 119 | fi |
| 120 | |
| 121 | # Create a new volume |
| 122 | nova volume-create --display_name $VOL_NAME --display_description "test volume: $VOL_NAME" 1 |
| 123 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then |
| 124 | echo "Volume $VOL_NAME not created" |
| 125 | exit 1 |
| 126 | fi |
| 127 | |
| 128 | # Get volume ID |
| 129 | VOL_ID=`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f2 | sed 's/ //g'` |
| 130 | |
| 131 | # Attach to server |
| 132 | DEVICE=/dev/vdb |
| 133 | nova volume-attach $VM_UUID $VOL_ID $DEVICE |
| 134 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then |
| 135 | echo "Volume $VOL_NAME not attached to $NAME" |
| 136 | exit 1 |
| 137 | fi |
| 138 | |
| 139 | VOL_ATTACH=`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f6 | sed 's/ //g'` |
| 140 | if [[ "$VOL_ATTACH" != $VM_UUID ]]; then |
| 141 | echo "Volume not attached to correct instance" |
| 142 | exit 1 |
| 143 | fi |
| 144 | |
| 145 | # Detach volume |
| 146 | nova volume-detach $VM_UUID $VOL_ID |
| 147 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then |
| 148 | echo "Volume $VOL_NAME not detached from $NAME" |
| 149 | exit 1 |
| 150 | fi |
| 151 | |
| 152 | # Delete volume |
| 153 | nova volume-delete $VOL_ID |
| 154 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME; do sleep 1; done"; then |
| 155 | echo "Volume $VOL_NAME not deleted" |
| 156 | exit 1 |
| 157 | fi |
| 158 | |
| 159 | # shutdown the server |
| 160 | nova delete $NAME |