blob: 14d00492f628bf2d321c497a315d9f764dca7d4a [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
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
Dean Troyerda85cda2013-02-15 11:07:14 -060083# Create a secgroup
84if ! nova secgroup-list | grep -q $SECGROUP; then
85 nova secgroup-create $SECGROUP "$SECGROUP description"
86 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
87 echo "Security group not created"
88 exit 1
89 fi
Anthony Young440be4b2012-02-10 21:42:39 -080090fi
91
Dean Troyerda85cda2013-02-15 11:07:14 -060092# Configure Security Group Rules
93if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
94 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
95fi
96if ! nova secgroup-list-rules $SECGROUP | grep -q " tcp .* 22 "; then
97 nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
98fi
Anthony Young440be4b2012-02-10 21:42:39 -080099
Dean Troyerda85cda2013-02-15 11:07:14 -0600100# List secgroup rules
101nova secgroup-list-rules $SECGROUP
102
103# Set up instance
104# ---------------
105
106# List flavors
107nova flavor-list
108
109# Select a flavor
110INSTANCE_TYPE=$(nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1)
Anthony Young440be4b2012-02-10 21:42:39 -0800111if [[ -z "$INSTANCE_TYPE" ]]; then
112 # grab the first flavor in the list to launch if default doesn't exist
Dean Troyerda85cda2013-02-15 11:07:14 -0600113 INSTANCE_TYPE=$(nova flavor-list | head -n 4 | tail -n 1 | get_field 1)
114fi
115
116# Clean-up from previous runs
117nova delete $VM_NAME || true
118if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VM_NAME; do sleep 1; done"; then
119 echo "server didn't terminate!"
120 exit 1
Anthony Young440be4b2012-02-10 21:42:39 -0800121fi
122
123# Setup Keypair
124KEY_NAME=test_key
125KEY_FILE=key.pem
126nova keypair-delete $KEY_NAME || true
127nova keypair-add $KEY_NAME > $KEY_FILE
128chmod 600 $KEY_FILE
129
Dean Troyerda85cda2013-02-15 11:07:14 -0600130# Set up volume
131# -------------
132
133# Delete any old volume
John Griffith161e2802012-11-05 13:59:49 -0700134cinder delete $VOL_NAME || true
Dean Troyerda85cda2013-02-15 11:07:14 -0600135if ! timeout $ACTIVE_TIMEOUT sh -c "while cinder list | grep $VOL_NAME; do sleep 1; done"; then
136 echo "Volume $VOL_NAME not deleted"
Anthony Young440be4b2012-02-10 21:42:39 -0800137 exit 1
138fi
139
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +0100140# Create the bootable volume
Dean Troyerda85cda2013-02-15 11:07:14 -0600141start_time=$(date +%s)
142cinder 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 -0800143 die $LINENO "Failure creating volume $VOL_NAME"
John Griffith161e2802012-11-05 13:59:49 -0700144if ! 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 -0800145 echo "Volume $VOL_NAME not created"
146 exit 1
147fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600148end_time=$(date +%s)
149echo "Completed cinder create in $((end_time - start_time)) seconds"
Anthony Young440be4b2012-02-10 21:42:39 -0800150
Dean Troyerda85cda2013-02-15 11:07:14 -0600151# Get volume ID
152VOL_ID=$(cinder list | grep $VOL_NAME | get_field 1)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800153die_if_not_set $LINENO VOL_ID "Failure retrieving volume ID for $VOL_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800154
Dean Troyerda85cda2013-02-15 11:07:14 -0600155# Boot instance
156# -------------
157
158# Boot using the --block_device_mapping param. The format of mapping is:
Anthony Young440be4b2012-02-10 21:42:39 -0800159# <dev_name>=<id>:<type>:<size(GB)>:<delete_on_terminate>
160# Leaving the middle two fields blank appears to do-the-right-thing
Dean Troyerda85cda2013-02-15 11:07:14 -0600161VM_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 -0800162die_if_not_set $LINENO VM_UUID "Failure launching $VM_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800163
164# Check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyerda85cda2013-02-15 11:07:14 -0600165if ! 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 -0800166 echo "server didn't become active!"
167 exit 1
168fi
169
Dean Troyerda85cda2013-02-15 11:07:14 -0600170# Get the instance IP
171IP=$(nova show $VM_UUID | grep "$PRIVATE_NETWORK_NAME" | get_field 2)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800172die_if_not_set $LINENO IP "Failure retrieving IP address"
Anthony Young440be4b2012-02-10 21:42:39 -0800173
Dean Troyerda85cda2013-02-15 11:07:14 -0600174# Private IPs can be pinged in single node deployments
175ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
Anthony Young440be4b2012-02-10 21:42:39 -0800176
Dean Troyerda85cda2013-02-15 11:07:14 -0600177# Clean up
178# --------
Anthony Young440be4b2012-02-10 21:42:39 -0800179
180# Delete volume backed instance
Nachi Ueno07115eb2013-02-26 12:38:18 -0800181nova delete $VM_UUID || die $LINENO "Failure deleting instance $VM_NAME"
Dean Troyerda85cda2013-02-15 11:07:14 -0600182if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
183 echo "Server $VM_NAME not deleted"
Anthony Young440be4b2012-02-10 21:42:39 -0800184 exit 1
185fi
186
Dean Troyerda85cda2013-02-15 11:07:14 -0600187# Wait for volume to be released
188if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep available; do sleep 1; done"; then
189 echo "Volume $VOL_NAME not released"
190 exit 1
191fi
Anthony Young440be4b2012-02-10 21:42:39 -0800192
Dean Troyerda85cda2013-02-15 11:07:14 -0600193# Delete volume
194start_time=$(date +%s)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800195cinder delete $VOL_ID || die $LINENO "Failure deleting volume $VOLUME_NAME"
Dean Troyerda85cda2013-02-15 11:07:14 -0600196if ! timeout $ACTIVE_TIMEOUT sh -c "while cinder list | grep $VOL_NAME; do sleep 1; done"; then
197 echo "Volume $VOL_NAME not deleted"
198 exit 1
199fi
200end_time=$(date +%s)
201echo "Completed cinder delete in $((end_time - start_time)) seconds"
Anthony Young440be4b2012-02-10 21:42:39 -0800202
Dean Troyerda85cda2013-02-15 11:07:14 -0600203# Delete secgroup
Nachi Ueno07115eb2013-02-26 12:38:18 -0800204nova secgroup-delete $SECGROUP || die $LINENO "Failure deleting security group $SECGROUP"
Anthony Young440be4b2012-02-10 21:42:39 -0800205
206set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500207echo "*********************************************************************"
208echo "SUCCESS: End DevStack Exercise: $0"
209echo "*********************************************************************"