blob: 5ada2370b855d7513962a9481d97311b458773e3 [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
Nachi Ueno5db5bfa2012-10-29 11:25:29 -070038fi
39
Anthony Young440be4b2012-02-10 21:42:39 -080040# Import exercise configuration
41source $TOP_DIR/exerciserc
42
Joe Gordon6fd28112012-11-13 16:55:41 -080043# If cinder is not enabled we exit with exitcode 55 so that
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +010044# the exercise is skipped
Joe Gordon6fd28112012-11-13 16:55:41 -080045is_service_enabled cinder || exit 55
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +010046
Anthony Young440be4b2012-02-10 21:42:39 -080047# Boot this image, use first AMI image if unset
48DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
49
50# Instance type
51DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
52
53# Default floating IP pool name
54DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova}
55
Devananda van der Veenc0c6f002012-07-06 17:49:12 -070056# Default user
57DEFAULT_INSTANCE_USER=${DEFAULT_INSTANCE_USER:-cirros}
Dean Troyer27e32692012-03-16 16:16:56 -050058
Dean Troyer96288ba2012-08-17 14:11:55 -050059# Security group name
60SECGROUP=${SECGROUP:-boot_secgroup}
61
62
Dean Troyer27e32692012-03-16 16:16:56 -050063# Launching servers
64# =================
65
Anthony Young440be4b2012-02-10 21:42:39 -080066# Grab the id of the image to launch
Dean Troyer45495252012-04-13 13:16:38 -050067IMAGE=`glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1`
Anthony Young440be4b2012-02-10 21:42:39 -080068die_if_not_set IMAGE "Failure getting image"
69
70# Instance and volume names
Anthony Young440be4b2012-02-10 21:42:39 -080071VOL_INSTANCE_NAME=${VOL_INSTANCE_NAME:-test_vol_instance}
72VOL_NAME=${VOL_NAME:-test_volume}
73
74# Clean-up from previous runs
75nova delete $VOL_INSTANCE_NAME || true
Anthony Young440be4b2012-02-10 21:42:39 -080076
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +010077if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VOL_INSTANCE_NAME; do sleep 1; done"; then
Anthony Young440be4b2012-02-10 21:42:39 -080078 echo "server didn't terminate!"
79 exit 1
80fi
81
82# Configure Security Groups
Anthony Young440be4b2012-02-10 21:42:39 -080083nova secgroup-delete $SECGROUP || true
84nova secgroup-create $SECGROUP "$SECGROUP description"
85nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
86nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
87
88# Determinine instance type
89INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2`
90if [[ -z "$INSTANCE_TYPE" ]]; then
91 # grab the first flavor in the list to launch if default doesn't exist
92 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2`
93fi
94
95# Setup Keypair
96KEY_NAME=test_key
97KEY_FILE=key.pem
98nova keypair-delete $KEY_NAME || true
99nova keypair-add $KEY_NAME > $KEY_FILE
100chmod 600 $KEY_FILE
101
Anthony Young440be4b2012-02-10 21:42:39 -0800102# Delete the old volume
John Griffith161e2802012-11-05 13:59:49 -0700103cinder delete $VOL_NAME || true
Anthony Young440be4b2012-02-10 21:42:39 -0800104
105# Free every floating ips - setting FREE_ALL_FLOATING_IPS=True in localrc will make life easier for testers
106if [ "$FREE_ALL_FLOATING_IPS" = "True" ]; then
107 nova floating-ip-list | grep nova | cut -d "|" -f2 | tr -d " " | xargs -n1 nova floating-ip-delete || true
108fi
109
110# Allocate floating ip
Dean Troyer27e32692012-03-16 16:16:56 -0500111FLOATING_IP=`nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | get_field 1`
Anthony Young440be4b2012-02-10 21:42:39 -0800112
113# Make sure the ip gets allocated
114if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then
115 echo "Floating IP not allocated"
116 exit 1
117fi
118
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +0100119# Create the bootable volume
Armando Migliacciob0d8a822012-12-13 16:08:48 +0000120cinder create --display_name=$VOL_NAME --image-id $IMAGE $DEFAULT_VOLUME_SIZE
Anthony Young440be4b2012-02-10 21:42:39 -0800121
122# Wait for volume to activate
John Griffith161e2802012-11-05 13:59:49 -0700123if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep available; do sleep 1; done"; then
Anthony Young440be4b2012-02-10 21:42:39 -0800124 echo "Volume $VOL_NAME not created"
125 exit 1
126fi
127
John Griffith161e2802012-11-05 13:59:49 -0700128VOLUME_ID=`cinder list | grep $VOL_NAME | get_field 1`
Anthony Young440be4b2012-02-10 21:42:39 -0800129
130# Boot instance from volume! This is done with the --block_device_mapping param.
131# The format of mapping is:
132# <dev_name>=<id>:<type>:<size(GB)>:<delete_on_terminate>
133# Leaving the middle two fields blank appears to do-the-right-thing
Dean Troyer27e32692012-03-16 16:16:56 -0500134VOL_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`
135die_if_not_set VOL_VM_UUID "Failure launching $VOL_INSTANCE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800136
137# Check that the status is active within ACTIVE_TIMEOUT seconds
138if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VOL_VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
139 echo "server didn't become active!"
140 exit 1
141fi
142
143# Add floating ip to our server
Anthony Young440be4b2012-02-10 21:42:39 -0800144nova add-floating-ip $VOL_VM_UUID $FLOATING_IP
145
146# Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
Nachi Uenofda946e2012-10-24 17:26:02 -0700147ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT
Anthony Young440be4b2012-02-10 21:42:39 -0800148
149# Make sure our volume-backed instance launched
Nachi Uenofda946e2012-10-24 17:26:02 -0700150ssh_check "$PUBLIC_NETWORK_NAME" $KEY_FILE $FLOATING_IP $DEFAULT_INSTANCE_USER $ACTIVE_TIMEOUT
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +0100151
152# Remove floating ip from volume-backed instance
153nova remove-floating-ip $VOL_VM_UUID $FLOATING_IP
Anthony Young440be4b2012-02-10 21:42:39 -0800154
155# Delete volume backed instance
Dean Troyer27e32692012-03-16 16:16:56 -0500156nova delete $VOL_INSTANCE_NAME || \
157 die "Failure deleting instance volume $VOL_INSTANCE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800158
159# Wait till our volume is no longer in-use
John Griffith161e2802012-11-05 13:59:49 -0700160if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep available; do sleep 1; done"; then
Anthony Young440be4b2012-02-10 21:42:39 -0800161 echo "Volume $VOL_NAME not created"
162 exit 1
163fi
164
165# Delete the volume
John Griffith161e2802012-11-05 13:59:49 -0700166cinder delete $VOL_NAME || \
Dean Troyer27e32692012-03-16 16:16:56 -0500167 die "Failure deleting volume $VOLUME_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800168
Anthony Young440be4b2012-02-10 21:42:39 -0800169# De-allocate the floating ip
Dean Troyer27e32692012-03-16 16:16:56 -0500170nova floating-ip-delete $FLOATING_IP || \
171 die "Failure deleting floating IP $FLOATING_IP"
Anthony Young440be4b2012-02-10 21:42:39 -0800172
Dean Troyer27e32692012-03-16 16:16:56 -0500173# Delete a secgroup
Dean Troyer96288ba2012-08-17 14:11:55 -0500174nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP"
Anthony Young440be4b2012-02-10 21:42:39 -0800175
176set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500177echo "*********************************************************************"
178echo "SUCCESS: End DevStack Exercise: $0"
179echo "*********************************************************************"