blob: a2ae275b04f4d2e6f0df69cf1d9fd6e78e6b3892 [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:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01006#
7# * Create a bootable volume
8# * Boot a volume-backed instance
Anthony Young440be4b2012-02-10 21:42:39 -08009
Dean Troyer27e32692012-03-16 16:16:56 -050010echo "*********************************************************************"
Anthony Young440be4b2012-02-10 21:42:39 -080011echo "Begin DevStack Exercise: $0"
Dean Troyer27e32692012-03-16 16:16:56 -050012echo "*********************************************************************"
Anthony Young440be4b2012-02-10 21:42:39 -080013
14# This script exits on an error so that errors don't compound and you see
Joe Gordon46400262013-06-30 04:32:27 -070015# only the first error that occurred.
Anthony Young440be4b2012-02-10 21:42:39 -080016set -o errexit
17
18# Print the commands being run so that we can see the command that triggers
19# an error. It is also useful for following allowing as the install occurs.
20set -o xtrace
21
22
23# Settings
24# ========
25
26# Keep track of the current directory
27EXERCISE_DIR=$(cd $(dirname "$0") && pwd)
28TOP_DIR=$(cd $EXERCISE_DIR/..; pwd)
29
30# Import common functions
31source $TOP_DIR/functions
32
Dean Troyere4fa7212014-01-15 15:04:49 -060033# Import project functions
34source $TOP_DIR/lib/cinder
Brian Haley9bbecb72014-02-28 11:19:28 -050035source $TOP_DIR/lib/neutron
Dean Troyere4fa7212014-01-15 15:04:49 -060036
Anthony Young440be4b2012-02-10 21:42:39 -080037# Import configuration
38source $TOP_DIR/openrc
39
40# 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
Adam Gandelmanb875d012014-03-17 19:47:14 -070047# Ironic does not support boot from volume.
48[ "$VIRT_DRIVER" == "ironic" ] && exit 55
49
Dean Troyerda85cda2013-02-15 11:07:14 -060050# Instance type to create
Anthony Young440be4b2012-02-10 21:42:39 -080051DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
52
Dean Troyerda85cda2013-02-15 11:07:14 -060053# Boot this image, use first AMI image if unset
54DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
Dean Troyer27e32692012-03-16 16:16:56 -050055
Dean Troyer96288ba2012-08-17 14:11:55 -050056# Security group name
57SECGROUP=${SECGROUP:-boot_secgroup}
58
Dean Troyerda85cda2013-02-15 11:07:14 -060059# Instance and volume names
60VM_NAME=${VM_NAME:-ex-bfv-inst}
61VOL_NAME=${VOL_NAME:-ex-vol-bfv}
Dean Troyer96288ba2012-08-17 14:11:55 -050062
Dean Troyerda85cda2013-02-15 11:07:14 -060063
64# Launching a server
65# ==================
66
67# List servers for tenant:
68nova list
69
70# Images
71# ------
72
73# List the images available
Steve Martinelli5c206c22014-08-02 20:32:31 -040074openstack image list
Dean Troyer27e32692012-03-16 16:16:56 -050075
Anthony Young440be4b2012-02-10 21:42:39 -080076# Grab the id of the image to launch
Steve Martinelli5c206c22014-08-02 20:32:31 -040077IMAGE=$(openstack image list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
Nachi Ueno07115eb2013-02-26 12:38:18 -080078die_if_not_set $LINENO IMAGE "Failure getting image $DEFAULT_IMAGE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -080079
Dean Troyerda85cda2013-02-15 11:07:14 -060080# Security Groups
81# ---------------
Anthony Young440be4b2012-02-10 21:42:39 -080082
Dean Troyerda85cda2013-02-15 11:07:14 -060083# List security groups
84nova secgroup-list
Anthony Young440be4b2012-02-10 21:42:39 -080085
Chris Behrensc62c2b92013-07-24 03:56:13 -070086if is_service_enabled n-cell; then
87 # Cells does not support security groups, so force the use of "default"
88 SECGROUP="default"
89 echo "Using the default security group because of Cells."
90else
91 # Create a secgroup
92 if ! nova secgroup-list | grep -q $SECGROUP; then
93 nova secgroup-create $SECGROUP "$SECGROUP description"
94 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
95 echo "Security group not created"
96 exit 1
97 fi
Dean Troyerda85cda2013-02-15 11:07:14 -060098 fi
Anthony Young440be4b2012-02-10 21:42:39 -080099fi
100
Dean Troyerda85cda2013-02-15 11:07:14 -0600101# Configure Security Group Rules
102if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
103 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
104fi
105if ! nova secgroup-list-rules $SECGROUP | grep -q " tcp .* 22 "; then
106 nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
107fi
Anthony Young440be4b2012-02-10 21:42:39 -0800108
Dean Troyerda85cda2013-02-15 11:07:14 -0600109# List secgroup rules
110nova secgroup-list-rules $SECGROUP
111
112# Set up instance
113# ---------------
114
115# List flavors
116nova flavor-list
117
118# Select a flavor
119INSTANCE_TYPE=$(nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1)
Anthony Young440be4b2012-02-10 21:42:39 -0800120if [[ -z "$INSTANCE_TYPE" ]]; then
121 # grab the first flavor in the list to launch if default doesn't exist
Sean Dague922c8ae2013-10-22 10:06:06 -0400122 INSTANCE_TYPE=$(nova flavor-list | head -n 4 | tail -n 1 | get_field 1)
Dean Troyerda85cda2013-02-15 11:07:14 -0600123fi
124
125# Clean-up from previous runs
126nova delete $VM_NAME || true
127if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VM_NAME; do sleep 1; done"; then
128 echo "server didn't terminate!"
129 exit 1
Anthony Young440be4b2012-02-10 21:42:39 -0800130fi
131
132# Setup Keypair
133KEY_NAME=test_key
134KEY_FILE=key.pem
135nova keypair-delete $KEY_NAME || true
136nova keypair-add $KEY_NAME > $KEY_FILE
137chmod 600 $KEY_FILE
138
Dean Troyerda85cda2013-02-15 11:07:14 -0600139# Set up volume
140# -------------
141
142# Delete any old volume
John Griffith161e2802012-11-05 13:59:49 -0700143cinder delete $VOL_NAME || true
Dean Troyerda85cda2013-02-15 11:07:14 -0600144if ! timeout $ACTIVE_TIMEOUT sh -c "while cinder list | grep $VOL_NAME; do sleep 1; done"; then
145 echo "Volume $VOL_NAME not deleted"
Anthony Young440be4b2012-02-10 21:42:39 -0800146 exit 1
147fi
148
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +0100149# Create the bootable volume
Dean Troyerda85cda2013-02-15 11:07:14 -0600150start_time=$(date +%s)
Dean Troyer526b79f2013-11-22 11:30:44 -0600151cinder 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 -0800152 die $LINENO "Failure creating volume $VOL_NAME"
John Griffith161e2802012-11-05 13:59:49 -0700153if ! 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 -0800154 echo "Volume $VOL_NAME not created"
155 exit 1
156fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600157end_time=$(date +%s)
158echo "Completed cinder create in $((end_time - start_time)) seconds"
Anthony Young440be4b2012-02-10 21:42:39 -0800159
Dean Troyerda85cda2013-02-15 11:07:14 -0600160# Get volume ID
161VOL_ID=$(cinder list | grep $VOL_NAME | get_field 1)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800162die_if_not_set $LINENO VOL_ID "Failure retrieving volume ID for $VOL_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800163
Dean Troyerda85cda2013-02-15 11:07:14 -0600164# Boot instance
165# -------------
166
Dean Troyer526b79f2013-11-22 11:30:44 -0600167# Boot using the --block-device-mapping param. The format of mapping is:
Anthony Young440be4b2012-02-10 21:42:39 -0800168# <dev_name>=<id>:<type>:<size(GB)>:<delete_on_terminate>
169# Leaving the middle two fields blank appears to do-the-right-thing
Dean Troyer526b79f2013-11-22 11:30:44 -0600170VM_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 -0800171die_if_not_set $LINENO VM_UUID "Failure launching $VM_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800172
173# Check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyerda85cda2013-02-15 11:07:14 -0600174if ! 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 -0800175 echo "server didn't become active!"
176 exit 1
177fi
178
Dean Troyerda85cda2013-02-15 11:07:14 -0600179# Get the instance IP
Nachi Ueno6769b162013-08-12 18:18:56 -0700180IP=$(get_instance_ip $VM_UUID $PRIVATE_NETWORK_NAME)
181
Nachi Ueno07115eb2013-02-26 12:38:18 -0800182die_if_not_set $LINENO IP "Failure retrieving IP address"
Anthony Young440be4b2012-02-10 21:42:39 -0800183
Dean Troyerda85cda2013-02-15 11:07:14 -0600184# Private IPs can be pinged in single node deployments
185ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
Anthony Young440be4b2012-02-10 21:42:39 -0800186
Dean Troyerda85cda2013-02-15 11:07:14 -0600187# Clean up
188# --------
Anthony Young440be4b2012-02-10 21:42:39 -0800189
190# Delete volume backed instance
Nachi Ueno07115eb2013-02-26 12:38:18 -0800191nova delete $VM_UUID || die $LINENO "Failure deleting instance $VM_NAME"
Dean Troyerda85cda2013-02-15 11:07:14 -0600192if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
193 echo "Server $VM_NAME not deleted"
Anthony Young440be4b2012-02-10 21:42:39 -0800194 exit 1
195fi
196
Dean Troyerda85cda2013-02-15 11:07:14 -0600197# Wait for volume to be released
198if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep available; do sleep 1; done"; then
199 echo "Volume $VOL_NAME not released"
200 exit 1
201fi
Anthony Young440be4b2012-02-10 21:42:39 -0800202
Dean Troyerda85cda2013-02-15 11:07:14 -0600203# Delete volume
204start_time=$(date +%s)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800205cinder delete $VOL_ID || die $LINENO "Failure deleting volume $VOLUME_NAME"
Dean Troyerda85cda2013-02-15 11:07:14 -0600206if ! timeout $ACTIVE_TIMEOUT sh -c "while cinder list | grep $VOL_NAME; do sleep 1; done"; then
207 echo "Volume $VOL_NAME not deleted"
208 exit 1
209fi
210end_time=$(date +%s)
211echo "Completed cinder delete in $((end_time - start_time)) seconds"
Anthony Young440be4b2012-02-10 21:42:39 -0800212
Chris Behrensc62c2b92013-07-24 03:56:13 -0700213if [[ $SECGROUP = "default" ]] ; then
214 echo "Skipping deleting default security group"
215else
216 # Delete secgroup
217 nova secgroup-delete $SECGROUP || die $LINENO "Failure deleting security group $SECGROUP"
218fi
Anthony Young440be4b2012-02-10 21:42:39 -0800219
220set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500221echo "*********************************************************************"
222echo "SUCCESS: End DevStack Exercise: $0"
223echo "*********************************************************************"