blob: 460b50cf105e5e9e67e7703ecbcb5cfd74c16621 [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
35# Import exercise configuration
36source $TOP_DIR/exerciserc
37
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +010038# If cinder or n-vol are not enabled we exit with exitcode 55 so that
39# the exercise is skipped
40is_service_enabled cinder n-vol || exit 55
41
Anthony Young440be4b2012-02-10 21:42:39 -080042# Boot this image, use first AMI image if unset
43DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
44
45# Instance type
46DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
47
48# Default floating IP pool name
49DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova}
50
Devananda van der Veenc0c6f002012-07-06 17:49:12 -070051# Default user
52DEFAULT_INSTANCE_USER=${DEFAULT_INSTANCE_USER:-cirros}
Dean Troyer27e32692012-03-16 16:16:56 -050053
Dean Troyer96288ba2012-08-17 14:11:55 -050054# Security group name
55SECGROUP=${SECGROUP:-boot_secgroup}
56
57
Dean Troyer27e32692012-03-16 16:16:56 -050058# Launching servers
59# =================
60
Anthony Young440be4b2012-02-10 21:42:39 -080061# Grab the id of the image to launch
Dean Troyer45495252012-04-13 13:16:38 -050062IMAGE=`glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1`
Anthony Young440be4b2012-02-10 21:42:39 -080063die_if_not_set IMAGE "Failure getting image"
64
65# Instance and volume names
Anthony Young440be4b2012-02-10 21:42:39 -080066VOL_INSTANCE_NAME=${VOL_INSTANCE_NAME:-test_vol_instance}
67VOL_NAME=${VOL_NAME:-test_volume}
68
69# Clean-up from previous runs
70nova delete $VOL_INSTANCE_NAME || true
Anthony Young440be4b2012-02-10 21:42:39 -080071
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +010072if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VOL_INSTANCE_NAME; do sleep 1; done"; then
Anthony Young440be4b2012-02-10 21:42:39 -080073 echo "server didn't terminate!"
74 exit 1
75fi
76
77# Configure Security Groups
Anthony Young440be4b2012-02-10 21:42:39 -080078nova secgroup-delete $SECGROUP || true
79nova secgroup-create $SECGROUP "$SECGROUP description"
80nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
81nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
82
83# Determinine instance type
84INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2`
85if [[ -z "$INSTANCE_TYPE" ]]; then
86 # grab the first flavor in the list to launch if default doesn't exist
87 INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2`
88fi
89
90# Setup Keypair
91KEY_NAME=test_key
92KEY_FILE=key.pem
93nova keypair-delete $KEY_NAME || true
94nova keypair-add $KEY_NAME > $KEY_FILE
95chmod 600 $KEY_FILE
96
Anthony Young440be4b2012-02-10 21:42:39 -080097# Delete the old volume
98nova volume-delete $VOL_NAME || true
99
100# Free every floating ips - setting FREE_ALL_FLOATING_IPS=True in localrc will make life easier for testers
101if [ "$FREE_ALL_FLOATING_IPS" = "True" ]; then
102 nova floating-ip-list | grep nova | cut -d "|" -f2 | tr -d " " | xargs -n1 nova floating-ip-delete || true
103fi
104
105# Allocate floating ip
Dean Troyer27e32692012-03-16 16:16:56 -0500106FLOATING_IP=`nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | get_field 1`
Anthony Young440be4b2012-02-10 21:42:39 -0800107
108# Make sure the ip gets allocated
109if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then
110 echo "Floating IP not allocated"
111 exit 1
112fi
113
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +0100114# Create the bootable volume
115nova volume-create --display_name=$VOL_NAME --image-id $IMAGE 1
Anthony Young440be4b2012-02-10 21:42:39 -0800116
117# Wait for volume to activate
118if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
119 echo "Volume $VOL_NAME not created"
120 exit 1
121fi
122
Dean Troyer27e32692012-03-16 16:16:56 -0500123VOLUME_ID=`nova volume-list | grep $VOL_NAME | get_field 1`
Anthony Young440be4b2012-02-10 21:42:39 -0800124
125# Boot instance from volume! This is done with the --block_device_mapping param.
126# The format of mapping is:
127# <dev_name>=<id>:<type>:<size(GB)>:<delete_on_terminate>
128# Leaving the middle two fields blank appears to do-the-right-thing
Dean Troyer27e32692012-03-16 16:16:56 -0500129VOL_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`
130die_if_not_set VOL_VM_UUID "Failure launching $VOL_INSTANCE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800131
132# Check that the status is active within ACTIVE_TIMEOUT seconds
133if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VOL_VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
134 echo "server didn't become active!"
135 exit 1
136fi
137
138# Add floating ip to our server
Anthony Young440be4b2012-02-10 21:42:39 -0800139nova add-floating-ip $VOL_VM_UUID $FLOATING_IP
140
141# Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
Nachi Uenofda946e2012-10-24 17:26:02 -0700142ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT
Anthony Young440be4b2012-02-10 21:42:39 -0800143
144# Make sure our volume-backed instance launched
Nachi Uenofda946e2012-10-24 17:26:02 -0700145ssh_check "$PUBLIC_NETWORK_NAME" $KEY_FILE $FLOATING_IP $DEFAULT_INSTANCE_USER $ACTIVE_TIMEOUT
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +0100146
147# Remove floating ip from volume-backed instance
148nova remove-floating-ip $VOL_VM_UUID $FLOATING_IP
Anthony Young440be4b2012-02-10 21:42:39 -0800149
150# Delete volume backed instance
Dean Troyer27e32692012-03-16 16:16:56 -0500151nova delete $VOL_INSTANCE_NAME || \
152 die "Failure deleting instance volume $VOL_INSTANCE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800153
154# Wait till our volume is no longer in-use
155if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
156 echo "Volume $VOL_NAME not created"
157 exit 1
158fi
159
160# Delete the volume
Dean Troyer27e32692012-03-16 16:16:56 -0500161nova volume-delete $VOL_NAME || \
162 die "Failure deleting volume $VOLUME_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800163
Anthony Young440be4b2012-02-10 21:42:39 -0800164# De-allocate the floating ip
Dean Troyer27e32692012-03-16 16:16:56 -0500165nova floating-ip-delete $FLOATING_IP || \
166 die "Failure deleting floating IP $FLOATING_IP"
Anthony Young440be4b2012-02-10 21:42:39 -0800167
Dean Troyer27e32692012-03-16 16:16:56 -0500168# Delete a secgroup
Dean Troyer96288ba2012-08-17 14:11:55 -0500169nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP"
Anthony Young440be4b2012-02-10 21:42:39 -0800170
171set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500172echo "*********************************************************************"
173echo "SUCCESS: End DevStack Exercise: $0"
174echo "*********************************************************************"