Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 3 | # **volumes.sh** |
| 4 | |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 5 | # Test nova volumes with the nova command from python-novaclient |
| 6 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 7 | echo "*********************************************************************" |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 8 | echo "Begin DevStack Exercise: $0" |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 9 | echo "*********************************************************************" |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 10 | |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 11 | # This script exits on an error so that errors don't compound and you see |
| 12 | # only the first error that occured. |
| 13 | set -o errexit |
| 14 | |
| 15 | # Print the commands being run so that we can see the command that triggers |
| 16 | # an error. It is also useful for following allowing as the install occurs. |
| 17 | set -o xtrace |
| 18 | |
| 19 | |
| 20 | # Settings |
| 21 | # ======== |
| 22 | |
Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -0600 | [diff] [blame] | 23 | # Keep track of the current directory |
| 24 | EXERCISE_DIR=$(cd $(dirname "$0") && pwd) |
| 25 | TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 26 | |
| 27 | # Import common functions |
Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -0600 | [diff] [blame] | 28 | source $TOP_DIR/functions |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 29 | |
| 30 | # Import configuration |
Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -0600 | [diff] [blame] | 31 | source $TOP_DIR/openrc |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 32 | |
Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -0600 | [diff] [blame] | 33 | # Import exercise configuration |
| 34 | source $TOP_DIR/exerciserc |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 35 | |
Dean Troyer | 67787e6 | 2012-05-02 11:48:15 -0500 | [diff] [blame] | 36 | # If cinder or n-vol are not enabled we exit with exitcode 55 which mean |
| 37 | # exercise is skipped. |
| 38 | is_service_enabled cinder n-vol || exit 55 |
| 39 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 40 | # Instance type to create |
| 41 | DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny} |
| 42 | |
| 43 | # Boot this image, use first AMi image if unset |
| 44 | DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami} |
| 45 | |
Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 46 | # Security group name |
| 47 | SECGROUP=${SECGROUP:-vol_secgroup} |
| 48 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 49 | |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 50 | # Launching a server |
| 51 | # ================== |
| 52 | |
| 53 | # List servers for tenant: |
| 54 | nova list |
| 55 | |
| 56 | # Images |
| 57 | # ------ |
| 58 | |
| 59 | # Nova has a **deprecated** way of listing images. |
| 60 | nova image-list |
| 61 | |
| 62 | # But we recommend using glance directly |
Dean Troyer | 4549525 | 2012-04-13 13:16:38 -0500 | [diff] [blame] | 63 | glance image-list |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 64 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 65 | # Grab the id of the image to launch |
Dean Troyer | 4549525 | 2012-04-13 13:16:38 -0500 | [diff] [blame] | 66 | IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1) |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 67 | |
Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 68 | # Security Groups |
| 69 | # --------------- |
| 70 | |
| 71 | # List of secgroups: |
| 72 | nova secgroup-list |
| 73 | |
| 74 | # Create a secgroup |
| 75 | if ! nova secgroup-list | grep -q $SECGROUP; then |
| 76 | nova secgroup-create $SECGROUP "$SECGROUP description" |
| 77 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then |
| 78 | echo "Security group not created" |
| 79 | exit 1 |
| 80 | fi |
| 81 | fi |
| 82 | |
| 83 | # Configure Security Group Rules |
| 84 | nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0 |
| 85 | nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0 |
| 86 | |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 87 | # determinine instance type |
| 88 | # ------------------------- |
| 89 | |
| 90 | # List of instance types: |
| 91 | nova flavor-list |
| 92 | |
Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 93 | INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1` |
Dean Troyer | 1d6e0e1 | 2011-12-23 12:45:13 -0600 | [diff] [blame] | 94 | if [[ -z "$INSTANCE_TYPE" ]]; then |
| 95 | # grab the first flavor in the list to launch if default doesn't exist |
Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 96 | INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | get_field 1` |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 97 | fi |
| 98 | |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 99 | NAME="ex-vol" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 100 | |
Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 101 | VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2` |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 102 | die_if_not_set VM_UUID "Failure launching $NAME" |
| 103 | |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 104 | |
| 105 | # Testing |
| 106 | # ======= |
| 107 | |
| 108 | # First check if it spins up (becomes active and responds to ping on |
| 109 | # internal ip). If you run this script from a nova node, you should |
| 110 | # bypass security groups and have direct access to the server. |
| 111 | |
| 112 | # Waiting for boot |
| 113 | # ---------------- |
| 114 | |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 115 | # check that the status is active within ACTIVE_TIMEOUT seconds |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 116 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 117 | echo "server didn't become active!" |
| 118 | exit 1 |
| 119 | fi |
| 120 | |
| 121 | # get the IP of the server |
Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 122 | IP=`nova show $VM_UUID | grep "private network" | get_field 2` |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 123 | die_if_not_set IP "Failure retrieving IP address" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 124 | |
| 125 | # for single node deployments, we can ping private ips |
Armando Migliaccio | 7d13f30 | 2012-04-19 22:26:16 +0100 | [diff] [blame] | 126 | MULTI_HOST=`trueorfalse False $MULTI_HOST` |
| 127 | if [ "$MULTI_HOST" = "False" ]; then |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 128 | # sometimes the first ping fails (10 seconds isn't enough time for the VM's |
| 129 | # network to respond?), so let's ping for a default of 15 seconds with a |
| 130 | # timeout of a second for each ping. |
| 131 | if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then |
| 132 | echo "Couldn't ping server" |
| 133 | exit 1 |
| 134 | fi |
| 135 | else |
| 136 | # On a multi-host system, without vm net access, do a sleep to wait for the boot |
| 137 | sleep $BOOT_TIMEOUT |
| 138 | fi |
| 139 | |
| 140 | # Volumes |
| 141 | # ------- |
| 142 | |
| 143 | VOL_NAME="myvol-$(openssl rand -hex 4)" |
| 144 | |
| 145 | # Verify it doesn't exist |
Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 146 | if [[ -n "`nova volume-list | grep $VOL_NAME | head -1 | get_field 2`" ]]; then |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 147 | echo "Volume $VOL_NAME already exists" |
| 148 | exit 1 |
| 149 | fi |
| 150 | |
| 151 | # Create a new volume |
| 152 | nova volume-create --display_name $VOL_NAME --display_description "test volume: $VOL_NAME" 1 |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 153 | if [[ $? != 0 ]]; then |
| 154 | echo "Failure creating volume $VOL_NAME" |
| 155 | exit 1 |
| 156 | fi |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 157 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then |
| 158 | echo "Volume $VOL_NAME not created" |
| 159 | exit 1 |
| 160 | fi |
| 161 | |
| 162 | # Get volume ID |
Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 163 | VOL_ID=`nova volume-list | grep $VOL_NAME | head -1 | get_field 1` |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 164 | die_if_not_set VOL_ID "Failure retrieving volume ID for $VOL_NAME" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 165 | |
| 166 | # Attach to server |
| 167 | DEVICE=/dev/vdb |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 168 | nova volume-attach $VM_UUID $VOL_ID $DEVICE || \ |
| 169 | die "Failure attaching volume $VOL_NAME to $NAME" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 170 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then |
| 171 | echo "Volume $VOL_NAME not attached to $NAME" |
| 172 | exit 1 |
| 173 | fi |
| 174 | |
Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 175 | VOL_ATTACH=`nova volume-list | grep $VOL_NAME | head -1 | get_field -1` |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 176 | die_if_not_set VOL_ATTACH "Failure retrieving $VOL_NAME status" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 177 | if [[ "$VOL_ATTACH" != $VM_UUID ]]; then |
| 178 | echo "Volume not attached to correct instance" |
| 179 | exit 1 |
| 180 | fi |
| 181 | |
| 182 | # Detach volume |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 183 | nova volume-detach $VM_UUID $VOL_ID || die "Failure detaching volume $VOL_NAME from $NAME" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 184 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then |
| 185 | echo "Volume $VOL_NAME not detached from $NAME" |
| 186 | exit 1 |
| 187 | fi |
| 188 | |
| 189 | # Delete volume |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 190 | nova volume-delete $VOL_ID || die "Failure deleting volume $VOL_NAME" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 191 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME; do sleep 1; done"; then |
| 192 | echo "Volume $VOL_NAME not deleted" |
| 193 | exit 1 |
| 194 | fi |
| 195 | |
Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 196 | # Shutdown the server |
| 197 | nova delete $VM_UUID || die "Failure deleting instance $NAME" |
| 198 | |
| 199 | # Wait for termination |
| 200 | if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then |
| 201 | echo "Server $NAME not deleted" |
| 202 | exit 1 |
| 203 | fi |
| 204 | |
| 205 | # Delete a secgroup |
| 206 | nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP" |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 207 | |
| 208 | set +o xtrace |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 209 | echo "*********************************************************************" |
| 210 | echo "SUCCESS: End DevStack Exercise: $0" |
| 211 | echo "*********************************************************************" |