blob: 4c7890bbb4f14eba8683c784b9bd0478dcd6ea01 [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:
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +01006# * Create a bootable volume
7# * Boot a volume-backed instance
Anthony Young440be4b2012-02-10 21:42:39 -08008
Dean Troyer27e32692012-03-16 16:16:56 -05009echo "*********************************************************************"
Anthony Young440be4b2012-02-10 21:42:39 -080010echo "Begin DevStack Exercise: $0"
Dean Troyer27e32692012-03-16 16:16:56 -050011echo "*********************************************************************"
Anthony Young440be4b2012-02-10 21:42:39 -080012
13# This script exits on an error so that errors don't compound and you see
14# only the first error that occured.
15set -o errexit
16
17# Print the commands being run so that we can see the command that triggers
18# an error. It is also useful for following allowing as the install occurs.
19set -o xtrace
20
21
22# Settings
23# ========
24
25# Keep track of the current directory
26EXERCISE_DIR=$(cd $(dirname "$0") && pwd)
27TOP_DIR=$(cd $EXERCISE_DIR/..; pwd)
28
29# Import common functions
30source $TOP_DIR/functions
31
32# Import configuration
33source $TOP_DIR/openrc
34
Nachi Ueno5db5bfa2012-10-29 11:25:29 -070035# Import quantum functions if needed
36if is_service_enabled quantum; then
37 source $TOP_DIR/lib/quantum
38 setup_quantum
39fi
40
Anthony Young440be4b2012-02-10 21:42:39 -080041# Import exercise configuration
42source $TOP_DIR/exerciserc
43
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +010044# If cinder or n-vol are not enabled we exit with exitcode 55 so that
45# the exercise is skipped
46is_service_enabled cinder n-vol || exit 55
47
Anthony Young440be4b2012-02-10 21:42:39 -080048# Boot this image, use first AMI image if unset
49DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
50
51# Instance type
52DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
53
54# Default floating IP pool name
55DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova}
56
Devananda van der Veenc0c6f002012-07-06 17:49:12 -070057# Default user
58DEFAULT_INSTANCE_USER=${DEFAULT_INSTANCE_USER:-cirros}
Dean Troyer27e32692012-03-16 16:16:56 -050059
Dean Troyer96288ba2012-08-17 14:11:55 -050060# Security group name
61SECGROUP=${SECGROUP:-boot_secgroup}
62
63
Dean Troyer27e32692012-03-16 16:16:56 -050064# Launching servers
65# =================
66
Anthony Young440be4b2012-02-10 21:42:39 -080067# Grab the id of the image to launch
Dean Troyer45495252012-04-13 13:16:38 -050068IMAGE=`glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1`
Anthony Young440be4b2012-02-10 21:42:39 -080069die_if_not_set IMAGE "Failure getting image"
70
71# Instance and volume names
Anthony Young440be4b2012-02-10 21:42:39 -080072VOL_INSTANCE_NAME=${VOL_INSTANCE_NAME:-test_vol_instance}
73VOL_NAME=${VOL_NAME:-test_volume}
74
75# Clean-up from previous runs
76nova delete $VOL_INSTANCE_NAME || true
Anthony Young440be4b2012-02-10 21:42:39 -080077
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +010078if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VOL_INSTANCE_NAME; do sleep 1; done"; then
Anthony Young440be4b2012-02-10 21:42:39 -080079 echo "server didn't terminate!"
80 exit 1
81fi
82
83# Configure Security Groups
Anthony Young440be4b2012-02-10 21:42:39 -080084nova secgroup-delete $SECGROUP || true
85nova secgroup-create $SECGROUP "$SECGROUP description"
86nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
87nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
88
89# Determinine instance type
90INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2`
91if [[ -z "$INSTANCE_TYPE" ]]; then
92 # grab the first flavor in the list to launch if default doesn't exist
93 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2`
94fi
95
96# Setup Keypair
97KEY_NAME=test_key
98KEY_FILE=key.pem
99nova keypair-delete $KEY_NAME || true
100nova keypair-add $KEY_NAME > $KEY_FILE
101chmod 600 $KEY_FILE
102
Anthony Young440be4b2012-02-10 21:42:39 -0800103# 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
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +0100120# Create the bootable volume
121nova volume-create --display_name=$VOL_NAME --image-id $IMAGE 1
Anthony Young440be4b2012-02-10 21:42:39 -0800122
123# Wait for volume to activate
124if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
125 echo "Volume $VOL_NAME not created"
126 exit 1
127fi
128
Dean Troyer27e32692012-03-16 16:16:56 -0500129VOLUME_ID=`nova volume-list | grep $VOL_NAME | get_field 1`
Anthony Young440be4b2012-02-10 21:42:39 -0800130
131# Boot instance from volume! This is done with the --block_device_mapping param.
132# The format of mapping is:
133# <dev_name>=<id>:<type>:<size(GB)>:<delete_on_terminate>
134# Leaving the middle two fields blank appears to do-the-right-thing
Dean Troyer27e32692012-03-16 16:16:56 -0500135VOL_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`
136die_if_not_set VOL_VM_UUID "Failure launching $VOL_INSTANCE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800137
138# Check that the status is active within ACTIVE_TIMEOUT seconds
139if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VOL_VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
140 echo "server didn't become active!"
141 exit 1
142fi
143
144# Add floating ip to our server
Anthony Young440be4b2012-02-10 21:42:39 -0800145nova add-floating-ip $VOL_VM_UUID $FLOATING_IP
146
147# Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
Nachi Uenofda946e2012-10-24 17:26:02 -0700148ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT
Anthony Young440be4b2012-02-10 21:42:39 -0800149
150# Make sure our volume-backed instance launched
Nachi Uenofda946e2012-10-24 17:26:02 -0700151ssh_check "$PUBLIC_NETWORK_NAME" $KEY_FILE $FLOATING_IP $DEFAULT_INSTANCE_USER $ACTIVE_TIMEOUT
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +0100152
153# Remove floating ip from volume-backed instance
154nova remove-floating-ip $VOL_VM_UUID $FLOATING_IP
Anthony Young440be4b2012-02-10 21:42:39 -0800155
156# Delete volume backed instance
Dean Troyer27e32692012-03-16 16:16:56 -0500157nova delete $VOL_INSTANCE_NAME || \
158 die "Failure deleting instance volume $VOL_INSTANCE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800159
160# Wait till our volume is no longer in-use
161if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
162 echo "Volume $VOL_NAME not created"
163 exit 1
164fi
165
166# Delete the volume
Dean Troyer27e32692012-03-16 16:16:56 -0500167nova volume-delete $VOL_NAME || \
168 die "Failure deleting volume $VOLUME_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800169
Anthony Young440be4b2012-02-10 21:42:39 -0800170# De-allocate the floating ip
Dean Troyer27e32692012-03-16 16:16:56 -0500171nova floating-ip-delete $FLOATING_IP || \
172 die "Failure deleting floating IP $FLOATING_IP"
Anthony Young440be4b2012-02-10 21:42:39 -0800173
Dean Troyer27e32692012-03-16 16:16:56 -0500174# Delete a secgroup
Dean Troyer96288ba2012-08-17 14:11:55 -0500175nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP"
Anthony Young440be4b2012-02-10 21:42:39 -0800176
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700177if is_service_enabled quantum; then
178 teardown_quantum
179fi
180
Anthony Young440be4b2012-02-10 21:42:39 -0800181set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500182echo "*********************************************************************"
183echo "SUCCESS: End DevStack Exercise: $0"
184echo "*********************************************************************"