blob: 7fe81ba0b4468b3f7d2475142bb9926683c00637 [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
52# Launching servers
53# =================
54
Anthony Young440be4b2012-02-10 21:42:39 -080055# Grab the id of the image to launch
Dean Troyer45495252012-04-13 13:16:38 -050056IMAGE=`glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1`
Anthony Young440be4b2012-02-10 21:42:39 -080057die_if_not_set IMAGE "Failure getting image"
58
59# Instance and volume names
60INSTANCE_NAME=${INSTANCE_NAME:-test_instance}
61VOL_INSTANCE_NAME=${VOL_INSTANCE_NAME:-test_vol_instance}
62VOL_NAME=${VOL_NAME:-test_volume}
63
64# Clean-up from previous runs
65nova delete $VOL_INSTANCE_NAME || true
66nova delete $INSTANCE_NAME || true
67
68# Wait till server is gone
69if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $INSTANCE_NAME; do sleep 1; done"; then
70 echo "server didn't terminate!"
71 exit 1
72fi
73
74# Configure Security Groups
75SECGROUP=${SECGROUP:-test_secgroup}
76nova secgroup-delete $SECGROUP || true
77nova secgroup-create $SECGROUP "$SECGROUP description"
78nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
79nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
80
81# Determinine instance type
82INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2`
83if [[ -z "$INSTANCE_TYPE" ]]; then
84 # grab the first flavor in the list to launch if default doesn't exist
85 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2`
86fi
87
88# Setup Keypair
89KEY_NAME=test_key
90KEY_FILE=key.pem
91nova keypair-delete $KEY_NAME || true
92nova keypair-add $KEY_NAME > $KEY_FILE
93chmod 600 $KEY_FILE
94
95# Boot our instance
Dean Troyer27e32692012-03-16 16:16:56 -050096VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE --security_groups=$SECGROUP --key_name $KEY_NAME $INSTANCE_NAME | grep ' id ' | get_field 2`
97die_if_not_set VM_UUID "Failure launching $INSTANCE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -080098
99# check that the status is active within ACTIVE_TIMEOUT seconds
100if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
101 echo "server didn't become active!"
102 exit 1
103fi
104
105# Delete the old volume
106nova volume-delete $VOL_NAME || true
107
108# Free every floating ips - setting FREE_ALL_FLOATING_IPS=True in localrc will make life easier for testers
109if [ "$FREE_ALL_FLOATING_IPS" = "True" ]; then
110 nova floating-ip-list | grep nova | cut -d "|" -f2 | tr -d " " | xargs -n1 nova floating-ip-delete || true
111fi
112
113# Allocate floating ip
Dean Troyer27e32692012-03-16 16:16:56 -0500114FLOATING_IP=`nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | get_field 1`
Anthony Young440be4b2012-02-10 21:42:39 -0800115
116# Make sure the ip gets allocated
117if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then
118 echo "Floating IP not allocated"
119 exit 1
120fi
121
122# Add floating ip to our server
123nova add-floating-ip $VM_UUID $FLOATING_IP
124
125# Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
126if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
127 echo "Couldn't ping server with floating ip"
128 exit 1
129fi
130
131# Create our volume
132nova volume-create --display_name=$VOL_NAME 1
133
134# Wait for volume to activate
135if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
136 echo "Volume $VOL_NAME not created"
137 exit 1
138fi
139
140# FIXME (anthony) - python-novaclient should accept a volume_name for the attachment param?
141DEVICE=/dev/vdb
Dean Troyer27e32692012-03-16 16:16:56 -0500142VOLUME_ID=`nova volume-list | grep $VOL_NAME | get_field 1`
Anthony Young440be4b2012-02-10 21:42:39 -0800143nova volume-attach $INSTANCE_NAME $VOLUME_ID $DEVICE
144
145# Wait till volume is attached
146if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then
147 echo "Volume $VOL_NAME not created"
148 exit 1
149fi
150
151# The following script builds our bootable volume.
152# To do this, ssh to the builder instance, mount volume, and build a volume-backed image.
153STAGING_DIR=/tmp/stage
154CIRROS_DIR=/tmp/cirros
Devananda van der Veenc0c6f002012-07-06 17:49:12 -0700155ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP << EOF
Anthony Young440be4b2012-02-10 21:42:39 -0800156set -o errexit
157set -o xtrace
158sudo mkdir -p $STAGING_DIR
159sudo mkfs.ext3 -b 1024 $DEVICE 1048576
160sudo mount $DEVICE $STAGING_DIR
161# The following lines create a writable empty file so that we can scp
162# the actual file
163sudo touch $STAGING_DIR/cirros-0.3.0-x86_64-rootfs.img.gz
164sudo chown cirros $STAGING_DIR/cirros-0.3.0-x86_64-rootfs.img.gz
165EOF
166
167# Download cirros
168if [ ! -e cirros-0.3.0-x86_64-rootfs.img.gz ]; then
169 wget http://images.ansolabs.com/cirros-0.3.0-x86_64-rootfs.img.gz
170fi
171
172# Copy cirros onto the volume
Devananda van der Veenc0c6f002012-07-06 17:49:12 -0700173scp -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 -0800174
175# Unpack cirros into volume
Devananda van der Veenc0c6f002012-07-06 17:49:12 -0700176ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP << EOF
Anthony Young440be4b2012-02-10 21:42:39 -0800177set -o errexit
178set -o xtrace
179cd $STAGING_DIR
180sudo mkdir -p $CIRROS_DIR
181sudo gunzip cirros-0.3.0-x86_64-rootfs.img.gz
182sudo mount cirros-0.3.0-x86_64-rootfs.img $CIRROS_DIR
183
184# Copy cirros into our volume
185sudo cp -pr $CIRROS_DIR/* $STAGING_DIR/
186
187cd
188sync
189sudo umount $CIRROS_DIR
190# The following typically fails. Don't know why.
191sudo umount $STAGING_DIR || true
192EOF
193
194# Detach the volume from the builder instance
195nova volume-detach $INSTANCE_NAME $VOLUME_ID
196
197# Boot instance from volume! This is done with the --block_device_mapping param.
198# The format of mapping is:
199# <dev_name>=<id>:<type>:<size(GB)>:<delete_on_terminate>
200# Leaving the middle two fields blank appears to do-the-right-thing
Dean Troyer27e32692012-03-16 16:16:56 -0500201VOL_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`
202die_if_not_set VOL_VM_UUID "Failure launching $VOL_INSTANCE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800203
204# Check that the status is active within ACTIVE_TIMEOUT seconds
205if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VOL_VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
206 echo "server didn't become active!"
207 exit 1
208fi
209
210# Add floating ip to our server
Dean Troyer27e32692012-03-16 16:16:56 -0500211nova remove-floating-ip $VM_UUID $FLOATING_IP
Anthony Young440be4b2012-02-10 21:42:39 -0800212
213# Gratuitous sleep, probably hiding a race condition :/
214sleep 1
215
216# Add floating ip to our server
217nova add-floating-ip $VOL_VM_UUID $FLOATING_IP
218
219# Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
220if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
221 echo "Couldn't ping volume-backed server with floating ip"
222 exit 1
223fi
224
225# Make sure our volume-backed instance launched
Devananda van der Veenc0c6f002012-07-06 17:49:12 -0700226ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP << EOF
Anthony Young440be4b2012-02-10 21:42:39 -0800227echo "success!"
228EOF
229
230# Delete volume backed instance
Dean Troyer27e32692012-03-16 16:16:56 -0500231nova delete $VOL_INSTANCE_NAME || \
232 die "Failure deleting instance volume $VOL_INSTANCE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800233
234# Wait till our volume is no longer in-use
235if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
236 echo "Volume $VOL_NAME not created"
237 exit 1
238fi
239
240# Delete the volume
Dean Troyer27e32692012-03-16 16:16:56 -0500241nova volume-delete $VOL_NAME || \
242 die "Failure deleting volume $VOLUME_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800243
244# Delete instance
Dean Troyer27e32692012-03-16 16:16:56 -0500245nova delete $INSTANCE_NAME || \
246 die "Failure deleting instance $INSTANCE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800247
248# Wait for termination
249if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $INSTANCE_NAME; do sleep 1; done"; then
250 echo "server didn't terminate!"
251 exit 1
252fi
253
254# De-allocate the floating ip
Dean Troyer27e32692012-03-16 16:16:56 -0500255nova floating-ip-delete $FLOATING_IP || \
256 die "Failure deleting floating IP $FLOATING_IP"
Anthony Young440be4b2012-02-10 21:42:39 -0800257
Dean Troyer27e32692012-03-16 16:16:56 -0500258# Delete a secgroup
259nova secgroup-delete $SECGROUP || \
260 die "Failure deleting security group $SECGROUP"
Anthony Young440be4b2012-02-10 21:42:39 -0800261
262set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500263echo "*********************************************************************"
264echo "SUCCESS: End DevStack Exercise: $0"
265echo "*********************************************************************"