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: |
Eoghan Glynn | fc65cfe | 2012-10-19 21:26:41 +0100 | [diff] [blame] | 6 | # * Create a bootable volume |
| 7 | # * Boot a volume-backed instance |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 8 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 9 | echo "*********************************************************************" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 10 | echo "Begin DevStack Exercise: $0" |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 11 | echo "*********************************************************************" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 12 | |
| 13 | # This script exits on an error so that errors don't compound and you see |
| 14 | # only the first error that occured. |
| 15 | set -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. |
| 19 | set -o xtrace |
| 20 | |
| 21 | |
| 22 | # Settings |
| 23 | # ======== |
| 24 | |
| 25 | # Keep track of the current directory |
| 26 | EXERCISE_DIR=$(cd $(dirname "$0") && pwd) |
| 27 | TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) |
| 28 | |
| 29 | # Import common functions |
| 30 | source $TOP_DIR/functions |
| 31 | |
| 32 | # Import configuration |
| 33 | source $TOP_DIR/openrc |
| 34 | |
Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 35 | # Import quantum functions if needed |
| 36 | if is_service_enabled quantum; then |
| 37 | source $TOP_DIR/lib/quantum |
Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 38 | fi |
| 39 | |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 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 | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 47 | # Instance type to create |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 48 | DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny} |
| 49 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 50 | # Boot this image, use first AMI image if unset |
| 51 | DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami} |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 52 | |
Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 53 | # Security group name |
| 54 | SECGROUP=${SECGROUP:-boot_secgroup} |
| 55 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 56 | # Instance and volume names |
| 57 | VM_NAME=${VM_NAME:-ex-bfv-inst} |
| 58 | VOL_NAME=${VOL_NAME:-ex-vol-bfv} |
Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 59 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 60 | |
| 61 | # Launching a server |
| 62 | # ================== |
| 63 | |
| 64 | # List servers for tenant: |
| 65 | nova list |
| 66 | |
| 67 | # Images |
| 68 | # ------ |
| 69 | |
| 70 | # List the images available |
| 71 | glance image-list |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 72 | |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 73 | # Grab the id of the image to launch |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 74 | IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1) |
| 75 | die_if_not_set IMAGE "Failure getting image $DEFAULT_IMAGE_NAME" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 76 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 77 | # Security Groups |
| 78 | # --------------- |
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 | # List security groups |
| 81 | nova secgroup-list |
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 | # Create a secgroup |
| 84 | if ! 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 Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 90 | fi |
| 91 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 92 | # Configure Security Group Rules |
| 93 | if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then |
| 94 | nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0 |
| 95 | fi |
| 96 | if ! nova secgroup-list-rules $SECGROUP | grep -q " tcp .* 22 "; then |
| 97 | nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0 |
| 98 | fi |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 99 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 100 | # List secgroup rules |
| 101 | nova secgroup-list-rules $SECGROUP |
| 102 | |
| 103 | # Set up instance |
| 104 | # --------------- |
| 105 | |
| 106 | # List flavors |
| 107 | nova flavor-list |
| 108 | |
| 109 | # Select a flavor |
| 110 | INSTANCE_TYPE=$(nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1) |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 111 | if [[ -z "$INSTANCE_TYPE" ]]; then |
| 112 | # grab the first flavor in the list to launch if default doesn't exist |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 113 | INSTANCE_TYPE=$(nova flavor-list | head -n 4 | tail -n 1 | get_field 1) |
| 114 | fi |
| 115 | |
| 116 | # Clean-up from previous runs |
| 117 | nova delete $VM_NAME || true |
| 118 | if ! 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 Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 121 | fi |
| 122 | |
| 123 | # Setup Keypair |
| 124 | KEY_NAME=test_key |
| 125 | KEY_FILE=key.pem |
| 126 | nova keypair-delete $KEY_NAME || true |
| 127 | nova keypair-add $KEY_NAME > $KEY_FILE |
| 128 | chmod 600 $KEY_FILE |
| 129 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 130 | # Set up volume |
| 131 | # ------------- |
| 132 | |
| 133 | # Delete any old volume |
John Griffith | 161e280 | 2012-11-05 13:59:49 -0700 | [diff] [blame] | 134 | cinder delete $VOL_NAME || true |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 135 | if ! timeout $ACTIVE_TIMEOUT sh -c "while cinder list | grep $VOL_NAME; do sleep 1; done"; then |
| 136 | echo "Volume $VOL_NAME not deleted" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 137 | exit 1 |
| 138 | fi |
| 139 | |
Eoghan Glynn | fc65cfe | 2012-10-19 21:26:41 +0100 | [diff] [blame] | 140 | # Create the bootable volume |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 141 | start_time=$(date +%s) |
| 142 | cinder create --image-id $IMAGE --display_name=$VOL_NAME --display_description "test bootable volume: $VOL_NAME" $DEFAULT_VOLUME_SIZE || \ |
| 143 | die "Failure creating volume $VOL_NAME" |
John Griffith | 161e280 | 2012-11-05 13:59:49 -0700 | [diff] [blame] | 144 | 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] | 145 | echo "Volume $VOL_NAME not created" |
| 146 | exit 1 |
| 147 | fi |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 148 | end_time=$(date +%s) |
| 149 | echo "Completed cinder create in $((end_time - start_time)) seconds" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 150 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 151 | # Get volume ID |
| 152 | VOL_ID=$(cinder list | grep $VOL_NAME | get_field 1) |
| 153 | die_if_not_set VOL_ID "Failure retrieving volume ID for $VOL_NAME" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 154 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 155 | # Boot instance |
| 156 | # ------------- |
| 157 | |
| 158 | # Boot using the --block_device_mapping param. The format of mapping is: |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 159 | # <dev_name>=<id>:<type>:<size(GB)>:<delete_on_terminate> |
| 160 | # Leaving the middle two fields blank appears to do-the-right-thing |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 161 | 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) |
| 162 | die_if_not_set VM_UUID "Failure launching $VM_NAME" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 163 | |
| 164 | # Check that the status is active within ACTIVE_TIMEOUT seconds |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 165 | 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] | 166 | echo "server didn't become active!" |
| 167 | exit 1 |
| 168 | fi |
| 169 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 170 | # Get the instance IP |
| 171 | IP=$(nova show $VM_UUID | grep "$PRIVATE_NETWORK_NAME" | get_field 2) |
| 172 | die_if_not_set IP "Failure retrieving IP address" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 173 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 174 | # Private IPs can be pinged in single node deployments |
| 175 | ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 176 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 177 | # Clean up |
| 178 | # -------- |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 179 | |
| 180 | # Delete volume backed instance |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 181 | nova delete $VM_UUID || die "Failure deleting instance $VM_NAME" |
| 182 | if ! 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 Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 184 | exit 1 |
| 185 | fi |
| 186 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 187 | # Wait for volume to be released |
| 188 | if ! 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 |
| 191 | fi |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 192 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame^] | 193 | # Delete volume |
| 194 | start_time=$(date +%s) |
| 195 | cinder delete $VOL_ID || die "Failure deleting volume $VOLUME_NAME" |
| 196 | if ! 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 |
| 199 | fi |
| 200 | end_time=$(date +%s) |
| 201 | echo "Completed cinder delete in $((end_time - start_time)) seconds" |
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 secgroup |
Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 204 | nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 205 | |
| 206 | set +o xtrace |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 207 | echo "*********************************************************************" |
| 208 | echo "SUCCESS: End DevStack Exercise: $0" |
| 209 | echo "*********************************************************************" |