blob: 36524ede4b606e6483a0c38da7b573c239eff41b [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
Joe Gordon46400262013-06-30 04:32:27 -070014# only the first error that occurred.
Anthony Young440be4b2012-02-10 21:42:39 -080015set -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
Mark McClainb05c8762013-07-06 23:29:39 -040035# Import neutron functions if needed
36if is_service_enabled neutron; then
37 source $TOP_DIR/lib/neutron
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
Dean Troyerda85cda2013-02-15 11:07:14 -060047# Instance type to create
Anthony Young440be4b2012-02-10 21:42:39 -080048DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
49
Dean Troyerda85cda2013-02-15 11:07:14 -060050# Boot this image, use first AMI image if unset
51DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
Dean Troyer27e32692012-03-16 16:16:56 -050052
Dean Troyer96288ba2012-08-17 14:11:55 -050053# Security group name
54SECGROUP=${SECGROUP:-boot_secgroup}
55
Dean Troyerda85cda2013-02-15 11:07:14 -060056# Instance and volume names
57VM_NAME=${VM_NAME:-ex-bfv-inst}
58VOL_NAME=${VOL_NAME:-ex-vol-bfv}
Dean Troyer96288ba2012-08-17 14:11:55 -050059
Dean Troyerda85cda2013-02-15 11:07:14 -060060
61# Launching a server
62# ==================
63
64# List servers for tenant:
65nova list
66
67# Images
68# ------
69
70# List the images available
71glance image-list
Dean Troyer27e32692012-03-16 16:16:56 -050072
Anthony Young440be4b2012-02-10 21:42:39 -080073# Grab the id of the image to launch
Dean Troyerda85cda2013-02-15 11:07:14 -060074IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
Nachi Ueno07115eb2013-02-26 12:38:18 -080075die_if_not_set $LINENO IMAGE "Failure getting image $DEFAULT_IMAGE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -080076
Dean Troyerda85cda2013-02-15 11:07:14 -060077# Security Groups
78# ---------------
Anthony Young440be4b2012-02-10 21:42:39 -080079
Dean Troyerda85cda2013-02-15 11:07:14 -060080# List security groups
81nova secgroup-list
Anthony Young440be4b2012-02-10 21:42:39 -080082
Chris Behrensc62c2b92013-07-24 03:56:13 -070083if is_service_enabled n-cell; then
84 # Cells does not support security groups, so force the use of "default"
85 SECGROUP="default"
86 echo "Using the default security group because of Cells."
87else
88 # Create a secgroup
89 if ! nova secgroup-list | grep -q $SECGROUP; then
90 nova secgroup-create $SECGROUP "$SECGROUP description"
91 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
92 echo "Security group not created"
93 exit 1
94 fi
Dean Troyerda85cda2013-02-15 11:07:14 -060095 fi
Anthony Young440be4b2012-02-10 21:42:39 -080096fi
97
Dean Troyerda85cda2013-02-15 11:07:14 -060098# Configure Security Group Rules
99if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
100 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
101fi
102if ! nova secgroup-list-rules $SECGROUP | grep -q " tcp .* 22 "; then
103 nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
104fi
Anthony Young440be4b2012-02-10 21:42:39 -0800105
Dean Troyerda85cda2013-02-15 11:07:14 -0600106# List secgroup rules
107nova secgroup-list-rules $SECGROUP
108
109# Set up instance
110# ---------------
111
112# List flavors
113nova flavor-list
114
115# Select a flavor
116INSTANCE_TYPE=$(nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1)
Anthony Young440be4b2012-02-10 21:42:39 -0800117if [[ -z "$INSTANCE_TYPE" ]]; then
118 # grab the first flavor in the list to launch if default doesn't exist
Dean Troyerda85cda2013-02-15 11:07:14 -0600119 INSTANCE_TYPE=$(nova flavor-list | head -n 4 | tail -n 1 | get_field 1)
120fi
121
122# Clean-up from previous runs
123nova delete $VM_NAME || true
124if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VM_NAME; do sleep 1; done"; then
125 echo "server didn't terminate!"
126 exit 1
Anthony Young440be4b2012-02-10 21:42:39 -0800127fi
128
129# Setup Keypair
130KEY_NAME=test_key
131KEY_FILE=key.pem
132nova keypair-delete $KEY_NAME || true
133nova keypair-add $KEY_NAME > $KEY_FILE
134chmod 600 $KEY_FILE
135
Dean Troyerda85cda2013-02-15 11:07:14 -0600136# Set up volume
137# -------------
138
139# Delete any old volume
John Griffith161e2802012-11-05 13:59:49 -0700140cinder delete $VOL_NAME || true
Dean Troyerda85cda2013-02-15 11:07:14 -0600141if ! timeout $ACTIVE_TIMEOUT sh -c "while cinder list | grep $VOL_NAME; do sleep 1; done"; then
142 echo "Volume $VOL_NAME not deleted"
Anthony Young440be4b2012-02-10 21:42:39 -0800143 exit 1
144fi
145
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +0100146# Create the bootable volume
Dean Troyerda85cda2013-02-15 11:07:14 -0600147start_time=$(date +%s)
148cinder create --image-id $IMAGE --display_name=$VOL_NAME --display_description "test bootable volume: $VOL_NAME" $DEFAULT_VOLUME_SIZE || \
Nachi Ueno07115eb2013-02-26 12:38:18 -0800149 die $LINENO "Failure creating volume $VOL_NAME"
John Griffith161e2802012-11-05 13:59:49 -0700150if ! 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 -0800151 echo "Volume $VOL_NAME not created"
152 exit 1
153fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600154end_time=$(date +%s)
155echo "Completed cinder create in $((end_time - start_time)) seconds"
Anthony Young440be4b2012-02-10 21:42:39 -0800156
Dean Troyerda85cda2013-02-15 11:07:14 -0600157# Get volume ID
158VOL_ID=$(cinder list | grep $VOL_NAME | get_field 1)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800159die_if_not_set $LINENO VOL_ID "Failure retrieving volume ID for $VOL_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800160
Dean Troyerda85cda2013-02-15 11:07:14 -0600161# Boot instance
162# -------------
163
164# Boot using the --block_device_mapping param. The format of mapping is:
Anthony Young440be4b2012-02-10 21:42:39 -0800165# <dev_name>=<id>:<type>:<size(GB)>:<delete_on_terminate>
166# Leaving the middle two fields blank appears to do-the-right-thing
Dean Troyerda85cda2013-02-15 11:07:14 -0600167VM_UUID=$(nova boot --flavor $INSTANCE_TYPE --image $IMAGE --block-device-mapping vda=$VOL_ID --security_groups=$SECGROUP --key_name $KEY_NAME $VM_NAME | grep ' id ' | get_field 2)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800168die_if_not_set $LINENO VM_UUID "Failure launching $VM_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800169
170# Check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyerda85cda2013-02-15 11:07:14 -0600171if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
Anthony Young440be4b2012-02-10 21:42:39 -0800172 echo "server didn't become active!"
173 exit 1
174fi
175
Dean Troyerda85cda2013-02-15 11:07:14 -0600176# Get the instance IP
Nachi Ueno6769b162013-08-12 18:18:56 -0700177IP=$(get_instance_ip $VM_UUID $PRIVATE_NETWORK_NAME)
178
Nachi Ueno07115eb2013-02-26 12:38:18 -0800179die_if_not_set $LINENO IP "Failure retrieving IP address"
Anthony Young440be4b2012-02-10 21:42:39 -0800180
Dean Troyerda85cda2013-02-15 11:07:14 -0600181# Private IPs can be pinged in single node deployments
182ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
Anthony Young440be4b2012-02-10 21:42:39 -0800183
Dean Troyerda85cda2013-02-15 11:07:14 -0600184# Clean up
185# --------
Anthony Young440be4b2012-02-10 21:42:39 -0800186
187# Delete volume backed instance
Nachi Ueno07115eb2013-02-26 12:38:18 -0800188nova delete $VM_UUID || die $LINENO "Failure deleting instance $VM_NAME"
Dean Troyerda85cda2013-02-15 11:07:14 -0600189if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
190 echo "Server $VM_NAME not deleted"
Anthony Young440be4b2012-02-10 21:42:39 -0800191 exit 1
192fi
193
Dean Troyerda85cda2013-02-15 11:07:14 -0600194# Wait for volume to be released
195if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep available; do sleep 1; done"; then
196 echo "Volume $VOL_NAME not released"
197 exit 1
198fi
Anthony Young440be4b2012-02-10 21:42:39 -0800199
Dean Troyerda85cda2013-02-15 11:07:14 -0600200# Delete volume
201start_time=$(date +%s)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800202cinder delete $VOL_ID || die $LINENO "Failure deleting volume $VOLUME_NAME"
Dean Troyerda85cda2013-02-15 11:07:14 -0600203if ! timeout $ACTIVE_TIMEOUT sh -c "while cinder list | grep $VOL_NAME; do sleep 1; done"; then
204 echo "Volume $VOL_NAME not deleted"
205 exit 1
206fi
207end_time=$(date +%s)
208echo "Completed cinder delete in $((end_time - start_time)) seconds"
Anthony Young440be4b2012-02-10 21:42:39 -0800209
Chris Behrensc62c2b92013-07-24 03:56:13 -0700210if [[ $SECGROUP = "default" ]] ; then
211 echo "Skipping deleting default security group"
212else
213 # Delete secgroup
214 nova secgroup-delete $SECGROUP || die $LINENO "Failure deleting security group $SECGROUP"
215fi
Anthony Young440be4b2012-02-10 21:42:39 -0800216
217set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500218echo "*********************************************************************"
219echo "SUCCESS: End DevStack Exercise: $0"
220echo "*********************************************************************"