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 | |
Chris Behrens | c62c2b9 | 2013-07-24 03:56:13 -0700 | [diff] [blame] | 46 | # Cells does not support aggregates. |
| 47 | is_service_enabled n-cell && exit 55 |
John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 48 | |
| 49 | # Create an aggregate |
| 50 | # =================== |
| 51 | |
| 52 | AGGREGATE_NAME=test_aggregate_$RANDOM |
Joe Gordon | b7ef539 | 2012-08-01 16:13:42 -0700 | [diff] [blame] | 53 | AGGREGATE2_NAME=test_aggregate_$RANDOM |
John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 54 | AGGREGATE_A_ZONE=nova |
| 55 | |
| 56 | exit_if_aggregate_present() { |
| 57 | aggregate_name=$1 |
| 58 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 59 | if [ $(nova aggregate-list | grep -c " $aggregate_name ") == 0 ]; then |
John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 60 | echo "SUCCESS $aggregate_name not present" |
| 61 | else |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 62 | die $LINENO "found aggregate: $aggregate_name" |
John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 63 | exit -1 |
| 64 | fi |
| 65 | } |
| 66 | |
| 67 | exit_if_aggregate_present $AGGREGATE_NAME |
| 68 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 69 | 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] | 70 | die_if_not_set $LINENO AGGREGATE_ID "Failure creating AGGREGATE_ID for $AGGREGATE_NAME $AGGREGATE_A_ZONE" |
| 71 | |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 72 | 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] | 73 | 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] | 74 | |
| 75 | # check aggregate created |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 76 | 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] | 77 | |
| 78 | |
| 79 | # Ensure creating a duplicate fails |
| 80 | # ================================= |
| 81 | |
| 82 | if nova aggregate-create $AGGREGATE_NAME $AGGREGATE_A_ZONE; then |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 83 | die $LINENO "could create duplicate aggregate" |
John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 84 | fi |
| 85 | |
| 86 | |
| 87 | # Test aggregate-update (and aggregate-details) |
| 88 | # ============================================= |
| 89 | AGGREGATE_NEW_NAME=test_aggregate_$RANDOM |
| 90 | |
| 91 | nova aggregate-update $AGGREGATE_ID $AGGREGATE_NEW_NAME |
| 92 | nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_NEW_NAME |
| 93 | nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_A_ZONE |
| 94 | |
| 95 | nova aggregate-update $AGGREGATE_ID $AGGREGATE_NAME $AGGREGATE_A_ZONE |
| 96 | nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_NAME |
| 97 | nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_A_ZONE |
| 98 | |
| 99 | |
| 100 | # Test aggregate-set-metadata |
| 101 | # =========================== |
| 102 | META_DATA_1_KEY=asdf |
| 103 | META_DATA_2_KEY=foo |
| 104 | META_DATA_3_KEY=bar |
| 105 | |
Joe Gordon | 5c1bedd | 2012-12-12 12:03:19 +0000 | [diff] [blame] | 106 | #ensure no additional metadata is set |
Sahid Orentino Ferdjaoui | 75e851a | 2013-10-16 08:34:05 +0000 | [diff] [blame] | 107 | nova aggregate-details $AGGREGATE_ID | egrep "\|[{u ]*'availability_zone.+$AGGREGATE_A_ZONE'[ }]*\|" |
John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 108 | |
| 109 | nova aggregate-set-metadata $AGGREGATE_ID ${META_DATA_1_KEY}=123 |
| 110 | nova aggregate-details $AGGREGATE_ID | grep $META_DATA_1_KEY |
| 111 | nova aggregate-details $AGGREGATE_ID | grep 123 |
| 112 | |
| 113 | nova aggregate-set-metadata $AGGREGATE_ID ${META_DATA_2_KEY}=456 |
| 114 | nova aggregate-details $AGGREGATE_ID | grep $META_DATA_1_KEY |
| 115 | nova aggregate-details $AGGREGATE_ID | grep $META_DATA_2_KEY |
| 116 | |
| 117 | nova aggregate-set-metadata $AGGREGATE_ID $META_DATA_2_KEY ${META_DATA_3_KEY}=789 |
| 118 | nova aggregate-details $AGGREGATE_ID | grep $META_DATA_1_KEY |
| 119 | nova aggregate-details $AGGREGATE_ID | grep $META_DATA_3_KEY |
| 120 | |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 121 | 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] | 122 | |
| 123 | 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] | 124 | nova aggregate-details $AGGREGATE_ID | egrep "\|[{u ]*'availability_zone.+$AGGREGATE_A_ZONE'[ }]*\|" |
John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 125 | |
| 126 | |
| 127 | # Test aggregate-add/remove-host |
| 128 | # ============================== |
| 129 | if [ "$VIRT_DRIVER" == "xenserver" ]; then |
Joe Gordon | b7ef539 | 2012-08-01 16:13:42 -0700 | [diff] [blame] | 130 | echo "TODO(johngarbutt) add tests for add/remove host from pool aggregate" |
John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 131 | fi |
Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 132 | FIRST_HOST=$(nova host-list | grep compute | get_field 1 | head -1) |
Joe Gordon | b7ef539 | 2012-08-01 16:13:42 -0700 | [diff] [blame] | 133 | # Make sure can add two aggregates to same host |
Mate Lakat | 178b840 | 2012-09-05 10:42:10 +0100 | [diff] [blame] | 134 | nova aggregate-add-host $AGGREGATE_ID $FIRST_HOST |
| 135 | nova aggregate-add-host $AGGREGATE2_ID $FIRST_HOST |
| 136 | if nova aggregate-add-host $AGGREGATE2_ID $FIRST_HOST; then |
Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 137 | die $LINENO "could add duplicate host to single aggregate" |
Joe Gordon | b7ef539 | 2012-08-01 16:13:42 -0700 | [diff] [blame] | 138 | fi |
Mate Lakat | 178b840 | 2012-09-05 10:42:10 +0100 | [diff] [blame] | 139 | nova aggregate-remove-host $AGGREGATE2_ID $FIRST_HOST |
| 140 | nova aggregate-remove-host $AGGREGATE_ID $FIRST_HOST |
John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 141 | |
| 142 | # Test aggregate-delete |
| 143 | # ===================== |
| 144 | nova aggregate-delete $AGGREGATE_ID |
Joe Gordon | b7ef539 | 2012-08-01 16:13:42 -0700 | [diff] [blame] | 145 | nova aggregate-delete $AGGREGATE2_ID |
John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 146 | exit_if_aggregate_present $AGGREGATE_NAME |
| 147 | |
John Garbutt | fd1c87e | 2012-02-24 14:52:54 +0000 | [diff] [blame] | 148 | set +o xtrace |
| 149 | echo "**************************************************" |
| 150 | echo "End DevStack Exercise: $0" |
| 151 | echo "**************************************************" |