Browse code

Directly create bootable volume based on image ID.

Now that a bootable volume can be created directly based on
image ID, we can dispense with the complexity around using
a builder instance to acheive the same effect.

Change-Id: Ied1f6863a4dd21685e2f135841b9e2c4d499675f

Eoghan Glynn authored on 2012/10/20 05:26:41
Showing 1 changed files
... ...
@@ -3,10 +3,8 @@
3 3
 # **boot_from_volume.sh**
4 4
 
5 5
 # This script demonstrates how to boot from a volume.  It does the following:
6
-#  *  Create a 'builder' instance
7
-#  *  Attach a volume to the instance
8
-#  *  Format and install an os onto the volume
9
-#  *  Detach volume from builder, and then boot volume-backed instance
6
+#  *  Create a bootable volume
7
+#  *  Boot a volume-backed instance
10 8
 
11 9
 echo "*********************************************************************"
12 10
 echo "Begin DevStack Exercise: $0"
... ...
@@ -37,6 +35,10 @@ source $TOP_DIR/openrc
37 37
 # Import exercise configuration
38 38
 source $TOP_DIR/exerciserc
39 39
 
40
+# If cinder or n-vol are not enabled we exit with exitcode 55 so that
41
+# the exercise is skipped
42
+is_service_enabled cinder n-vol || exit 55
43
+
40 44
 # Boot this image, use first AMI image if unset
41 45
 DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
42 46
 
... ...
@@ -61,16 +63,13 @@ IMAGE=`glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1`
61 61
 die_if_not_set IMAGE "Failure getting image"
62 62
 
63 63
 # Instance and volume names
64
-INSTANCE_NAME=${INSTANCE_NAME:-test_instance}
65 64
 VOL_INSTANCE_NAME=${VOL_INSTANCE_NAME:-test_vol_instance}
66 65
 VOL_NAME=${VOL_NAME:-test_volume}
67 66
 
68 67
 # Clean-up from previous runs
69 68
 nova delete $VOL_INSTANCE_NAME || true
70
-nova delete $INSTANCE_NAME || true
71 69
 
72
-# Wait till server is gone
73
-if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $INSTANCE_NAME; do sleep 1; done"; then
70
+if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VOL_INSTANCE_NAME; do sleep 1; done"; then
74 71
     echo "server didn't terminate!"
75 72
     exit 1
76 73
 fi
... ...
@@ -95,16 +94,6 @@ nova keypair-delete $KEY_NAME || true
95 95
 nova keypair-add $KEY_NAME > $KEY_FILE
96 96
 chmod 600 $KEY_FILE
97 97
 
98
-# Boot our instance
99
-VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE --security_groups=$SECGROUP --key_name $KEY_NAME $INSTANCE_NAME | grep ' id ' | get_field 2`
100
-die_if_not_set VM_UUID "Failure launching $INSTANCE_NAME"
101
-
102
-# check that the status is active within ACTIVE_TIMEOUT seconds
103
-if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
104
-    echo "server didn't become active!"
105
-    exit 1
106
-fi
107
-
108 98
 # Delete the old volume
109 99
 nova volume-delete $VOL_NAME || true
110 100
 
... ...
@@ -122,17 +111,8 @@ if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $
122 122
     exit 1
123 123
 fi
124 124
 
125
-# Add floating ip to our server
126
-nova add-floating-ip $VM_UUID $FLOATING_IP
127
-
128
-# Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
129
-if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
130
-    echo "Couldn't ping server with floating ip"
131
-    exit 1
132
-fi
133
-
134
-# Create our volume
135
-nova volume-create --display_name=$VOL_NAME 1
125
+# Create the bootable volume
126
+nova volume-create --display_name=$VOL_NAME --image-id $IMAGE 1
136 127
 
137 128
 # Wait for volume to activate
138 129
 if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
... ...
@@ -140,62 +120,7 @@ if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME |
140 140
     exit 1
141 141
 fi
142 142
 
143
-# FIXME (anthony) - python-novaclient should accept a volume_name for the attachment param?
144
-DEVICE=/dev/vdb
145 143
 VOLUME_ID=`nova volume-list | grep $VOL_NAME  | get_field 1`
146
-nova volume-attach $INSTANCE_NAME $VOLUME_ID $DEVICE
147
-
148
-# Wait till volume is attached
149
-if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then
150
-    echo "Volume $VOL_NAME not created"
151
-    exit 1
152
-fi
153
-
154
-# The following script builds our bootable volume.
155
-# To do this, ssh to the builder instance, mount volume, and build a volume-backed image.
156
-STAGING_DIR=/tmp/stage
157
-CIRROS_DIR=/tmp/cirros
158
-ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP << EOF
159
-set -o errexit
160
-set -o xtrace
161
-sudo mkdir -p $STAGING_DIR
162
-sudo mkfs.ext3 -b 1024 $DEVICE 1048576
163
-sudo mount $DEVICE $STAGING_DIR
164
-# The following lines create a writable empty file so that we can scp
165
-# the actual file
166
-sudo touch $STAGING_DIR/cirros-0.3.0-x86_64-rootfs.img.gz
167
-sudo chown cirros $STAGING_DIR/cirros-0.3.0-x86_64-rootfs.img.gz
168
-EOF
169
-
170
-# Download cirros
171
-if [ ! -e cirros-0.3.0-x86_64-rootfs.img.gz ]; then
172
-    wget http://images.ansolabs.com/cirros-0.3.0-x86_64-rootfs.img.gz
173
-fi
174
-
175
-# Copy cirros onto the volume
176
-scp -o StrictHostKeyChecking=no -i $KEY_FILE cirros-0.3.0-x86_64-rootfs.img.gz ${DEFAULT_INSTANCE_USER}@$FLOATING_IP:$STAGING_DIR
177
-
178
-# Unpack cirros into volume
179
-ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP << EOF
180
-set -o errexit
181
-set -o xtrace
182
-cd $STAGING_DIR
183
-sudo mkdir -p $CIRROS_DIR
184
-sudo gunzip cirros-0.3.0-x86_64-rootfs.img.gz
185
-sudo mount cirros-0.3.0-x86_64-rootfs.img $CIRROS_DIR
186
-
187
-# Copy cirros into our volume
188
-sudo cp -pr $CIRROS_DIR/* $STAGING_DIR/
189
-
190
-cd
191
-sync
192
-sudo umount $CIRROS_DIR
193
-# The following typically fails.  Don't know why.
194
-sudo umount $STAGING_DIR || true
195
-EOF
196
-
197
-# Detach the volume from the builder instance
198
-nova volume-detach $INSTANCE_NAME $VOLUME_ID
199 144
 
200 145
 # Boot instance from volume!  This is done with the --block_device_mapping param.
201 146
 # The format of mapping is:
... ...
@@ -211,12 +136,6 @@ if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VOL_VM_UUID | grep status
211 211
 fi
212 212
 
213 213
 # Add floating ip to our server
214
-nova remove-floating-ip $VM_UUID $FLOATING_IP
215
-
216
-# Gratuitous sleep, probably hiding a race condition :/
217
-sleep 1
218
-
219
-# Add floating ip to our server
220 214
 nova add-floating-ip $VOL_VM_UUID $FLOATING_IP
221 215
 
222 216
 # Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
... ...
@@ -226,9 +145,13 @@ if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sle
226 226
 fi
227 227
 
228 228
 # Make sure our volume-backed instance launched
229
-ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP << EOF
230
-echo "success!"
231
-EOF
229
+if ! timeout $ACTIVE_TIMEOUT sh -c "while ! ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP echo success ; do sleep 1; done"; then
230
+    echo "server didn't become ssh-able!"
231
+    exit 1
232
+fi
233
+
234
+# Remove floating ip from volume-backed instance
235
+nova remove-floating-ip $VOL_VM_UUID $FLOATING_IP
232 236
 
233 237
 # Delete volume backed instance
234 238
 nova delete $VOL_INSTANCE_NAME || \
... ...
@@ -244,16 +167,6 @@ fi
244 244
 nova volume-delete $VOL_NAME || \
245 245
     die "Failure deleting volume $VOLUME_NAME"
246 246
 
247
-# Delete instance
248
-nova delete $INSTANCE_NAME || \
249
-    die "Failure deleting instance $INSTANCE_NAME"
250
-
251
-# Wait for termination
252
-if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
253
-    echo "Server $NAME not deleted"
254
-    exit 1
255
-fi
256
-
257 247
 # De-allocate the floating ip
258 248
 nova floating-ip-delete $FLOATING_IP || \
259 249
     die "Failure deleting floating IP $FLOATING_IP"