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