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: |
| 6 | # * Create a 'builder' instance |
| 7 | # * Attach a volume to the instance |
| 8 | # * Format and install an os onto the volume |
| 9 | # * Detach volume from builder, and then boot volume-backed instance |
| 10 | |
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 | echo "Begin DevStack Exercise: $0" |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 13 | echo "*********************************************************************" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 14 | |
| 15 | # This script exits on an error so that errors don't compound and you see |
| 16 | # only the first error that occured. |
| 17 | set -o errexit |
| 18 | |
| 19 | # Print the commands being run so that we can see the command that triggers |
| 20 | # an error. It is also useful for following allowing as the install occurs. |
| 21 | set -o xtrace |
| 22 | |
| 23 | |
| 24 | # Settings |
| 25 | # ======== |
| 26 | |
| 27 | # Keep track of the current directory |
| 28 | EXERCISE_DIR=$(cd $(dirname "$0") && pwd) |
| 29 | TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) |
| 30 | |
| 31 | # Import common functions |
| 32 | source $TOP_DIR/functions |
| 33 | |
| 34 | # Import configuration |
| 35 | source $TOP_DIR/openrc |
| 36 | |
| 37 | # Import exercise configuration |
| 38 | source $TOP_DIR/exerciserc |
| 39 | |
| 40 | # Boot this image, use first AMI image if unset |
| 41 | DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami} |
| 42 | |
| 43 | # Instance type |
| 44 | DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny} |
| 45 | |
| 46 | # Default floating IP pool name |
| 47 | DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova} |
| 48 | |
Devananda van der Veen | c0c6f00 | 2012-07-06 17:49:12 -0700 | [diff] [blame^] | 49 | # Default user |
| 50 | DEFAULT_INSTANCE_USER=${DEFAULT_INSTANCE_USER:-cirros} |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 51 | |
| 52 | # Launching servers |
| 53 | # ================= |
| 54 | |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 55 | # Grab the id of the image to launch |
Dean Troyer | 4549525 | 2012-04-13 13:16:38 -0500 | [diff] [blame] | 56 | IMAGE=`glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1` |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 57 | die_if_not_set IMAGE "Failure getting image" |
| 58 | |
| 59 | # Instance and volume names |
| 60 | INSTANCE_NAME=${INSTANCE_NAME:-test_instance} |
| 61 | VOL_INSTANCE_NAME=${VOL_INSTANCE_NAME:-test_vol_instance} |
| 62 | VOL_NAME=${VOL_NAME:-test_volume} |
| 63 | |
| 64 | # Clean-up from previous runs |
| 65 | nova delete $VOL_INSTANCE_NAME || true |
| 66 | nova delete $INSTANCE_NAME || true |
| 67 | |
| 68 | # Wait till server is gone |
| 69 | if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $INSTANCE_NAME; do sleep 1; done"; then |
| 70 | echo "server didn't terminate!" |
| 71 | exit 1 |
| 72 | fi |
| 73 | |
| 74 | # Configure Security Groups |
| 75 | SECGROUP=${SECGROUP:-test_secgroup} |
| 76 | nova secgroup-delete $SECGROUP || true |
| 77 | nova secgroup-create $SECGROUP "$SECGROUP description" |
| 78 | nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0 |
| 79 | nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0 |
| 80 | |
| 81 | # Determinine instance type |
| 82 | INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2` |
| 83 | if [[ -z "$INSTANCE_TYPE" ]]; then |
| 84 | # grab the first flavor in the list to launch if default doesn't exist |
| 85 | INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2` |
| 86 | fi |
| 87 | |
| 88 | # Setup Keypair |
| 89 | KEY_NAME=test_key |
| 90 | KEY_FILE=key.pem |
| 91 | nova keypair-delete $KEY_NAME || true |
| 92 | nova keypair-add $KEY_NAME > $KEY_FILE |
| 93 | chmod 600 $KEY_FILE |
| 94 | |
| 95 | # Boot our instance |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 96 | VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE --security_groups=$SECGROUP --key_name $KEY_NAME $INSTANCE_NAME | grep ' id ' | get_field 2` |
| 97 | die_if_not_set VM_UUID "Failure launching $INSTANCE_NAME" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 98 | |
| 99 | # check that the status is active within ACTIVE_TIMEOUT seconds |
| 100 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then |
| 101 | echo "server didn't become active!" |
| 102 | exit 1 |
| 103 | fi |
| 104 | |
| 105 | # Delete the old volume |
| 106 | nova volume-delete $VOL_NAME || true |
| 107 | |
| 108 | # Free every floating ips - setting FREE_ALL_FLOATING_IPS=True in localrc will make life easier for testers |
| 109 | if [ "$FREE_ALL_FLOATING_IPS" = "True" ]; then |
| 110 | nova floating-ip-list | grep nova | cut -d "|" -f2 | tr -d " " | xargs -n1 nova floating-ip-delete || true |
| 111 | fi |
| 112 | |
| 113 | # Allocate floating ip |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 114 | 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] | 115 | |
| 116 | # Make sure the ip gets allocated |
| 117 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then |
| 118 | echo "Floating IP not allocated" |
| 119 | exit 1 |
| 120 | fi |
| 121 | |
| 122 | # Add floating ip to our server |
| 123 | nova add-floating-ip $VM_UUID $FLOATING_IP |
| 124 | |
| 125 | # Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds |
| 126 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then |
| 127 | echo "Couldn't ping server with floating ip" |
| 128 | exit 1 |
| 129 | fi |
| 130 | |
| 131 | # Create our volume |
| 132 | nova volume-create --display_name=$VOL_NAME 1 |
| 133 | |
| 134 | # Wait for volume to activate |
| 135 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then |
| 136 | echo "Volume $VOL_NAME not created" |
| 137 | exit 1 |
| 138 | fi |
| 139 | |
| 140 | # FIXME (anthony) - python-novaclient should accept a volume_name for the attachment param? |
| 141 | DEVICE=/dev/vdb |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 142 | VOLUME_ID=`nova volume-list | grep $VOL_NAME | get_field 1` |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 143 | nova volume-attach $INSTANCE_NAME $VOLUME_ID $DEVICE |
| 144 | |
| 145 | # Wait till volume is attached |
| 146 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then |
| 147 | echo "Volume $VOL_NAME not created" |
| 148 | exit 1 |
| 149 | fi |
| 150 | |
| 151 | # The following script builds our bootable volume. |
| 152 | # To do this, ssh to the builder instance, mount volume, and build a volume-backed image. |
| 153 | STAGING_DIR=/tmp/stage |
| 154 | CIRROS_DIR=/tmp/cirros |
Devananda van der Veen | c0c6f00 | 2012-07-06 17:49:12 -0700 | [diff] [blame^] | 155 | ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP << EOF |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 156 | set -o errexit |
| 157 | set -o xtrace |
| 158 | sudo mkdir -p $STAGING_DIR |
| 159 | sudo mkfs.ext3 -b 1024 $DEVICE 1048576 |
| 160 | sudo mount $DEVICE $STAGING_DIR |
| 161 | # The following lines create a writable empty file so that we can scp |
| 162 | # the actual file |
| 163 | sudo touch $STAGING_DIR/cirros-0.3.0-x86_64-rootfs.img.gz |
| 164 | sudo chown cirros $STAGING_DIR/cirros-0.3.0-x86_64-rootfs.img.gz |
| 165 | EOF |
| 166 | |
| 167 | # Download cirros |
| 168 | if [ ! -e cirros-0.3.0-x86_64-rootfs.img.gz ]; then |
| 169 | wget http://images.ansolabs.com/cirros-0.3.0-x86_64-rootfs.img.gz |
| 170 | fi |
| 171 | |
| 172 | # Copy cirros onto the volume |
Devananda van der Veen | c0c6f00 | 2012-07-06 17:49:12 -0700 | [diff] [blame^] | 173 | scp -o StrictHostKeyChecking=no -i $KEY_FILE cirros-0.3.0-x86_64-rootfs.img.gz ${DEFAULT_INSTANCE_USER}@$FLOATING_IP:$STAGING_DIR |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 174 | |
| 175 | # Unpack cirros into volume |
Devananda van der Veen | c0c6f00 | 2012-07-06 17:49:12 -0700 | [diff] [blame^] | 176 | ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP << EOF |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 177 | set -o errexit |
| 178 | set -o xtrace |
| 179 | cd $STAGING_DIR |
| 180 | sudo mkdir -p $CIRROS_DIR |
| 181 | sudo gunzip cirros-0.3.0-x86_64-rootfs.img.gz |
| 182 | sudo mount cirros-0.3.0-x86_64-rootfs.img $CIRROS_DIR |
| 183 | |
| 184 | # Copy cirros into our volume |
| 185 | sudo cp -pr $CIRROS_DIR/* $STAGING_DIR/ |
| 186 | |
| 187 | cd |
| 188 | sync |
| 189 | sudo umount $CIRROS_DIR |
| 190 | # The following typically fails. Don't know why. |
| 191 | sudo umount $STAGING_DIR || true |
| 192 | EOF |
| 193 | |
| 194 | # Detach the volume from the builder instance |
| 195 | nova volume-detach $INSTANCE_NAME $VOLUME_ID |
| 196 | |
| 197 | # Boot instance from volume! This is done with the --block_device_mapping param. |
| 198 | # The format of mapping is: |
| 199 | # <dev_name>=<id>:<type>:<size(GB)>:<delete_on_terminate> |
| 200 | # Leaving the middle two fields blank appears to do-the-right-thing |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 201 | 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` |
| 202 | die_if_not_set VOL_VM_UUID "Failure launching $VOL_INSTANCE_NAME" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 203 | |
| 204 | # Check that the status is active within ACTIVE_TIMEOUT seconds |
| 205 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VOL_VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then |
| 206 | echo "server didn't become active!" |
| 207 | exit 1 |
| 208 | fi |
| 209 | |
| 210 | # Add floating ip to our server |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 211 | nova remove-floating-ip $VM_UUID $FLOATING_IP |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 212 | |
| 213 | # Gratuitous sleep, probably hiding a race condition :/ |
| 214 | sleep 1 |
| 215 | |
| 216 | # Add floating ip to our server |
| 217 | nova add-floating-ip $VOL_VM_UUID $FLOATING_IP |
| 218 | |
| 219 | # Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds |
| 220 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then |
| 221 | echo "Couldn't ping volume-backed server with floating ip" |
| 222 | exit 1 |
| 223 | fi |
| 224 | |
| 225 | # Make sure our volume-backed instance launched |
Devananda van der Veen | c0c6f00 | 2012-07-06 17:49:12 -0700 | [diff] [blame^] | 226 | ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP << EOF |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 227 | echo "success!" |
| 228 | EOF |
| 229 | |
| 230 | # Delete volume backed instance |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 231 | nova delete $VOL_INSTANCE_NAME || \ |
| 232 | die "Failure deleting instance volume $VOL_INSTANCE_NAME" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 233 | |
| 234 | # Wait till our volume is no longer in-use |
| 235 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then |
| 236 | echo "Volume $VOL_NAME not created" |
| 237 | exit 1 |
| 238 | fi |
| 239 | |
| 240 | # Delete the volume |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 241 | nova volume-delete $VOL_NAME || \ |
| 242 | die "Failure deleting volume $VOLUME_NAME" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 243 | |
| 244 | # Delete instance |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 245 | nova delete $INSTANCE_NAME || \ |
| 246 | die "Failure deleting instance $INSTANCE_NAME" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 247 | |
| 248 | # Wait for termination |
| 249 | if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $INSTANCE_NAME; do sleep 1; done"; then |
| 250 | echo "server didn't terminate!" |
| 251 | exit 1 |
| 252 | fi |
| 253 | |
| 254 | # De-allocate the floating ip |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 255 | nova floating-ip-delete $FLOATING_IP || \ |
| 256 | die "Failure deleting floating IP $FLOATING_IP" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 257 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 258 | # Delete a secgroup |
| 259 | nova secgroup-delete $SECGROUP || \ |
| 260 | die "Failure deleting security group $SECGROUP" |
Anthony Young | 440be4b | 2012-02-10 21:42:39 -0800 | [diff] [blame] | 261 | |
| 262 | set +o xtrace |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 263 | echo "*********************************************************************" |
| 264 | echo "SUCCESS: End DevStack Exercise: $0" |
| 265 | echo "*********************************************************************" |