blob: 6a0937ab15e494b256785119afef5ef4ae741bd0 [file] [log] [blame]
Anthony Young440be4b2012-02-10 21:42:39 -08001#!/usr/bin/env bash
2
3# **boot_from_volume.sh**
4
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
10
Dean Troyer27e32692012-03-16 16:16:56 -050011echo "*********************************************************************"
Anthony Young440be4b2012-02-10 21:42:39 -080012echo "Begin DevStack Exercise: $0"
Dean Troyer27e32692012-03-16 16:16:56 -050013echo "*********************************************************************"
Anthony Young440be4b2012-02-10 21:42:39 -080014
15# This script exits on an error so that errors don't compound and you see
16# only the first error that occured.
17set -o errexit
18
19# Print the commands being run so that we can see the command that triggers
20# an error. It is also useful for following allowing as the install occurs.
21set -o xtrace
22
23
24# Settings
25# ========
26
27# Keep track of the current directory
28EXERCISE_DIR=$(cd $(dirname "$0") && pwd)
29TOP_DIR=$(cd $EXERCISE_DIR/..; pwd)
30
31# Import common functions
32source $TOP_DIR/functions
33
34# Import configuration
35source $TOP_DIR/openrc
36
37# Import exercise configuration
38source $TOP_DIR/exerciserc
39
40# Boot this image, use first AMI image if unset
41DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
42
43# Instance type
44DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
45
46# Default floating IP pool name
47DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova}
48
Dean Troyer27e32692012-03-16 16:16:56 -050049
50# Launching servers
51# =================
52
Anthony Young440be4b2012-02-10 21:42:39 -080053# Grab the id of the image to launch
Dean Troyer45495252012-04-13 13:16:38 -050054IMAGE=`glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1`
Anthony Young440be4b2012-02-10 21:42:39 -080055die_if_not_set IMAGE "Failure getting image"
56
57# Instance and volume names
58INSTANCE_NAME=${INSTANCE_NAME:-test_instance}
59VOL_INSTANCE_NAME=${VOL_INSTANCE_NAME:-test_vol_instance}
60VOL_NAME=${VOL_NAME:-test_volume}
61
62# Clean-up from previous runs
63nova delete $VOL_INSTANCE_NAME || true
64nova delete $INSTANCE_NAME || true
65
66# Wait till server is gone
67if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $INSTANCE_NAME; do sleep 1; done"; then
68 echo "server didn't terminate!"
69 exit 1
70fi
71
72# Configure Security Groups
73SECGROUP=${SECGROUP:-test_secgroup}
74nova secgroup-delete $SECGROUP || true
75nova secgroup-create $SECGROUP "$SECGROUP description"
76nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
77nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
78
79# Determinine instance type
80INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2`
81if [[ -z "$INSTANCE_TYPE" ]]; then
82 # grab the first flavor in the list to launch if default doesn't exist
83 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2`
84fi
85
86# Setup Keypair
87KEY_NAME=test_key
88KEY_FILE=key.pem
89nova keypair-delete $KEY_NAME || true
90nova keypair-add $KEY_NAME > $KEY_FILE
91chmod 600 $KEY_FILE
92
93# Boot our instance
Dean Troyer27e32692012-03-16 16:16:56 -050094VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE --security_groups=$SECGROUP --key_name $KEY_NAME $INSTANCE_NAME | grep ' id ' | get_field 2`
95die_if_not_set VM_UUID "Failure launching $INSTANCE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -080096
97# check that the status is active within ACTIVE_TIMEOUT seconds
98if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
99 echo "server didn't become active!"
100 exit 1
101fi
102
103# Delete the old volume
104nova volume-delete $VOL_NAME || true
105
106# Free every floating ips - setting FREE_ALL_FLOATING_IPS=True in localrc will make life easier for testers
107if [ "$FREE_ALL_FLOATING_IPS" = "True" ]; then
108 nova floating-ip-list | grep nova | cut -d "|" -f2 | tr -d " " | xargs -n1 nova floating-ip-delete || true
109fi
110
111# Allocate floating ip
Dean Troyer27e32692012-03-16 16:16:56 -0500112FLOATING_IP=`nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | get_field 1`
Anthony Young440be4b2012-02-10 21:42:39 -0800113
114# Make sure the ip gets allocated
115if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then
116 echo "Floating IP not allocated"
117 exit 1
118fi
119
120# Add floating ip to our server
121nova add-floating-ip $VM_UUID $FLOATING_IP
122
123# Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
124if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
125 echo "Couldn't ping server with floating ip"
126 exit 1
127fi
128
129# Create our volume
130nova volume-create --display_name=$VOL_NAME 1
131
132# Wait for volume to activate
133if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
134 echo "Volume $VOL_NAME not created"
135 exit 1
136fi
137
138# FIXME (anthony) - python-novaclient should accept a volume_name for the attachment param?
139DEVICE=/dev/vdb
Dean Troyer27e32692012-03-16 16:16:56 -0500140VOLUME_ID=`nova volume-list | grep $VOL_NAME | get_field 1`
Anthony Young440be4b2012-02-10 21:42:39 -0800141nova volume-attach $INSTANCE_NAME $VOLUME_ID $DEVICE
142
143# Wait till volume is attached
144if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then
145 echo "Volume $VOL_NAME not created"
146 exit 1
147fi
148
149# The following script builds our bootable volume.
150# To do this, ssh to the builder instance, mount volume, and build a volume-backed image.
151STAGING_DIR=/tmp/stage
152CIRROS_DIR=/tmp/cirros
153ssh -o StrictHostKeyChecking=no -i $KEY_FILE cirros@$FLOATING_IP << EOF
154set -o errexit
155set -o xtrace
156sudo mkdir -p $STAGING_DIR
157sudo mkfs.ext3 -b 1024 $DEVICE 1048576
158sudo mount $DEVICE $STAGING_DIR
159# The following lines create a writable empty file so that we can scp
160# the actual file
161sudo touch $STAGING_DIR/cirros-0.3.0-x86_64-rootfs.img.gz
162sudo chown cirros $STAGING_DIR/cirros-0.3.0-x86_64-rootfs.img.gz
163EOF
164
165# Download cirros
166if [ ! -e cirros-0.3.0-x86_64-rootfs.img.gz ]; then
167 wget http://images.ansolabs.com/cirros-0.3.0-x86_64-rootfs.img.gz
168fi
169
170# Copy cirros onto the volume
171scp -o StrictHostKeyChecking=no -i $KEY_FILE cirros-0.3.0-x86_64-rootfs.img.gz cirros@$FLOATING_IP:$STAGING_DIR
172
173# Unpack cirros into volume
174ssh -o StrictHostKeyChecking=no -i $KEY_FILE cirros@$FLOATING_IP << EOF
175set -o errexit
176set -o xtrace
177cd $STAGING_DIR
178sudo mkdir -p $CIRROS_DIR
179sudo gunzip cirros-0.3.0-x86_64-rootfs.img.gz
180sudo mount cirros-0.3.0-x86_64-rootfs.img $CIRROS_DIR
181
182# Copy cirros into our volume
183sudo cp -pr $CIRROS_DIR/* $STAGING_DIR/
184
185cd
186sync
187sudo umount $CIRROS_DIR
188# The following typically fails. Don't know why.
189sudo umount $STAGING_DIR || true
190EOF
191
192# Detach the volume from the builder instance
193nova volume-detach $INSTANCE_NAME $VOLUME_ID
194
195# Boot instance from volume! This is done with the --block_device_mapping param.
196# The format of mapping is:
197# <dev_name>=<id>:<type>:<size(GB)>:<delete_on_terminate>
198# Leaving the middle two fields blank appears to do-the-right-thing
Dean Troyer27e32692012-03-16 16:16:56 -0500199VOL_VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE --block_device_mapping vda=$VOLUME_ID:::0 --security_groups=$SECGROUP --key_name $KEY_NAME $VOL_INSTANCE_NAME | grep ' id ' | get_field 2`
200die_if_not_set VOL_VM_UUID "Failure launching $VOL_INSTANCE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800201
202# Check that the status is active within ACTIVE_TIMEOUT seconds
203if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VOL_VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
204 echo "server didn't become active!"
205 exit 1
206fi
207
208# Add floating ip to our server
Dean Troyer27e32692012-03-16 16:16:56 -0500209nova remove-floating-ip $VM_UUID $FLOATING_IP
Anthony Young440be4b2012-02-10 21:42:39 -0800210
211# Gratuitous sleep, probably hiding a race condition :/
212sleep 1
213
214# Add floating ip to our server
215nova add-floating-ip $VOL_VM_UUID $FLOATING_IP
216
217# Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
218if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
219 echo "Couldn't ping volume-backed server with floating ip"
220 exit 1
221fi
222
223# Make sure our volume-backed instance launched
224ssh -o StrictHostKeyChecking=no -i $KEY_FILE cirros@$FLOATING_IP << EOF
225echo "success!"
226EOF
227
228# Delete volume backed instance
Dean Troyer27e32692012-03-16 16:16:56 -0500229nova delete $VOL_INSTANCE_NAME || \
230 die "Failure deleting instance volume $VOL_INSTANCE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800231
232# Wait till our volume is no longer in-use
233if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
234 echo "Volume $VOL_NAME not created"
235 exit 1
236fi
237
238# Delete the volume
Dean Troyer27e32692012-03-16 16:16:56 -0500239nova volume-delete $VOL_NAME || \
240 die "Failure deleting volume $VOLUME_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800241
242# Delete instance
Dean Troyer27e32692012-03-16 16:16:56 -0500243nova delete $INSTANCE_NAME || \
244 die "Failure deleting instance $INSTANCE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800245
246# Wait for termination
247if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $INSTANCE_NAME; do sleep 1; done"; then
248 echo "server didn't terminate!"
249 exit 1
250fi
251
252# De-allocate the floating ip
Dean Troyer27e32692012-03-16 16:16:56 -0500253nova floating-ip-delete $FLOATING_IP || \
254 die "Failure deleting floating IP $FLOATING_IP"
Anthony Young440be4b2012-02-10 21:42:39 -0800255
Dean Troyer27e32692012-03-16 16:16:56 -0500256# Delete a secgroup
257nova secgroup-delete $SECGROUP || \
258 die "Failure deleting security group $SECGROUP"
Anthony Young440be4b2012-02-10 21:42:39 -0800259
260set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500261echo "*********************************************************************"
262echo "SUCCESS: End DevStack Exercise: $0"
263echo "*********************************************************************"