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: |
| 6 | # * Create an Aggregate |
| 7 | # * Updating Aggregate details |
| 8 | # * Testing Aggregate metadata |
| 9 | # * Testing Aggregate delete |
| 10 | # * TODO(johngar) - test adding a host (idealy with two hosts) |
| 11 | |
| 12 | echo "**************************************************" |
| 13 | echo "Begin DevStack Exercise: $0" |
| 14 | echo "**************************************************" |
| 15 | |
| 16 | # This script exits on an error so that errors don't compound and you see |
| 17 | # only the first error that occured. |
| 18 | set -o errexit |
| 19 | |
| 20 | # Print the commands being run so that we can see the command that triggers |
| 21 | # an error. It is also useful for following allowing as the install occurs. |
| 22 | set -o xtrace |
| 23 | |
| 24 | |
| 25 | # Settings |
| 26 | # ======== |
| 27 | |
| 28 | # Keep track of the current directory |
| 29 | EXERCISE_DIR=$(cd $(dirname "$0") && pwd) |
| 30 | TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) |
| 31 | |
| 32 | # Import common functions |
| 33 | source $TOP_DIR/functions |
| 34 | |
| 35 | # Import configuration |
| 36 | source $TOP_DIR/openrc |
| 37 | |
| 38 | # Import exercise configuration |
| 39 | source $TOP_DIR/exerciserc |
| 40 | |
| 41 | # run test as the admin user |
| 42 | _OLD_USERNAME=$OS_USERNAME |
| 43 | OS_USERNAME=admin |
| 44 | |
| 45 | |
| 46 | # Create an aggregate |
| 47 | # =================== |
| 48 | |
| 49 | AGGREGATE_NAME=test_aggregate_$RANDOM |
| 50 | AGGREGATE_A_ZONE=nova |
| 51 | |
| 52 | exit_if_aggregate_present() { |
| 53 | aggregate_name=$1 |
| 54 | |
| 55 | if [ `nova aggregate-list | grep -c " $aggregate_name "` == 0 ]; then |
| 56 | echo "SUCCESS $aggregate_name not present" |
| 57 | else |
| 58 | echo "ERROR found aggregate: $aggregate_name" |
| 59 | exit -1 |
| 60 | fi |
| 61 | } |
| 62 | |
| 63 | exit_if_aggregate_present $AGGREGATE_NAME |
| 64 | |
| 65 | AGGREGATE_ID=`nova aggregate-create $AGGREGATE_NAME $AGGREGATE_A_ZONE | grep " $AGGREGATE_NAME " | get_field 1` |
| 66 | |
| 67 | # check aggregate created |
| 68 | nova aggregate-list | grep -q " $AGGREGATE_NAME " || die "Aggregate $AGGREGATE_NAME not created" |
| 69 | |
| 70 | |
| 71 | # Ensure creating a duplicate fails |
| 72 | # ================================= |
| 73 | |
| 74 | if nova aggregate-create $AGGREGATE_NAME $AGGREGATE_A_ZONE; then |
| 75 | echo "ERROR could create duplicate aggregate" |
| 76 | exit -1 |
| 77 | fi |
| 78 | |
| 79 | |
| 80 | # Test aggregate-update (and aggregate-details) |
| 81 | # ============================================= |
| 82 | AGGREGATE_NEW_NAME=test_aggregate_$RANDOM |
| 83 | |
| 84 | nova aggregate-update $AGGREGATE_ID $AGGREGATE_NEW_NAME |
| 85 | nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_NEW_NAME |
| 86 | nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_A_ZONE |
| 87 | |
| 88 | nova aggregate-update $AGGREGATE_ID $AGGREGATE_NAME $AGGREGATE_A_ZONE |
| 89 | nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_NAME |
| 90 | nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_A_ZONE |
| 91 | |
| 92 | |
| 93 | # Test aggregate-set-metadata |
| 94 | # =========================== |
| 95 | META_DATA_1_KEY=asdf |
| 96 | META_DATA_2_KEY=foo |
| 97 | META_DATA_3_KEY=bar |
| 98 | |
| 99 | #ensure no metadata is set |
| 100 | nova aggregate-details $AGGREGATE_ID | grep {} |
| 101 | |
| 102 | nova aggregate-set-metadata $AGGREGATE_ID ${META_DATA_1_KEY}=123 |
| 103 | nova aggregate-details $AGGREGATE_ID | grep $META_DATA_1_KEY |
| 104 | nova aggregate-details $AGGREGATE_ID | grep 123 |
| 105 | |
| 106 | nova aggregate-set-metadata $AGGREGATE_ID ${META_DATA_2_KEY}=456 |
| 107 | nova aggregate-details $AGGREGATE_ID | grep $META_DATA_1_KEY |
| 108 | nova aggregate-details $AGGREGATE_ID | grep $META_DATA_2_KEY |
| 109 | |
| 110 | nova aggregate-set-metadata $AGGREGATE_ID $META_DATA_2_KEY ${META_DATA_3_KEY}=789 |
| 111 | nova aggregate-details $AGGREGATE_ID | grep $META_DATA_1_KEY |
| 112 | nova aggregate-details $AGGREGATE_ID | grep $META_DATA_3_KEY |
| 113 | |
| 114 | nova aggregate-details $AGGREGATE_ID | grep $META_DATA_2_KEY && die "ERROR metadata was not cleared" |
| 115 | |
| 116 | nova aggregate-set-metadata $AGGREGATE_ID $META_DATA_3_KEY $META_DATA_1_KEY |
| 117 | nova aggregate-details $AGGREGATE_ID | grep {} |
| 118 | |
| 119 | |
| 120 | # Test aggregate-add/remove-host |
| 121 | # ============================== |
| 122 | if [ "$VIRT_DRIVER" == "xenserver" ]; then |
| 123 | echo "TODO(johngarbutt) add tests for add/remove host from aggregate" |
| 124 | fi |
| 125 | |
| 126 | |
| 127 | # Test aggregate-delete |
| 128 | # ===================== |
| 129 | nova aggregate-delete $AGGREGATE_ID |
| 130 | exit_if_aggregate_present $AGGREGATE_NAME |
| 131 | |
| 132 | |
| 133 | # Test complete |
| 134 | # ============= |
| 135 | OS_USERNAME=$_OLD_USERNAME |
| 136 | echo "AGGREGATE TEST PASSED" |
| 137 | |
| 138 | set +o xtrace |
| 139 | echo "**************************************************" |
| 140 | echo "End DevStack Exercise: $0" |
| 141 | echo "**************************************************" |