blob: c2288de2c680032b11536dea1e80131d4a572d72 [file] [log] [blame]
Dean Troyera8dda172011-12-16 12:22:02 -06001#!/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.
7set -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.
11set -o xtrace
12
13
14# Settings
15# ========
16
17# Use openrc + stackrc + localrc for settings
18pushd $(cd $(dirname "$0")/.. && pwd)
19source ./openrc
20popd
21
Dean Troyer751c1522012-01-10 15:34:34 -060022# Max time to wait while vm goes from build to active state
23ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-30}
24
25# Max time till the vm is bootable
26BOOT_TIMEOUT=${BOOT_TIMEOUT:-30}
27
28# Max time to wait for proper association and dis-association.
29ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-15}
30
31# Instance type to create
32DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
33
34# Boot this image, use first AMi image if unset
35DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
36
Dean Troyera8dda172011-12-16 12:22:02 -060037# 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
44TOKEN=`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:
50nova list
51
52# Images
53# ------
54
55# Nova has a **deprecated** way of listing images.
56nova image-list
57
58# But we recommend using glance directly
Dean Troyer751c1522012-01-10 15:34:34 -060059glance -f -A $TOKEN index
Dean Troyera8dda172011-12-16 12:22:02 -060060
Dean Troyer751c1522012-01-10 15:34:34 -060061# Grab the id of the image to launch
62IMAGE=`glance -f -A $TOKEN index | egrep $DEFAULT_IMAGE_NAME | head -1 | cut -d" " -f1`
Dean Troyera8dda172011-12-16 12:22:02 -060063
64# determinine instance type
65# -------------------------
66
67# List of instance types:
68nova flavor-list
69
Dean Troyer1d6e0e12011-12-23 12:45:13 -060070INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2`
71if [[ -z "$INSTANCE_TYPE" ]]; then
72 # grab the first flavor in the list to launch if default doesn't exist
Dean Troyera8dda172011-12-16 12:22:02 -060073 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2`
74fi
75
76NAME="myserver"
77
78VM_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 Troyera8dda172011-12-16 12:22:02 -060090# check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyer751c1522012-01-10 15:34:34 -060091if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
Dean Troyera8dda172011-12-16 12:22:02 -060092 echo "server didn't become active!"
93 exit 1
94fi
95
96# get the IP of the server
97IP=`nova show $VM_UUID | grep "private network" | cut -d"|" -f3`
Dean Troyera8dda172011-12-16 12:22:02 -060098
99# for single node deployments, we can ping private ips
100MULTI_HOST=${MULTI_HOST:-0}
101if [ "$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
109else
110 # On a multi-host system, without vm net access, do a sleep to wait for the boot
111 sleep $BOOT_TIMEOUT
112fi
113
114# Volumes
115# -------
116
117VOL_NAME="myvol-$(openssl rand -hex 4)"
118
119# Verify it doesn't exist
120if [[ -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
123fi
124
125# Create a new volume
126nova volume-create --display_name $VOL_NAME --display_description "test volume: $VOL_NAME" 1
127if ! 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
130fi
131
132# Get volume ID
133VOL_ID=`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f2 | sed 's/ //g'`
134
135# Attach to server
136DEVICE=/dev/vdb
137nova volume-attach $VM_UUID $VOL_ID $DEVICE
138if ! 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
141fi
142
143VOL_ATTACH=`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f6 | sed 's/ //g'`
144if [[ "$VOL_ATTACH" != $VM_UUID ]]; then
145 echo "Volume not attached to correct instance"
146 exit 1
147fi
148
149# Detach volume
150nova volume-detach $VM_UUID $VOL_ID
151if ! 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
154fi
155
156# Delete volume
157nova volume-delete $VOL_ID
158if ! 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
161fi
162
163# shutdown the server
164nova delete $NAME