blob: ed8ba6310ef75a5b7ac6d5cde3755363efe3a1a2 [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
33# Import configuration
34source $TOP_DIR/openrc
35
Mark McClainb05c8762013-07-06 23:29:39 -040036# Import neutron functions if needed
37if is_service_enabled neutron; then
38 source $TOP_DIR/lib/neutron
Nachi Ueno5db5bfa2012-10-29 11:25:29 -070039fi
40
Anthony Young440be4b2012-02-10 21:42:39 -080041# Import exercise configuration
42source $TOP_DIR/exerciserc
43
Joe Gordon6fd28112012-11-13 16:55:41 -080044# If cinder is not enabled we exit with exitcode 55 so that
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +010045# the exercise is skipped
Joe Gordon6fd28112012-11-13 16:55:41 -080046is_service_enabled cinder || exit 55
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +010047
Dean Troyer2aa2a892013-08-04 19:53:19 -050048# Also skip if the hypervisor is Docker
49[[ "$VIRT_DRIVER" == "docker" ]] && exit 55
50
Dean Troyerda85cda2013-02-15 11:07:14 -060051# Instance type to create
Anthony Young440be4b2012-02-10 21:42:39 -080052DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
53
Dean Troyerda85cda2013-02-15 11:07:14 -060054# Boot this image, use first AMI image if unset
55DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
Dean Troyer27e32692012-03-16 16:16:56 -050056
Dean Troyer96288ba2012-08-17 14:11:55 -050057# Security group name
58SECGROUP=${SECGROUP:-boot_secgroup}
59
Dean Troyerda85cda2013-02-15 11:07:14 -060060# Instance and volume names
61VM_NAME=${VM_NAME:-ex-bfv-inst}
62VOL_NAME=${VOL_NAME:-ex-vol-bfv}
Dean Troyer96288ba2012-08-17 14:11:55 -050063
Dean Troyerda85cda2013-02-15 11:07:14 -060064
65# Launching a server
66# ==================
67
68# List servers for tenant:
69nova list
70
71# Images
72# ------
73
74# List the images available
75glance image-list
Dean Troyer27e32692012-03-16 16:16:56 -050076
Anthony Young440be4b2012-02-10 21:42:39 -080077# Grab the id of the image to launch
Dean Troyerda85cda2013-02-15 11:07:14 -060078IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
Nachi Ueno07115eb2013-02-26 12:38:18 -080079die_if_not_set $LINENO IMAGE "Failure getting image $DEFAULT_IMAGE_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -080080
Dean Troyerda85cda2013-02-15 11:07:14 -060081# Security Groups
82# ---------------
Anthony Young440be4b2012-02-10 21:42:39 -080083
Dean Troyerda85cda2013-02-15 11:07:14 -060084# List security groups
85nova secgroup-list
Anthony Young440be4b2012-02-10 21:42:39 -080086
Chris Behrensc62c2b92013-07-24 03:56:13 -070087if is_service_enabled n-cell; then
88 # Cells does not support security groups, so force the use of "default"
89 SECGROUP="default"
90 echo "Using the default security group because of Cells."
91else
92 # Create a secgroup
93 if ! nova secgroup-list | grep -q $SECGROUP; then
94 nova secgroup-create $SECGROUP "$SECGROUP description"
95 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
96 echo "Security group not created"
97 exit 1
98 fi
Dean Troyerda85cda2013-02-15 11:07:14 -060099 fi
Anthony Young440be4b2012-02-10 21:42:39 -0800100fi
101
Dean Troyerda85cda2013-02-15 11:07:14 -0600102# Configure Security Group Rules
103if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
104 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
105fi
106if ! nova secgroup-list-rules $SECGROUP | grep -q " tcp .* 22 "; then
107 nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
108fi
Anthony Young440be4b2012-02-10 21:42:39 -0800109
Dean Troyerda85cda2013-02-15 11:07:14 -0600110# List secgroup rules
111nova secgroup-list-rules $SECGROUP
112
113# Set up instance
114# ---------------
115
116# List flavors
117nova flavor-list
118
119# Select a flavor
120INSTANCE_TYPE=$(nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1)
Anthony Young440be4b2012-02-10 21:42:39 -0800121if [[ -z "$INSTANCE_TYPE" ]]; then
122 # grab the first flavor in the list to launch if default doesn't exist
Sean Dague922c8ae2013-10-22 10:06:06 -0400123 INSTANCE_TYPE=$(nova flavor-list | head -n 4 | tail -n 1 | get_field 1)
Dean Troyerda85cda2013-02-15 11:07:14 -0600124fi
125
126# Clean-up from previous runs
127nova delete $VM_NAME || true
128if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VM_NAME; do sleep 1; done"; then
129 echo "server didn't terminate!"
130 exit 1
Anthony Young440be4b2012-02-10 21:42:39 -0800131fi
132
133# Setup Keypair
134KEY_NAME=test_key
135KEY_FILE=key.pem
136nova keypair-delete $KEY_NAME || true
137nova keypair-add $KEY_NAME > $KEY_FILE
138chmod 600 $KEY_FILE
139
Dean Troyerda85cda2013-02-15 11:07:14 -0600140# Set up volume
141# -------------
142
143# Delete any old volume
John Griffith161e2802012-11-05 13:59:49 -0700144cinder delete $VOL_NAME || true
Dean Troyerda85cda2013-02-15 11:07:14 -0600145if ! timeout $ACTIVE_TIMEOUT sh -c "while cinder list | grep $VOL_NAME; do sleep 1; done"; then
146 echo "Volume $VOL_NAME not deleted"
Anthony Young440be4b2012-02-10 21:42:39 -0800147 exit 1
148fi
149
Eoghan Glynnfc65cfe2012-10-19 21:26:41 +0100150# Create the bootable volume
Dean Troyerda85cda2013-02-15 11:07:14 -0600151start_time=$(date +%s)
Dean Troyer526b79f2013-11-22 11:30:44 -0600152cinder 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 -0800153 die $LINENO "Failure creating volume $VOL_NAME"
John Griffith161e2802012-11-05 13:59:49 -0700154if ! 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 -0800155 echo "Volume $VOL_NAME not created"
156 exit 1
157fi
Dean Troyerda85cda2013-02-15 11:07:14 -0600158end_time=$(date +%s)
159echo "Completed cinder create in $((end_time - start_time)) seconds"
Anthony Young440be4b2012-02-10 21:42:39 -0800160
Dean Troyerda85cda2013-02-15 11:07:14 -0600161# Get volume ID
162VOL_ID=$(cinder list | grep $VOL_NAME | get_field 1)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800163die_if_not_set $LINENO VOL_ID "Failure retrieving volume ID for $VOL_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800164
Dean Troyerda85cda2013-02-15 11:07:14 -0600165# Boot instance
166# -------------
167
Dean Troyer526b79f2013-11-22 11:30:44 -0600168# Boot using the --block-device-mapping param. The format of mapping is:
Anthony Young440be4b2012-02-10 21:42:39 -0800169# <dev_name>=<id>:<type>:<size(GB)>:<delete_on_terminate>
170# Leaving the middle two fields blank appears to do-the-right-thing
Dean Troyer526b79f2013-11-22 11:30:44 -0600171VM_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 -0800172die_if_not_set $LINENO VM_UUID "Failure launching $VM_NAME"
Anthony Young440be4b2012-02-10 21:42:39 -0800173
174# Check that the status is active within ACTIVE_TIMEOUT seconds
Dean Troyerda85cda2013-02-15 11:07:14 -0600175if ! 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 -0800176 echo "server didn't become active!"
177 exit 1
178fi
179
Dean Troyerda85cda2013-02-15 11:07:14 -0600180# Get the instance IP
Nachi Ueno6769b162013-08-12 18:18:56 -0700181IP=$(get_instance_ip $VM_UUID $PRIVATE_NETWORK_NAME)
182
Nachi Ueno07115eb2013-02-26 12:38:18 -0800183die_if_not_set $LINENO IP "Failure retrieving IP address"
Anthony Young440be4b2012-02-10 21:42:39 -0800184
Dean Troyerda85cda2013-02-15 11:07:14 -0600185# Private IPs can be pinged in single node deployments
186ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
Anthony Young440be4b2012-02-10 21:42:39 -0800187
Dean Troyerda85cda2013-02-15 11:07:14 -0600188# Clean up
189# --------
Anthony Young440be4b2012-02-10 21:42:39 -0800190
191# Delete volume backed instance
Nachi Ueno07115eb2013-02-26 12:38:18 -0800192nova delete $VM_UUID || die $LINENO "Failure deleting instance $VM_NAME"
Dean Troyerda85cda2013-02-15 11:07:14 -0600193if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
194 echo "Server $VM_NAME not deleted"
Anthony Young440be4b2012-02-10 21:42:39 -0800195 exit 1
196fi
197
Dean Troyerda85cda2013-02-15 11:07:14 -0600198# Wait for volume to be released
199if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep available; do sleep 1; done"; then
200 echo "Volume $VOL_NAME not released"
201 exit 1
202fi
Anthony Young440be4b2012-02-10 21:42:39 -0800203
Dean Troyerda85cda2013-02-15 11:07:14 -0600204# Delete volume
205start_time=$(date +%s)
Nachi Ueno07115eb2013-02-26 12:38:18 -0800206cinder delete $VOL_ID || die $LINENO "Failure deleting volume $VOLUME_NAME"
Dean Troyerda85cda2013-02-15 11:07:14 -0600207if ! timeout $ACTIVE_TIMEOUT sh -c "while cinder list | grep $VOL_NAME; do sleep 1; done"; then
208 echo "Volume $VOL_NAME not deleted"
209 exit 1
210fi
211end_time=$(date +%s)
212echo "Completed cinder delete in $((end_time - start_time)) seconds"
Anthony Young440be4b2012-02-10 21:42:39 -0800213
Chris Behrensc62c2b92013-07-24 03:56:13 -0700214if [[ $SECGROUP = "default" ]] ; then
215 echo "Skipping deleting default security group"
216else
217 # Delete secgroup
218 nova secgroup-delete $SECGROUP || die $LINENO "Failure deleting security group $SECGROUP"
219fi
Anthony Young440be4b2012-02-10 21:42:39 -0800220
221set +o xtrace
Dean Troyer27e32692012-03-16 16:16:56 -0500222echo "*********************************************************************"
223echo "SUCCESS: End DevStack Exercise: $0"
224echo "*********************************************************************"