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