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 | |
Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame^] | 33 | # Import quantum functions if needed |
| 34 | if is_service_enabled quantum; then |
| 35 | source $TOP_DIR/lib/quantum |
| 36 | setup_quantum |
| 37 | fi |
| 38 | |
Dean Troyer | 51fb454 | 2012-03-09 22:21:59 -0600 | [diff] [blame] | 39 | # Import exercise configuration |
| 40 | source $TOP_DIR/exerciserc |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 41 | |
Dean Troyer | 67787e6 | 2012-05-02 11:48:15 -0500 | [diff] [blame] | 42 | # If cinder or n-vol are not enabled we exit with exitcode 55 which mean |
| 43 | # exercise is skipped. |
| 44 | is_service_enabled cinder n-vol || exit 55 |
| 45 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 46 | # Instance type to create |
| 47 | DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny} |
| 48 | |
| 49 | # Boot this image, use first AMi image if unset |
| 50 | DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami} |
| 51 | |
Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 52 | # Security group name |
| 53 | SECGROUP=${SECGROUP:-vol_secgroup} |
| 54 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 55 | |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 56 | # Launching a server |
| 57 | # ================== |
| 58 | |
| 59 | # List servers for tenant: |
| 60 | nova list |
| 61 | |
| 62 | # Images |
| 63 | # ------ |
| 64 | |
| 65 | # Nova has a **deprecated** way of listing images. |
| 66 | nova image-list |
| 67 | |
| 68 | # But we recommend using glance directly |
Dean Troyer | 4549525 | 2012-04-13 13:16:38 -0500 | [diff] [blame] | 69 | glance image-list |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 70 | |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 71 | # Grab the id of the image to launch |
Dean Troyer | 4549525 | 2012-04-13 13:16:38 -0500 | [diff] [blame] | 72 | IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1) |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 73 | |
Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 74 | # Security Groups |
| 75 | # --------------- |
| 76 | |
| 77 | # List of secgroups: |
| 78 | nova secgroup-list |
| 79 | |
| 80 | # Create a secgroup |
| 81 | if ! nova secgroup-list | grep -q $SECGROUP; then |
| 82 | nova secgroup-create $SECGROUP "$SECGROUP description" |
| 83 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then |
| 84 | echo "Security group not created" |
| 85 | exit 1 |
| 86 | fi |
| 87 | fi |
| 88 | |
| 89 | # Configure Security Group Rules |
| 90 | nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0 |
| 91 | nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0 |
| 92 | |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 93 | # determinine instance type |
| 94 | # ------------------------- |
| 95 | |
| 96 | # List of instance types: |
| 97 | nova flavor-list |
| 98 | |
Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 99 | INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1` |
Dean Troyer | 1d6e0e1 | 2011-12-23 12:45:13 -0600 | [diff] [blame] | 100 | if [[ -z "$INSTANCE_TYPE" ]]; then |
| 101 | # 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] | 102 | 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] | 103 | fi |
| 104 | |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 105 | NAME="ex-vol" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 106 | |
Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 107 | 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] | 108 | die_if_not_set VM_UUID "Failure launching $NAME" |
| 109 | |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 110 | |
| 111 | # Testing |
| 112 | # ======= |
| 113 | |
| 114 | # First check if it spins up (becomes active and responds to ping on |
| 115 | # internal ip). If you run this script from a nova node, you should |
| 116 | # bypass security groups and have direct access to the server. |
| 117 | |
| 118 | # Waiting for boot |
| 119 | # ---------------- |
| 120 | |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 121 | # check that the status is active within ACTIVE_TIMEOUT seconds |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 122 | 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] | 123 | echo "server didn't become active!" |
| 124 | exit 1 |
| 125 | fi |
| 126 | |
| 127 | # get the IP of the server |
Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 128 | IP=`nova show $VM_UUID | grep "$PRIVATE_NETWORK_NAME" | get_field 2` |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 129 | die_if_not_set IP "Failure retrieving IP address" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 130 | |
| 131 | # for single node deployments, we can ping private ips |
Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 132 | ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 133 | |
| 134 | # Volumes |
| 135 | # ------- |
| 136 | |
| 137 | VOL_NAME="myvol-$(openssl rand -hex 4)" |
| 138 | |
| 139 | # Verify it doesn't exist |
Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 140 | 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] | 141 | echo "Volume $VOL_NAME already exists" |
| 142 | exit 1 |
| 143 | fi |
| 144 | |
| 145 | # Create a new volume |
| 146 | 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] | 147 | if [[ $? != 0 ]]; then |
| 148 | echo "Failure creating volume $VOL_NAME" |
| 149 | exit 1 |
| 150 | fi |
John Griffith | 496ffc7 | 2012-09-26 15:09:52 -0600 | [diff] [blame] | 151 | |
| 152 | start_time=`date +%s` |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 153 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then |
| 154 | echo "Volume $VOL_NAME not created" |
| 155 | exit 1 |
| 156 | fi |
John Griffith | 496ffc7 | 2012-09-26 15:09:52 -0600 | [diff] [blame] | 157 | end_time=`date +%s` |
| 158 | echo "Completed volume-create in $((end_time - start_time)) seconds" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 159 | |
| 160 | # Get volume ID |
Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 161 | 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] | 162 | 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] | 163 | |
| 164 | # Attach to server |
| 165 | DEVICE=/dev/vdb |
John Griffith | 496ffc7 | 2012-09-26 15:09:52 -0600 | [diff] [blame] | 166 | start_time=`date +%s` |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 167 | nova volume-attach $VM_UUID $VOL_ID $DEVICE || \ |
| 168 | die "Failure attaching volume $VOL_NAME to $NAME" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 169 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then |
| 170 | echo "Volume $VOL_NAME not attached to $NAME" |
| 171 | exit 1 |
| 172 | fi |
John Griffith | 496ffc7 | 2012-09-26 15:09:52 -0600 | [diff] [blame] | 173 | end_time=`date +%s` |
| 174 | echo "Completed volume-attach in $((end_time - start_time)) seconds" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 175 | |
Vishvananda Ishaya | 854d8c9 | 2012-02-27 22:41:54 +0000 | [diff] [blame] | 176 | 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] | 177 | die_if_not_set VOL_ATTACH "Failure retrieving $VOL_NAME status" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 178 | if [[ "$VOL_ATTACH" != $VM_UUID ]]; then |
| 179 | echo "Volume not attached to correct instance" |
| 180 | exit 1 |
| 181 | fi |
| 182 | |
| 183 | # Detach volume |
John Griffith | 496ffc7 | 2012-09-26 15:09:52 -0600 | [diff] [blame] | 184 | start_time=`date +%s` |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 185 | 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] | 186 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then |
| 187 | echo "Volume $VOL_NAME not detached from $NAME" |
| 188 | exit 1 |
| 189 | fi |
John Griffith | 496ffc7 | 2012-09-26 15:09:52 -0600 | [diff] [blame] | 190 | end_time=`date +%s` |
| 191 | echo "Completed volume-detach in $((end_time - start_time)) seconds" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 192 | |
| 193 | # Delete volume |
John Griffith | 496ffc7 | 2012-09-26 15:09:52 -0600 | [diff] [blame] | 194 | start_time=`date +%s` |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 195 | nova volume-delete $VOL_ID || die "Failure deleting volume $VOL_NAME" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 196 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME; do sleep 1; done"; then |
| 197 | echo "Volume $VOL_NAME not deleted" |
| 198 | exit 1 |
| 199 | fi |
John Griffith | 496ffc7 | 2012-09-26 15:09:52 -0600 | [diff] [blame] | 200 | end_time=`date +%s` |
| 201 | echo "Completed volume-delete in $((end_time - start_time)) seconds" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 202 | |
Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 203 | # Shutdown the server |
| 204 | nova delete $VM_UUID || die "Failure deleting instance $NAME" |
| 205 | |
| 206 | # Wait for termination |
| 207 | if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then |
| 208 | echo "Server $NAME not deleted" |
| 209 | exit 1 |
| 210 | fi |
| 211 | |
| 212 | # Delete a secgroup |
| 213 | nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP" |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 214 | |
Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame^] | 215 | if is_service_enabled quantum; then |
| 216 | teardown_quantum |
| 217 | fi |
| 218 | |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 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 "*********************************************************************" |