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 | da85cda | 2013-02-15 11:07:14 -0600 | [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 | |
Mark McClain | b05c876 | 2013-07-06 23:29:39 -0400 | [diff] [blame] | 33 | # Import neutron functions if needed |
| 34 | if is_service_enabled neutron; then |
| 35 | source $TOP_DIR/lib/neutron |
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 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 48 | # Boot this image, use first AMI image if unset |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 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 | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 54 | # Instance and volume names |
| 55 | VM_NAME=${VM_NAME:-ex-vol-inst} |
| 56 | VOL_NAME="ex-vol-$(openssl rand -hex 4)" |
| 57 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 58 | |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 59 | # Launching a server |
| 60 | # ================== |
| 61 | |
| 62 | # List servers for tenant: |
| 63 | nova list |
| 64 | |
| 65 | # Images |
| 66 | # ------ |
| 67 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 68 | # List the images available |
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) |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 73 | die_if_not_set $LINENO IMAGE "Failure getting image $DEFAULT_IMAGE_NAME" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 74 | |
Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 75 | # Security Groups |
| 76 | # --------------- |
| 77 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 78 | # List security groups |
Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 79 | nova secgroup-list |
| 80 | |
| 81 | # Create a secgroup |
| 82 | if ! nova secgroup-list | grep -q $SECGROUP; then |
| 83 | nova secgroup-create $SECGROUP "$SECGROUP description" |
| 84 | if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then |
| 85 | echo "Security group not created" |
| 86 | exit 1 |
| 87 | fi |
| 88 | fi |
| 89 | |
| 90 | # Configure Security Group Rules |
Dean Troyer | 15bda3e | 2013-01-11 15:07:53 -0600 | [diff] [blame] | 91 | if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then |
| 92 | nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0 |
| 93 | fi |
| 94 | if ! nova secgroup-list-rules $SECGROUP | grep -q " tcp .* 22 "; then |
| 95 | nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0 |
| 96 | fi |
Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 97 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 98 | # List secgroup rules |
| 99 | nova secgroup-list-rules $SECGROUP |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 100 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 101 | # Set up instance |
| 102 | # --------------- |
| 103 | |
| 104 | # List flavors |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 105 | nova flavor-list |
| 106 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 107 | # Select a flavor |
| 108 | INSTANCE_TYPE=$(nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1) |
Dean Troyer | 1d6e0e1 | 2011-12-23 12:45:13 -0600 | [diff] [blame] | 109 | if [[ -z "$INSTANCE_TYPE" ]]; then |
| 110 | # grab the first flavor in the list to launch if default doesn't exist |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 111 | 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] | 112 | fi |
| 113 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 114 | # Clean-up from previous runs |
| 115 | nova delete $VM_NAME || true |
| 116 | if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $VM_NAME; do sleep 1; done"; then |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 117 | die $LINENO "server didn't terminate!" |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 118 | fi |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 119 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 120 | # Boot instance |
| 121 | # ------------- |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 122 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 123 | VM_UUID=$(nova boot --flavor $INSTANCE_TYPE --image $IMAGE --security_groups=$SECGROUP $VM_NAME | grep ' id ' | get_field 2) |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 124 | die_if_not_set $LINENO VM_UUID "Failure launching $VM_NAME" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 125 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 126 | # Check that the status is active within ACTIVE_TIMEOUT seconds |
Dean Troyer | 751c152 | 2012-01-10 15:34:34 -0600 | [diff] [blame] | 127 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 128 | die $LINENO "server didn't become active!" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 129 | fi |
| 130 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 131 | # Get the instance IP |
| 132 | IP=$(nova show $VM_UUID | grep "$PRIVATE_NETWORK_NAME" | get_field 2) |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 133 | die_if_not_set $LINENO IP "Failure retrieving IP address" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 134 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 135 | # Private IPs can be pinged in single node deployments |
Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 136 | ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 137 | |
| 138 | # Volumes |
| 139 | # ------- |
| 140 | |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 141 | # Verify it doesn't exist |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 142 | if [[ -n $(cinder list | grep $VOL_NAME | head -1 | get_field 2) ]]; then |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 143 | die $LINENO "Volume $VOL_NAME already exists" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 144 | fi |
| 145 | |
| 146 | # Create a new volume |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 147 | start_time=$(date +%s) |
| 148 | cinder create --display_name $VOL_NAME --display_description "test volume: $VOL_NAME" $DEFAULT_VOLUME_SIZE || \ |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 149 | die $LINENO "Failure creating volume $VOL_NAME" |
John Griffith | 161e280 | 2012-11-05 13:59:49 -0700 | [diff] [blame] | 150 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep available; do sleep 1; done"; then |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 151 | die $LINENO "Volume $VOL_NAME not created" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 152 | fi |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 153 | end_time=$(date +%s) |
John Griffith | 161e280 | 2012-11-05 13:59:49 -0700 | [diff] [blame] | 154 | echo "Completed cinder create in $((end_time - start_time)) seconds" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 155 | |
| 156 | # Get volume ID |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 157 | VOL_ID=$(cinder list | grep $VOL_NAME | head -1 | get_field 1) |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 158 | die_if_not_set $LINENO VOL_ID "Failure retrieving volume ID for $VOL_NAME" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 159 | |
| 160 | # Attach to server |
| 161 | DEVICE=/dev/vdb |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 162 | start_time=$(date +%s) |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 163 | nova volume-attach $VM_UUID $VOL_ID $DEVICE || \ |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 164 | die $LINENO "Failure attaching volume $VOL_NAME to $VM_NAME" |
John Griffith | 161e280 | 2012-11-05 13:59:49 -0700 | [diff] [blame] | 165 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 166 | die $LINENO "Volume $VOL_NAME not attached to $VM_NAME" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 167 | fi |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 168 | end_time=$(date +%s) |
John Griffith | 496ffc7 | 2012-09-26 15:09:52 -0600 | [diff] [blame] | 169 | echo "Completed volume-attach in $((end_time - start_time)) seconds" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 170 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 171 | VOL_ATTACH=$(cinder list | grep $VOL_NAME | head -1 | get_field -1) |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 172 | die_if_not_set $LINENO VOL_ATTACH "Failure retrieving $VOL_NAME status" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 173 | if [[ "$VOL_ATTACH" != $VM_UUID ]]; then |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 174 | die $LINENO "Volume not attached to correct instance" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 175 | fi |
| 176 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 177 | # Clean up |
| 178 | # -------- |
| 179 | |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 180 | # Detach volume |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 181 | start_time=$(date +%s) |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 182 | nova volume-detach $VM_UUID $VOL_ID || die $LINENO "Failure detaching volume $VOL_NAME from $VM_NAME" |
John Griffith | 161e280 | 2012-11-05 13:59:49 -0700 | [diff] [blame] | 183 | if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep $VOL_NAME | grep available; do sleep 1; done"; then |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 184 | die $LINENO "Volume $VOL_NAME not detached from $VM_NAME" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 185 | fi |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 186 | end_time=$(date +%s) |
John Griffith | 496ffc7 | 2012-09-26 15:09:52 -0600 | [diff] [blame] | 187 | echo "Completed volume-detach in $((end_time - start_time)) seconds" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 188 | |
| 189 | # Delete volume |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 190 | start_time=$(date +%s) |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 191 | cinder delete $VOL_ID || die $LINENO "Failure deleting volume $VOL_NAME" |
Adam Gandelman | 756c842 | 2013-01-04 13:37:22 -0800 | [diff] [blame] | 192 | if ! timeout $ACTIVE_TIMEOUT sh -c "while cinder list | grep $VOL_NAME; do sleep 1; done"; then |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 193 | die $LINENO "Volume $VOL_NAME not deleted" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 194 | fi |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 195 | end_time=$(date +%s) |
John Griffith | 161e280 | 2012-11-05 13:59:49 -0700 | [diff] [blame] | 196 | echo "Completed cinder delete in $((end_time - start_time)) seconds" |
Dean Troyer | a8dda17 | 2011-12-16 12:22:02 -0600 | [diff] [blame] | 197 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 198 | # Delete instance |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 199 | nova delete $VM_UUID || die $LINENO "Failure deleting instance $VM_NAME" |
Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 200 | if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 201 | die $LINENO "Server $VM_NAME not deleted" |
Dean Troyer | 96288ba | 2012-08-17 14:11:55 -0500 | [diff] [blame] | 202 | fi |
| 203 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 204 | # Delete secgroup |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 205 | nova secgroup-delete $SECGROUP || die $LINENO "Failure deleting security group $SECGROUP" |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 206 | |
| 207 | set +o xtrace |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 208 | echo "*********************************************************************" |
| 209 | echo "SUCCESS: End DevStack Exercise: $0" |
| 210 | echo "*********************************************************************" |