| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 1 | #!/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 Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 6 | # | 
|  | 7 | # *  Create a bootable volume | 
|  | 8 | # *  Boot a volume-backed instance | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 9 |  | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 10 | echo "*********************************************************************" | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 11 | echo "Begin DevStack Exercise: $0" | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 12 | echo "*********************************************************************" | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 13 |  | 
|  | 14 | # This script exits on an error so that errors don't compound and you see | 
| Joe Gordon | 4640026 | 2013-06-30 04:32:27 -0700 | [diff] [blame] | 15 | # only the first error that occurred. | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 16 | set -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. | 
|  | 20 | set -o xtrace | 
|  | 21 |  | 
|  | 22 |  | 
|  | 23 | # Settings | 
|  | 24 | # ======== | 
|  | 25 |  | 
|  | 26 | # Keep track of the current directory | 
|  | 27 | EXERCISE_DIR=$(cd $(dirname "$0") && pwd) | 
|  | 28 | TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) | 
|  | 29 |  | 
|  | 30 | # Import common functions | 
|  | 31 | source $TOP_DIR/functions | 
|  | 32 |  | 
| Dean Troyer | e4fa721 | 2014-01-15 15:04:49 -0600 | [diff] [blame] | 33 | # Import project functions | 
|  | 34 | source $TOP_DIR/lib/cinder | 
| Brian Haley | 9bbecb7 | 2014-02-28 11:19:28 -0500 | [diff] [blame] | 35 | source $TOP_DIR/lib/neutron | 
| Dean Troyer | e4fa721 | 2014-01-15 15:04:49 -0600 | [diff] [blame] | 36 |  | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 37 | # Import configuration | 
|  | 38 | source $TOP_DIR/openrc | 
|  | 39 |  | 
|  | 40 | # Import exercise configuration | 
|  | 41 | source $TOP_DIR/exerciserc | 
|  | 42 |  | 
| Joe Gordon | 6fd2811 | 2012-11-13 16:55:41 -0800 | [diff] [blame] | 43 | # If cinder is not enabled we exit with exitcode 55 so that | 
| Eoghan Glynn | fc65cfe | 2012-10-19 21:26:41 +0100 | [diff] [blame] | 44 | # the exercise is skipped | 
| Joe Gordon | 6fd2811 | 2012-11-13 16:55:41 -0800 | [diff] [blame] | 45 | is_service_enabled cinder || exit 55 | 
| Eoghan Glynn | fc65cfe | 2012-10-19 21:26:41 +0100 | [diff] [blame] | 46 |  | 
| Dean Troyer | 2aa2a89 | 2013-08-04 19:53:19 -0500 | [diff] [blame] | 47 | # Also skip if the hypervisor is Docker | 
|  | 48 | [[ "$VIRT_DRIVER" == "docker" ]] && exit 55 | 
|  | 49 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 50 | # Instance type to create | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 51 | DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny} | 
|  | 52 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 53 | # Boot this image, use first AMI image if unset | 
|  | 54 | DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami} | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 55 |  | 
| Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 56 | # Security group name | 
|  | 57 | SECGROUP=${SECGROUP:-boot_secgroup} | 
|  | 58 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 59 | # Instance and volume names | 
|  | 60 | VM_NAME=${VM_NAME:-ex-bfv-inst} | 
|  | 61 | VOL_NAME=${VOL_NAME:-ex-vol-bfv} | 
| Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 62 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 63 |  | 
|  | 64 | # Launching a server | 
|  | 65 | # ================== | 
|  | 66 |  | 
|  | 67 | # List servers for tenant: | 
|  | 68 | nova list | 
|  | 69 |  | 
|  | 70 | # Images | 
|  | 71 | # ------ | 
|  | 72 |  | 
|  | 73 | # List the images available | 
|  | 74 | glance image-list | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 75 |  | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 76 | # Grab the id of the image to launch | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 77 | IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1) | 
| Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 78 | die_if_not_set $LINENO IMAGE "Failure getting image $DEFAULT_IMAGE_NAME" | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 79 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 80 | # Security Groups | 
|  | 81 | # --------------- | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 82 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 83 | # List security groups | 
|  | 84 | nova secgroup-list | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 85 |  | 
| Chris Behrens | c62c2b9 | 2013-07-24 03:56:13 -0700 | [diff] [blame] | 86 | if 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." | 
|  | 90 | else | 
|  | 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 Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 98 | fi | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 99 | fi | 
|  | 100 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 101 | # Configure Security Group Rules | 
|  | 102 | if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then | 
|  | 103 | nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0 | 
|  | 104 | fi | 
|  | 105 | if ! nova secgroup-list-rules $SECGROUP | grep -q " tcp .* 22 "; then | 
|  | 106 | nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0 | 
|  | 107 | fi | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 108 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 109 | # List secgroup rules | 
|  | 110 | nova secgroup-list-rules $SECGROUP | 
|  | 111 |  | 
|  | 112 | # Set up instance | 
|  | 113 | # --------------- | 
|  | 114 |  | 
|  | 115 | # List flavors | 
|  | 116 | nova flavor-list | 
|  | 117 |  | 
|  | 118 | # Select a flavor | 
|  | 119 | INSTANCE_TYPE=$(nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1) | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 120 | if [[ -z "$INSTANCE_TYPE" ]]; then | 
|  | 121 | # grab the first flavor in the list to launch if default doesn't exist | 
| Sean Dague | 922c8ae | 2013-10-22 10:06:06 -0400 | [diff] [blame] | 122 | INSTANCE_TYPE=$(nova flavor-list | head -n 4 | tail -n 1 | get_field 1) | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 123 | fi | 
|  | 124 |  | 
|  | 125 | # Clean-up from previous runs | 
|  | 126 | nova delete $VM_NAME || true | 
|  | 127 | if ! 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 Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 130 | fi | 
|  | 131 |  | 
|  | 132 | # Setup Keypair | 
|  | 133 | KEY_NAME=test_key | 
|  | 134 | KEY_FILE=key.pem | 
|  | 135 | nova keypair-delete $KEY_NAME || true | 
|  | 136 | nova keypair-add $KEY_NAME > $KEY_FILE | 
|  | 137 | chmod 600 $KEY_FILE | 
|  | 138 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 139 | # Set up volume | 
|  | 140 | # ------------- | 
|  | 141 |  | 
|  | 142 | # Delete any old volume | 
| John Griffith | 161e280 | 2012-11-05 13:59:49 -0700 | [diff] [blame] | 143 | cinder delete $VOL_NAME || true | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 144 | if ! timeout $ACTIVE_TIMEOUT sh -c "while cinder list | grep $VOL_NAME; do sleep 1; done"; then | 
|  | 145 | echo "Volume $VOL_NAME not deleted" | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 146 | exit 1 | 
|  | 147 | fi | 
|  | 148 |  | 
| Eoghan Glynn | fc65cfe | 2012-10-19 21:26:41 +0100 | [diff] [blame] | 149 | # Create the bootable volume | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 150 | start_time=$(date +%s) | 
| Dean Troyer | 526b79f | 2013-11-22 11:30:44 -0600 | [diff] [blame] | 151 | cinder create --image-id $IMAGE --display-name=$VOL_NAME --display-description "test bootable volume: $VOL_NAME" $DEFAULT_VOLUME_SIZE || \ | 
| Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 152 | die $LINENO "Failure creating volume $VOL_NAME" | 
| John Griffith | 161e280 | 2012-11-05 13:59:49 -0700 | [diff] [blame] | 153 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep available; do sleep 1; done"; then | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 154 | echo "Volume $VOL_NAME not created" | 
|  | 155 | exit 1 | 
|  | 156 | fi | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 157 | end_time=$(date +%s) | 
|  | 158 | echo "Completed cinder create in $((end_time - start_time)) seconds" | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 159 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 160 | # Get volume ID | 
|  | 161 | VOL_ID=$(cinder list | grep $VOL_NAME  | get_field 1) | 
| Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 162 | die_if_not_set $LINENO VOL_ID "Failure retrieving volume ID for $VOL_NAME" | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 163 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 164 | # Boot instance | 
|  | 165 | # ------------- | 
|  | 166 |  | 
| Dean Troyer | 526b79f | 2013-11-22 11:30:44 -0600 | [diff] [blame] | 167 | # Boot using the --block-device-mapping param. The format of mapping is: | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 168 | # <dev_name>=<id>:<type>:<size(GB)>:<delete_on_terminate> | 
|  | 169 | # Leaving the middle two fields blank appears to do-the-right-thing | 
| Dean Troyer | 526b79f | 2013-11-22 11:30:44 -0600 | [diff] [blame] | 170 | VM_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 Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 171 | die_if_not_set $LINENO VM_UUID "Failure launching $VM_NAME" | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 172 |  | 
|  | 173 | # Check that the status is active within ACTIVE_TIMEOUT seconds | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 174 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 175 | echo "server didn't become active!" | 
|  | 176 | exit 1 | 
|  | 177 | fi | 
|  | 178 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 179 | # Get the instance IP | 
| Nachi Ueno | 6769b16 | 2013-08-12 18:18:56 -0700 | [diff] [blame] | 180 | IP=$(get_instance_ip $VM_UUID $PRIVATE_NETWORK_NAME) | 
|  | 181 |  | 
| Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 182 | die_if_not_set $LINENO IP "Failure retrieving IP address" | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 183 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 184 | # Private IPs can be pinged in single node deployments | 
|  | 185 | ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 186 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 187 | # Clean up | 
|  | 188 | # -------- | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 189 |  | 
|  | 190 | # Delete volume backed instance | 
| Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 191 | nova delete $VM_UUID || die $LINENO "Failure deleting instance $VM_NAME" | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 192 | if ! 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 Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 194 | exit 1 | 
|  | 195 | fi | 
|  | 196 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 197 | # Wait for volume to be released | 
|  | 198 | if ! 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 | 
|  | 201 | fi | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 202 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 203 | # Delete volume | 
|  | 204 | start_time=$(date +%s) | 
| Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 205 | cinder delete $VOL_ID || die $LINENO "Failure deleting volume $VOLUME_NAME" | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 206 | if ! 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 | 
|  | 209 | fi | 
|  | 210 | end_time=$(date +%s) | 
|  | 211 | echo "Completed cinder delete in $((end_time - start_time)) seconds" | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 212 |  | 
| Chris Behrens | c62c2b9 | 2013-07-24 03:56:13 -0700 | [diff] [blame] | 213 | if [[ $SECGROUP = "default" ]] ; then | 
|  | 214 | echo "Skipping deleting default security group" | 
|  | 215 | else | 
|  | 216 | # Delete secgroup | 
|  | 217 | nova secgroup-delete $SECGROUP || die $LINENO "Failure deleting security group $SECGROUP" | 
|  | 218 | fi | 
| Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 219 |  | 
|  | 220 | set +o xtrace | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 221 | echo "*********************************************************************" | 
|  | 222 | echo "SUCCESS: End DevStack Exercise: $0" | 
|  | 223 | echo "*********************************************************************" |