| John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 1 | #!/usr/bin/env bash | 
|  | 2 |  | 
|  | 3 | # **aggregates.sh** | 
|  | 4 |  | 
|  | 5 | # This script demonstrates how to use host aggregates: | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 6 | # | 
|  | 7 | # *  Create an Aggregate | 
|  | 8 | # *  Updating Aggregate details | 
|  | 9 | # *  Testing Aggregate metadata | 
|  | 10 | # *  Testing Aggregate delete | 
|  | 11 | # *  Testing General Aggregates (https://blueprints.launchpad.net/nova/+spec/general-host-aggregates) | 
|  | 12 | # *  Testing add/remove hosts (with one host) | 
| John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 13 |  | 
|  | 14 | echo "**************************************************" | 
|  | 15 | echo "Begin DevStack Exercise: $0" | 
|  | 16 | echo "**************************************************" | 
|  | 17 |  | 
|  | 18 | # This script exits on an error so that errors don't compound and you see | 
| Joe Gordon | b7ef539 | 2012-08-01 16:13:42 -0700 | [diff] [blame] | 19 | # only the first error that occurred. | 
| John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 20 | set -o errexit | 
|  | 21 |  | 
|  | 22 | # Print the commands being run so that we can see the command that triggers | 
|  | 23 | # an error.  It is also useful for following allowing as the install occurs. | 
|  | 24 | set -o xtrace | 
|  | 25 |  | 
|  | 26 |  | 
|  | 27 | # Settings | 
|  | 28 | # ======== | 
|  | 29 |  | 
|  | 30 | # Keep track of the current directory | 
|  | 31 | EXERCISE_DIR=$(cd $(dirname "$0") && pwd) | 
|  | 32 | TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) | 
|  | 33 |  | 
|  | 34 | # Import common functions | 
|  | 35 | source $TOP_DIR/functions | 
|  | 36 |  | 
|  | 37 | # Import configuration | 
|  | 38 | source $TOP_DIR/openrc | 
|  | 39 |  | 
|  | 40 | # Import exercise configuration | 
|  | 41 | source $TOP_DIR/exerciserc | 
|  | 42 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 43 | # Test as the admin user | 
| Mate Lakat | c497a05 | 2013-02-21 15:25:51 +0000 | [diff] [blame] | 44 | . $TOP_DIR/openrc admin admin | 
| John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 45 |  | 
| Kiall Mac Innes | a16c821 | 2014-01-12 19:35:43 +0000 | [diff] [blame] | 46 | # If nova api is not enabled we exit with exitcode 55 so that | 
|  | 47 | # the exercise is skipped | 
|  | 48 | is_service_enabled n-api || exit 55 | 
|  | 49 |  | 
| Chris Behrens | c62c2b9 | 2013-07-24 03:56:13 -0700 | [diff] [blame] | 50 | # Cells does not support aggregates. | 
|  | 51 | is_service_enabled n-cell && exit 55 | 
| John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 52 |  | 
|  | 53 | # Create an aggregate | 
|  | 54 | # =================== | 
|  | 55 |  | 
|  | 56 | AGGREGATE_NAME=test_aggregate_$RANDOM | 
| Joe Gordon | b7ef539 | 2012-08-01 16:13:42 -0700 | [diff] [blame] | 57 | AGGREGATE2_NAME=test_aggregate_$RANDOM | 
| John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 58 | AGGREGATE_A_ZONE=nova | 
|  | 59 |  | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 60 | function exit_if_aggregate_present { | 
| John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 61 | aggregate_name=$1 | 
|  | 62 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 63 | if [ $(nova aggregate-list | grep -c " $aggregate_name ") == 0 ]; then | 
| John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 64 | echo "SUCCESS $aggregate_name not present" | 
|  | 65 | else | 
| Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 66 | die $LINENO "found aggregate: $aggregate_name" | 
| John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 67 | exit -1 | 
|  | 68 | fi | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | exit_if_aggregate_present $AGGREGATE_NAME | 
|  | 72 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 73 | AGGREGATE_ID=$(nova aggregate-create $AGGREGATE_NAME $AGGREGATE_A_ZONE | grep " $AGGREGATE_NAME " | get_field 1) | 
| DennyZhang | 23178a9 | 2013-10-22 17:07:32 -0500 | [diff] [blame] | 74 | die_if_not_set $LINENO AGGREGATE_ID "Failure creating AGGREGATE_ID for $AGGREGATE_NAME $AGGREGATE_A_ZONE" | 
|  | 75 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 76 | AGGREGATE2_ID=$(nova aggregate-create $AGGREGATE2_NAME $AGGREGATE_A_ZONE | grep " $AGGREGATE2_NAME " | get_field 1) | 
| DennyZhang | 23178a9 | 2013-10-22 17:07:32 -0500 | [diff] [blame] | 77 | die_if_not_set $LINENO AGGREGATE2_ID "Fail creating AGGREGATE2_ID for $AGGREGATE2_NAME $AGGREGATE_A_ZONE" | 
| John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 78 |  | 
|  | 79 | # check aggregate created | 
| Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 80 | nova aggregate-list | grep -q " $AGGREGATE_NAME " || die $LINENO "Aggregate $AGGREGATE_NAME not created" | 
| John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 81 |  | 
|  | 82 |  | 
|  | 83 | # Ensure creating a duplicate fails | 
|  | 84 | # ================================= | 
|  | 85 |  | 
|  | 86 | if nova aggregate-create $AGGREGATE_NAME $AGGREGATE_A_ZONE; then | 
| Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 87 | die $LINENO "could create duplicate aggregate" | 
| John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 88 | fi | 
|  | 89 |  | 
|  | 90 |  | 
|  | 91 | # Test aggregate-update (and aggregate-details) | 
|  | 92 | # ============================================= | 
|  | 93 | AGGREGATE_NEW_NAME=test_aggregate_$RANDOM | 
|  | 94 |  | 
|  | 95 | nova aggregate-update $AGGREGATE_ID $AGGREGATE_NEW_NAME | 
|  | 96 | nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_NEW_NAME | 
|  | 97 | nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_A_ZONE | 
|  | 98 |  | 
|  | 99 | nova aggregate-update $AGGREGATE_ID $AGGREGATE_NAME $AGGREGATE_A_ZONE | 
|  | 100 | nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_NAME | 
|  | 101 | nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_A_ZONE | 
|  | 102 |  | 
|  | 103 |  | 
|  | 104 | # Test aggregate-set-metadata | 
|  | 105 | # =========================== | 
|  | 106 | META_DATA_1_KEY=asdf | 
|  | 107 | META_DATA_2_KEY=foo | 
|  | 108 | META_DATA_3_KEY=bar | 
|  | 109 |  | 
| Joe Gordon | 5c1bedd | 2012-12-12 12:03:19 +0000 | [diff] [blame] | 110 | #ensure no additional metadata is set | 
| Sahid Orentino Ferdjaoui | 75e851a | 2013-10-16 08:34:05 +0000 | [diff] [blame] | 111 | nova aggregate-details $AGGREGATE_ID | egrep "\|[{u ]*'availability_zone.+$AGGREGATE_A_ZONE'[ }]*\|" | 
| John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 112 |  | 
|  | 113 | nova aggregate-set-metadata $AGGREGATE_ID ${META_DATA_1_KEY}=123 | 
|  | 114 | nova aggregate-details $AGGREGATE_ID | grep $META_DATA_1_KEY | 
|  | 115 | nova aggregate-details $AGGREGATE_ID | grep 123 | 
|  | 116 |  | 
|  | 117 | nova aggregate-set-metadata $AGGREGATE_ID ${META_DATA_2_KEY}=456 | 
|  | 118 | nova aggregate-details $AGGREGATE_ID | grep $META_DATA_1_KEY | 
|  | 119 | nova aggregate-details $AGGREGATE_ID | grep $META_DATA_2_KEY | 
|  | 120 |  | 
|  | 121 | nova aggregate-set-metadata $AGGREGATE_ID $META_DATA_2_KEY ${META_DATA_3_KEY}=789 | 
|  | 122 | nova aggregate-details $AGGREGATE_ID | grep $META_DATA_1_KEY | 
|  | 123 | nova aggregate-details $AGGREGATE_ID | grep $META_DATA_3_KEY | 
|  | 124 |  | 
| Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 125 | nova aggregate-details $AGGREGATE_ID | grep $META_DATA_2_KEY && die $LINENO "ERROR metadata was not cleared" | 
| John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 126 |  | 
|  | 127 | nova aggregate-set-metadata $AGGREGATE_ID $META_DATA_3_KEY $META_DATA_1_KEY | 
| Sahid Orentino Ferdjaoui | 75e851a | 2013-10-16 08:34:05 +0000 | [diff] [blame] | 128 | nova aggregate-details $AGGREGATE_ID | egrep "\|[{u ]*'availability_zone.+$AGGREGATE_A_ZONE'[ }]*\|" | 
| John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 129 |  | 
|  | 130 |  | 
|  | 131 | # Test aggregate-add/remove-host | 
|  | 132 | # ============================== | 
|  | 133 | if [ "$VIRT_DRIVER" == "xenserver" ]; then | 
| Joe Gordon | b7ef539 | 2012-08-01 16:13:42 -0700 | [diff] [blame] | 134 | echo "TODO(johngarbutt) add tests for add/remove host from pool aggregate" | 
| John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 135 | fi | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 136 | FIRST_HOST=$(nova host-list | grep compute | get_field 1 | head -1) | 
| Joe Gordon | b7ef539 | 2012-08-01 16:13:42 -0700 | [diff] [blame] | 137 | # Make sure can add two aggregates to same host | 
| Mate Lakat | 178b840 | 2012-09-05 10:42:10 +0100 | [diff] [blame] | 138 | nova aggregate-add-host $AGGREGATE_ID $FIRST_HOST | 
|  | 139 | nova aggregate-add-host $AGGREGATE2_ID $FIRST_HOST | 
|  | 140 | if nova aggregate-add-host $AGGREGATE2_ID $FIRST_HOST; then | 
| Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 141 | die $LINENO "could add duplicate host to single aggregate" | 
| Joe Gordon | b7ef539 | 2012-08-01 16:13:42 -0700 | [diff] [blame] | 142 | fi | 
| Mate Lakat | 178b840 | 2012-09-05 10:42:10 +0100 | [diff] [blame] | 143 | nova aggregate-remove-host $AGGREGATE2_ID $FIRST_HOST | 
|  | 144 | nova aggregate-remove-host $AGGREGATE_ID $FIRST_HOST | 
| John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 145 |  | 
|  | 146 | # Test aggregate-delete | 
|  | 147 | # ===================== | 
|  | 148 | nova aggregate-delete $AGGREGATE_ID | 
| Joe Gordon | b7ef539 | 2012-08-01 16:13:42 -0700 | [diff] [blame] | 149 | nova aggregate-delete $AGGREGATE2_ID | 
| John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 150 | exit_if_aggregate_present $AGGREGATE_NAME | 
|  | 151 |  | 
| John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 152 | set +o xtrace | 
|  | 153 | echo "**************************************************" | 
|  | 154 | echo "End DevStack Exercise: $0" | 
|  | 155 | echo "**************************************************" |