blob: c967e3916b2eca5eb3e7a261637d7fe560c0b74f [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
Devananda van der Veenc0c6f002012-07-06 17:49:12 -070049# Default user
50DEFAULT_INSTANCE_USER=${DEFAULT_INSTANCE_USER:-cirros}
Dean Troyer27e32692012-03-16 16:16:56 -050051
Dean Troyer96288ba2012-08-17 14:11:55 -050052# Security group name
53SECGROUP=${SECGROUP:-boot_secgroup}
54
55
Dean Troyer27e32692012-03-16 16:16:56 -050056# Launching servers
57# =================
58
Anthony Young440be4b2012-02-10 21:42:39 -080059# Grab the id of the image to launch
Dean Troyer45495252012-04-13 13:16:38 -050060IMAGE=`glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1`
Anthony Young440be4b2012-02-10 21:42:39 -080061die_if_not_set IMAGE "Failure getting image"
62
63# Instance and volume names
64INSTANCE_NAME=${INSTANCE_NAME:-test_instance}
65VOL_INSTANCE_NAME=${VOL_INSTANCE_NAME:-test_vol_instance}
66VOL_NAME=${VOL_NAME:-test_volume}
67
68# Clean-up from previous runs
69nova delete $VOL_INSTANCE_NAME || true
70nova delete $INSTANCE_NAME || true
71
72# Wait till server is gone
73if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $INSTANCE_NAME; do sleep 1; done"; then
74 echo "server didn't terminate!"
75 exit 1
76fi
77
78# Configure Security Groups
Anthony Young440be4b2012-02-10 21:42:39 -080079nova secgroup-delete $SECGROUP || true
80nova secgroup-create $SECGROUP "$SECGROUP description"
81nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
82nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
83
84# Determinine instance type
85INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2`
86if [[ -z "$INSTANCE_TYPE" ]]; then
87 # grab the first flavor in the list to launch if default doesn't exist
88 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2`
89fi
90
91# Setup Keypair
92KEY_NAME=test_key
93KEY_FILE=key.pem
94nova keypair-delete $KEY_NAME || true
95nova keypair-add $KEY_NAME > $KEY_FILE
96chmod 600 $KEY_FILE
97
98# Boot our instance
Dean Troyer27e32692012-03-16 16:16:56 -050099VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE --security_groups=$SECGROUP --key_name $KEY_NAME $INSTANCE_NAME | grep ' id ' | get_field 2`
100die_if_not_set VM_UUID "Failure launching $INSTANCE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800101
102# check that the status is active within ACTIVE_TIMEOUT seconds
103if ! 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
106fi
107
108# Delete the old volume
109nova volume-delete $VOL_NAME || true
110
111# Free every floating ips - setting FREE_ALL_FLOATING_IPS=True in localrc will make life easier for testers
112if [ "$FREE_ALL_FLOATING_IPS" = "True" ]; then
113 nova floating-ip-list | grep nova | cut -d "|" -f2 | tr -d " " | xargs -n1 nova floating-ip-delete || true
114fi
115
116# Allocate floating ip
Dean Troyer27e32692012-03-16 16:16:56 -0500117FLOATING_IP=`nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | get_field 1`
Anthony Young440be4b2012-02-10 21:42:39 -0800118
119# Make sure the ip gets allocated
120if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then
121 echo "Floating IP not allocated"
122 exit 1
123fi
124
125# Add floating ip to our server
126nova add-floating-ip $VM_UUID $FLOATING_IP
127
128# Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
129if ! 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
132fi
133
134# Create our volume
135nova volume-create --display_name=$VOL_NAME 1
136
137# Wait for volume to activate
138if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
139 echo "Volume $VOL_NAME not created"
140 exit 1
141fi
142
143# FIXME (anthony) - python-novaclient should accept a volume_name for the attachment param?
144DEVICE=/dev/vdb
Dean Troyer27e32692012-03-16 16:16:56 -0500145VOLUME_ID=`nova volume-list | grep $VOL_NAME | get_field 1`
Anthony Young440be4b2012-02-10 21:42:39 -0800146nova volume-attach $INSTANCE_NAME $VOLUME_ID $DEVICE
147
148# Wait till volume is attached
149if ! 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
152fi
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.
156STAGING_DIR=/tmp/stage
157CIRROS_DIR=/tmp/cirros
Devananda van der Veenc0c6f002012-07-06 17:49:12 -0700158ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP << EOF
Anthony Young440be4b2012-02-10 21:42:39 -0800159set -o errexit
160set -o xtrace
161sudo mkdir -p $STAGING_DIR
162sudo mkfs.ext3 -b 1024 $DEVICE 1048576
163sudo mount $DEVICE $STAGING_DIR
164# The following lines create a writable empty file so that we can scp
165# the actual file
166sudo touch $STAGING_DIR/cirros-0.3.0-x86_64-rootfs.img.gz
167sudo chown cirros $STAGING_DIR/cirros-0.3.0-x86_64-rootfs.img.gz
168EOF
169
170# Download cirros
171if [ ! -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
173fi
174
175# Copy cirros onto the volume
Devananda van der Veenc0c6f002012-07-06 17:49:12 -0700176scp -o StrictHostKeyChecking=no -i $KEY_FILE cirros-0.3.0-x86_64-rootfs.img.gz ${DEFAULT_INSTANCE_USER}@$FLOATING_IP:$STAGING_DIR
Anthony Young440be4b2012-02-10 21:42:39 -0800177
178# Unpack cirros into volume
Devananda van der Veenc0c6f002012-07-06 17:49:12 -0700179ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP << EOF
Anthony Young440be4b2012-02-10 21:42:39 -0800180set -o errexit
181set -o xtrace
182cd $STAGING_DIR
183sudo mkdir -p $CIRROS_DIR
184sudo gunzip cirros-0.3.0-x86_64-rootfs.img.gz
185sudo mount cirros-0.3.0-x86_64-rootfs.img $CIRROS_DIR
186
187# Copy cirros into our volume
188sudo cp -pr $CIRROS_DIR/* $STAGING_DIR/
189
190cd
191sync
192sudo umount $CIRROS_DIR
193# The following typically fails. Don't know why.
194sudo umount $STAGING_DIR || true
195EOF
196
197# Detach the volume from the builder instance
198nova volume-detach $INSTANCE_NAME $VOLUME_ID
199
200# Boot instance from volume! This is done with the --block_device_mapping param.
201# The format of mapping is:
202# <dev_name>=<id>:<type>:<size(GB)>:<delete_on_terminate>
203# Leaving the middle two fields blank appears to do-the-right-thing
Dean Troyer27e32692012-03-16 16:16:56 -0500204VOL_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`
205die_if_not_set VOL_VM_UUID "Failure launching $VOL_INSTANCE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800206
207# Check that the status is active within ACTIVE_TIMEOUT seconds
208if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VOL_VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
209 echo "server didn't become active!"
210 exit 1
211fi
212
213# Add floating ip to our server
Dean Troyer27e32692012-03-16 16:16:56 -0500214nova remove-floating-ip $VM_UUID $FLOATING_IP
Anthony Young440be4b2012-02-10 21:42:39 -0800215
216# Gratuitous sleep, probably hiding a race condition :/
217sleep 1
218
219# Add floating ip to our server
220nova add-floating-ip $VOL_VM_UUID $FLOATING_IP
221
222# Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
223if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
224 echo "Couldn't ping volume-backed server with floating ip"
225 exit 1
226fi
227
228# Make sure our volume-backed instance launched
Devananda van der Veenc0c6f002012-07-06 17:49:12 -0700229ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP << EOF
Anthony Young440be4b2012-02-10 21:42:39 -0800230echo "success!"
231EOF
232
233# Delete volume backed instance
Dean Troyer27e32692012-03-16 16:16:56 -0500234nova delete $VOL_INSTANCE_NAME || \
235 die "Failure deleting instance volume $VOL_INSTANCE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800236
237# Wait till our volume is no longer in-use
238if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
239 echo "Volume $VOL_NAME not created"
240 exit 1
241fi
242
243# Delete the volume
Dean Troyer27e32692012-03-16 16:16:56 -0500244nova volume-delete $VOL_NAME || \
245 die "Failure deleting volume $VOLUME_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800246
247# Delete instance
Dean Troyer27e32692012-03-16 16:16:56 -0500248nova delete $INSTANCE_NAME || \
249 die "Failure deleting instance $INSTANCE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800250
251# Wait for termination
Dean Troyer96288ba2012-08-17 14:11:55 -0500252if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
253 echo "Server $NAME not deleted"
Anthony Young440be4b2012-02-10 21:42:39 -0800254 exit 1
255fi
256
257# De-allocate the floating ip
Dean Troyer27e32692012-03-16 16:16:56 -0500258nova floating-ip-delete $FLOATING_IP || \
259 die "Failure deleting floating IP $FLOATING_IP"
Anthony Young440be4b2012-02-10 21:42:39 -0800260
Dean Troyer27e32692012-03-16 16:16:56 -0500261# Delete a secgroup
Dean Troyer96288ba2012-08-17 14:11:55 -0500262nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP"
Anthony Young440be4b2012-02-10 21:42:39 -0800263
264set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500265echo "*********************************************************************"
266echo "SUCCESS: End DevStack Exercise: $0"
267echo "*********************************************************************"