blob: 79120460b8e0cd257611a6b2656917a49f900791 [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
35
Anthony Young440be4b2012-02-10 21:42:39 -080036# Import configuration
37source $TOP_DIR/openrc
38
39# Import exercise configuration
40source $TOP_DIR/exerciserc
41
Joe Gordon6fd28112012-11-13 16:55:41 -080042# If cinder is not enabled we exit with exitcode 55 so that
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +010043# the exercise is skipped
Joe Gordon6fd28112012-11-13 16:55:41 -080044is_service_enabled cinder || exit 55
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +010045
Dean Troyer2aa2a892013-08-04 19:53:19 -050046# Also skip if the hypervisor is Docker
47[[ "$VIRT_DRIVER" == "docker" ]] && exit 55
48
Dean Troyerda85cda2013-02-15 11:07:14 -060049# Instance type to create
Anthony Young440be4b2012-02-10 21:42:39 -080050DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
51
Dean Troyerda85cda2013-02-15 11:07:14 -060052# Boot this image, use first AMI image if unset
53DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
Dean Troyer27e32692012-03-16 16:16:56 -050054
Dean Troyer96288ba2012-08-17 14:11:55 -050055# Security group name
56SECGROUP=${SECGROUP:-boot_secgroup}
57
Dean Troyerda85cda2013-02-15 11:07:14 -060058# Instance and volume names
59VM_NAME=${VM_NAME:-ex-bfv-inst}
60VOL_NAME=${VOL_NAME:-ex-vol-bfv}
Dean Troyer96288ba2012-08-17 14:11:55 -050061
Dean Troyerda85cda2013-02-15 11:07:14 -060062
63# Launching a server
64# ==================
65
66# List servers for tenant:
67nova list
68
69# Images
70# ------
71
72# List the images available
73glance image-list
Dean Troyer27e32692012-03-16 16:16:56 -050074
Anthony Young440be4b2012-02-10 21:42:39 -080075# Grab the id of the image to launch
Dean Troyerda85cda2013-02-15 11:07:14 -060076IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
Nachi Ueno07115eb2013-02-26 12:38:18 -080077die_if_not_set $LINENO IMAGE "Failure getting image $DEFAULT_IMAGE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -080078
Dean Troyerda85cda2013-02-15 11:07:14 -060079# Security Groups
80# ---------------
Anthony Young440be4b2012-02-10 21:42:39 -080081
Dean Troyerda85cda2013-02-15 11:07:14 -060082# List security groups
83nova secgroup-list
Anthony Young440be4b2012-02-10 21:42:39 -080084
Chris Behrensc62c2b92013-07-24 03:56:13 -070085if is_service_enabled n-cell; then
86 # Cells does not support security groups, so force the use of "default"
87 SECGROUP="default"
88 echo "Using the default security group because of Cells."
89else
90 # Create a secgroup
91 if ! nova secgroup-list | grep -q $SECGROUP; then
92 nova secgroup-create $SECGROUP "$SECGROUP description"
93 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
94 echo "Security group not created"
95 exit 1
96 fi
Dean Troyerda85cda2013-02-15 11:07:14 -060097 fi
Anthony Young440be4b2012-02-10 21:42:39 -080098fi
99
Dean Troyerda85cda2013-02-15 11:07:14 -0600100# Configure Security Group Rules
101if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
102 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
103fi
104if ! nova secgroup-list-rules $SECGROUP | grep -q " tcp .* 22 "; then
105 nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
106fi
Anthony Young440be4b2012-02-10 21:42:39 -0800107
Dean Troyerda85cda2013-02-15 11:07:14 -0600108# List secgroup rules
109nova secgroup-list-rules $SECGROUP
110
111# Set up instance
112# ---------------
113
114# List flavors
115nova flavor-list
116
117# Select a flavor
118INSTANCE_TYPE=$(nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1)
Anthony Young440be4b2012-02-10 21:42:39 -0800119if [[ -z "$INSTANCE_TYPE" ]]; then
120 # grab the first flavor in the list to launch if default doesn't exist
Sean Dague922c8ae2013-10-22 10:06:06 -0400121 INSTANCE_TYPE=$(nova flavor-list | head -n 4 | tail -n 1 | get_field 1)
Dean Troyerda85cda2013-02-15 11:07:14 -0600122fi
123
124# Clean-up from previous runs
125nova delete $VM_NAME || true
126if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VM_NAME; do sleep 1; done"; then
127 echo "server didn't terminate!"
128 exit 1
Anthony Young440be4b2012-02-10 21:42:39 -0800129fi
130
131# Setup Keypair
132KEY_NAME=test_key
133KEY_FILE=key.pem
134nova keypair-delete $KEY_NAME || true
135nova keypair-add $KEY_NAME > $KEY_FILE
136chmod 600 $KEY_FILE
137
Dean Troyerda85cda2013-02-15 11:07:14 -0600138# Set up volume
139# -------------
140
141# Delete any old volume
John Griffith161e2802012-11-05 13:59:49 -0700142cinder delete $VOL_NAME || true
Dean Troyerda85cda2013-02-15 11:07:14 -0600143if ! timeout $ACTIVE_TIMEOUT sh -c "while cinder list | grep $VOL_NAME; do sleep 1; done"; then
144 echo "Volume $VOL_NAME not deleted"
Anthony Young440be4b2012-02-10 21:42:39 -0800145 exit 1
146fi
147
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +0100148# Create the bootable volume
Dean Troyerda85cda2013-02-15 11:07:14 -0600149start_time=$(date +%s)
Dean Troyer526b79f2013-11-22 11:30:44 -0600150cinder 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 -0800151 die $LINENO "Failure creating volume $VOL_NAME"
John Griffith161e2802012-11-05 13:59:49 -0700152if ! 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 -0800153 echo "Volume $VOL_NAME not created"
154 exit 1
155fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600156end_time=$(date +%s)
157echo "Completed cinder create in $((end_time - start_time)) seconds"
Anthony Young440be4b2012-02-10 21:42:39 -0800158
Dean Troyerda85cda2013-02-15 11:07:14 -0600159# Get volume ID
160VOL_ID=$(cinder list | grep $VOL_NAME | get_field 1)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800161die_if_not_set $LINENO VOL_ID "Failure retrieving volume ID for $VOL_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800162
Dean Troyerda85cda2013-02-15 11:07:14 -0600163# Boot instance
164# -------------
165
Dean Troyer526b79f2013-11-22 11:30:44 -0600166# Boot using the --block-device-mapping param. The format of mapping is:
Anthony Young440be4b2012-02-10 21:42:39 -0800167# <dev_name>=<id>:<type>:<size(GB)>:<delete_on_terminate>
168# Leaving the middle two fields blank appears to do-the-right-thing
Dean Troyer526b79f2013-11-22 11:30:44 -0600169VM_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 -0800170die_if_not_set $LINENO VM_UUID "Failure launching $VM_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800171
172# Check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyerda85cda2013-02-15 11:07:14 -0600173if ! 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 -0800174 echo "server didn't become active!"
175 exit 1
176fi
177
Dean Troyerda85cda2013-02-15 11:07:14 -0600178# Get the instance IP
Nachi Ueno6769b162013-08-12 18:18:56 -0700179IP=$(get_instance_ip $VM_UUID $PRIVATE_NETWORK_NAME)
180
Nachi Ueno07115eb2013-02-26 12:38:18 -0800181die_if_not_set $LINENO IP "Failure retrieving IP address"
Anthony Young440be4b2012-02-10 21:42:39 -0800182
Dean Troyerda85cda2013-02-15 11:07:14 -0600183# Private IPs can be pinged in single node deployments
184ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
Anthony Young440be4b2012-02-10 21:42:39 -0800185
Dean Troyerda85cda2013-02-15 11:07:14 -0600186# Clean up
187# --------
Anthony Young440be4b2012-02-10 21:42:39 -0800188
189# Delete volume backed instance
Nachi Ueno07115eb2013-02-26 12:38:18 -0800190nova delete $VM_UUID || die $LINENO "Failure deleting instance $VM_NAME"
Dean Troyerda85cda2013-02-15 11:07:14 -0600191if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
192 echo "Server $VM_NAME not deleted"
Anthony Young440be4b2012-02-10 21:42:39 -0800193 exit 1
194fi
195
Dean Troyerda85cda2013-02-15 11:07:14 -0600196# Wait for volume to be released
197if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep available; do sleep 1; done"; then
198 echo "Volume $VOL_NAME not released"
199 exit 1
200fi
Anthony Young440be4b2012-02-10 21:42:39 -0800201
Dean Troyerda85cda2013-02-15 11:07:14 -0600202# Delete volume
203start_time=$(date +%s)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800204cinder delete $VOL_ID || die $LINENO "Failure deleting volume $VOLUME_NAME"
Dean Troyerda85cda2013-02-15 11:07:14 -0600205if ! timeout $ACTIVE_TIMEOUT sh -c "while cinder list | grep $VOL_NAME; do sleep 1; done"; then
206 echo "Volume $VOL_NAME not deleted"
207 exit 1
208fi
209end_time=$(date +%s)
210echo "Completed cinder delete in $((end_time - start_time)) seconds"
Anthony Young440be4b2012-02-10 21:42:39 -0800211
Chris Behrensc62c2b92013-07-24 03:56:13 -0700212if [[ $SECGROUP = "default" ]] ; then
213 echo "Skipping deleting default security group"
214else
215 # Delete secgroup
216 nova secgroup-delete $SECGROUP || die $LINENO "Failure deleting security group $SECGROUP"
217fi
Anthony Young440be4b2012-02-10 21:42:39 -0800218
219set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500220echo "*********************************************************************"
221echo "SUCCESS: End DevStack Exercise: $0"
222echo "*********************************************************************"