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 | |
| 35 | # Import exercise configuration |
| 36 | source $TOP_DIR/exerciserc |
| 37 | |
Eoghan Glynn | fc65cfe | 2012-10-19 21:26:41 +0100 | [diff] [blame] | 38 | # If cinder or n-vol are not enabled we exit with exitcode 55 so that |
| 39 | # the exercise is skipped |
| 40 | is_service_enabled cinder n-vol || exit 55 |
| 41 | |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 42 | # Boot this image, use first AMI image if unset |
| 43 | DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami} |
| 44 | |
| 45 | # Instance type |
| 46 | DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny} |
| 47 | |
| 48 | # Default floating IP pool name |
| 49 | DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova} |
| 50 | |
Devananda van der Veen | c0c6f00 | 2012-07-06 17:49:12 -0700 | [diff] [blame] | 51 | # Default user |
| 52 | DEFAULT_INSTANCE_USER=${DEFAULT_INSTANCE_USER:-cirros} |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 53 | |
Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 54 | # Security group name |
| 55 | SECGROUP=${SECGROUP:-boot_secgroup} |
| 56 | |
| 57 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 58 | # Launching servers |
| 59 | # ================= |
| 60 | |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 61 | # Grab the id of the image to launch |
Dean Troyer | 4549525 | 2012-04-13 13:16:38 -0500 | [diff] [blame] | 62 | IMAGE=`glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1` |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 63 | die_if_not_set IMAGE "Failure getting image" |
| 64 | |
| 65 | # Instance and volume names |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 66 | VOL_INSTANCE_NAME=${VOL_INSTANCE_NAME:-test_vol_instance} |
| 67 | VOL_NAME=${VOL_NAME:-test_volume} |
| 68 | |
| 69 | # Clean-up from previous runs |
| 70 | nova delete $VOL_INSTANCE_NAME || true |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 71 | |
Eoghan Glynn | fc65cfe | 2012-10-19 21:26:41 +0100 | [diff] [blame] | 72 | if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VOL_INSTANCE_NAME; do sleep 1; done"; then |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 73 | echo "server didn't terminate!" |
| 74 | exit 1 |
| 75 | fi |
| 76 | |
| 77 | # Configure Security Groups |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 78 | nova secgroup-delete $SECGROUP || true |
| 79 | nova secgroup-create $SECGROUP "$SECGROUP description" |
| 80 | nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0 |
| 81 | nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0 |
| 82 | |
| 83 | # Determinine instance type |
| 84 | INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2` |
| 85 | if [[ -z "$INSTANCE_TYPE" ]]; then |
| 86 | # grab the first flavor in the list to launch if default doesn't exist |
| 87 | INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2` |
| 88 | fi |
| 89 | |
| 90 | # Setup Keypair |
| 91 | KEY_NAME=test_key |
| 92 | KEY_FILE=key.pem |
| 93 | nova keypair-delete $KEY_NAME || true |
| 94 | nova keypair-add $KEY_NAME > $KEY_FILE |
| 95 | chmod 600 $KEY_FILE |
| 96 | |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 97 | # Delete the old volume |
John Griffith | 161e280 | 2012-11-05 13:59:49 -0700 | [diff] [blame^] | 98 | cinder delete $VOL_NAME || true |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 99 | |
| 100 | # Free every floating ips - setting FREE_ALL_FLOATING_IPS=True in localrc will make life easier for testers |
| 101 | if [ "$FREE_ALL_FLOATING_IPS" = "True" ]; then |
| 102 | nova floating-ip-list | grep nova | cut -d "|" -f2 | tr -d " " | xargs -n1 nova floating-ip-delete || true |
| 103 | fi |
| 104 | |
| 105 | # Allocate floating ip |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 106 | FLOATING_IP=`nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | get_field 1` |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 107 | |
| 108 | # Make sure the ip gets allocated |
| 109 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then |
| 110 | echo "Floating IP not allocated" |
| 111 | exit 1 |
| 112 | fi |
| 113 | |
Eoghan Glynn | fc65cfe | 2012-10-19 21:26:41 +0100 | [diff] [blame] | 114 | # Create the bootable volume |
John Griffith | 161e280 | 2012-11-05 13:59:49 -0700 | [diff] [blame^] | 115 | cinder create --display_name=$VOL_NAME --image-id $IMAGE 1 |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 116 | |
| 117 | # Wait for volume to activate |
John Griffith | 161e280 | 2012-11-05 13:59:49 -0700 | [diff] [blame^] | 118 | 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] | 119 | echo "Volume $VOL_NAME not created" |
| 120 | exit 1 |
| 121 | fi |
| 122 | |
John Griffith | 161e280 | 2012-11-05 13:59:49 -0700 | [diff] [blame^] | 123 | VOLUME_ID=`cinder list | grep $VOL_NAME | get_field 1` |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 124 | |
| 125 | # Boot instance from volume! This is done with the --block_device_mapping param. |
| 126 | # The format of mapping is: |
| 127 | # <dev_name>=<id>:<type>:<size(GB)>:<delete_on_terminate> |
| 128 | # Leaving the middle two fields blank appears to do-the-right-thing |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 129 | VOL_VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE --block_device_mapping vda=$VOLUME_ID:::0 --security_groups=$SECGROUP --key_name $KEY_NAME $VOL_INSTANCE_NAME | grep ' id ' | get_field 2` |
| 130 | die_if_not_set VOL_VM_UUID "Failure launching $VOL_INSTANCE_NAME" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 131 | |
| 132 | # Check that the status is active within ACTIVE_TIMEOUT seconds |
| 133 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VOL_VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then |
| 134 | echo "server didn't become active!" |
| 135 | exit 1 |
| 136 | fi |
| 137 | |
| 138 | # Add floating ip to our server |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 139 | nova add-floating-ip $VOL_VM_UUID $FLOATING_IP |
| 140 | |
| 141 | # Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds |
Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 142 | ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 143 | |
| 144 | # Make sure our volume-backed instance launched |
Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 145 | ssh_check "$PUBLIC_NETWORK_NAME" $KEY_FILE $FLOATING_IP $DEFAULT_INSTANCE_USER $ACTIVE_TIMEOUT |
Eoghan Glynn | fc65cfe | 2012-10-19 21:26:41 +0100 | [diff] [blame] | 146 | |
| 147 | # Remove floating ip from volume-backed instance |
| 148 | nova remove-floating-ip $VOL_VM_UUID $FLOATING_IP |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 149 | |
| 150 | # Delete volume backed instance |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 151 | nova delete $VOL_INSTANCE_NAME || \ |
| 152 | die "Failure deleting instance volume $VOL_INSTANCE_NAME" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 153 | |
| 154 | # Wait till our volume is no longer in-use |
John Griffith | 161e280 | 2012-11-05 13:59:49 -0700 | [diff] [blame^] | 155 | 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] | 156 | echo "Volume $VOL_NAME not created" |
| 157 | exit 1 |
| 158 | fi |
| 159 | |
| 160 | # Delete the volume |
John Griffith | 161e280 | 2012-11-05 13:59:49 -0700 | [diff] [blame^] | 161 | cinder delete $VOL_NAME || \ |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 162 | die "Failure deleting volume $VOLUME_NAME" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 163 | |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 164 | # De-allocate the floating ip |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 165 | nova floating-ip-delete $FLOATING_IP || \ |
| 166 | die "Failure deleting floating IP $FLOATING_IP" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 167 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 168 | # Delete a secgroup |
Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 169 | nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 170 | |
| 171 | set +o xtrace |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 172 | echo "*********************************************************************" |
| 173 | echo "SUCCESS: End DevStack Exercise: $0" |
| 174 | echo "*********************************************************************" |