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