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