Browse code

Add volume.sh exercise

Change-Id: Ic339c34c85493d21f9fbf5280bb5ff1660644f98

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